-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow unit tests to import and link any main modules of executables t…
…hat are implemented in Swift. This uses a new Swift compiler flag to set the name of the entry point when emitting object code, and then uses linker flags to rename the main executable module's entry point back to `_main` again when actually linking the executable. This should possibly be guarded by a tools version check, since packages written this way won't be testable on older toolchains.
- Loading branch information
1 parent
f73c0c7
commit 668f8d1
Showing
10 changed files
with
167 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// swift-tools-version:5.3 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "TestableExe", | ||
targets: [ | ||
.target( | ||
name: "TestableExe1" | ||
), | ||
.target( | ||
name: "TestableExe2" | ||
), | ||
.target( | ||
name: "TestableExe3" | ||
), | ||
.testTarget( | ||
name: "TestableExeTests", | ||
dependencies: [ | ||
"TestableExe1", | ||
"TestableExe2", | ||
"TestableExe3", | ||
] | ||
), | ||
] | ||
) |
5 changes: 5 additions & 0 deletions
5
Fixtures/Miscellaneous/TestableExe/Sources/TestableExe1/main.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public func GetGreeting1() -> String { | ||
return "Hello, world" | ||
} | ||
|
||
print("\(GetGreeting1())!") |
5 changes: 5 additions & 0 deletions
5
Fixtures/Miscellaneous/TestableExe/Sources/TestableExe2/main.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public func GetGreeting2() -> String { | ||
return "Hello, planet" | ||
} | ||
|
||
print("\(GetGreeting2())!") |
1 change: 1 addition & 0 deletions
1
Fixtures/Miscellaneous/TestableExe/Sources/TestableExe3/include/TestableExe3.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const char * GetGreeting3(); |
10 changes: 10 additions & 0 deletions
10
Fixtures/Miscellaneous/TestableExe/Sources/TestableExe3/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdio.h> | ||
#include "include/TestableExe3.h" | ||
|
||
const char * GetGreeting3() { | ||
return "Hello, universe"; | ||
} | ||
|
||
int main() { | ||
printf("%s!\n", GetGreeting3()); | ||
} |
73 changes: 73 additions & 0 deletions
73
Fixtures/Miscellaneous/TestableExe/Tests/TestableExeTests/TestableExeTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import XCTest | ||
import TestableExe1 | ||
import TestableExe2 | ||
// import TestableExe3 | ||
import class Foundation.Bundle | ||
|
||
final class TestableExeTests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
|
||
print(GetGreeting1()) | ||
XCTAssertEqual(GetGreeting1(), "Hello, world") | ||
print(GetGreeting2()) | ||
XCTAssertEqual(GetGreeting2(), "Hello, planet") | ||
// XCTAssertEqual(String(cString: GetGreeting3()), "Hello, universe") | ||
|
||
// Some of the APIs that we use below are available in macOS 10.13 and above. | ||
guard #available(macOS 10.13, *) else { | ||
return | ||
} | ||
|
||
var execPath = productsDirectory.appendingPathComponent("TestableExe1") | ||
var process = Process() | ||
process.executableURL = execPath | ||
var pipe = Pipe() | ||
process.standardOutput = pipe | ||
try process.run() | ||
process.waitUntilExit() | ||
var data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
var output = String(data: data, encoding: .utf8) | ||
XCTAssertEqual(output, "Hello, world!\n") | ||
|
||
execPath = productsDirectory.appendingPathComponent("TestableExe2") | ||
process = Process() | ||
process.executableURL = execPath | ||
pipe = Pipe() | ||
process.standardOutput = pipe | ||
try process.run() | ||
process.waitUntilExit() | ||
data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
output = String(data: data, encoding: .utf8) | ||
XCTAssertEqual(output, "Hello, planet!\n") | ||
|
||
execPath = productsDirectory.appendingPathComponent("TestableExe3") | ||
process = Process() | ||
process.executableURL = execPath | ||
pipe = Pipe() | ||
process.standardOutput = pipe | ||
try process.run() | ||
process.waitUntilExit() | ||
data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
output = String(data: data, encoding: .utf8) | ||
XCTAssertEqual(output, "Hello, universe!\n") | ||
} | ||
|
||
/// Returns path to the built products directory. | ||
var productsDirectory: URL { | ||
#if os(macOS) | ||
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { | ||
return bundle.bundleURL.deletingLastPathComponent() | ||
} | ||
fatalError("couldn't find the products directory") | ||
#else | ||
return Bundle.main.bundleURL | ||
#endif | ||
} | ||
|
||
static var allTests = [ | ||
("testExample", testExample), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters