@@ -227,6 +227,22 @@ class AnalyzerTests: AppTestCase {
227227 $0. git. getTags = { @Sendable _ in [ . tag( 1 , 0 , 0 ) , . tag( 1 , 1 , 1 ) ] }
228228 $0. git. hasBranch = { @Sendable _, _ in true }
229229 $0. git. lastCommitDate = { @Sendable _ in . t2 }
230+ $0. git. revisionInfo = { @Sendable ref, _ in
231+ // simulate the following scenario:
232+ // - main branch has moved from commit0 -> commit3 (timestamp t3)
233+ // - 1.0.0 has been re-tagged (!) from commit0 -> commit1 (timestamp t1)
234+ // - 1.1.1 has been added at commit2 (timestamp t2)
235+ switch ref {
236+ case _ where ref == . tag( 1 , 0 , 0 ) :
237+ return . init( commit: " commit1 " , date: . t1)
238+ case _ where ref == . tag( 1 , 1 , 1 ) :
239+ return . init( commit: " commit2 " , date: . t2)
240+ case . branch( " main " ) :
241+ return . init( commit: " commit3 " , date: . t3)
242+ default :
243+ fatalError ( " unexpected reference: \( ref) " )
244+ }
245+ }
230246 $0. httpClient. mastodonPost = { @Sendable _ in }
231247 } operation: {
232248 // setup
@@ -251,22 +267,6 @@ class AnalyzerTests: AppTestCase {
251267 packageName: " foo-1 " ,
252268 reference: . tag( 1 , 0 , 0 ) ) . save ( on: app. db)
253269
254- Current . git. revisionInfo = { @Sendable ref, _ in
255- // simulate the following scenario:
256- // - main branch has moved from commit0 -> commit3 (timestamp t3)
257- // - 1.0.0 has been re-tagged (!) from commit0 -> commit1 (timestamp t1)
258- // - 1.1.1 has been added at commit2 (timestamp t2)
259- switch ref {
260- case _ where ref == . tag( 1 , 0 , 0 ) :
261- return . init( commit: " commit1 " , date: . t1)
262- case _ where ref == . tag( 1 , 1 , 1 ) :
263- return . init( commit: " commit2 " , date: . t2)
264- case . branch( " main " ) :
265- return . init( commit: " commit3 " , date: . t3)
266- default :
267- fatalError ( " unexpected reference: \( ref) " )
268- }
269- }
270270 Current . git. shortlog = { @Sendable _ in
271271 """
272272 10 \t Person 1
@@ -366,6 +366,7 @@ class AnalyzerTests: AppTestCase {
366366 $0. git. getTags = { @Sendable _ in [ . tag( 1 , 0 , 0 ) ] }
367367 $0. git. hasBranch = { @Sendable _, _ in true }
368368 $0. git. lastCommitDate = { @Sendable _ in . t1 }
369+ $0. git. revisionInfo = { @Sendable _, _ in . init( commit: " sha " , date: . t0) }
369370 } operation: {
370371 // setup
371372 let urls = [ " https://github.com/foo/1 " , " https://github.com/foo/2 " ]
@@ -375,7 +376,6 @@ class AnalyzerTests: AppTestCase {
375376 }
376377 let lastUpdate = Date ( )
377378
378- Current . git. revisionInfo = { @Sendable _, _ in . init( commit: " sha " , date: . t0) }
379379 Current . git. shortlog = { @Sendable _ in
380380 """
381381 10 \t Person 1
@@ -565,9 +565,9 @@ class AnalyzerTests: AppTestCase {
565565 try await withDependencies {
566566 $0. git. getTags = { @Sendable _ in [ . tag( 1 , 2 , 3 ) ] }
567567 $0. git. hasBranch = { @Sendable _, _ in true }
568+ $0. git. revisionInfo = { @Sendable ref, _ in . init( commit: " sha- \( ref) " , date: . t0) }
568569 } operation: {
569570 // setup
570- Current . git. revisionInfo = { @Sendable ref, _ in . init( commit: " sha- \( ref) " , date: . t0) }
571571 do {
572572 let pkg = Package ( id: . id0, url: " 1 " . asGithubUrl. url)
573573 try await pkg. save ( on: app. db)
@@ -628,13 +628,13 @@ class AnalyzerTests: AppTestCase {
628628 try await withDependencies {
629629 $0. git. getTags = { @Sendable _ in [ . tag( 1 , 2 , 3 ) ] }
630630 $0. git. hasBranch = { @Sendable _, _ in true }
631- } operation: {
632- //setup
633- Current . git. revisionInfo = { @Sendable ref, _ in
631+ $0. git. revisionInfo = { @Sendable ref, _ in
634632 if ref == . branch( " main " ) { return . init( commit: " sha.main " , date: . t0) }
635633 if ref == . tag( 1 , 2 , 3 ) { return . init( commit: " sha.1.2.3 " , date: . t1) }
636634 fatalError ( " unknown ref: \( ref) " )
637635 }
636+ } operation: {
637+ //setup
638638 Current . shell. run = { @Sendable cmd, _ in throw TestError . unknownCommand }
639639 let pkgId = UUID ( )
640640 do {
@@ -910,9 +910,9 @@ class AnalyzerTests: AppTestCase {
910910 $0. git. getTags = { @Sendable _ in [ . tag( 1 , 0 , 0 ) , . tag( 2 , 0 , 0 ) ] }
911911 $0. git. hasBranch = { @Sendable _, _ in true }
912912 $0. git. lastCommitDate = { @Sendable _ in . t1 }
913+ $0. git. revisionInfo = { @Sendable _, _ in . init( commit: " sha " , date: . t0) }
913914 } operation: {
914915 // setup
915- Current . git. revisionInfo = { @Sendable _, _ in . init( commit: " sha " , date: . t0) }
916916 Current . git. shortlog = { @Sendable _ in
917917 """
918918 10 \t Person 1
@@ -1344,9 +1344,9 @@ class AnalyzerTests: AppTestCase {
13441344 }
13451345 Current . shell. run = { @Sendable cmd, path in " " }
13461346
1347- do { // first scenario: bad getTags
1348- Current . git. revisionInfo = { @Sendable _, _ in . init( commit: " " , date: . t1) }
1349-
1347+ try await withDependencies { // first scenario: bad getTags
1348+ $0 . git. revisionInfo = { @Sendable _, _ in . init( commit: " " , date: . t1) }
1349+ } operation : {
13501350 // MUT
13511351 try await Analyze . analyze ( client: app. client,
13521352 database: app. db,
@@ -1361,9 +1361,8 @@ class AnalyzerTests: AppTestCase {
13611361
13621362 try await withDependencies { // second scenario: revisionInfo throws
13631363 $0. git. getTags = { @Sendable _ in [ . tag( 1 , 0 , 0 ) ] }
1364+ $0. git. revisionInfo = { @Sendable _, _ in throw TestError . unspecifiedError }
13641365 } operation: {
1365- Current . git. revisionInfo = { @Sendable _, _ in throw TestError . unspecifiedError }
1366-
13671366 // MUT
13681367 try await Analyze . analyze ( client: app. client,
13691368 database: app. db,
@@ -1378,9 +1377,8 @@ class AnalyzerTests: AppTestCase {
13781377
13791378 try await withDependencies { // second scenario: gitTags throws
13801379 $0. git. getTags = { @Sendable _ in throw TestError . unspecifiedError }
1380+ $0. git. revisionInfo = { @Sendable _, _ in . init( commit: " " , date: . t1) }
13811381 } operation: {
1382- Current . git. revisionInfo = { @Sendable _, _ in . init( commit: " " , date: . t1) }
1383-
13841382 // MUT
13851383 try await Analyze . analyze ( client: app. client,
13861384 database: app. db,
@@ -1395,8 +1393,8 @@ class AnalyzerTests: AppTestCase {
13951393
13961394 try await withDependencies { // third scenario: everything throws
13971395 $0. git. getTags = { @Sendable _ in throw TestError . unspecifiedError }
1396+ $0. git. revisionInfo = { @Sendable _, _ in throw TestError . unspecifiedError }
13981397 } operation: {
1399- Current . git. revisionInfo = { @Sendable _, _ in throw TestError . unspecifiedError }
14001398 Current . shell. run = { @Sendable _, _ in throw TestError . unspecifiedError }
14011399
14021400 // MUT
@@ -1453,8 +1451,8 @@ class AnalyzerTests: AppTestCase {
14531451 }
14541452 Current . shell. run = { @Sendable cmd, path in return " " }
14551453
1456- do { // ensure happy path passes test (no revision changes)
1457- Current . git. revisionInfo = { @Sendable ref, _ in
1454+ try await withDependencies { // ensure happy path passes test (no revision changes)
1455+ $0 . git. revisionInfo = { @Sendable ref, _ in
14581456 switch ref {
14591457 case . tag( . init( 1 , 0 , 0 ) , " 1.0.0 " ) :
14601458 return . init( commit: " commit0 " , date: . t0)
@@ -1464,7 +1462,7 @@ class AnalyzerTests: AppTestCase {
14641462 throw Error ( )
14651463 }
14661464 }
1467-
1465+ } operation : {
14681466 // MUT
14691467 try await Analyze . analyze ( client: app. client,
14701468 database: app. db,
@@ -1482,8 +1480,8 @@ class AnalyzerTests: AppTestCase {
14821480 pkg. processingStage = . ingestion
14831481 try await pkg. save ( on: app. db)
14841482
1485- do { // simulate "main" branch moving forward to ("commit0", .t1)
1486- Current . git. revisionInfo = { @Sendable ref, _ in
1483+ try await withDependencies { // simulate "main" branch moving forward to ("commit0", .t1)
1484+ $0 . git. revisionInfo = { @Sendable ref, _ in
14871485 switch ref {
14881486 case . tag( . init( 1 , 0 , 0 ) , " 1.0.0 " ) :
14891487 return . init( commit: " commit0 " , date: . t0)
@@ -1494,6 +1492,7 @@ class AnalyzerTests: AppTestCase {
14941492 throw Error ( )
14951493 }
14961494 }
1495+ } operation: {
14971496 Current . shell. run = { @Sendable cmd, path in
14981497 // simulate error in getPackageInfo by failing checkout
14991498 if cmd == . gitCheckout( branch: " main " ) {
@@ -1535,6 +1534,7 @@ class AnalyzerTests: AppTestCase {
15351534 $0. git. getTags = { @Sendable _ in [ ] }
15361535 $0. git. hasBranch = { @Sendable _, _ in true }
15371536 $0. git. lastCommitDate = { @Sendable _ in . t1 }
1537+ $0. git. revisionInfo = { @Sendable _, _ in . init( commit: " sha1 " , date: . t0) }
15381538 } operation: {
15391539 // setup
15401540 let pkg = try await savePackage ( on: app. db, id: . id0, " https://github.com/foo/1 " . url, processingStage: . ingestion)
@@ -1543,7 +1543,6 @@ class AnalyzerTests: AppTestCase {
15431543 name: " 1 " ,
15441544 owner: " foo " ,
15451545 stars: 100 ) . save ( on: app. db)
1546- Current . git. revisionInfo = { @Sendable _, _ in . init( commit: " sha1 " , date: . t0) }
15471546 Current . git. shortlog = { @Sendable _ in " 10 \t Person 1 " }
15481547 Current . shell. run = { @Sendable cmd, path in
15491548 if cmd == . swiftDumpPackage { return . packageDump( name: " foo1 " ) }
@@ -1576,15 +1575,16 @@ class AnalyzerTests: AppTestCase {
15761575 XCTAssertEqual ( pkg? . scoreDetails? . numberOfDependencies, 1 )
15771576 }
15781577
1579- // now we simulate a new version on the default branch
1580- Current . git. revisionInfo = { @Sendable _, _ in . init( commit: " sha2 " , date: . t1) }
1581-
1582- // third analysis pass
1583- try await Analyze . analyze ( client: app. client, database: app. db, mode: . id( . id0) )
1584- do { // validate
1585- let pkg = try await Package . query ( on: app. db) . first ( )
1586- // numberOfDependencies must be preserved as 1, even though we've not built this version yet
1587- XCTAssertEqual ( pkg? . scoreDetails? . numberOfDependencies, 1 )
1578+ try await withDependencies { // now we simulate a new version on the default branch
1579+ $0. git. revisionInfo = { @Sendable _, _ in . init( commit: " sha2 " , date: . t1) }
1580+ } operation: {
1581+ // third analysis pass
1582+ try await Analyze . analyze ( client: app. client, database: app. db, mode: . id( . id0) )
1583+ do { // validate
1584+ let pkg = try await Package . query ( on: app. db) . first ( )
1585+ // numberOfDependencies must be preserved as 1, even though we've not built this version yet
1586+ XCTAssertEqual ( pkg? . scoreDetails? . numberOfDependencies, 1 )
1587+ }
15881588 }
15891589 }
15901590}
0 commit comments