1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
#if os(macOS)
import class Foundation.Bundle
#endif
import TSCTestSupport
import XCTest
import struct Basics.AsyncProcessResult
import struct TSCUtility.Version
@_exported import func TSCTestSupport.XCTAssertMatch
@_exported import func TSCTestSupport.XCTAssertNoMatch
@_exported import func TSCTestSupport.XCTAssertResultSuccess
@_exported import func TSCTestSupport.XCTAssertThrows
public func XCTAssertFileExists(_ path: AbsolutePath, file: StaticString = #file, line: UInt = #line) {
TSCTestSupport.XCTAssertFileExists(TSCAbsolutePath(path), file: file, line: line)
}
public func XCTAssertDirectoryExists(_ path: AbsolutePath, file: StaticString = #file, line: UInt = #line) {
TSCTestSupport.XCTAssertDirectoryExists(TSCAbsolutePath(path), file: file, line: line)
}
public func XCTAssertNoSuchPath(_ path: AbsolutePath, file: StaticString = #file, line: UInt = #line) {
TSCTestSupport.XCTAssertNoSuchPath(TSCAbsolutePath(path), file: file, line: line)
}
public func XCTAssertEqual<T:Equatable, U:Equatable> (_ lhs:(T,U), _ rhs:(T,U), file: StaticString = #file, line: UInt = #line) {
TSCTestSupport.XCTAssertEqual(lhs, rhs, file: file, line: line)
}
public func XCTSkipIfCI(file: StaticString = #filePath, line: UInt = #line) throws {
if let ci = ProcessInfo.processInfo.environment["CI"] as? NSString, ci.boolValue {
throw XCTSkip("Skipping because the test is being run on CI", file: file, line: line)
}
}
/// An `async`-friendly replacement for `XCTAssertThrowsError`.
public func XCTAssertAsyncThrowsError<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line,
_ errorHandler: (_ error: any Error) -> Void = { _ in }
) async {
do {
_ = try await expression()
XCTFail(message(), file: file, line: line)
} catch {
errorHandler(error)
}
}
package func XCTAssertAsyncNoThrow<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) async {
do {
_ = try await expression()
} catch {
XCTAssertNoThrow({ throw error }, message(), file: file, line: line)
}
}
public func XCTAssertBuilds(
_ path: AbsolutePath,
configurations: Set<Configuration> = [.Debug, .Release],
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Xswiftc: [String] = [],
env: Environment? = nil,
file: StaticString = #file,
line: UInt = #line
) async {
for conf in configurations {
await XCTAssertAsyncNoThrow(
try await executeSwiftBuild(
path,
configuration: conf,
extraArgs: extraArgs,
Xcc: Xcc,
Xld: Xld,
Xswiftc: Xswiftc,
env: env
),
file: file,
line: line
)
}
}
public func XCTAssertSwiftTest(
_ path: AbsolutePath,
configuration: Configuration = .Debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Xswiftc: [String] = [],
env: Environment? = nil,
file: StaticString = #file,
line: UInt = #line
) async {
await XCTAssertAsyncNoThrow(
try await executeSwiftTest(
path,
configuration: configuration,
extraArgs: extraArgs,
Xcc: Xcc,
Xld: Xld,
Xswiftc: Xswiftc,
env: env
),
file: file,
line: line
)
}
@discardableResult
public func XCTAssertBuildFails(
_ path: AbsolutePath,
Xcc: [String] = [],
Xld: [String] = [],
Xswiftc: [String] = [],
env: Environment? = nil,
file: StaticString = #file,
line: UInt = #line
) async -> CommandExecutionError? {
var failure: CommandExecutionError? = nil
await XCTAssertThrowsCommandExecutionError(try await executeSwiftBuild(path, Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc), file: file, line: line) { error in
failure = error
}
return failure
}
public func XCTAssertEqual<T: CustomStringConvertible>(
_ assignment: [(container: T, version: Version)],
_ expected: [T: Version],
file: StaticString = #file,
line: UInt = #line
) where T: Hashable {
var actual = [T: Version]()
for (identifier, binding) in assignment {
actual[identifier] = binding
}
XCTAssertEqual(actual, expected, file: file, line: line)
}
public func XCTAssertAsyncTrue(
_ expression: @autoclosure () async throws -> Bool,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) async rethrows {
let result = try await expression()
XCTAssertTrue(result, message(), file: file, line: line)
}
public func XCTAssertAsyncFalse(
_ expression: @autoclosure () async throws -> Bool,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) async rethrows {
let result = try await expression()
XCTAssertFalse(result, message(), file: file, line: line)
}
package func XCTAssertAsyncNil(
_ expression: @autoclosure () async throws -> Any?,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) async rethrows {
let result = try await expression()
XCTAssertNil(result, message(), file: file, line: line)
}
public func XCTAssertThrowsCommandExecutionError<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line,
_ errorHandler: (_ error: CommandExecutionError) -> Void = { _ in }
) async {
await XCTAssertAsyncThrowsError(try await expression(), message(), file: file, line: line) { error in
guard case SwiftPMError.executionFailure(let processError, let stdout, let stderr) = error,
case AsyncProcessResult.Error.nonZeroExit(let processResult) = processError,
processResult.exitStatus != .terminated(code: 0) else {
return XCTFail("Unexpected error type: \(error.interpolationDescription)", file: file, line: line)
}
errorHandler(CommandExecutionError(result: processResult, stdout: stdout, stderr: stderr))
}
}
public func XCTAssertAsyncEqual<T: Equatable>(
_ expression1: @autoclosure () async throws -> T,
_ expression2: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #file,
line: UInt = #line
) async rethrows {
let value1 = try await expression1()
let value2 = try await expression2()
XCTAssertEqual(value1, value2, message(), file: file, line: line)
}
struct XCAsyncTestErrorWhileUnwrappingOptional: Error {}
public func XCTAsyncUnwrap<T>(
_ expression: @autoclosure () async throws -> T?,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) async throws -> T {
guard let result = try await expression() else {
throw XCAsyncTestErrorWhileUnwrappingOptional()
}
return result
}
public struct CommandExecutionError: Error {
package let result: AsyncProcessResult
public let stdout: String
public let stderr: String
}
|