Skip to content

Commit ecc50f2

Browse files
committed
Reducing code duplication
1 parent 61d4737 commit ecc50f2

File tree

5 files changed

+152
-69
lines changed

5 files changed

+152
-69
lines changed

Sources/PublicModules/PADProjectBuilder/ProjectPlatform.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//
2-
// ProjectPlatform.swift
3-
// public-api-diff
2+
// Copyright (c) 2024 Adyen N.V.
43
//
5-
// Created by Alexander Guretzki on 13/11/2024.
4+
// This file is open source and available under the MIT license. See the LICENSE file for more info.
65
//
76

87
/// The platform to build the project on

Sources/PublicModules/PADProjectBuilder/SwiftInterfaceProducer/XcodeTools.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ struct XcodeTools {
6464
case .iOS:
6565
commandComponents += [
6666
"-sdk `\(Constants.simulatorSdkCommand)`",
67-
"-destination \"generic/platform=iOS\"",
67+
"-destination \"generic/platform=iOS\""
6868
]
6969
case .macOS:
7070
commandComponents += [
71-
"-destination \"generic/platform=macOS\"",
71+
"-destination \"generic/platform=macOS\""
7272
]
7373
}
7474

Sources/Shared/Public/PADSwiftInterfaceFileLocator/SwiftInterfaceType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
public enum SwiftInterfaceType {
1111
case `private`
1212
case `public`
13-
case `package`
13+
case package
1414

1515
var name: String {
1616
switch self {

Tests/UnitTests/GitTests.swift

Lines changed: 90 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,30 @@ class GitTests: XCTestCase {
1616
let targetDirectoryPath = "targetDirectoryPath"
1717
let shellResult = "shell-result"
1818

19-
let shellExpectation = expectation(description: "MockShell.execute was called once")
20-
let fileHandlerExpectation = expectation(description: "MockFileHandler.handleContentsOfDirectory was called once")
21-
let loggerLogExpectation = expectation(description: "MockLogger.handleLog was called once")
22-
let loggerDebugExpectation = expectation(description: "MockLogger.handleDebug was called once")
23-
let allExpectations = [shellExpectation, fileHandlerExpectation, loggerLogExpectation, loggerDebugExpectation]
19+
let shellSetup = setupShell(
20+
branch: branch,
21+
repository: repository,
22+
targetDirectoryPath: targetDirectoryPath,
23+
result: shellResult
24+
)
2425

25-
let mockShell = MockShell { command in
26-
XCTAssertEqual(command, "git clone -b \(branch) \(repository) \(targetDirectoryPath)")
27-
shellExpectation.fulfill()
28-
return shellResult
29-
}
30-
var mockFileHandler = MockFileHandler()
31-
mockFileHandler.handleContentsOfDirectory = { directoryPath in
32-
XCTAssertEqual(targetDirectoryPath, directoryPath)
33-
fileHandlerExpectation.fulfill()
34-
return ["NonEmpty"]
35-
}
36-
var mockLogger = MockLogger()
37-
mockLogger.handleLog = { message, subsystem in
38-
XCTAssertEqual(message, "🐱 Cloning repository @ branch into targetDirectoryPath")
39-
XCTAssertEqual(subsystem, "Git")
40-
loggerLogExpectation.fulfill()
41-
}
42-
mockLogger.handleDebug = { message, subsystem in
43-
XCTAssertEqual(message, shellResult)
44-
XCTAssertEqual(subsystem, "Git")
45-
loggerDebugExpectation.fulfill()
46-
}
26+
let fileHandlerSetup = setupFileHandler(
27+
targetDirectoryPath: targetDirectoryPath,
28+
result: ["NonEmpty"]
29+
)
30+
31+
let loggerSetup = setupLogger(
32+
shellResult: shellResult
33+
)
34+
35+
let allExpectations = [shellSetup.expectation, fileHandlerSetup.expectation] + loggerSetup.expectations
36+
37+
let git = Git(
38+
shell: shellSetup.shell,
39+
fileHandler: fileHandlerSetup.fileHandler,
40+
logger: loggerSetup.logger
41+
)
4742

48-
let git = Git(shell: mockShell, fileHandler: mockFileHandler, logger: mockLogger)
4943
try git.clone(repository, at: branch, targetDirectoryPath: targetDirectoryPath)
5044

5145
wait(for: allExpectations, timeout: 1)
@@ -58,24 +52,85 @@ class GitTests: XCTestCase {
5852
let targetDirectoryPath = "targetDirectoryPath"
5953
let shellResult = "shell-result"
6054

55+
let shellSetup = setupShell(
56+
branch: branch,
57+
repository: repository,
58+
targetDirectoryPath: targetDirectoryPath,
59+
result: shellResult
60+
)
61+
62+
let fileHandlerSetup = setupFileHandler(
63+
targetDirectoryPath: targetDirectoryPath,
64+
result: []
65+
)
66+
67+
let loggerSetup = setupLogger(
68+
shellResult: shellResult
69+
)
70+
71+
let allExpectations = [shellSetup.expectation, fileHandlerSetup.expectation] + loggerSetup.expectations
72+
73+
let git = Git(
74+
shell: shellSetup.shell,
75+
fileHandler: fileHandlerSetup.fileHandler,
76+
logger: loggerSetup.logger
77+
)
78+
79+
do {
80+
try git.clone(repository, at: branch, targetDirectoryPath: targetDirectoryPath)
81+
XCTFail("Clone should have thrown an error")
82+
} catch {
83+
let fileHandlerError = try XCTUnwrap(error as? GitError)
84+
XCTAssertEqual(fileHandlerError, GitError.couldNotClone(branchOrTag: branch, repository: repository))
85+
}
86+
87+
wait(for: allExpectations, timeout: 1)
88+
}
89+
}
90+
91+
private extension GitTests {
92+
93+
func setupShell(
94+
branch: String,
95+
repository: String,
96+
targetDirectoryPath: String,
97+
result: String
98+
) -> (shell: MockShell, expectation: XCTestExpectation) {
99+
61100
let shellExpectation = expectation(description: "MockShell.execute was called once")
62-
let fileHandlerExpectation = expectation(description: "MockFileHandler.handleContentsOfDirectory was called once")
63-
let loggerLogExpectation = expectation(description: "MockLogger.handleLog was called once")
64-
let loggerDebugExpectation = expectation(description: "MockLogger.handleDebug was called once")
65-
let allExpectations = [shellExpectation, fileHandlerExpectation, loggerLogExpectation, loggerDebugExpectation]
66101

67102
let mockShell = MockShell { command in
68103
XCTAssertEqual(command, "git clone -b \(branch) \(repository) \(targetDirectoryPath)")
69104
shellExpectation.fulfill()
70-
return shellResult
105+
return result
71106
}
72107

108+
return (mockShell, shellExpectation)
109+
}
110+
111+
func setupFileHandler(
112+
targetDirectoryPath: String,
113+
result: [String]
114+
) -> (fileHandler: MockFileHandler, expectation: XCTestExpectation) {
115+
116+
let fileHandlerExpectation = expectation(description: "MockFileHandler.handleContentsOfDirectory was called once")
117+
73118
var mockFileHandler = MockFileHandler()
74119
mockFileHandler.handleContentsOfDirectory = { directoryPath in
75120
XCTAssertEqual(targetDirectoryPath, directoryPath)
76121
fileHandlerExpectation.fulfill()
77-
return []
122+
return result
78123
}
124+
return (mockFileHandler, fileHandlerExpectation)
125+
}
126+
127+
func setupLogger(
128+
shellResult: String
129+
) -> (logger: MockLogger, expectations: [XCTestExpectation]) {
130+
131+
let loggerLogExpectation = expectation(description: "MockLogger.handleLog was called once")
132+
let loggerDebugExpectation = expectation(description: "MockLogger.handleDebug was called once")
133+
79134
var mockLogger = MockLogger()
80135
mockLogger.handleLog = { message, subsystem in
81136
XCTAssertEqual(message, "🐱 Cloning repository @ branch into targetDirectoryPath")
@@ -88,16 +143,6 @@ class GitTests: XCTestCase {
88143
loggerDebugExpectation.fulfill()
89144
}
90145

91-
let git = Git(shell: mockShell, fileHandler: mockFileHandler, logger: mockLogger)
92-
93-
do {
94-
try git.clone(repository, at: branch, targetDirectoryPath: targetDirectoryPath)
95-
XCTFail("Clone should have thrown an error")
96-
} catch {
97-
let fileHandlerError = try XCTUnwrap(error as? GitError)
98-
XCTAssertEqual(fileHandlerError, GitError.couldNotClone(branchOrTag: branch, repository: repository))
99-
}
100-
101-
wait(for: allExpectations, timeout: 1)
146+
return (mockLogger, [loggerLogExpectation, loggerDebugExpectation])
102147
}
103148
}

Tests/UnitTests/XcodeToolsTests.swift

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,93 @@ import XCTest
99

1010
class XcodeToolsTests: XCTestCase {
1111

12-
func test_archive_swiftPackage() async throws {
12+
func test_archive_swiftPackage_iOS() async throws {
1313

1414
let projectDirectoryPath = "PROJECT_DIRECTORY_PATH"
15-
let scheme = "SCHEME"
1615

1716
try await testArchiving(
1817
projectDirectoryPath: projectDirectoryPath,
19-
scheme: scheme,
2018
projectType: .swiftPackage,
2119
platform: .iOS
2220
)
2321
}
2422

25-
func test_archive_xcodeProject() async throws {
23+
func test_archive_xcodeProject_iOS() async throws {
2624

2725
let projectDirectoryPath = "PROJECT_DIRECTORY_PATH"
28-
let scheme = "SCHEME"
2926

3027
try await testArchiving(
3128
projectDirectoryPath: projectDirectoryPath,
32-
scheme: scheme,
33-
projectType: .xcodeProject(scheme: scheme),
29+
projectType: .xcodeProject(scheme: "SCHEME"),
3430
platform: .iOS
3531
)
3632
}
33+
34+
func test_archive_swiftPackage_macOS() async throws {
35+
36+
let projectDirectoryPath = "PROJECT_DIRECTORY_PATH"
37+
38+
try await testArchiving(
39+
projectDirectoryPath: projectDirectoryPath,
40+
projectType: .swiftPackage,
41+
platform: .macOS
42+
)
43+
}
44+
45+
func test_archive_xcodeProject_macOS() async throws {
46+
47+
let projectDirectoryPath = "PROJECT_DIRECTORY_PATH"
48+
49+
try await testArchiving(
50+
projectDirectoryPath: projectDirectoryPath,
51+
projectType: .xcodeProject(scheme: "SCHEME"),
52+
platform: .macOS
53+
)
54+
}
3755
}
3856

3957
private extension XcodeToolsTests {
4058

59+
func expectedCommand(projectDirectoryPath: String, scheme: String, projectType: ProjectType, platform: ProjectPlatform) -> String {
60+
61+
var commandComponents = [
62+
"cd \(projectDirectoryPath);",
63+
"xcodebuild clean build -scheme \"\(scheme)\"",
64+
"-derivedDataPath .build BUILD_LIBRARY_FOR_DISTRIBUTION=YES"
65+
]
66+
67+
switch platform {
68+
case .macOS:
69+
commandComponents += ["-destination \"generic/platform=macOS\""]
70+
case .iOS:
71+
commandComponents += ["-sdk `xcrun --sdk iphonesimulator --show-sdk-path` -destination \"generic/platform=iOS\""]
72+
}
73+
74+
switch projectType {
75+
case .swiftPackage:
76+
commandComponents += ["-skipPackagePluginValidation"]
77+
case .xcodeProject:
78+
break // Nothing specific to add
79+
}
80+
81+
return String(commandComponents.joined(separator: " "))
82+
}
83+
4184
func testArchiving(
4285
projectDirectoryPath: String,
43-
scheme: String,
4486
projectType: ProjectType,
4587
platform: ProjectPlatform
4688
) async throws {
4789

90+
let scheme = "SCHEME"
4891
let archiveResult = "ARCHIVE_RESULT"
4992
let expectedDerivedDataPath = "\(projectDirectoryPath)/.build"
50-
var expectedHandleExecuteCalls: [String] = {
51-
let command = "cd \(projectDirectoryPath); xcodebuild clean build -scheme \"\(scheme)\" -derivedDataPath .build BUILD_LIBRARY_FOR_DISTRIBUTION=YES"
52-
let iOSSpecificParameters = "-sdk `xcrun --sdk iphonesimulator --show-sdk-path` -destination \"generic/platform=iOS\""
53-
switch projectType {
54-
case .swiftPackage:
55-
return ["\(command) \(iOSSpecificParameters) -skipPackagePluginValidation"]
56-
case let .xcodeProject(scheme):
57-
return ["\(command) \(iOSSpecificParameters)"]
58-
}
59-
}()
93+
var expectedHandleExecuteCalls: [String] = { [expectedCommand(
94+
projectDirectoryPath: projectDirectoryPath,
95+
scheme: scheme,
96+
projectType: projectType,
97+
platform: platform
98+
)] }()
6099
var expectedHandleLogCalls: [(message: String, subsystem: String)] = [
61100
("📦 Archiving SCHEME from PROJECT_DIRECTORY_PATH", "XcodeTools")
62101
]

0 commit comments

Comments
 (0)