Skip to content

Commit fe63707

Browse files
committed
Included testcase for the implementation of XDG specification.
1 parent f539667 commit fe63707

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Foundation/NSHTTPCookieStorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99
import Dispatch
10+
import CoreFoundation
1011

1112
/*!
1213
@enum NSHTTPCookieAcceptPolicy
@@ -46,8 +47,7 @@ open class HTTPCookieStorage: NSObject {
4647
allCookies = [:]
4748
cookieAcceptPolicy = .always
4849
super.init()
49-
//TODO: cookieFilePath = filePath(path: _CFXDGCreateConfigHomePath()._swiftObject, fileName: "/.cookies." + cookieStorageName)
50-
cookieFilePath = filePath(path: NSHomeDirectory() + "/.config", fileName: "/.cookies." + cookieStorageName)
50+
cookieFilePath = filePath(path: _CFXDGCreateConfigHomePath()._swiftObject, fileName: "/.cookies." + cookieStorageName)
5151
loadPersistedCookies()
5252
}
5353

TestFoundation/TestNSHTTPCookieStorage.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestNSHTTPCookieStorage: XCTestCase {
3030
("test_setCookiesForURL", test_setCookiesForURL),
3131
("test_getCookiesForURL", test_getCookiesForURL),
3232
("test_setCookiesForURLWithMainDocumentURL", test_setCookiesForURLWithMainDocumentURL),
33+
("test_cookieInXDGSpecPath", test_cookieInXDGSpecPath),
3334
]
3435
}
3536

@@ -213,4 +214,43 @@ class TestNSHTTPCookieStorage: XCTestCase {
213214
storage.setCookies([simpleCookie1], for: url1, mainDocumentURL: mainUrl)
214215
XCTAssertEqual(storage.cookies(for: url1!)!.count, 0)
215216
}
217+
218+
func test_cookieInXDGSpecPath() {
219+
//Test without setting the environment variable
220+
let testCookie = HTTPCookie(properties: [
221+
.name: "TestCookie0",
222+
.value: "Test @#$%^$&*99mam",
223+
.path: "/",
224+
.domain: "sample.com",
225+
])!
226+
let storage = HTTPCookieStorage.shared
227+
storage.setCookie(testCookie)
228+
XCTAssertEqual(storage.cookies!.count, 3)
229+
var destPath: String
230+
if let xdg_config_home = getenv("XDG_CONFIG_HOME") {
231+
destPath = String(utf8String: xdg_config_home)! + "/.cookies.shared"
232+
} else {
233+
destPath = NSHomeDirectory() + "/.config/.cookies.shared"
234+
}
235+
let fm = FileManager.default
236+
var isDir = false
237+
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
238+
XCTAssertTrue(exists)
239+
//Test by setting the environmental variable
240+
let bundle = Bundle.main
241+
let bundlePath = bundle.bundlePath
242+
var pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound
243+
let task = Process()
244+
task.launchPath = bundlePath.substring(to: pathIndex!) + "/xdgTestHelper/xdgTestHelper"
245+
var environment = ProcessInfo.processInfo.environment
246+
environment["XDG_CONFIG_HOME"] = NSHomeDirectory() + "/TestXDG"
247+
task.environment = environment
248+
// Launch the task
249+
task.launch()
250+
task.waitUntilExit()
251+
let status = task.terminationStatus
252+
XCTAssertEqual(status, 0)
253+
let terminationReason = task.terminationReason
254+
XCTAssertEqual(terminationReason, Process.TerminationReason.exit)
255+
}
216256
}

build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@
487487
Configuration.current.extra_ld_flags += ' -L'+Configuration.current.variables["LIBDISPATCH_BUILD_DIR"]+'/src/.libs'
488488

489489
foundation_tests.add_dependency(foundation_tests_resources)
490+
xdgTestHelper = SwiftExecutable('xdgTestHelper', ['TestFoundation/XDGTestHelper.swift'])
491+
foundation_tests.add_dependency(xdgTestHelper)
492+
foundation.add_phase(xdgTestHelper)
490493
foundation.add_phase(foundation_tests_resources)
491494
foundation.add_phase(foundation_tests)
492495

lib/phases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ class SwiftExecutable(BuildPhase):
417417
def __init__(self, executableName, sources):
418418
BuildAction.__init__(self, output=executableName)
419419
self.executableName = executableName
420+
self.name = executableName
420421
self.sources = sources
421422

422423
def generate(self):

0 commit comments

Comments
 (0)