Skip to content

Commit

Permalink
Make tests run on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Mar 8, 2022
1 parent 3b3b779 commit 6b1b327
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
7 changes: 4 additions & 3 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ public struct AbsolutePath: Path {
self.filepath = filepath.lexicallyNormalized()
return
}
var filepath = filepath.lexicallyNormalized()
var normalizedFilePath = filepath.lexicallyNormalized()
guard filepath.root?.string == "\\" else {
preconditionFailure()
preconditionFailure("\(filepath) is not a valid absolute path")
}
self.filepath = AbsolutePath.root.filepath.pushing(filepath)
normalizedFilePath.root = AbsolutePath.root.filepath.root
self.filepath = normalizedFilePath.lexicallyNormalized()
#else
precondition(filepath.isAbsolute)
self.filepath = filepath.lexicallyNormalized()
Expand Down
4 changes: 4 additions & 0 deletions Sources/TSCTestSupport/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import func XCTest.XCTFail
import struct XCTest.XCTSkip
import class Foundation.NSDate
import class Foundation.Thread

Expand Down Expand Up @@ -61,6 +62,9 @@ public func systemQuietly(_ args: String...) throws {
/// from different threads, the environment will neither be setup nor restored
/// correctly.
public func withCustomEnv(_ env: [String: String], body: () throws -> Void) throws {
#if os(Windows)
throw XCTSkip("'withCustomEnv(_:body:)' is broken on Windows")
#endif
let state = Array(env.keys).map({ ($0, ProcessEnv.vars[$0]) })
let restore = {
for (key, value) in state {
Expand Down
7 changes: 6 additions & 1 deletion Tests/TSCBasicTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ class FileSystemTests: XCTestCase {
}
}

func testResolvingSymlinks() {
func testResolvingSymlinks() throws {
#if os(Windows)
throw XCTSkip("Symlink resolving on Windows often crashes due to some Foundation bugs.")
#endif
// Make sure the root path resolves to itself.
XCTAssertEqual(resolveSymlinks(AbsolutePath.root), AbsolutePath.root)

Expand Down Expand Up @@ -186,6 +189,7 @@ class FileSystemTests: XCTestCase {
}
}

#if !os(Windows)
func testLocalReadableWritable() throws {
try testWithTemporaryDirectory { tmpdir in
let fs = localFileSystem
Expand Down Expand Up @@ -253,6 +257,7 @@ class FileSystemTests: XCTestCase {
}
}
}
#endif

func testLocalCreateDirectory() throws {
let fs = TSCBasic.localFileSystem
Expand Down
11 changes: 8 additions & 3 deletions Tests/TSCBasicTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ class PathTests: XCTestCase {
}

func testBaseNameWithoutExt() {
XCTAssertEqual(AbsolutePath("/").basenameWithoutExt, "/")
XCTAssertEqual(AbsolutePath("/").basenameWithoutExt, AbsolutePath.root.pathString)
XCTAssertEqual(AbsolutePath("/a").basenameWithoutExt, "a")
XCTAssertEqual(AbsolutePath("/./a").basenameWithoutExt, "a")
XCTAssertEqual(AbsolutePath("/../..").basenameWithoutExt, "/")
XCTAssertEqual(AbsolutePath("/../..").basenameWithoutExt, AbsolutePath.root.pathString)
XCTAssertEqual(RelativePath("../..").basenameWithoutExt, "..")
XCTAssertEqual(RelativePath("../a").basenameWithoutExt, "a")
XCTAssertEqual(RelativePath("../a/..").basenameWithoutExt, "..")
Expand Down Expand Up @@ -311,7 +311,12 @@ class PathTests: XCTestCase {
}

func testAbsolutePathValidation() {
XCTAssertNoThrow(try AbsolutePath(validating: "/a/b/c/d"))
#if os(Windows)
let pathString = #"C:\a\b\c\d"#
#else
let pathString = "/a/b/c/d"
#endif
XCTAssertNoThrow(try AbsolutePath(validating: pathString))

XCTAssertThrowsError(try AbsolutePath(validating: "~/a/b/d")) { error in
XCTAssertEqual("\(error)", "invalid absolute path '~/a/b/d'")
Expand Down
2 changes: 0 additions & 2 deletions Tests/TSCBasicTests/TemporaryFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class TemporaryFileTests: XCTestCase {
XCTAssertFalse(localFileSystem.isDirectory(pathTwo))
}

#if !os(Windows)
/// Check that the temporary file doesn't leak file descriptors.
func testLeaks() throws {
// We check this by testing that we get back the same FD after a
Expand All @@ -153,5 +152,4 @@ class TemporaryFileTests: XCTestCase {
XCTAssertEqual(initialFD, endFD)
#endif
}
#endif
}
24 changes: 16 additions & 8 deletions Tests/TSCBasicTests/miscTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import TSCBasic
class miscTests: XCTestCase {

func testExecutableLookup() throws {
#if os(Windows)
throw XCTSkip("TODO")
#endif
try testWithTemporaryDirectory { path in

let pathEnv1 = path.appending(component: "pathEnv1")
try localFileSystem.createDirectory(pathEnv1)
let pathEnvClang = pathEnv1.appending(component: "clang")
Expand All @@ -28,15 +31,15 @@ class miscTests: XCTestCase {
// nil and empty string should fail.
XCTAssertNil(lookupExecutablePath(filename: nil, currentWorkingDirectory: path, searchPaths: pathEnv))
XCTAssertNil(lookupExecutablePath(filename: "", currentWorkingDirectory: path, searchPaths: pathEnv))

// Absolute path to a binary should return it.
var exec = lookupExecutablePath(filename: pathEnvClang.pathString, currentWorkingDirectory: path, searchPaths: pathEnv)
XCTAssertEqual(exec, pathEnvClang)

// This should lookup from PATH variable since executable is not present in cwd.
exec = lookupExecutablePath(filename: "clang", currentWorkingDirectory: path, searchPaths: pathEnv)
XCTAssertEqual(exec, pathEnvClang)

// Create the binary relative to cwd and make it executable.
let clang = path.appending(component: "clang")
try localFileSystem.writeFileContents(clang, bytes: "")
Expand All @@ -46,18 +49,23 @@ class miscTests: XCTestCase {
XCTAssertEqual(exec, clang)
}
}

func testEnvSearchPaths() throws {
#if os(Windows)
let pathString = "something;.;abc/../.build/debug;/usr/bin:/bin/"
#else
let pathString = "something:.:abc/../.build/debug:/usr/bin:/bin/"
#endif
let cwd = AbsolutePath("/dummy")
let paths = getEnvSearchPaths(pathString: "something:.:abc/../.build/debug:/usr/bin:/bin/", currentWorkingDirectory: cwd)
let paths = getEnvSearchPaths(pathString: pathString, currentWorkingDirectory: cwd)
XCTAssertEqual(paths, ["/dummy/something", "/dummy", "/dummy/.build/debug", "/usr/bin", "/bin"].map({AbsolutePath($0)}))
}

func testEmptyEnvSearchPaths() throws {
let cwd = AbsolutePath("/dummy")
let paths = getEnvSearchPaths(pathString: "", currentWorkingDirectory: cwd)
XCTAssertEqual(paths, [])

let nilPaths = getEnvSearchPaths(pathString: nil, currentWorkingDirectory: cwd)
XCTAssertEqual(nilPaths, [])
}
Expand Down
12 changes: 7 additions & 5 deletions Tests/TSCUtilityTests/PkgConfigParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ final class PkgConfigParserTests: XCTestCase {
"/usr/lib/pkgconfig/foo.pc",
"/usr/local/opt/foo/lib/pkgconfig/foo.pc",
"/custom/foo.pc")
XCTAssertEqual("/custom/foo.pc", try PCFileFinder(diagnostics: diagnostics, brewPrefix: nil).locatePCFile(name: "foo", customSearchPaths: [AbsolutePath("/custom")], fileSystem: fs).pathString)
XCTAssertEqual("/custom/foo.pc", try PkgConfig(name: "foo", additionalSearchPaths: [AbsolutePath("/custom")], diagnostics: diagnostics, fileSystem: fs, brewPrefix: nil).pcFile.pathString)
XCTAssertEqual("/usr/lib/pkgconfig/foo.pc", try PCFileFinder(diagnostics: diagnostics, brewPrefix: nil).locatePCFile(name: "foo", customSearchPaths: [], fileSystem: fs).pathString)
XCTAssertEqual(AbsolutePath("/custom/foo.pc"), try PCFileFinder(diagnostics: diagnostics, brewPrefix: nil).locatePCFile(name: "foo", customSearchPaths: [AbsolutePath("/custom")], fileSystem: fs))
XCTAssertEqual(AbsolutePath("/custom/foo.pc"), try PkgConfig(name: "foo", additionalSearchPaths: [AbsolutePath("/custom")], diagnostics: diagnostics, fileSystem: fs, brewPrefix: nil).pcFile)
XCTAssertEqual(AbsolutePath("/usr/lib/pkgconfig/foo.pc"), try PCFileFinder(diagnostics: diagnostics, brewPrefix: nil).locatePCFile(name: "foo", customSearchPaths: [], fileSystem: fs))
try withCustomEnv(["PKG_CONFIG_PATH": "/usr/local/opt/foo/lib/pkgconfig"]) {
XCTAssertEqual("/usr/local/opt/foo/lib/pkgconfig/foo.pc", try PkgConfig(name: "foo", diagnostics: diagnostics, fileSystem: fs, brewPrefix: nil).pcFile.pathString)
XCTAssertEqual(AbsolutePath("/usr/local/opt/foo/lib/pkgconfig/foo.pc"), try PkgConfig(name: "foo", diagnostics: diagnostics, fileSystem: fs, brewPrefix: nil).pcFile)
}
try withCustomEnv(["PKG_CONFIG_PATH": "/usr/local/opt/foo/lib/pkgconfig:/usr/lib/pkgconfig"]) {
XCTAssertEqual("/usr/local/opt/foo/lib/pkgconfig/foo.pc", try PkgConfig(name: "foo", diagnostics: diagnostics, fileSystem: fs, brewPrefix: nil).pcFile.pathString)
XCTAssertEqual(AbsolutePath("/usr/local/opt/foo/lib/pkgconfig/foo.pc"), try PkgConfig(name: "foo", diagnostics: diagnostics, fileSystem: fs, brewPrefix: nil).pcFile)
}
}

Expand Down Expand Up @@ -142,6 +142,7 @@ final class PkgConfigParserTests: XCTestCase {
XCTAssertEqual(PCFileFinder.pkgConfigPaths, [AbsolutePath("/Volumes/BestDrive/pkgconfig")])
}

#if !os(Windows) // pkg-config is not compatible with Windows paths.
func testAbsolutePathDependency() throws {

let libffiPath = "/usr/local/opt/libffi/lib/pkgconfig/libffi.pc"
Expand Down Expand Up @@ -171,6 +172,7 @@ final class PkgConfigParserTests: XCTestCase {
fileSystem: fileSystem,
brewPrefix: AbsolutePath("/usr/local")))
}
#endif

func testUnevenQuotes() throws {
do {
Expand Down

0 comments on commit 6b1b327

Please sign in to comment.