@@ -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}
0 commit comments