-
-
Notifications
You must be signed in to change notification settings - Fork 508
/
TuistTestCase.swift
113 lines (93 loc) · 3.58 KB
/
TuistTestCase.swift
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
import Basic
import Foundation
import XCTest
@testable import TuistSupport
// This mock file handler is used to override both, the current path and the temporary directory
// returned by the inTemporaryDirectory method. The temporary directory is lazily created if either
// the test case or subject consume the API.
private class MockFileHandler: FileHandler {
let temporaryDirectory: () throws -> (AbsolutePath)
init(temporaryDirectory: @escaping () throws -> (AbsolutePath)) {
self.temporaryDirectory = temporaryDirectory
super.init()
}
// swiftlint:disable:next force_try
override var currentPath: AbsolutePath { try! self.temporaryDirectory() }
override func inTemporaryDirectory(_ closure: (AbsolutePath) throws -> Void) throws {
try closure(temporaryDirectory())
}
}
public class TuistTestCase: XCTestCase {
fileprivate var temporaryDirectory: TemporaryDirectory!
public var printer: MockPrinter!
public override func setUp() {
super.setUp()
// Printer
printer = MockPrinter()
Printer.shared = printer
// FileHandler
FileHandler.shared = MockFileHandler(temporaryDirectory: { try self.temporaryPath() })
}
public override func tearDown() {
// Printer
printer = nil
Printer.shared = Printer()
temporaryDirectory = nil
super.tearDown()
}
public func temporaryPath() throws -> AbsolutePath {
if temporaryDirectory == nil {
temporaryDirectory = try TemporaryDirectory(removeTreeOnDeinit: true)
}
return temporaryDirectory.path
}
@discardableResult
public func createFiles(_ files: [String]) throws -> [AbsolutePath] {
let temporaryPath = try self.temporaryPath()
let fileHandler = FileHandler()
let paths = files.map { temporaryPath.appending(RelativePath($0)) }
try paths.forEach {
try fileHandler.touch($0)
}
return paths
}
@discardableResult
public func createFolders(_ folders: [String]) throws -> [AbsolutePath] {
let temporaryPath = try self.temporaryPath()
let fileHandler = FileHandler()
let paths = folders.map { temporaryPath.appending(RelativePath($0)) }
try paths.forEach {
try fileHandler.createFolder($0)
}
return paths
}
public func XCTAssertPrinterOutputContains(_ expected: String, file: StaticString = #file, line: UInt = #line) {
let message = """
The standard output:
===========
\(printer.standardOutput)
Doesn't contain the expected output:
===========
\(expected)
"""
XCTAssertTrue(printer.standardOutputMatches(with: expected), message, file: file, line: line)
}
public func XCTAssertPrinterErrorContains(_ expected: String, file: StaticString = #file, line: UInt = #line) {
let message = """
The standard error:
===========
\(printer.standardError)
Doesn't contain the expected output:
===========
\(expected)
"""
XCTAssertTrue(printer.standardErrorMatches(with: expected), message, file: file, line: line)
}
public func temporaryFixture(_ pathString: String) throws -> AbsolutePath {
let path = RelativePath(pathString)
let fixturePath = self.fixturePath(path: path)
let destinationPath = (try temporaryPath()).appending(component: path.basename)
try FileHandler.shared.copy(from: fixturePath, to: destinationPath)
return destinationPath
}
}