Skip to content

Commit 09e73d8

Browse files
authored
improve repository cache information emmited to delegate (#3705)
motivation: fix flaky test, more accurate representaiton of cache usage information changes: * dont fail the cache lookup when the parent directory for the target is not found, create it instead * more reliabily report the status of when the repositories cache is updated and used * change the test to not rely on a side-effect of parent directory not being found, which is no longer the case * adjust the test to the new status reporting and add another step to test the behavior when cache is poopulated
1 parent 2ba7fbe commit 09e73d8

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

Sources/SourceControl/RepositoryManager.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ public class RepositoryManager {
231231
/// - Returns: Details about the performed fetch.
232232
@discardableResult
233233
func fetchAndPopulateCache(handle: RepositoryHandle, repositoryPath: AbsolutePath) throws -> FetchDetails {
234-
var updatedCache = false
235-
var fromCache = false
234+
var cacheUsed = false
235+
var cacheUpdated = false
236236

237237
// We are expecting handle.repository.url to always be a resolved absolute path.
238238
let isLocal = (try? AbsolutePath(validating: handle.repository.url)) != nil
@@ -248,29 +248,29 @@ public class RepositoryManager {
248248
if (fileSystem.exists(cachedRepositoryPath)) {
249249
let repo = try self.provider.open(repository: handle.repository, at: cachedRepositoryPath)
250250
try repo.fetch()
251+
cacheUsed = true
251252
} else {
252253
try self.provider.fetch(repository: handle.repository, to: cachedRepositoryPath)
253254
}
254-
updatedCache = true
255+
cacheUpdated = true
255256
// Copy the repository from the cache into the repository path.
257+
try fileSystem.createDirectory(repositoryPath.parentDirectory, recursive: true)
256258
try self.provider.copy(from: cachedRepositoryPath, to: repositoryPath)
257-
fromCache = true
258259
}
259260
}
260261
} catch {
262+
cacheUsed = false
261263
// Fetch without populating the cache in the case of an error.
262264
print("Skipping cache due to an error: \(error)")
263265
// It is possible that we already created the directory before failing, so clear leftover data if present.
264266
try fileSystem.removeFileTree(repositoryPath)
265267
try self.provider.fetch(repository: handle.repository, to: repositoryPath)
266-
fromCache = false
267268
}
268269
} else {
269270
// Fetch without populating the cache when no `cachePath` is set.
270271
try self.provider.fetch(repository: handle.repository, to: repositoryPath)
271-
fromCache = false
272272
}
273-
return FetchDetails(fromCache: fromCache, updatedCache: updatedCache)
273+
return FetchDetails(fromCache: cacheUsed, updatedCache: cacheUpdated)
274274
}
275275

276276
public func openWorkingCopy(at path: AbsolutePath) throws -> WorkingCheckout {

Tests/SourceControlTests/RepositoryManagerTests.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ class RepositoryManagerTests: XCTestCase {
355355
// Avoid a crasher that seems to happen only on macOS 10.15, but leave an environment variable for testing.
356356
try XCTSkipUnless(ProcessEnv.vars["SWIFTPM_ENABLE_FLAKY_REPOSITORYMANAGERTESTS"] == "1", "skipping test that sometimes crashes in CI (rdar://70540298)")
357357
}
358-
359-
try XCTSkipIf(true, "test became racy")
360358

361359
fixture(name: "DependencyResolution/External/Simple") { prefix in
362360
let cachePath = prefix.appending(component: "cache")
@@ -388,6 +386,7 @@ class RepositoryManagerTests: XCTestCase {
388386
XCTAssertEqual(delegate.didFetch[0].fetchDetails,
389387
RepositoryManager.FetchDetails(fromCache: false, updatedCache: true))
390388

389+
// removing the repositories path to force re-fetch
391390
try localFileSystem.removeFileTree(repositoriesPath)
392391

393392
// fetch packages from the cache
@@ -398,10 +397,11 @@ class RepositoryManagerTests: XCTestCase {
398397
XCTAssertEqual(delegate.willFetch[1].fetchDetails,
399398
RepositoryManager.FetchDetails(fromCache: true, updatedCache: false))
400399
XCTAssertEqual(delegate.didFetch[1].fetchDetails,
401-
RepositoryManager.FetchDetails(fromCache: false, updatedCache: true))
400+
RepositoryManager.FetchDetails(fromCache: true, updatedCache: true))
402401

403-
try localFileSystem.removeFileTree(repositoriesPath)
402+
// reset the state on disk
404403
try localFileSystem.removeFileTree(cachePath)
404+
try localFileSystem.removeFileTree(repositoriesPath)
405405

406406
// fetch packages and populate cache
407407
delegate.willFetchGroup?.enter()
@@ -413,6 +413,13 @@ class RepositoryManagerTests: XCTestCase {
413413
RepositoryManager.FetchDetails(fromCache: false, updatedCache: false))
414414
XCTAssertEqual(delegate.didFetch[2].fetchDetails,
415415
RepositoryManager.FetchDetails(fromCache: false, updatedCache: true))
416+
417+
// update packages from the cache
418+
delegate.willUpdateGroup?.enter()
419+
delegate.willUpdateGroup?.enter()
420+
_ = try manager.lookup(repository: repo)
421+
XCTAssertEqual(delegate.willUpdate[0].fileSystemIdentifier, repo.fileSystemIdentifier)
422+
XCTAssertEqual(delegate.didUpdate[0].fileSystemIdentifier, repo.fileSystemIdentifier)
416423
}
417424
}
418425

0 commit comments

Comments
 (0)