1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
//===---- SwiftDriverToolingInterfaceTests.swift - Swift Driver Tests ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(Testing) import SwiftDriver
import SwiftOptions
import TSCBasic
import XCTest
import CToolingTestShim
final class SwiftDriverToolingInterfaceTests: XCTestCase {
func testCreateCompilerInvocation() throws {
try withTemporaryDirectory { path in
let inputFile = path.appending(components: "test.swift")
try localFileSystem.writeFileContents(inputFile) { $0.send("public func foo()") }
// Expected success scenarios:
do {
let testCommand = inputFile.description
var emittedDiagnostics: [Diagnostic] = []
XCTAssertFalse(getSingleFrontendInvocationFromDriverArgumentsV2(driverPath: "swiftc",
argList: testCommand.components(separatedBy: " "),
action: { _ in false },
diagnostics: &emittedDiagnostics,
diagnosticCallback: {_,_ in }))
}
do {
let testCommand = "-emit-executable " + inputFile.description + " main.swift lib.swift -module-name createCompilerInvocation -emit-module -emit-objc-header -o t.out"
var emittedDiagnostics: [Diagnostic] = []
XCTAssertFalse(getSingleFrontendInvocationFromDriverArgumentsV2(driverPath: "swiftc",
argList: testCommand.components(separatedBy: " "),
action: { _ in false },
diagnostics: &emittedDiagnostics,
diagnosticCallback: {_,_ in }))
}
do {
let testCommand = "-c " + inputFile.description + " main.swift lib.swift -module-name createCompilerInvocation -emit-module -emit-objc-header"
var emittedDiagnostics: [Diagnostic] = []
XCTAssertFalse(getSingleFrontendInvocationFromDriverArgumentsV2(driverPath: "swiftc",
argList: testCommand.components(separatedBy: " "),
action: { _ in false },
diagnostics: &emittedDiagnostics,
diagnosticCallback: {_,_ in }))
}
do {
let testCommand = inputFile.description + " -enable-batch-mode"
var emittedDiagnostics: [Diagnostic] = []
XCTAssertFalse(getSingleFrontendInvocationFromDriverArgumentsV2(driverPath: "swiftc",
argList: testCommand.components(separatedBy: " "),
action: { _ in false },
diagnostics: &emittedDiagnostics,
diagnosticCallback: {_,_ in }))
}
do { // Force no outputs
let testCommand = "-module-name foo -emit-module -emit-module-path /tmp/foo.swiftmodule -emit-objc-header -emit-objc-header-path /tmp/foo.h -enable-library-evolution -emit-module-interface -emit-module-interface-path /tmp/foo.swiftinterface -emit-library -emit-tbd -emit-tbd-path /tmp/foo.tbd -emit-dependencies -serialize-diagnostics " + inputFile.description
var resultingFrontendArgs: [String] = []
var emittedDiagnostics: [Diagnostic] = []
XCTAssertFalse(getSingleFrontendInvocationFromDriverArgumentsV2(driverPath: "swiftc",
argList: testCommand.components(separatedBy: " "),
action: { args in
resultingFrontendArgs = args
return false
},
diagnostics: &emittedDiagnostics,
diagnosticCallback: {_,_ in },
forceNoOutputs: true))
XCTAssertFalse(resultingFrontendArgs.contains("-emit-module-interface-path"))
XCTAssertFalse(resultingFrontendArgs.contains("-emit-objc-header"))
XCTAssertFalse(resultingFrontendArgs.contains("-emit-objc-header-path"))
XCTAssertFalse(resultingFrontendArgs.contains("-emit-module-path"))
XCTAssertFalse(resultingFrontendArgs.contains("-emit-tbd-path"))
}
// Expected failure scenarios:
do {
let testCommand = "-v" // No inputs
var emittedDiagnostics: [Diagnostic] = []
XCTAssertTrue(getSingleFrontendInvocationFromDriverArgumentsV2(driverPath: "swiftc",
argList: testCommand.components(separatedBy: " "),
action: { _ in false },
diagnostics: &emittedDiagnostics,
diagnosticCallback: {_,_ in }))
let errorMessage = try XCTUnwrap(emittedDiagnostics.first?.message.text)
XCTAssertEqual(errorMessage, "unable to handle compilation, expected exactly one frontend job")
}
}
}
func testCreateCompilerInvocationCAPI() throws {
try withTemporaryDirectory { path in
let inputFile = path.appending(components: "test.swift")
try localFileSystem.writeFileContents(inputFile) { $0.send("public func foo()") }
let driverPath = "swiftc"
// Basic compilation test
do {
let testCommandStr = "-emit-executable " + inputFile.description + " main.swift lib.swift -module-name createCompilerInvocation -emit-module -emit-objc-header -o t.out"
let testCommand = testCommandStr.split(separator: " ").compactMap { String($0) }
// Invoke the C shim from CToolingTestShim which calls the `getSingleFrontendInvocationFromDriverArgumentsV2`
// C API defined in Swift in ToolingUtil
XCTAssertFalse(driverPath.withCString { CBridgedDriverPath in
withArrayOfCStrings(testCommand) { CBridgedArgList in
getSingleFrontendInvocationFromDriverArgumentsTest(
CBridgedDriverPath,
CInt(testCommand.count),
CBridgedArgList!,
{ argc, argvPtr in
// Bridge argc back to [String]
let argvBufferPtr = UnsafeBufferPointer<UnsafePointer<CChar>?>(start: argvPtr, count: Int(argc))
let resultingFrontendArgs = argvBufferPtr.map { String(cString: $0!) }
print(resultingFrontendArgs)
XCTAssertTrue(resultingFrontendArgs.contains("-frontend"))
XCTAssertTrue(resultingFrontendArgs.contains("-c"))
XCTAssertTrue(resultingFrontendArgs.contains("-emit-module-path"))
XCTAssertTrue(resultingFrontendArgs.contains("-o"))
return false
},
{ diagKind, diagMessage in },
false)
}
})
}
// Diagnostic callback test
do {
let testCommandStr = "-v"
let testCommand = testCommandStr.split(separator: " ").compactMap { String($0) }
XCTAssertTrue(driverPath.withCString { CBridgedDriverPath in
withArrayOfCStrings(testCommand) { CBridgedArgList in
getSingleFrontendInvocationFromDriverArgumentsTest(
CBridgedDriverPath,
CInt(testCommand.count),
CBridgedArgList!,
{ argc, argvPtr in false },
{ diagKind, diagMessage in
guard let nonOptionalDiagMessage = diagMessage else {
XCTFail("Invalid tooling diagnostic handler message")
return
}
XCTAssertEqual(diagKind, SWIFTDRIVER_TOOLING_DIAGNOSTIC_ERROR)
XCTAssertEqual(String(cString: nonOptionalDiagMessage),
"unable to handle compilation, expected exactly one frontend job")
},
false)
}
})
}
}
}
}
|