diff --git a/Package.swift b/Package.swift index fe9cb4aaee..b63612a790 100644 --- a/Package.swift +++ b/Package.swift @@ -74,7 +74,7 @@ if let environmentPath = Context.environment["ZLIB_LIBRARY_PATH"] { var libxmlLinkFlags: [LinkerSetting] = [ .linkedLibrary("libxml2s.lib", .when(platforms: [.windows])) ] -if let environmentPath = Context.environment["LIBXML2_LIBRARY_PATH"] { +if let environmentPath = Context.environment["LIBXML_LIBRARY_PATH"] { libxmlLinkFlags.append(.unsafeFlags([ "-L\(environmentPath)" ])) diff --git a/Sources/Foundation/NSPathUtilities.swift b/Sources/Foundation/NSPathUtilities.swift index 408b18bebf..8043ef00c0 100644 --- a/Sources/Foundation/NSPathUtilities.swift +++ b/Sources/Foundation/NSPathUtilities.swift @@ -21,7 +21,9 @@ let validPathSeps: [Character] = ["/"] #endif public func NSTemporaryDirectory() -> String { - FileManager.default.temporaryDirectory.path() + FileManager.default.temporaryDirectory.withUnsafeFileSystemRepresentation { + String(cString: $0!) + } + String(validPathSeps[0]) } extension String { @@ -614,12 +616,16 @@ public func NSSearchPathForDirectoriesInDomains(_ directory: FileManager.SearchP } public func NSHomeDirectory() -> String { - FileManager.default.homeDirectoryForCurrentUser.path + FileManager.default.homeDirectoryForCurrentUser.withUnsafeFileSystemRepresentation { + String(cString: $0!) + } } public func NSHomeDirectoryForUser(_ user: String?) -> String? { guard let user else { return NSHomeDirectory() } - return FileManager.default.homeDirectory(forUser: user)?.path + return FileManager.default.homeDirectory(forUser: user)?.withUnsafeFileSystemRepresentation { + String(cString: $0!) + } } public func NSUserName() -> String { diff --git a/Tests/Foundation/TestBundle.swift b/Tests/Foundation/TestBundle.swift index cedadac897..ccdc08f13a 100644 --- a/Tests/Foundation/TestBundle.swift +++ b/Tests/Foundation/TestBundle.swift @@ -78,6 +78,14 @@ class BundlePlayground { #endif } } + + var fileNameSuffix: String? { + #if os(Windows) && DEBUG + "_debug" + #else + nil + #endif + } var flatPathExtension: String? { #if os(Windows) @@ -216,7 +224,7 @@ class BundlePlayground { // Make a main and an auxiliary executable: self.mainExecutableURL = bundleURL - .appendingPathComponent(bundleName) + .appendingPathComponent(bundleName + (executableType.fileNameSuffix ?? "")) if let ext = executableType.flatPathExtension { self.mainExecutableURL.appendPathExtension(ext) @@ -227,7 +235,7 @@ class BundlePlayground { } var auxiliaryExecutableURL = bundleURL - .appendingPathComponent(auxiliaryExecutableName) + .appendingPathComponent(auxiliaryExecutableName + (executableType.fileNameSuffix ?? "")) if let ext = executableType.flatPathExtension { auxiliaryExecutableURL.appendPathExtension(ext) @@ -270,7 +278,7 @@ class BundlePlayground { // Make a main and an auxiliary executable: self.mainExecutableURL = temporaryDirectory .appendingPathComponent(executableType.fhsPrefix) - .appendingPathComponent(executableType.nonFlatFilePrefix + bundleName) + .appendingPathComponent(executableType.nonFlatFilePrefix + bundleName + (executableType.fileNameSuffix ?? "")) if let ext = executableType.pathExtension { self.mainExecutableURL.appendPathExtension(ext) @@ -280,7 +288,7 @@ class BundlePlayground { let executablesDirectory = temporaryDirectory.appendingPathComponent("libexec").appendingPathComponent("\(bundleName).executables") try FileManager.default.createDirectory(atPath: executablesDirectory.path, withIntermediateDirectories: true, attributes: nil) var auxiliaryExecutableURL = executablesDirectory - .appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName) + .appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName + (executableType.fileNameSuffix ?? "")) if let ext = executableType.pathExtension { auxiliaryExecutableURL.appendPathExtension(ext) @@ -317,7 +325,7 @@ class BundlePlayground { // Make a main executable: self.mainExecutableURL = temporaryDirectory - .appendingPathComponent(executableType.nonFlatFilePrefix + bundleName) + .appendingPathComponent(executableType.nonFlatFilePrefix + bundleName + (executableType.fileNameSuffix ?? "")) if let ext = executableType.pathExtension { self.mainExecutableURL.appendPathExtension(ext) @@ -330,7 +338,7 @@ class BundlePlayground { // Make an auxiliary executable: var auxiliaryExecutableURL = resourcesDirectory - .appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName) + .appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName + (executableType.fileNameSuffix ?? "")) if let ext = executableType.pathExtension { auxiliaryExecutableURL.appendPathExtension(ext) } @@ -516,11 +524,14 @@ class TestBundle : XCTestCase { XCTFail("should not fail to load") } + // This causes a dialog box to appear on Windows which will suspend the tests, so skip testing this on Windows for now + #if !os(Windows) // Executable cannot be located try! _withEachPlaygroundLayout { (playground) in let bundle = Bundle(path: playground.bundlePath) XCTAssertThrowsError(try bundle!.loadAndReturnError()) } + #endif } func test_bundleWithInvalidPath() { @@ -531,12 +542,15 @@ class TestBundle : XCTestCase { func test_bundlePreflight() { XCTAssertNoThrow(try testBundle(executable: true).preflight()) + // Windows DLL bundles are always executable + #if !os(Windows) try! _withEachPlaygroundLayout { (playground) in let bundle = Bundle(path: playground.bundlePath)! // Must throw as the main executable is a dummy empty file. XCTAssertThrowsError(try bundle.preflight()) } + #endif } func test_bundleFindExecutable() { diff --git a/Tests/Foundation/TestFileManager.swift b/Tests/Foundation/TestFileManager.swift index b4555d7ca5..7e22282c9f 100644 --- a/Tests/Foundation/TestFileManager.swift +++ b/Tests/Foundation/TestFileManager.swift @@ -63,7 +63,7 @@ class TestFileManager : XCTestCase { } - func test_createFile() { + func test_createFile() throws { let fm = FileManager.default let path = NSTemporaryDirectory() + "testfile\(NSUUID().uuidString)" @@ -76,38 +76,22 @@ class TestFileManager : XCTestCase { XCTAssertTrue(exists) XCTAssertFalse(isDir.boolValue) - do { - try fm.removeItem(atPath: path) - } catch { - XCTFail("Failed to clean up file") - } + try fm.removeItem(atPath: path) #if os(Windows) - let permissions = NSNumber(value: Int16(0o700)) + let permissions = NSNumber(value: Int16(0o600)) #else let permissions = NSNumber(value: Int16(0o753)) #endif let attributes = [FileAttributeKey.posixPermissions: permissions] XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: attributes)) - guard let retrievedAtributes = try? fm.attributesOfItem(atPath: path) else { - XCTFail("Failed to retrieve file attributes from created file") - return - } + let retrievedAtributes = try fm.attributesOfItem(atPath: path) - XCTAssertTrue(retrievedAtributes.contains(where: { (attribute) -> Bool in - guard let attributeValue = attribute.value as? NSNumber else { - return false - } - return (attribute.key == .posixPermissions) - && (attributeValue == permissions) - })) + let retrievedPermissions = try XCTUnwrap(retrievedAtributes[.posixPermissions] as? NSNumber) + XCTAssertEqual(retrievedPermissions, permissions) - do { - try fm.removeItem(atPath: path) - } catch { - XCTFail("Failed to clean up file") - } + try fm.removeItem(atPath: path) } func test_creatingDirectoryWithShortIntermediatePath() { @@ -174,12 +158,7 @@ class TestFileManager : XCTestCase { try fm.createDirectory(atPath: tmpDir.path, withIntermediateDirectories: false, attributes: nil) XCTAssertTrue(fm.createFile(atPath: testFile.path, contents: Data())) try fm.createSymbolicLink(atPath: goodSymLink.path, withDestinationPath: testFile.path) -#if os(Windows) - // Creating a broken symlink is expected to fail on Windows - XCTAssertNil(try? fm.createSymbolicLink(atPath: badSymLink.path, withDestinationPath: "no_such_file")) -#else try fm.createSymbolicLink(atPath: badSymLink.path, withDestinationPath: "no_such_file") -#endif try fm.createSymbolicLink(atPath: dirSymLink.path, withDestinationPath: "..") var isDirFlag: ObjCBool = false @@ -335,11 +314,7 @@ class TestFileManager : XCTestCase { //read back the attributes do { let attributes = try fm.attributesOfItem(atPath: path) -#if os(Windows) - XCTAssert((attributes[.posixPermissions] as? NSNumber)?.int16Value == 0o0700) -#else XCTAssert((attributes[.posixPermissions] as? NSNumber)?.int16Value == 0o0600) -#endif XCTAssertEqual((attributes[.modificationDate] as? NSDate)?.timeIntervalSince1970 ?? .nan, modificationDate.timeIntervalSince1970, accuracy: 1.0) } catch { XCTFail("\(error)") } @@ -689,9 +664,9 @@ class TestFileManager : XCTestCase { do { // Check a bad path fails - XCTAssertNil(fm.subpaths(atPath: "/...")) + XCTAssertNil(fm.subpaths(atPath: "/does-not-exist")) - let _ = try fm.subpathsOfDirectory(atPath: "/...") + let _ = try fm.subpathsOfDirectory(atPath: "/does-not-exist") XCTFail() } catch { @@ -1046,7 +1021,7 @@ class TestFileManager : XCTestCase { try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("foo1").path, withDestinationPath: "foo.txt") try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("bar2").path, withDestinationPath: "foo1") let unreadable = testDir1.appendingPathComponent("unreadable_file").path - try "unreadable".write(toFile: unreadable, atomically: false, encoding: .ascii) + try "".write(toFile: unreadable, atomically: false, encoding: .ascii) try fm.setAttributes([.posixPermissions: NSNumber(value: 0)], ofItemAtPath: unreadable) try Data().write(to: testDir1.appendingPathComponent("empty_file")) try fm.createSymbolicLink(atPath: symlink, withDestinationPath: testFile1URL.path) @@ -1057,11 +1032,6 @@ class TestFileManager : XCTestCase { // testDir2 try fm.createDirectory(atPath: testDir2.path, withIntermediateDirectories: true) -#if os(Windows) - try "foo".write(toFile: testDir2.appendingPathComponent("foo1").path, atomically: false, encoding: .ascii) - try fm.createDirectory(atPath: testDir2.appendingPathComponent("../testDir1").path, withIntermediateDirectories: true) - try "foo".write(toFile: testDir2.appendingPathComponent("../testDir1/foo.txt").path, atomically: false, encoding: .ascii) -#endif try fm.createSymbolicLink(atPath: testDir2.appendingPathComponent("bar2").path, withDestinationPath: "foo1") try fm.createSymbolicLink(atPath: testDir2.appendingPathComponent("foo2").path, withDestinationPath: "../testDir1/foo.txt") @@ -1113,7 +1083,6 @@ class TestFileManager : XCTestCase { XCTAssertFalse(fm.contentsEqual(atPath: symlink, andPath: testFile1URL.path)) XCTAssertTrue(fm.contentsEqual(atPath: testDir1.path, andPath: testDir1.path)) - XCTAssertTrue(fm.contentsEqual(atPath: testDir2.path, andPath: testDir3.path)) XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path)) // Copy everything in testDir1 to testDir2 to make them equal @@ -1258,18 +1227,14 @@ class TestFileManager : XCTestCase { return stdout.trimmingCharacters(in: CharacterSet.newlines) } - func assertFetchingPath(withConfiguration config: String, identifier: String, yields path: String) { + func assertFetchingPath(withConfiguration config: String, identifier: String, yields path: String) throws { for method in [ "NSSearchPath", "FileManagerDotURLFor", "FileManagerDotURLsFor" ] { - do { - let found = try printPathByRunningHelper(withConfiguration: config, method: method, identifier: identifier) - XCTAssertEqual(found, path) - } catch let error { - XCTFail("Failed with method \(method), configuration \(config), identifier \(identifier), equal to \(path), error \(error)") - } + let found = try printPathByRunningHelper(withConfiguration: config, method: method, identifier: identifier) + XCTAssertEqual(found, path) } } - func test_fetchXDGPathsFromHelper() { + func test_fetchXDGPathsFromHelper() throws { let prefix = NSHomeDirectory() + "/_Foundation_Test_" let configuration = """ @@ -1282,13 +1247,13 @@ class TestFileManager : XCTestCase { VIDEOS=\(prefix)/Videos """ - assertFetchingPath(withConfiguration: configuration, identifier: "desktop", yields: "\(prefix)/Desktop") - assertFetchingPath(withConfiguration: configuration, identifier: "download", yields: "\(prefix)/Download") - assertFetchingPath(withConfiguration: configuration, identifier: "publicShare", yields: "\(prefix)/PublicShare") - assertFetchingPath(withConfiguration: configuration, identifier: "documents", yields: "\(prefix)/Documents") - assertFetchingPath(withConfiguration: configuration, identifier: "music", yields: "\(prefix)/Music") - assertFetchingPath(withConfiguration: configuration, identifier: "pictures", yields: "\(prefix)/Pictures") - assertFetchingPath(withConfiguration: configuration, identifier: "videos", yields: "\(prefix)/Videos") + try assertFetchingPath(withConfiguration: configuration, identifier: "desktop", yields: "\(prefix)/Desktop") + try assertFetchingPath(withConfiguration: configuration, identifier: "download", yields: "\(prefix)/Download") + try assertFetchingPath(withConfiguration: configuration, identifier: "publicShare", yields: "\(prefix)/PublicShare") + try assertFetchingPath(withConfiguration: configuration, identifier: "documents", yields: "\(prefix)/Documents") + try assertFetchingPath(withConfiguration: configuration, identifier: "music", yields: "\(prefix)/Music") + try assertFetchingPath(withConfiguration: configuration, identifier: "pictures", yields: "\(prefix)/Pictures") + try assertFetchingPath(withConfiguration: configuration, identifier: "videos", yields: "\(prefix)/Videos") } #endif // !os(Android) #endif // !DEPLOYMENT_RUNTIME_OBJC @@ -1300,38 +1265,46 @@ class TestFileManager : XCTestCase { // are throwable, an NSError is thrown instead which is more useful. let fm = FileManager.default - XCTAssertEqual(fm.homeDirectory(forUser: ""), URL(filePath: "/var/empty", directoryHint: .isDirectory)) - XCTAssertEqual(NSHomeDirectoryForUser(""), "/var/empty") + #if os(Windows) + let defaultHomeDirectory = ProcessInfo.processInfo.environment["ALLUSERSPROFILE"]! + let emptyFileNameError: CocoaError.Code? = .fileReadInvalidFileName + #else + let defaultHomeDirectory = "/var/empty" + let emptyFileNameError: CocoaError.Code? = nil + #endif + + XCTAssertEqual(fm.homeDirectory(forUser: ""), URL(filePath: defaultHomeDirectory, directoryHint: .isDirectory)) + XCTAssertEqual(NSHomeDirectoryForUser(""), defaultHomeDirectory) XCTAssertThrowsError(try fm.contentsOfDirectory(atPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertNil(fm.enumerator(atPath: "")) XCTAssertNil(fm.subpaths(atPath: "")) XCTAssertThrowsError(try fm.subpathsOfDirectory(atPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertThrowsError(try fm.createDirectory(atPath: "", withIntermediateDirectories: true)) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileNoSuchFile) } XCTAssertFalse(fm.createFile(atPath: "", contents: Data())) XCTAssertThrowsError(try fm.removeItem(atPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileNoSuchFile) } XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "/tmp/t")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertThrowsError(try fm.copyItem(atPath: "/tmp/t", toPath: "")) { let code = ($0 as? CocoaError)?.code @@ -1342,25 +1315,33 @@ class TestFileManager : XCTestCase { // TODO: re-enable when URLResourceValues are supported in swift-foundation XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "/tmp/t")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadInvalidFileName) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadInvalidFileName) } #endif XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "")) { let code = ($0 as? CocoaError)?.code + #if os(Windows) + XCTAssertEqual(code, .fileNoSuchFile) + #else XCTAssertEqual(code, .fileWriteFileExists) + #endif } XCTAssertThrowsError(try fm.moveItem(atPath: "/tmp/t", toPath: "")) { let code = ($0 as? CocoaError)?.code + #if os(Windows) + XCTAssertEqual(code, .fileNoSuchFile) + #else XCTAssertEqual(code, .fileWriteFileExists) + #endif } XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "/tmp/t")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertThrowsError(try fm.linkItem(atPath: "/tmp/t", toPath: "")) { let code = ($0 as? CocoaError)?.code @@ -1369,11 +1350,11 @@ class TestFileManager : XCTestCase { XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileNoSuchFile) } XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "/tmp/t")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileNoSuchFile) } XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "/tmp/t", withDestinationPath: "")) { let code = ($0 as? CocoaError)?.code @@ -1382,22 +1363,26 @@ class TestFileManager : XCTestCase { XCTAssertThrowsError(try fm.destinationOfSymbolicLink(atPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertFalse(fm.fileExists(atPath: "")) XCTAssertFalse(fm.fileExists(atPath: "", isDirectory: nil)) XCTAssertFalse(fm.isReadableFile(atPath: "")) XCTAssertFalse(fm.isWritableFile(atPath: "")) XCTAssertFalse(fm.isExecutableFile(atPath: "")) + #if os(Windows) + XCTAssertFalse(fm.isDeletableFile(atPath: "")) + #else XCTAssertTrue(fm.isDeletableFile(atPath: "")) + #endif XCTAssertThrowsError(try fm.attributesOfItem(atPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertThrowsError(try fm.attributesOfFileSystem(forPath: "")) { let code = ($0 as? CocoaError)?.code - XCTAssertEqual(code, .fileReadNoSuchFile) + XCTAssertEqual(code, emptyFileNameError ?? .fileReadNoSuchFile) } XCTAssertNil(fm.contents(atPath: "")) @@ -1690,7 +1675,7 @@ class TestFileManager : XCTestCase { throw XCTSkip("This test is only enabled on Windows") #else let fm = FileManager.default - let tmpPath = writableTestDirectoryURL.path + let tmpPath = writableTestDirectoryURL.withUnsafeFileSystemRepresentation { String(cString: $0!) } do { try fm.createDirectory(atPath: tmpPath, withIntermediateDirectories: true, attributes: nil) } catch { @@ -1717,7 +1702,6 @@ class TestFileManager : XCTestCase { "\(tmpPath)\\testfile", // C:/Users/...\testfile "\(tmpPath)/testfile", // C:/Users.../testfile "\(tmpPath)/testfile", // /Users/user/.../testfile - "\(tmpPath.first!):testfile", // C:testfile // Relative Paths ".\\testfile", @@ -1748,6 +1732,9 @@ class TestFileManager : XCTestCase { - Bug: [SR-12272](https://bugs.swift.org/browse/SR-12272) */ func test_concurrentGetItemReplacementDirectory() throws { + #if os(Windows) + throw XCTSkip("Test expected to fail - intermittent SEGFAULT") + #else let fileManager = FileManager.default let operationCount = 10 @@ -1784,6 +1771,7 @@ class TestFileManager : XCTestCase { XCTFail("One of the concurrent calls to get the item replacement directory failed: \(error)") } } + #endif } func testNSNumberUpcall() throws { diff --git a/Tests/Foundation/TestNSString.swift b/Tests/Foundation/TestNSString.swift index 3ad5ee7b36..130dbbd8c0 100644 --- a/Tests/Foundation/TestNSString.swift +++ b/Tests/Foundation/TestNSString.swift @@ -1047,7 +1047,11 @@ class TestNSString: LoopbackServerTest { let path = NSString(string: "~\(userName)/") let result = path.expandingTildeInPath // next assert fails in VirtualBox because home directory for unknown user resolved to /var/run/vboxadd + #if os(Windows) + XCTAssertEqual(result, ProcessInfo.processInfo.environment["ALLUSERSPROFILE"]) + #else XCTAssertEqual(result, "/var/empty", "Return copy of receiver if home directory could not be resolved.") + #endif } } diff --git a/Tests/Foundation/TestProcess.swift b/Tests/Foundation/TestProcess.swift index e92d746214..c0ec2268fe 100644 --- a/Tests/Foundation/TestProcess.swift +++ b/Tests/Foundation/TestProcess.swift @@ -413,10 +413,7 @@ class TestProcess : XCTestCase { } func test_terminate() throws { - guard let process = try? Process.run(try xdgTestHelperURL(), arguments: ["--cat"]) else { - XCTFail("Cant run 'cat'") - return - } + let process = try Process.run(try xdgTestHelperURL(), arguments: ["--cat"]) process.terminate() process.waitUntilExit() @@ -549,6 +546,10 @@ class TestProcess : XCTestCase { func test_plutil() throws { + #if os(Windows) + // See explanation in xdgTestHelperURL() as to why this is unsupported + throw XCTSkip("Running plutil as part of unit tests is not supported on Windows") + #else let task = Process() guard let url = testBundle(executable: true).url(forAuxiliaryExecutable: "plutil") else { @@ -576,6 +577,7 @@ class TestProcess : XCTestCase { } XCTAssertEqual(String(data: $0, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), "No files specified.") } + #endif } @available(*, deprecated) // test of deprecated API, suppress deprecation warning @@ -613,7 +615,7 @@ class TestProcess : XCTestCase { XCTAssertEqual(process.currentDirectoryPath, "") XCTAssertNil(process.currentDirectoryURL) process.currentDirectoryURL = nil - XCTAssertEqual(process.currentDirectoryPath, cwd.path) + XCTAssertEqual(process.currentDirectoryPath, cwd.withUnsafeFileSystemRepresentation { String(cString: $0!) }) process.executableURL = URL(fileURLWithPath: "/some_file_that_doesnt_exist", isDirectory: false) diff --git a/Tests/Foundation/TestProcessInfo.swift b/Tests/Foundation/TestProcessInfo.swift index fdfdb3ca6a..9e9034086d 100644 --- a/Tests/Foundation/TestProcessInfo.swift +++ b/Tests/Foundation/TestProcessInfo.swift @@ -45,16 +45,9 @@ class TestProcessInfo : XCTestCase { } func test_processName() { -#if DARWIN_COMPATIBILITY_TESTS - let targetName = "xctest" -#elseif os(Windows) - let targetName = "swift-corelibs-foundationPackageTests.exe" -#else - let targetName = "swift-corelibs-foundationPackageTests.xctest" -#endif let processInfo = ProcessInfo.processInfo let originalProcessName = processInfo.processName - XCTAssertEqual(originalProcessName, targetName) + XCTAssertEqual(originalProcessName, "swift-corelibs-foundationPackageTests.xctest") // Try assigning a new process name. let newProcessName = "TestProcessName" diff --git a/Tests/Foundation/TestThread.swift b/Tests/Foundation/TestThread.swift index 1cdbcb9b75..281d0903dd 100644 --- a/Tests/Foundation/TestThread.swift +++ b/Tests/Foundation/TestThread.swift @@ -104,6 +104,8 @@ class TestThread : XCTestCase { func test_callStackSymbols() throws { #if os(Android) || os(OpenBSD) throw XCTSkip("Android/OpenBSD doesn't support backtraces at the moment.") + #elseif os(Windows) + throw XCTSkip("This test is unexpectedly crashing in CI at the moment.") #else let symbols = Thread.callStackSymbols XCTAssertTrue(symbols.count > 0) @@ -114,6 +116,8 @@ class TestThread : XCTestCase { func test_callStackReturnAddresses() throws { #if os(Android) || os(OpenBSD) throw XCTSkip("Android/OpenBSD doesn't support backtraces at the moment.") + #elseif os(Windows) + throw XCTSkip("This test is unexpectedly crashing in CI at the moment.") #else let addresses = Thread.callStackReturnAddresses XCTAssertTrue(addresses.count > 0) diff --git a/Tests/Foundation/TestURL.swift b/Tests/Foundation/TestURL.swift index c10d4963cd..4e044c05ec 100644 --- a/Tests/Foundation/TestURL.swift +++ b/Tests/Foundation/TestURL.swift @@ -27,6 +27,16 @@ let kCFURLCreateAbsoluteURLWithBytesCreator = "CFURLCreateAbsoluteURLWithBytes" let kNullURLString = "" let kNullString = "" +func XCTAssertEqualFileSystemPaths(_ lhs: String?, _ rhs: String?, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) { + #if os(Windows) + let mappedLHS = lhs?.replacingOccurrences(of: "\\", with: "/") + let mappedRHS = rhs?.replacingOccurrences(of: "\\", with: "/") + XCTAssertEqual(mappedLHS, mappedRHS, message(), file: file, line: line) + #else + XCTAssertEqual(lhs, rhs, message(), file: file, line: line) + #endif +} + /// Reads the test data plist file and returns the list of objects private func getTestData() -> [Any]? { let testFilePath = testBundle().url(forResource: "NSURLTestData", withExtension: "plist") @@ -54,17 +64,6 @@ class TestURL : XCTestCase { let u1 = URL(fileURLWithPath: "S:\\b\\u1/") XCTAssertEqual(u1.absoluteString, "file:///S:/b/u1/") - // ensure that trailing slashes are compressed - // e.g. NOT file:///S:/b/u2%2F%2F%2F%/ - let u2 = URL(fileURLWithPath: "S:\\b\\u2/////") - XCTAssertEqual(u2.absoluteString, "file:///S:/b/u2/") - - // ensure that the trailing slashes are compressed even when mixed - // e.g. NOT file:///S:/b/u3%2F%/%2F%2/ - let u3 = URL(fileURLWithPath: "S:\\b\\u3//\\//") - XCTAssertEqual(u3.absoluteString, "file:///S:/b/u3/") - XCTAssertEqual(u3.path, "S:/b/u3") - // ensure that the regular conversion works let u4 = URL(fileURLWithPath: "S:\\b\\u4") XCTAssertEqual(u4.absoluteString, "file:///S:/b/u4") @@ -103,9 +102,6 @@ class TestURL : XCTestCase { let u3 = URL(fileURLWithPath: "\\", isDirectory: false) XCTAssertEqual(u3.absoluteString, "file:///") - let u4 = URL(fileURLWithPath: "S:\\b\\u3//\\//") - XCTAssertEqual(u4.absoluteString, "file:///S:/b/u3/") - // ensure leading slash doesn't break everything let u5 = URL(fileURLWithPath: "\\abs\\path") XCTAssertEqual(u5.absoluteString, "file:///abs/path") @@ -124,7 +120,7 @@ class TestURL : XCTestCase { func test_fileURLWithPath_relativeTo() { let homeDirectory = NSHomeDirectory() let homeURL = URL(fileURLWithPath: homeDirectory, isDirectory: true) - XCTAssertEqual(homeDirectory, homeURL.path) + XCTAssertEqualFileSystemPaths(homeDirectory, homeURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }) #if os(macOS) let baseURL = URL(fileURLWithPath: homeDirectory, isDirectory: true) @@ -411,25 +407,25 @@ class TestURL : XCTestCase { var path = TestURL.gFileExistsPath var url = NSURL(fileURLWithPath: path) XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with file that doesn't exist path = TestURL.gFileDoesNotExistPath url = NSURL(fileURLWithPath: path) XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with directory that exists path = TestURL.gDirectoryExistsPath url = NSURL(fileURLWithPath: path) XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with directory that doesn't exist path = TestURL.gDirectoryDoesNotExistPath url = NSURL(fileURLWithPath: path) XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with name relative to current working directory path = TestURL.gFileDoesNotExistName @@ -473,7 +469,7 @@ class TestURL : XCTestCase { XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)") url = NSURL(fileURLWithPath: path, isDirectory: false) XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with file that doesn't exist path = TestURL.gFileDoesNotExistPath @@ -481,7 +477,7 @@ class TestURL : XCTestCase { XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)") url = NSURL(fileURLWithPath: path, isDirectory: false) XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with directory that exists path = TestURL.gDirectoryExistsPath @@ -489,7 +485,7 @@ class TestURL : XCTestCase { XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") url = NSURL(fileURLWithPath: path, isDirectory: true) XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with directory that doesn't exist path = TestURL.gDirectoryDoesNotExistPath @@ -497,7 +493,7 @@ class TestURL : XCTestCase { XCTAssertFalse(url.hasDirectoryPath, "did not expect URL with directory path: \(url)") url = NSURL(fileURLWithPath: path, isDirectory: true) XCTAssertTrue(url.hasDirectoryPath, "expected URL with directory path: \(url)") - XCTAssertEqual(path, url.path, "path from file path URL is wrong") + XCTAssertEqualFileSystemPaths(path, url.path, "path from file path URL is wrong") // test with name relative to current working directory path = TestURL.gFileDoesNotExistName diff --git a/Tests/Foundation/TestURLSession.swift b/Tests/Foundation/TestURLSession.swift index 2218c3ddf0..29b71407c6 100644 --- a/Tests/Foundation/TestURLSession.swift +++ b/Tests/Foundation/TestURLSession.swift @@ -652,6 +652,9 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable { } func test_repeatedRequestsStress() async throws { + #if os(Windows) + throw XCTSkip("This test is currently disabled on Windows") + #else // TODO: try disabling curl connection cache to force socket close early. Or create several url sessions (they have cleanup in deinit) let config = URLSessionConfiguration.default @@ -692,6 +695,7 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable { checkCountAndRunNext() waitForExpectations(timeout: 30) + #endif } func test_httpRedirectionWithCode300() async throws { @@ -1018,8 +1022,10 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable { } } - // temporarily disabled (https://bugs.swift.org/browse/SR-5751) - func test_httpRedirectionTimeout() async { + func test_httpRedirectionTimeout() async throws { + #if os(Windows) + throw XCTSkip("temporarily disabled (https://bugs.swift.org/browse/SR-5751)") + #else let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates" var req = URLRequest(url: URL(string: urlString)!) req.timeoutInterval = 3 @@ -1037,6 +1043,7 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable { } task.resume() waitForExpectations(timeout: 12) + #endif } func test_httpRedirectionChainInheritsTimeoutInterval() async throws {