-
Notifications
You must be signed in to change notification settings - Fork 2
/
runOptimisedUITests.swift
171 lines (140 loc) · 4.65 KB
/
runOptimisedUITests.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
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
#!/usr/bin/env swift
import Foundation
let workspace = "Pokedex.xcworkspace"
let scheme = "Pokedex-Workspace"
let device = "iPhone 15"
let osVersion = "17.2"
let testDataOutput = "output"
let platform = "iOS Simulator"
let sanityUITestTarget = "PokedexUITests"
// MARK: XcodeCommandBuilder
//
// xcodebuild -workspace Pokedex.xcworkspace -scheme Pokedex -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 14,OS=16.2" test
struct XcodeUITestingCommandBuilder {
static func buildTestCommand(workspace: String,
scheme: String,
platform: String,
device: String,
osVersion: String
) -> String {
"xcodebuild -workspace \(workspace) -scheme \(scheme) -sdk iphonesimulator -destination \"platform=\(platform),name=\(device),OS=\(osVersion)\" -enableCodeCoverage NO -quiet test"
}
static func buildDefaultTestCommand() -> String {
buildTestCommand(workspace: workspace,
scheme: scheme,
platform: platform,
device: device,
osVersion: osVersion)
}
}
let changedModules = findChangedModules()
let requiredTestingTargets = testingTargets(changedModules: changedModules)
let command = "tuist generate " + requiredTestingTargets
print(command)
var output = shell(command)
print(output)
print(XcodeUITestingCommandBuilder.buildDefaultTestCommand())
output = shell(XcodeUITestingCommandBuilder.buildDefaultTestCommand())
print(output)
func testingTargets(changedModules: [String]) -> String {
var targets = sanityUITestTarget
let uiTestingTargets = findAllUITestingTargets()
for module in changedModules {
if module != "Pokedex" {
let testingModule = module + "UITests"
if uiTestingTargets.contains(testingModule) {
targets += " \(testingModule)"
}
}
}
return targets
}
func findChangedModules() -> [String] {
let rawData = shell("bash ./scripts/filterModuleChanges.sh")
let trimmedFeatures = rawData.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
return Array(Set(trimmedFeatures.components(separatedBy: " ")))
}
func findAllUITestingTargets() -> [String] {
var output = shell("tuist generate --no-open")
output = shell("xcodebuild -list -project Pokedex.xcodeproj -json")
let jsonData = output.data(using: .utf8)!
guard let xcodeProject = try? JSONDecoder().decode(XcodeProject.self, from: jsonData) else {
return []
}
return xcodeProject.project.targets.filter { $0.contains("UITests")
}
}
// MARK: - XcodeProject
struct XcodeProject: Codable {
let project: Project
}
// MARK: - Project
struct Project: Codable {
let configurations: [String]
let name: String
let schemes, targets: [String]
}
// MARK: - shell wrapper function
func shell(_ command: String) -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/zsh"
task.standardInput = nil
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}
enum ANSIColor: String {
typealias This = ANSIColor
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case green = "\u{001B}[0;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
case `default` = "\u{001B}[0;0m"
static var values: [This] {
return [.black, .red, .green, .yellow, .blue, .magenta, .cyan, .white, .default]
}
static func + (lhs: This, rhs: String) -> String {
return lhs.rawValue + rhs
}
static func + (lhs: String, rhs: This) -> String {
return lhs + rhs.rawValue
}
}
extension String {
func colored(_ color: ANSIColor) -> String {
return color + self + ANSIColor.default
}
var black: String {
return colored(.black)
}
var red: String {
return colored(.red)
}
var green: String {
return colored(.green)
}
var yellow: String {
return colored(.yellow)
}
var blue: String {
return colored(.blue)
}
var magenta: String {
return colored(.magenta)
}
var cyan: String {
return colored(.cyan)
}
var white: String {
return colored(.white)
}
}