Skip to content
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

Add XCTAssertThrowsSpecific #535

Merged
merged 10 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Please, check out guidelines: https://keepachangelog.com/en/1.0.0/

### Added

- XCTAssertThrowsSpecific convenient function to test for specific errors https://github.com/tuist/tuist/pull/535 by @fortmarek
- `HTTPClient` utility class to `TuistEnvKit` https://github.com/tuist/tuist/pull/508 by @pepibumur.

### Changed
Expand Down
11 changes: 11 additions & 0 deletions Sources/TuistCoreTesting/Extensions/XCTestCase+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public extension XCTestCase {
return value
}

// swiftlint:disable:next line_length
func XCTAssertThrowsSpecific<Error: Swift.Error & Equatable, T>(_ closure: @autoclosure () throws -> T, _ error: Error, file: StaticString = #file, line: UInt = #line) {
do {
_ = try closure()
} catch let closureError as Error {
XCTAssertEqual(error, closureError, file: file, line: line)
} catch let closureError {
XCTFail("\(error) is not equal to: \(closureError)", file: file, line: line)
}
}

func XCTAssertCodableEqualToJson<C: Codable>(_ subject: C, _ json: String, file: StaticString = #file, line: UInt = #line) {
let decoder = JSONDecoder()
let decoded = XCTTry(try decoder.decode(C.self, from: json.data(using: .utf8)!), file: file, line: line)
Expand Down
4 changes: 3 additions & 1 deletion Sources/TuistKit/Models/Up/UpHomebrew.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class UpHomebrew: Up, GraphInitiatable {
override func meet(projectPath _: AbsolutePath) throws {
if !toolInstalled("brew") {
Printer.shared.print("Installing Homebrew")
try System.shared.runAndPrint("/usr/bin/ruby", "-e", "\"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"",
try System.shared.runAndPrint("/usr/bin/ruby",
"-e",
"\"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"",
verbose: true,
environment: System.shared.env)
}
Expand Down
4 changes: 1 addition & 3 deletions Tests/TuistCoreTests/Utils/OpenerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ final class OpenerTests: TuistUnitTestCase {
func test_open_when_path_doesnt_exist() throws {
let path = fileHandler.currentPath.appending(component: "tool")

XCTAssertThrowsError(try subject.open(path: path)) {
XCTAssertEqual($0 as? OpeningError, OpeningError.notFound(path))
}
XCTAssertThrowsSpecific(try subject.open(path: path), OpeningError.notFound(path))
}

func test_open() throws {
Expand Down
7 changes: 1 addition & 6 deletions Tests/TuistCoreTests/Xcode/XcodeControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ final class XcodeControllerTests: TuistUnitTestCase {
system.errorCommand(["xcode-select", "-p"])

// Then
do {
_ = try subject.selectedVersion()
XCTFail()
} catch {
XCTAssertEqual(error as? XcodeController.XcodeVersionError, XcodeController.XcodeVersionError.noXcode)
}
XCTAssertThrowsSpecific(try subject.selectedVersion(), XcodeController.XcodeVersionError.noXcode)
}

func test_selectedVersion_when_xcodeSelectReturnsThePath() throws {
Expand Down
4 changes: 1 addition & 3 deletions Tests/TuistEnvKitTests/Commands/BundleCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ final class BundleCommandTests: TuistUnitTestCase {

func test_run_throws_when_there_is_no_xmp_version_in_the_directory() throws {
let result = try parser.parse([])
XCTAssertThrowsError(try subject.run(with: result)) {
XCTAssertEqual($0 as? BundleCommandError, BundleCommandError.missingVersionFile(fileHandler.currentPath))
}
XCTAssertThrowsSpecific(try subject.run(with: result), BundleCommandError.missingVersionFile(fileHandler.currentPath))
}

func test_run_installs_the_app_if_it_doesnt_exist() throws {
Expand Down
4 changes: 1 addition & 3 deletions Tests/TuistEnvKitTests/GitHub/ReleaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ final class ReleaseTests: XCTestCase {
json["tag_name"] = "3.2"
let decoder = JSONDecoder()
let data = try JSONSerialization.data(withJSONObject: json, options: [])
XCTAssertThrowsError(try decoder.decode(Release.self, from: data)) {
XCTAssertEqual($0 as? ReleaseDecodeError, ReleaseDecodeError.invalidVersionFormat("3.2"))
}
XCTAssertThrowsSpecific(try decoder.decode(Release.self, from: data), ReleaseDecodeError.invalidVersionFormat("3.2"))
}

func test_release_coding_keys() {
Expand Down
10 changes: 3 additions & 7 deletions Tests/TuistEnvKitTests/Installer/InstallerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ final class InstallerTests: TuistUnitTestCase {
}

let expectedError = InstallerError.incompatibleSwiftVersion(local: "4.2.1", expected: "5.0.0")
XCTAssertThrowsError(try subject.install(version: version,
temporaryDirectory: temporaryDirectory)) { error in
XCTAssertEqual(error as? InstallerError, expectedError)
}
XCTAssertThrowsSpecific(try subject.install(version: version,
temporaryDirectory: temporaryDirectory), expectedError)
XCTAssertPrinterOutputContains("Verifying the Swift version is compatible with your version 4.2.1")
}

Expand Down Expand Up @@ -247,9 +245,7 @@ final class InstallerTests: TuistUnitTestCase {
error: "did not match any file(s) known to git ")

let expected = InstallerError.versionNotFound(version)
XCTAssertThrowsError(try subject.install(version: version, temporaryDirectory: temporaryDirectory)) {
XCTAssertEqual($0 as? InstallerError, expected)
}
XCTAssertThrowsSpecific(try subject.install(version: version, temporaryDirectory: temporaryDirectory), expected)
}

// MARK: - Fileprivate
Expand Down
5 changes: 2 additions & 3 deletions Tests/TuistGeneratorTests/Generator/ProjectGroupsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ final class ProjectGroupsTests: XCTestCase {
subject = ProjectGroups.generate(project: project, pbxproj: pbxproj, sourceRootPath: sourceRootPath, playgrounds: playgrounds)

// When / Then
XCTAssertThrowsError(try subject.projectGroup(named: "abc")) { error in
XCTAssertEqual(error as? ProjectGroupsError, ProjectGroupsError.missingGroup("abc"))
}
XCTAssertThrowsSpecific(try subject.projectGroup(named: "abc"),
ProjectGroupsError.missingGroup("abc"))
}

func test_projectGroup_knownProjectGroups() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ final class GraphCircularDetectorTests: XCTestCase {
func test_start_throws_when_a_circular_dependency_is_found() throws {
try subject.start(from: node("a"), to: node("b"))
try subject.start(from: node("b"), to: node("c"))
XCTAssertThrowsError(try subject.start(from: node("c"), to: node("a"))) { error in
XCTAssertEqual(error as? GraphLoadingError, GraphLoadingError.circularDependency(node("c"), node("a")))
}
XCTAssertThrowsSpecific(try subject.start(from: node("c"), to: node("a")),
GraphLoadingError.circularDependency(node("c"), node("a")))
}

func test_complete() throws {
Expand Down
5 changes: 2 additions & 3 deletions Tests/TuistGeneratorTests/Graph/SDKNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ final class SDKNodeTests: XCTestCase {
}

func test_sdk_usupportedTypes() throws {
XCTAssertThrowsError(try SDKNode(name: "FooBar", platform: .tvOS, status: .required)) { error in
XCTAssertEqual(error as? SDKNode.Error, .unsupported(sdk: "FooBar"))
}
XCTAssertThrowsSpecific(try SDKNode(name: "FooBar", platform: .tvOS, status: .required),
SDKNode.Error.unsupported(sdk: "FooBar"))
}

func test_sdk_errors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ final class CocoaPodsInteractorTests: TuistUnitTestCase {
cache.add(cocoapods: cocoapods)

// Then
XCTAssertThrowsError(try subject.install(graph: graph)) {
XCTAssertEqual($0 as? CocoaPodsInteractorError, CocoaPodsInteractorError.cocoapodsNotFound)
}
XCTAssertThrowsSpecific(try subject.install(graph: graph),
CocoaPodsInteractorError.cocoapodsNotFound)
}

func test_install_when_theCocoaPodsFromBundlerCanBeUsed() throws {
Expand Down
5 changes: 2 additions & 3 deletions Tests/TuistKitTests/Commands/DumpCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ final class DumpCommandTests: TuistUnitTestCase {
func test_run_throws_when_file_doesnt_exist() throws {
let tmpDir = try TemporaryDirectory(removeTreeOnDeinit: true)
let result = try parser.parse([DumpCommand.command, "-p", tmpDir.path.pathString])
XCTAssertThrowsError(try subject.run(with: result)) {
XCTAssertEqual($0 as? GraphManifestLoaderError, GraphManifestLoaderError.manifestNotFound(.project, tmpDir.path))
}
XCTAssertThrowsSpecific(try subject.run(with: result),
GraphManifestLoaderError.manifestNotFound(.project, tmpDir.path))
}

func test_run_throws_when_the_manifest_loading_fails() throws {
Expand Down
5 changes: 2 additions & 3 deletions Tests/TuistKitTests/Commands/GenerateCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ final class GenerateCommandTests: TuistUnitTestCase {
}

// When / Then
XCTAssertThrowsError(try subject.run(with: result)) {
XCTAssertEqual($0 as? GraphManifestLoaderError, GraphManifestLoaderError.manifestNotFound(path))
}
XCTAssertThrowsSpecific(try subject.run(with: result),
GraphManifestLoaderError.manifestNotFound(path))
}

func test_run_fatalErrors_when_theworkspaceGenerationFails() throws {
Expand Down
5 changes: 1 addition & 4 deletions Tests/TuistKitTests/Commands/InitCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ final class InitCommandTests: TuistUnitTestCase {

let result = try parser.parse(["init", "--path", path.pathString, "--name", "Test"])

XCTAssertThrowsError(try subject.run(with: result)) { error in
let expected = InitCommandError.nonEmptyDirectory(path)
XCTAssertEqual(error as? InitCommandError, expected)
}
XCTAssertThrowsSpecific(try subject.run(with: result), InitCommandError.nonEmptyDirectory(path))
}

func test_run_when_ios_application() throws {
Expand Down
5 changes: 2 additions & 3 deletions Tests/TuistKitTests/Embed/EmbeddableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ final class EmbeddableTests: XCTestCase {
func test_strip_throws() throws {
try withDSYM {
let path = $0.path
XCTAssertThrowsError(try $0.strip(keepingArchitectures: [])) {
XCTAssertEqual($0 as? EmbeddableError, EmbeddableError.unstrippableNonFatEmbeddable(path))
}
XCTAssertThrowsSpecific(try $0.strip(keepingArchitectures: []),
EmbeddableError.unstrippableNonFatEmbeddable(path))
}
}

Expand Down
7 changes: 2 additions & 5 deletions Tests/TuistKitTests/Generator/GeneratorModelLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,8 @@ class GeneratorModelLoaderTest: TuistUnitTestCase {
}

func test_platform_watchOSNotSupported() {
XCTAssertThrowsError(
try TuistGenerator.Platform.from(manifest: .watchOS)
) { error in
XCTAssertEqual(error as? GeneratorModelLoaderError, GeneratorModelLoaderError.featureNotYetSupported("watchOS platform"))
}
XCTAssertThrowsSpecific(try TuistGenerator.Platform.from(manifest: .watchOS),
GeneratorModelLoaderError.featureNotYetSupported("watchOS platform"))
}

func test_generatorModelLoaderError_type() {
Expand Down
10 changes: 4 additions & 6 deletions Tests/TuistKitTests/Playgrounds/PlaygroundGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ final class PlaygroundGeneratorTests: TuistUnitTestCase {
func test_generate_throws_when_playground_exists() throws {
let playgroundPath = fileHandler.currentPath.appending(component: "Test.playground")
try fileHandler.createFolder(playgroundPath)
let expectedError = PlaygroundGenerationError.alreadyExisting(playgroundPath)

XCTAssertThrowsError(try subject.generate(path: fileHandler.currentPath,
name: "Test",
platform: .iOS)) {
XCTAssertEqual($0 as? PlaygroundGenerationError, expectedError)
}
XCTAssertThrowsSpecific(try subject.generate(path: fileHandler.currentPath,
name: "Test",
platform: .iOS),
PlaygroundGenerationError.alreadyExisting(playgroundPath))
}

func test_generate_writes_content() throws {
Expand Down
10 changes: 4 additions & 6 deletions Tests/TuistKitTests/Setup/SetupLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ final class SetupLoaderTests: TuistUnitTestCase {
graphManifestLoader.loadSetupStub = { _ in throw GraphManifestLoaderError.manifestNotFound(.setup, projectPath) }

// when / then
XCTAssertThrowsError(try subject.meet(at: projectPath)) { error in
XCTAssertEqual(error as? GraphManifestLoaderError, GraphManifestLoaderError.manifestNotFound(.setup, projectPath))
}
XCTAssertThrowsSpecific(try subject.meet(at: projectPath),
GraphManifestLoaderError.manifestNotFound(.setup, projectPath))
}

func test_meet_when_actions_provided_then_lint_all_before_meet() {
Expand Down Expand Up @@ -102,9 +101,8 @@ final class SetupLoaderTests: TuistUnitTestCase {
graphManifestLoader.loadSetupStub = { _ in mockUps }

// when / then
XCTAssertThrowsError(try subject.meet(at: projectPath)) { error in
XCTAssertEqual(error as? LintingError, LintingError())
}
XCTAssertThrowsSpecific(try subject.meet(at: projectPath),
LintingError())

XCTAssertEqual(mockUps.map { $0.meetCallCount }, Array(repeating: 0, count: mockUps.count))
XCTAssertEqual(mockUps.map { $0.isMetCallCount }, Array(repeating: 0, count: mockUps.count))
Expand Down