Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@ import TSCBasic
import XCTest
import TestUtilities

private func argumentNeedsQuoting(_ argument: String) -> Bool {
if argument.isEmpty { return false }
let chars: Set<Character> = Set("\t \"&'()*<>\\`^|\n")
return argument.firstIndex(where: { chars.contains($0) }) != argument.endIndex
}

private func quoteArgument(_ argument: String) -> String {
#if os(Windows)
var unquoted: Substring = argument[...]
var quoted: String = "\""
while !unquoted.isEmpty {
guard let firstNonBS = unquoted.firstIndex(where: { $0 != "\\" }) else {
// The rest of the string is backslashes. Escape all of them and exit.
(0 ..< (2 * unquoted.count)).forEach { _ in quoted += "\\" }
break
}

let bsCount = unquoted.distance(from: unquoted.startIndex, to: firstNonBS)
if unquoted[firstNonBS] == "\"" {
// This is an embedded quote. Escape all preceding backslashes, then
// add one additional backslash to escape the quote.
(0 ..< (2 * bsCount + 1)).forEach { _ in quoted += "\\" }
quoted += "\""
} else {
// This is just a normal character. Don't escape any of the preceding
// backslashes, just append them as they are and then append the
// character.
(0 ..< bsCount).forEach { _ in quoted += "\\" }
quoted += "\(unquoted[firstNonBS])"
}

unquoted = unquoted.dropFirst(bsCount + 1)
}
return quoted + "\""
#else
return "'" + argument + "'"
#endif
}


private var testInputsPath: AbsolutePath {
get throws {
var root: AbsolutePath = try AbsolutePath(validating: #file)
Expand Down Expand Up @@ -1659,7 +1699,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
let deps =
try dependencyOracle.getImports(workingDirectory: path,
moduleAliases: ["Car": "Bar"],
commandLine: scannerCommand,
commandLine: scannerCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 }},
diagnostics: &scanDiagnostics)

XCTAssertTrue(deps.imports.contains("Bar"))
Expand Down Expand Up @@ -1891,7 +1931,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let dependencyGraph =
try dependencyOracle.getDependencies(workingDirectory: path,
commandLine: scannerCommand,
commandLine: scannerCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics)

let fooDependencyInfo = try XCTUnwrap(dependencyGraph.modules[.swiftPrebuiltExternal("Foo")])
Expand Down Expand Up @@ -1942,7 +1982,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let imports =
try dependencyOracle.getImports(workingDirectory: path,
commandLine: scannerCommand,
commandLine: scannerCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics)
let expectedImports = ["C", "E", "G", "Swift", "SwiftOnoneSupport"]
// Dependnig on how recent the platform we are running on, the _Concurrency module may or may not be present.
Expand Down Expand Up @@ -2049,7 +2089,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let _ =
try dependencyOracle.getDependencies(workingDirectory: path,
commandLine: scannerCommand,
commandLine: scannerCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics)
XCTAssertEqual(scanDiagnostics.count, 5)
let diags = try XCTUnwrap(scanDiagnostics)
Expand Down Expand Up @@ -2120,7 +2160,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let _ =
try dependencyOracle.getDependencies(workingDirectory: path,
commandLine: scannerCommand,
commandLine: scannerCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics)
XCTAssertEqual(scanDiagnostics.count, 2)
let diags = try XCTUnwrap(scanDiagnostics)
Expand Down Expand Up @@ -2299,7 +2339,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let dependencyGraph =
try dependencyOracle.getDependencies(workingDirectory: path,
commandLine: iterationCommand,
commandLine: iterationCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics)

// The _Concurrency and _StringProcessing modules are automatically
Expand Down Expand Up @@ -2452,7 +2492,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
do {
let _ =
try dependencyOracle.getDependencies(workingDirectory: path,
commandLine: scannerCommands[scanIndex],
commandLine: scannerCommands[scanIndex].map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics[scanIndex])
} catch {
XCTFail("Unexpected error: \(error)")
Expand Down Expand Up @@ -2627,7 +2667,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let dependencyGraph =
try dependencyOracle.getDependencies(workingDirectory: path,
commandLine: scannerCommand,
commandLine: scannerCommand.map { if argumentNeedsQuoting($0) { quoteArgument($0) } else { $0 } },
diagnostics: &scanDiagnostics)
let serializer = DOTModuleDependencyGraphSerializer(dependencyGraph)

Expand Down
Loading