Skip to content

Commit dcac8c5

Browse files
authored
feat(65): Allow project in subfolder (#66)
1 parent 63e4a61 commit dcac8c5

File tree

3 files changed

+130
-95
lines changed

3 files changed

+130
-95
lines changed

Sources/DependencyCalculator/DependencyGraph.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ extension WorkspaceInfo {
295295
var dependsOn: [TargetIdentity: Set<TargetIdentity>] = [:]
296296
var files: [TargetIdentity: Set<Path>] = [:]
297297
var folders: [Path: TargetIdentity] = [:]
298-
var candidateTestPlan: String? = nil
298+
var candidateTestPlan: Path? = nil
299299

300300
var packagesByName: [String: PackageTargetMetadata] = packages.toDictionary(path: \.name)
301301
let targetsByName = project.pbxproj.nativeTargets.toDictionary(path: \.name)
@@ -385,14 +385,14 @@ extension WorkspaceInfo {
385385
// Find existing test plans
386386
project.sharedData?.schemes.forEach { scheme in
387387
scheme.testAction?.testPlans?.forEach { plan in
388-
candidateTestPlan = plan.reference.replacingOccurrences(of: "container:", with: "")
388+
candidateTestPlan = path.parent() + plan.reference.replacingOccurrences(of: "container:", with: "")
389389
}
390390
}
391391

392392
return WorkspaceInfo(files: files,
393393
folders: folders,
394394
dependencyStructure: DependencyGraph(dependsOn: dependsOn),
395-
candidateTestPlan: candidateTestPlan)
395+
candidateTestPlan: candidateTestPlan?.string)
396396
}
397397

398398
private static func isSwiftVersion6Plus() throws -> Bool {

Tests/SelectiveTestingTests/IntegrationTestTool.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ import XCTest
1313
final class IntegrationTestTool {
1414
var projectPath: Path = ""
1515

16-
func setUp() throws {
16+
func setUp(subfolder: Bool = false) throws {
1717
let tmpPath = Path.temporary.absolute()
1818
guard let exampleInBundle = Bundle.module.path(forResource: "ExampleProject", ofType: "") else {
1919
fatalError("Missing ExampleProject in TestBundle")
2020
}
2121
projectPath = tmpPath + "ExampleProject"
2222
try? FileManager.default.removeItem(atPath: projectPath.string)
23-
try FileManager.default.copyItem(atPath: exampleInBundle, toPath: projectPath.string)
23+
if subfolder {
24+
let finalPath = (projectPath + "Subfolder").string
25+
try FileManager.default.createDirectory(atPath: projectPath.string, withIntermediateDirectories: true)
26+
try FileManager.default.copyItem(atPath: exampleInBundle, toPath: finalPath)
27+
}
28+
else {
29+
try FileManager.default.copyItem(atPath: exampleInBundle, toPath: projectPath.string)
30+
}
2431
FileManager.default.changeCurrentDirectoryPath(projectPath.string)
2532
try Shell.execOrFail("git init")
2633
try Shell.execOrFail("git config commit.gpgsign false")
@@ -33,6 +40,12 @@ final class IntegrationTestTool {
3340
func tearDown() throws {
3441
try? FileManager.default.removeItem(atPath: projectPath.string)
3542
}
43+
44+
func withTestTool(subfolder: Bool = false, closure: () async throws -> Void) async throws {
45+
try setUp(subfolder: subfolder)
46+
try await closure()
47+
try tearDown()
48+
}
3649

3750
func changeFile(at path: Path) throws {
3851
let handle = FileHandle(forUpdatingAtPath: path.string)!

Tests/SelectiveTestingTests/SelectiveTestingWorkspaceTests.swift

Lines changed: 112 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,114 +11,136 @@ import XCTest
1111
final class SelectiveTestingWorksapceTests: XCTestCase {
1212
let testTool = IntegrationTestTool()
1313

14-
override func setUp() async throws {
15-
try await super.setUp()
16-
17-
try testTool.setUp()
18-
}
19-
20-
override func tearDown() async throws {
21-
try await super.tearDown()
22-
23-
try testTool.tearDown()
24-
}
25-
2614
func testProjectLoading_empty() async throws {
27-
// given
28-
let tool = try testTool.createSUT()
29-
// when
30-
let result = try await tool.run()
31-
// then
32-
XCTAssertEqual(result, Set())
15+
try await testTool.withTestTool {
16+
// given
17+
let tool = try testTool.createSUT()
18+
// when
19+
let result = try await tool.run()
20+
// then
21+
XCTAssertEqual(result, Set())
22+
}
3323
}
3424

3525
func testProjectLoading_changeLibrary() async throws {
36-
// given
37-
let tool = try testTool.createSUT()
38-
// when
39-
try testTool.changeFile(at: testTool.projectPath + "ExampleLibrary/ExampleLibrary/ExampleLibrary.swift")
40-
41-
// then
42-
let result = try await tool.run()
43-
XCTAssertEqual(result, Set([testTool.mainProjectMainTarget,
44-
testTool.mainProjectTests,
45-
testTool.mainProjectUITests,
46-
testTool.exampleLibrary,
47-
testTool.exampleLibraryTests]))
26+
try await testTool.withTestTool {
27+
// given
28+
let tool = try testTool.createSUT()
29+
// when
30+
try testTool.changeFile(at: testTool.projectPath + "ExampleLibrary/ExampleLibrary/ExampleLibrary.swift")
31+
32+
// then
33+
let result = try await tool.run()
34+
XCTAssertEqual(result, Set([testTool.mainProjectMainTarget,
35+
testTool.mainProjectTests,
36+
testTool.mainProjectUITests,
37+
testTool.exampleLibrary,
38+
testTool.exampleLibraryTests]))
39+
}
4840
}
4941

5042
func testProjectLoading_changeAsset() async throws {
51-
// given
52-
let tool = try testTool.createSUT()
53-
// when
54-
try testTool.changeFile(at: testTool.projectPath + "ExampleProject/Assets.xcassets/Contents.json")
55-
56-
// then
57-
let result = try await tool.run()
58-
XCTAssertEqual(result, Set([testTool.mainProjectMainTarget,
59-
testTool.mainProjectTests,
60-
testTool.mainProjectUITests]))
43+
try await testTool.withTestTool {
44+
// given
45+
let tool = try testTool.createSUT()
46+
// when
47+
try testTool.changeFile(at: testTool.projectPath + "ExampleProject/Assets.xcassets/Contents.json")
48+
49+
// then
50+
let result = try await tool.run()
51+
XCTAssertEqual(result, Set([testTool.mainProjectMainTarget,
52+
testTool.mainProjectTests,
53+
testTool.mainProjectUITests]))
54+
}
6155
}
6256

6357
func testProjectLoading_testPlanChange() async throws {
64-
// given
65-
let tool = try testTool.createSUT()
66-
// when
67-
try testTool.changeFile(at: testTool.projectPath + "ExampleProject.xctestplan")
68-
69-
// then
70-
let result = try await tool.run()
71-
XCTAssertEqual(result, Set())
58+
try await testTool.withTestTool {
59+
// given
60+
let tool = try testTool.createSUT()
61+
// when
62+
try testTool.changeFile(at: testTool.projectPath + "ExampleProject.xctestplan")
63+
64+
// then
65+
let result = try await tool.run()
66+
XCTAssertEqual(result, Set())
67+
}
7268
}
7369

7470
func testProjectLoading_testWorkspaceFileChange() async throws {
75-
// given
76-
let tool = try testTool.createSUT()
77-
// when
78-
try testTool.changeFile(at: testTool.projectPath + "ExampleWorkspace.xcworkspace/contents.xcworkspacedata")
79-
// then
80-
let result = try await tool.run()
81-
XCTAssertEqual(result, Set([
82-
testTool.mainProjectMainTarget,
83-
testTool.mainProjectTests,
84-
testTool.mainProjectUITests,
85-
testTool.mainProjectLibrary,
86-
testTool.mainProjectLibraryTests,
87-
testTool.exampleLibraryTests,
88-
testTool.exampleLibrary,
89-
testTool.exampleLibraryInGroup,
90-
]))
71+
try await testTool.withTestTool {
72+
// given
73+
let tool = try testTool.createSUT()
74+
// when
75+
try testTool.changeFile(at: testTool.projectPath + "ExampleWorkspace.xcworkspace/contents.xcworkspacedata")
76+
// then
77+
let result = try await tool.run()
78+
XCTAssertEqual(result, Set([
79+
testTool.mainProjectMainTarget,
80+
testTool.mainProjectTests,
81+
testTool.mainProjectUITests,
82+
testTool.mainProjectLibrary,
83+
testTool.mainProjectLibraryTests,
84+
testTool.exampleLibraryTests,
85+
testTool.exampleLibrary,
86+
testTool.exampleLibraryInGroup,
87+
]))
88+
}
9189
}
9290

9391
func testProjectLoading_testProjectFileChange() async throws {
94-
// given
95-
let tool = try testTool.createSUT()
96-
// when
97-
try testTool.changeFile(at: testTool.projectPath + "ExampleProject.xcodeproj/project.pbxproj")
98-
99-
// then
100-
let result = try await tool.run()
101-
XCTAssertEqual(result, Set([
102-
testTool.mainProjectMainTarget,
103-
testTool.mainProjectTests,
104-
testTool.mainProjectUITests,
105-
testTool.mainProjectLibrary,
106-
testTool.mainProjectLibraryTests,
107-
]))
92+
try await testTool.withTestTool {
93+
// given
94+
let tool = try testTool.createSUT()
95+
// when
96+
try testTool.changeFile(at: testTool.projectPath + "ExampleProject.xcodeproj/project.pbxproj")
97+
98+
// then
99+
let result = try await tool.run()
100+
XCTAssertEqual(result, Set([
101+
testTool.mainProjectMainTarget,
102+
testTool.mainProjectTests,
103+
testTool.mainProjectUITests,
104+
testTool.mainProjectLibrary,
105+
testTool.mainProjectLibraryTests,
106+
]))
107+
}
108108
}
109109

110110
func testInferTestPlan() async throws {
111-
// given
112-
let tool = try testTool.createSUT(config: nil,
113-
testPlan: nil)
114-
// when
115-
try testTool.changeFile(at: testTool.projectPath + "ExampleLibrary/ExampleLibrary/ExampleLibrary.swift")
116-
117-
// then
118-
let _ = try await tool.run()
119-
try testTool.validateTestPlan(testPlanPath: testTool.projectPath + "ExampleProject.xctestplan",
120-
expected: Set([testTool.mainProjectTests,
121-
testTool.mainProjectUITests,
122-
testTool.exampleLibraryTests]))
111+
try await testTool.withTestTool {
112+
// given
113+
let tool = try testTool.createSUT(config: nil,
114+
testPlan: nil)
115+
// when
116+
try testTool.changeFile(at: testTool.projectPath + "ExampleLibrary/ExampleLibrary/ExampleLibrary.swift")
117+
118+
// then
119+
let _ = try await tool.run()
120+
try testTool.validateTestPlan(testPlanPath: testTool.projectPath + "ExampleProject.xctestplan",
121+
expected: Set([testTool.mainProjectTests,
122+
testTool.mainProjectUITests,
123+
testTool.exampleLibraryTests]))
124+
}
125+
}
126+
127+
func testInferTestPlanInSubfolder() async throws {
128+
try await testTool.withTestTool(subfolder: true) {
129+
// given
130+
let tool = try testTool.createSUT(
131+
config: nil,
132+
basePath: testTool.projectPath + "Subfolder",
133+
testPlan: nil)
134+
135+
// when
136+
try testTool.changeFile(at: testTool.projectPath + "Subfolder/ExampleLibrary/ExampleLibrary/ExampleLibrary.swift")
137+
138+
// then
139+
let _ = try await tool.run()
140+
try testTool.validateTestPlan(testPlanPath: testTool.projectPath + "Subfolder/ExampleProject.xctestplan",
141+
expected: Set([testTool.mainProjectTests,
142+
testTool.mainProjectUITests,
143+
testTool.exampleLibraryTests]))
144+
}
123145
}
124146
}

0 commit comments

Comments
 (0)