Skip to content

Commit 58919ef

Browse files
authored
Merge pull request swiftlang#1079 from compnerd/paths
Tests: simplify, homogenise path computation
2 parents eaac195 + d7e4cc4 commit 58919ef

File tree

2 files changed

+26
-57
lines changed

2 files changed

+26
-57
lines changed

Tests/SwiftDriverTests/ParsableMessageTests.swift

+11-17
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ final class ParsableMessageTests: XCTestCase {
142142
try withHijackedBufferedErrorStream(in: path) { errorBuffer in
143143
let resolver = try ArgsResolver(fileSystem: localFileSystem)
144144

145-
let workdir: AbsolutePath =
146-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/WorkDir", relativeTo: $0) } ?? AbsolutePath(validating: "/WorkDir")
145+
let workdir: AbsolutePath = localFileSystem.currentWorkingDirectory!.appending(components: "WorkDir")
147146

148147
var driver = try Driver(args: ["swiftc", "-o", "test.o",
149148
"main.swift", "test1.swift", "test2.swift",
@@ -170,16 +169,21 @@ final class ParsableMessageTests: XCTestCase {
170169
"name" : "compile",
171170
""").count - 1, 3)
172171

173-
/// One per primary
174-
XCTAssertTrue(errorOutput.contains(
175-
"""
176-
"pid" : -1000,
177-
"""))
178172
#if os(Windows)
179173
let mainPath: String = workdir.appending(component: "main.swift").nativePathString(escaped: true)
174+
let test1Path: String = workdir.appending(component: "test1.swift").nativePathString(escaped: true)
175+
let test2Path: String = workdir.appending(component: "test2.swift").nativePathString(escaped: true)
180176
#else
181177
let mainPath: String = workdir.appending(component: "main.swift").pathString.replacingOccurrences(of: "/", with: "\\/")
178+
let test1Path: String = workdir.appending(component: "test1.swift").pathString.replacingOccurrences(of: "/", with: "\\/")
179+
let test2Path: String = workdir.appending(component: "test2.swift").pathString.replacingOccurrences(of: "/", with: "\\/")
182180
#endif
181+
182+
/// One per primary
183+
XCTAssertTrue(errorOutput.contains(
184+
"""
185+
"pid" : -1000,
186+
"""))
183187
XCTAssertTrue(errorOutput.contains(
184188
"""
185189
\"inputs\" : [
@@ -190,11 +194,6 @@ final class ParsableMessageTests: XCTestCase {
190194
"""
191195
"pid" : -1001,
192196
"""))
193-
#if os(Windows)
194-
let test1Path: String = workdir.appending(component: "test1.swift").nativePathString(escaped: true)
195-
#else
196-
let test1Path: String = workdir.appending(component: "test1.swift").pathString.replacingOccurrences(of: "/", with: "\\/")
197-
#endif
198197
XCTAssertTrue(errorOutput.contains(
199198
"""
200199
\"inputs\" : [
@@ -205,11 +204,6 @@ final class ParsableMessageTests: XCTestCase {
205204
"""
206205
"pid" : -1002,
207206
"""))
208-
#if os(Windows)
209-
let test2Path: String = workdir.appending(component: "test2.swift").nativePathString(escaped: true)
210-
#else
211-
let test2Path: String = workdir.appending(component: "test2.swift").pathString.replacingOccurrences(of: "/", with: "\\/")
212-
#endif
213207
XCTAssertTrue(errorOutput.contains(
214208
"""
215209
\"inputs\" : [

Tests/SwiftDriverTests/SwiftDriverTests.swift

+15-40
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,8 @@ final class SwiftDriverTests: XCTestCase {
284284
[ TypedVirtualPath(file: VirtualPath.relative(RelativePath("a.swift")).intern(), type: .swift),
285285
TypedVirtualPath(file: VirtualPath.absolute(AbsolutePath("/tmp/b.swift")).intern(), type: .swift) ])
286286

287-
let workingDirectory =
288-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/wobble", relativeTo: $0) }
289-
?? AbsolutePath(validating: "/Foo/Bar")
290-
let tempDirectory =
291-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/tmp", relativeTo: $0) }
292-
?? AbsolutePath(validating: "/Foo/Bar")
287+
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "wobble")
288+
let tempDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "tmp")
293289

294290
let driver2 = try Driver(args: ["swiftc", "a.swift", "-working-directory", workingDirectory.pathString, rebase("b.swift", at: tempDirectory)])
295291
XCTAssertEqual(driver2.inputFiles,
@@ -649,9 +645,7 @@ final class SwiftDriverTests: XCTestCase {
649645
XCTAssertEqual(plannedJobs[2].outputs.first!.file, VirtualPath.relative(RelativePath(executableName("Test"))))
650646

651647
// Forwarding of arguments.
652-
let workingDirectory =
653-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/tmp", relativeTo: $0) }
654-
?? AbsolutePath(validating: "/Foo/Bar")
648+
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "tmp")
655649

656650
var driver2 = try Driver(args: ["swiftc", "-color-diagnostics", "foo.swift", "bar.swift", "-working-directory", workingDirectory.pathString, "-api-diff-data-file", "diff.txt", "-Xfrontend", "-HI", "-no-color-diagnostics", "-g"])
657651
let plannedJobs2 = try driver2.planBuild()
@@ -1097,8 +1091,7 @@ final class SwiftDriverTests: XCTestCase {
10971091
]
10981092
]
10991093

1100-
let root = try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/foo_root", relativeTo: $0) }
1101-
?? AbsolutePath(validating: "/foo_root")
1094+
let root = localFileSystem.currentWorkingDirectory!.appending(components: "foo_root")
11021095

11031096
let resolvedStringyEntries: [String: [FileType: String]] = [
11041097
"": [.swiftDeps: root.appending(components: "foo.build", "master.swiftdeps").pathString],
@@ -2749,8 +2742,7 @@ final class SwiftDriverTests: XCTestCase {
27492742
}
27502743

27512744
do {
2752-
let root = try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/foo/bar", relativeTo: $0) }
2753-
?? AbsolutePath(validating: "/foo/bar")
2745+
let root = localFileSystem.currentWorkingDirectory!.appending(components: "foo", "bar")
27542746

27552747
var driver = try Driver(args: ["swiftc", "foo.swift", "bar.swift", "-module-name", "Test", "-emit-module-path", rebase("Test.swiftmodule", at: root), "-no-emit-module-separately"])
27562748
let plannedJobs = try driver.planBuild()
@@ -2814,8 +2806,7 @@ final class SwiftDriverTests: XCTestCase {
28142806
envVars["SWIFT_DRIVER_LD_EXEC"] = ld.nativePathString(escaped: false)
28152807

28162808
do {
2817-
let root = try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/foo/bar", relativeTo: $0) }
2818-
?? AbsolutePath(validating: "/foo/bar")
2809+
let root = localFileSystem.currentWorkingDirectory!.appending(components: "foo", "bar")
28192810

28202811
var driver = try Driver(args: ["swiftc", "foo.swift", "bar.swift", "-module-name", "Test", "-emit-module-path", rebase("Test.swiftmodule", at: root), "-emit-symbol-graph", "-emit-symbol-graph-dir", "/foo/bar/", "-experimental-emit-module-separately", "-emit-library"],
28212812
env: envVars)
@@ -2837,8 +2828,7 @@ final class SwiftDriverTests: XCTestCase {
28372828
}
28382829

28392830
do {
2840-
let root = try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/foo/bar", relativeTo: $0) }
2841-
?? AbsolutePath(validating: "/foo/bar")
2831+
let root = localFileSystem.currentWorkingDirectory!.appending(components: "foo", "bar")
28422832

28432833
// We don't expect partial jobs when asking only for the swiftmodule with
28442834
// -experimental-emit-module-separately.
@@ -2886,8 +2876,7 @@ final class SwiftDriverTests: XCTestCase {
28862876
func testEmitModuleSeparatelyWMO() throws {
28872877
var envVars = ProcessEnv.vars
28882878
envVars["SWIFT_DRIVER_LD_EXEC"] = ld.nativePathString(escaped: false)
2889-
let root = try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/foo/bar", relativeTo: $0) }
2890-
?? AbsolutePath(validating: "/foo/bar")
2879+
let root = localFileSystem.currentWorkingDirectory!.appending(components: "foo", "bar")
28912880

28922881
do {
28932882
var driver = try Driver(args: ["swiftc", "foo.swift", "bar.swift", "-module-name", "Test", "-emit-module-path", rebase("Test.swiftmodule", at: root), "-emit-symbol-graph", "-emit-symbol-graph-dir", root.pathString, "-emit-library", "-target", "x86_64-apple-macosx10.15", "-wmo", "-emit-module-separately-wmo"],
@@ -4262,9 +4251,7 @@ final class SwiftDriverTests: XCTestCase {
42624251
}
42634252

42644253
func testLEqualPassedDownToLinkerInvocation() throws {
4265-
let workingDirectory =
4266-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/Foo/Bar", relativeTo: $0) }
4267-
?? AbsolutePath(validating: "/Foo/Bar")
4254+
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "Foo", "Bar")
42684255

42694256
var driver = try Driver(args: [
42704257
"swiftc", "-working-directory", workingDirectory.pathString, "-emit-executable", "test.swift", "-L=.", "-F=."
@@ -4283,9 +4270,7 @@ final class SwiftDriverTests: XCTestCase {
42834270
}
42844271

42854272
func testWorkingDirectoryForImplicitOutputs() throws {
4286-
let workingDirectory =
4287-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/Foo/Bar", relativeTo: $0) }
4288-
?? AbsolutePath(validating: "/Foo/Bar")
4273+
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "Foo", "Bar")
42894274

42904275
var driver = try Driver(args: [
42914276
"swiftc", "-working-directory", workingDirectory.pathString, "-emit-executable", "-c", "/tmp/main.swift"
@@ -4298,9 +4283,7 @@ final class SwiftDriverTests: XCTestCase {
42984283
}
42994284

43004285
func testWorkingDirectoryForImplicitModules() throws {
4301-
let workingDirectory =
4302-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/Foo/Bar", relativeTo: $0) }
4303-
?? AbsolutePath(validating: "/Foo/Bar")
4286+
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "Foo", "Bar")
43044287

43054288
var driver = try Driver(args: [
43064289
"swiftc", "-working-directory", workingDirectory.pathString, "-emit-module", "/tmp/main.swift"
@@ -4677,8 +4660,7 @@ final class SwiftDriverTests: XCTestCase {
46774660
// Replace the error stream with one we capture here.
46784661
let errorStream = stderrStream
46794662

4680-
let root = try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/build", relativeTo: $0) }
4681-
?? AbsolutePath(validating: "/build")
4663+
let root = localFileSystem.currentWorkingDirectory!.appending(components: "build")
46824664

46834665
let errorOutputFile = path.appending(component: "dummy_error_stream")
46844666
TSCBasic.stderrStream = try! ThreadSafeOutputByteStream(LocalFileOutputByteStream(errorOutputFile))
@@ -5903,9 +5885,7 @@ final class SwiftDriverTests: XCTestCase {
59035885

59045886
func testFrontendTargetInfoWithWorkingDirectory() throws {
59055887
do {
5906-
let workingDirectory =
5907-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/absolute/path", relativeTo: $0) }
5908-
?? AbsolutePath(validating: "/Foo/Bar")
5888+
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "absolute", "path")
59095889

59105890
var driver = try Driver(args: ["swiftc", "-typecheck", "foo.swift",
59115891
"-resource-dir", "resource/dir",
@@ -6352,13 +6332,8 @@ final class SwiftDriverTests: XCTestCase {
63526332

63536333
func testRegistrarLookup() throws {
63546334
#if os(Windows)
6355-
let SDKROOT: AbsolutePath =
6356-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/SDKROOT", relativeTo: $0) }
6357-
?? AbsolutePath(validating: "/SDKROOT")
6358-
6359-
let resourceDir: AbsolutePath =
6360-
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/swift/resources", relativeTo: $0) }
6361-
?? AbsolutePath(validating: "/swift/resources")
6335+
let SDKROOT: AbsolutePath = localFileSystem.currentWorkingDirectory!.appending(components: "SDKROOT")
6336+
let resourceDir: AbsolutePath = localFileSystem.currentWorkingDirectory!.appending(components: "swift", "resources")
63626337

63636338
let platform: String = "windows"
63646339
#if arch(x86_64)

0 commit comments

Comments
 (0)