diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 50bbd2df..4e8c8316 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -15,10 +15,11 @@ jobs: - name: Build on macOS 10.15 with Swift 5.2 run: | sudo xcode-select --switch /Applications/Xcode_11.7.app/Contents/Developer - swift build + swift test -c release --enable-test-discovery + swift build -c release brew bundle - cd TestApp && ../.build/debug/carton test - ../.build/debug/carton bundle + cd TestApp && ../.build/release/carton test + ../.build/release/carton bundle # the token is required to get around GitHub API limits when downloading the toolchain env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -31,10 +32,11 @@ jobs: - name: Build on macOS 11.0 with Swift 5.2 run: | sudo xcode-select --switch /Applications/Xcode_11.7.app/Contents/Developer - swift build + swift test -c release --enable-test-discovery + swift build -c release brew bundle - cd TestApp && ../.build/debug/carton test - ../.build/debug/carton bundle + cd TestApp && ../.build/release/carton test + ../.build/release/carton bundle env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -46,10 +48,11 @@ jobs: - name: Build on macOS 10.15 with Swift 5.3 run: | sudo xcode-select --switch /Applications/Xcode_12.app/Contents/Developer - swift build + swift test -c release --enable-test-discovery + swift build -c release brew bundle - cd TestApp && ../.build/debug/carton test - ../.build/debug/carton bundle + cd TestApp && ../.build/release/carton test + ../.build/release/carton bundle env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -61,10 +64,11 @@ jobs: - name: Build on macOS 11.0 with Swift 5.3 run: | sudo xcode-select --switch /Applications/Xcode_12.1.app/Contents/Developer - swift build + swift test -c release --enable-test-discovery + swift build -c release brew bundle - cd TestApp && ../.build/debug/carton test - ../.build/debug/carton bundle + cd TestApp && ../.build/release/carton test + ../.build/release/carton bundle env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -76,12 +80,13 @@ jobs: - name: Build on Ubuntu 18.04 with Swift 5.3 run: | - swift build + swift test -c release --enable-test-discovery + swift build -c release sudo ./install_ubuntu_deps.sh curl https://get.wasmer.io -sSfL | sh source /home/runner/.wasmer/wasmer.sh - cd TestApp && ../.build/debug/carton test - ../.build/debug/carton bundle + cd TestApp && ../.build/release/carton test + ../.build/release/carton bundle env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -93,12 +98,13 @@ jobs: - name: Build on Ubuntu 20.04 with Swift 5.3 run: | - swift build + swift test -c release --enable-test-discovery + swift build -c release sudo ./install_ubuntu_deps.sh curl https://get.wasmer.io -sSfL | sh source /home/runner/.wasmer/wasmer.sh - cd TestApp && ../.build/debug/carton test - ../.build/debug/carton bundle + cd TestApp && ../.build/release/carton test + ../.build/release/carton bundle env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Package.swift b/Package.swift index 0109bcab..a6ffffd6 100644 --- a/Package.swift +++ b/Package.swift @@ -77,7 +77,11 @@ let package = Package( ), .testTarget( name: "CartonTests", - dependencies: ["carton"] + dependencies: [ + "carton", + "CartonHelpers", + .product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"), + ] ), ] ) diff --git a/Sources/CartonHelpers/DiagnosticsParser.swift b/Sources/CartonHelpers/DiagnosticsParser.swift index 652eb1b0..49135349 100644 --- a/Sources/CartonHelpers/DiagnosticsParser.swift +++ b/Sources/CartonHelpers/DiagnosticsParser.swift @@ -88,7 +88,7 @@ private struct TerminalOutputFormat: OutputFormat { /// The compiler output often repeats iteself, and the diagnostics can sometimes be /// difficult to read. /// This reformats them to a more readable output. -struct DiagnosticsParser { +public struct DiagnosticsParser { // swiftlint:disable force_try enum Regex { /// The output has moved to a new file @@ -105,7 +105,7 @@ struct DiagnosticsParser { let line: String.SubSequence let char: String.SubSequence let code: String - let message: String.SubSequence + let message: String enum Kind: String { case error, warning, note @@ -121,7 +121,9 @@ struct DiagnosticsParser { fileprivate static let highlighter = SyntaxHighlighter(format: TerminalOutputFormat()) - func parse(_ output: String, _ terminal: InteractiveWriter) { + public init() {} + + public func parse(_ output: String, _ terminal: InteractiveWriter) { let lines = output.split(separator: "\n") var lineIdx = 0 @@ -157,7 +159,7 @@ struct DiagnosticsParser { line: components[0], char: components[1], code: String(lines[lineIdx]), - message: components[3] + message: components.dropFirst(3).joined(separator: ":") ) ) } diff --git a/Tests/CartonTests/CartonTests.swift b/Tests/CartonTests/CartonTests.swift index c9a76a4e..a98a4cd2 100644 --- a/Tests/CartonTests/CartonTests.swift +++ b/Tests/CartonTests/CartonTests.swift @@ -1,8 +1,22 @@ +import CartonHelpers import class Foundation.Bundle +import TSCBasic import XCTest final class CartonTests: XCTestCase { - func testExample() throws { + /// 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 + } + + func testVersion() throws { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct // results. @@ -20,28 +34,66 @@ final class CartonTests: XCTestCase { let pipe = Pipe() process.standardOutput = pipe + process.arguments = ["--version"] try process.run() process.waitUntilExit() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8) - XCTAssertEqual(output, "Hello, world!\n") + XCTAssertEqual(output?.trimmingCharacters(in: .whitespacesAndNewlines), "0.7.1") } - /// 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() + final class TestOutputStream: OutputByteStream { + var bytes: [UInt8] = [] + var currentOutput: String { + String(bytes: bytes, encoding: .utf8)! + } + + var position: Int = 0 + + init() {} + + func flush() {} + + func write(_ byte: UInt8) { + bytes.append(byte) + } + + func write(_ bytes: C) where C: Collection, C.Element == UInt8 { + self.bytes.append(contentsOf: bytes) } - fatalError("couldn't find the products directory") - #else - return Bundle.main.bundleURL - #endif } - static var allTests = [ - ("testExample", testExample), - ] + func testDiagnosticsParser() { + // swiftlint:disable line_length + let testDiagnostics = """ + [1/1] Compiling TokamakCore Font.swift + /Users/username/Project/Sources/TokamakCore/Tokens/Font.swift:58:15: error: invalid redeclaration of 'resolve(in:)' + public func resolve(in environment: EnvironmentValues) -> _Font { + ^ + /Users/username/Project/Sources/TokamakCore/Tokens/Font.swift:55:15: note: 'resolve(in:)' previously declared here + public func resolve(in environment: EnvironmentValues) -> _Font { + ^ + """ + let expectedOutput = """ + \u{001B}[1m\u{001B}[7m Font.swift \u{001B}[0m /Users/username/Project/Sources/TokamakCore/Tokens/Font.swift:58 + + \u{001B}[41;1m\u{001B}[37;1m ERROR \u{001B}[0m invalid redeclaration of 'resolve(in:)' + \u{001B}[36m58 | \u{001B}[0m \u{001B}[35;1mpublic\u{001B}[0m \u{001B}[35;1mfunc\u{001B}[0m resolve(in environment: \u{001B}[94mEnvironmentValues\u{001B}[0m) -> \u{001B}[94m_Font\u{001B}[0m { + | ^ + + \u{001B}[7m\u{001B}[37;1m NOTE \u{001B}[0m 'resolve(in:)' previously declared here + \u{001B}[36m55 | \u{001B}[0m \u{001B}[35;1mpublic\u{001B}[0m \u{001B}[35;1mfunc\u{001B}[0m resolve(in environment: \u{001B}[94mEnvironmentValues\u{001B}[0m) -> \u{001B}[94m_Font\u{001B}[0m { + | ^ + + + + """ + // swiftlint:enable line_length + let stream = TestOutputStream() + let writer = InteractiveWriter(stream: stream) + DiagnosticsParser().parse(testDiagnostics, writer) + XCTAssertEqual(stream.currentOutput, expectedOutput) + } } diff --git a/Tests/CartonTests/XCTestManifests.swift b/Tests/CartonTests/XCTestManifests.swift deleted file mode 100644 index beb955f2..00000000 --- a/Tests/CartonTests/XCTestManifests.swift +++ /dev/null @@ -1,9 +0,0 @@ -import XCTest - -#if !canImport(ObjectiveC) -public func allTests() -> [XCTestCaseEntry] { - [ - testCase(CartonTests.allTests), - ] -} -#endif diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 505761f5..a1e3520f 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -1,6 +1 @@ -import CartonTests -import XCTest - -var tests = [XCTestCaseEntry]() -tests += CartonTests.allTests() -XCTMain(tests) +fatalError("Use `swift test --enable-test-discovery` to run tests")