Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Rewrite Swift runner using XCTestObservation #448

Closed
@kazk

Description

@kazk

Swift runner is huge (#328) and takes forever to build (#422) because it's not following the best practices and compiling the entire Swift project from source.

This can be avoided by using the official builds, writing our own XCTMain and CodewarsObserver implementing XCTestObservation protocol.
Existing Kata should be able to migrate by replacing XCTMain with _XCTMain, but I'm not sure.

// `XCTMain` without filtering, using `CodewarsObserver`.
@_exported import Foundation
import XCTest
import Glibc

public func _XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
  XCTestObservationCenter.shared().addTestObserver(CodewarsObserver())
  let root = XCTestSuite(name: "All tests")
  let current = XCTestSuite(name: "\(Bundle.main.bundleURL.lastPathComponent).xctest")
  root.addTest(current)
  testCases.map(TestCaseSuite.init).forEach(current.addTest)
  root.run()
  exit(root.testRun!.totalFailureCount == 0 ? 0 : 1)
}

fileprivate class TestCaseSuite: XCTestSuite {
  // ...
}
import XCTest

public class CodewarsObserver: XCTestObservation {
  func testBundleWillStart(_ testBundle: Bundle) {}
  func testSuiteWillStart(_ testSuite: XCTestSuite) {}
  func testCaseWillStart(_ testCase: XCTestCase) {}
  func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) {}
  func testCaseDidFinish(_ testCase: XCTestCase) {}
  func testSuiteDidFinish(_ testSuite: XCTestSuite) {}
  func testBundleDidFinish(_ testBundle: Bundle) {}
}
// entry point
import XCTest

_XCTMain([testCase(T1.allTests), testCase(T2.allTests)])
<DESCRIBE::>T1
<IT::>test_foo
<PASSED::>Test Passed
<COMPLETEDIN::>0.000
<COMPLETEDIN::>0.000
<DESCRIBE::>T2
<IT::>test_bar
<PASSED::>Test Passed
<COMPLETEDIN::>0.000
<COMPLETEDIN::>0.000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions