-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allow unit test targets to import and link executable targets #3316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
abertelrud
merged 3 commits into
swiftlang:main
from
abertelrud:allow-test-products-to-link-against-executables-take-two
Mar 7, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
abe8c1c
Add a fast way to look up the package in which a particular target or…
abertelrud 97a1b22
Allow SwiftTargetDescription, ClangTargetDescription, and ProductDesc…
abertelrud 05babe9
Allow unit tests to import and link any main modules of executables t…
abertelrud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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: 999.0 | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @abertelrud does this stuff really not work on any platform but macOS? |
||
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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.