@@ -1459,6 +1459,66 @@ class AnalyzerTests: AppTestCase {
14591459 }
14601460 }
14611461
1462+ func test_issue_2873( ) async throws {
1463+ // Ensure we preserve dependency counts from previous default branch version
1464+ // https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2873
1465+ // setup
1466+ let pkg = try await savePackageAsync ( on: app. db, id: . id0, " https://github.com/foo/1 " . url, processingStage: . ingestion)
1467+ try await Repository ( package : pkg,
1468+ defaultBranch: " main " ,
1469+ name: " 1 " ,
1470+ owner: " foo " ,
1471+ stars: 100 ) . save ( on: app. db)
1472+ Current . git. commitCount = { _ in 12 }
1473+ Current . git. getTags = { _ in [ ] }
1474+ Current . git. hasBranch = { _, _ in true }
1475+ Current . git. firstCommitDate = { _ in . t0 }
1476+ Current . git. lastCommitDate = { _ in . t1 }
1477+ Current . git. revisionInfo = { _, _ in . init( commit: " sha1 " , date: . t0) }
1478+ Current . git. shortlog = { _ in " 10 \t Person 1 " }
1479+ Current . shell. run = { cmd, path in
1480+ if cmd == . swiftDumpPackage { return . packageDump( name: " foo1 " ) }
1481+ return " "
1482+ }
1483+
1484+ // MUT and validation
1485+
1486+ // first analysis pass
1487+ try await Analyze . analyze ( client: app. client, database: app. db, logger: app. logger, mode: . id( . id0) )
1488+ do { // validate
1489+ let pkg = try await Package . query ( on: app. db) . first ( )
1490+ // numberOfDependencies is nil here, because we've not yet received the info back from the build
1491+ XCTAssertEqual ( pkg? . scoreDetails? . numberOfDependencies, nil )
1492+ }
1493+
1494+ do { // receive build report - we could send an actual report here via the API but let's just update
1495+ // the field directly instead, we're not testing build reporting after all
1496+ let version = try await Version . query ( on: app. db) . first ( )
1497+ version? . resolvedDependencies = . some( [ . init( packageName: " dep " ,
1498+ repositoryURL: " https://github.com/some/dep " ) ] )
1499+ try await version? . save ( on: app. db)
1500+ }
1501+
1502+ // second analysis pass
1503+ try await Analyze . analyze ( client: app. client, database: app. db, logger: app. logger, mode: . id( . id0) )
1504+ do { // validate
1505+ let pkg = try await Package . query ( on: app. db) . first ( )
1506+ // numberOfDependencies is 1 now, because we see the updated version
1507+ XCTAssertEqual ( pkg? . scoreDetails? . numberOfDependencies, 1 )
1508+ }
1509+
1510+ // now we simulate a new version on the default branch
1511+ Current . git. revisionInfo = { _, _ in . init( commit: " sha2 " , date: . t1) }
1512+
1513+ // third analysis pass
1514+ try await Analyze . analyze ( client: app. client, database: app. db, logger: app. logger, mode: . id( . id0) )
1515+ do { // validate
1516+ let pkg = try await Package . query ( on: app. db) . first ( )
1517+ // numberOfDependencies must be preserved as 1, even though we've not built this version yet
1518+ XCTAssertEqual ( pkg? . scoreDetails? . numberOfDependencies, 1 )
1519+ }
1520+ }
1521+
14621522}
14631523
14641524
@@ -1559,3 +1619,24 @@ private enum TestError: Error {
15591619 case simulatedFetchError
15601620 case unknownCommand
15611621}
1622+
1623+
1624+ private extension String {
1625+ static func packageDump( name: String ) -> Self {
1626+ #"""
1627+ {
1628+ "name": " \#( name) ",
1629+ "products": [
1630+ {
1631+ "name": "p1",
1632+ "targets": ["t1"],
1633+ "type": {
1634+ "executable": null
1635+ }
1636+ }
1637+ ],
1638+ "targets": [{"name": "t1", "type": "executable"}]
1639+ }
1640+ """#
1641+ }
1642+ }
0 commit comments