diff --git a/Sources/MockingbirdCli/Interface/Generator+PruningPipeline.swift b/Sources/MockingbirdCli/Interface/Generator+PruningPipeline.swift index bb788aea..4af54e49 100644 --- a/Sources/MockingbirdCli/Interface/Generator+PruningPipeline.swift +++ b/Sources/MockingbirdCli/Interface/Generator+PruningPipeline.swift @@ -82,7 +82,8 @@ extension Generator { self.findMockedTypesOperation = findMockedTypesOperation } - func cache(cliVersion: String, + func cache(projectHash: String, + cliVersion: String, configHash: String, sourceRoot: Path, cacheDirectory: Path, @@ -95,6 +96,7 @@ extension Generator { target = try TestTarget(from: pipelineTarget, sourceRoot: sourceRoot, mockedTypeNames: mockedTypeNames, + projectHash: projectHash, cliVersion: cliVersion, configHash: configHash, environment: environment) @@ -106,6 +108,7 @@ extension Generator { target = try TestTarget(from: pipelineTarget, sourceRoot: sourceRoot, mockedTypeNames: mockedTypeNames, + projectHash: projectHash, cliVersion: cliVersion, configHash: configHash, environment: environment) @@ -119,6 +122,7 @@ extension Generator { } func findCachedTestTarget(for targetName: String, + projectHash: String, cliVersion: String, configHash: String, cacheDirectory: Path, @@ -140,6 +144,11 @@ extension Generator { return nil } + guard target.projectHash == projectHash else { + log("Invalidated cached test target metadata for \(targetName.singleQuoted) because the project hash changed from \(target.projectHash.singleQuoted) to \(projectHash.singleQuoted)") + return nil + } + guard cliVersion == target.cliVersion else { log("Invalidated cached test target metadata for \(target.name.singleQuoted) because the CLI version changed from \(target.cliVersion.singleQuoted) to \(cliVersion.singleQuoted)") return nil diff --git a/Sources/MockingbirdCli/Interface/Generator.swift b/Sources/MockingbirdCli/Interface/Generator.swift index 1e38c717..c41b7e47 100644 --- a/Sources/MockingbirdCli/Interface/Generator.swift +++ b/Sources/MockingbirdCli/Interface/Generator.swift @@ -107,7 +107,9 @@ class Generator { func getCachedTestTarget(targetName: String) -> TargetType? { guard !config.disableThunkStubs, let cacheDirectory = testTargetCacheDirectory, + let projectHash = getProjectHash(config.projectPath), let cachedTarget = findCachedTestTarget(for: targetName, + projectHash: projectHash, cliVersion: cliVersion, configHash: configHash, cacheDirectory: cacheDirectory, @@ -194,25 +196,26 @@ class Generator { } func cachePipelines(sourcePipelines: [Pipeline], pruningPipeline: PruningPipeline?) throws { + guard let projectHash = getProjectHash(config.projectPath) else { return } + // Cache source targets for generation. - if let projectHash = getProjectHash(config.projectPath) { - try sourceTargetCacheDirectory.mkpath() - try sourcePipelines.forEach({ - try $0.cache(projectHash: projectHash, - cliVersion: cliVersion, - configHash: configHash, - sourceRoot: config.sourceRoot, - cacheDirectory: sourceTargetCacheDirectory, - environment: getBuildEnvironment) - }) - } + try sourceTargetCacheDirectory.mkpath() + try sourcePipelines.forEach({ + try $0.cache(projectHash: projectHash, + cliVersion: cliVersion, + configHash: configHash, + sourceRoot: config.sourceRoot, + cacheDirectory: sourceTargetCacheDirectory, + environment: getBuildEnvironment) + }) // Cache test target for thunk pruning. if !config.disableThunkStubs { if let testTargetCacheDirectory = testTargetCacheDirectory, let environmentSourceRoot = config.environmentSourceRoot { try testTargetCacheDirectory.mkpath() - try pruningPipeline?.cache(cliVersion: cliVersion, + try pruningPipeline?.cache(projectHash: projectHash, + cliVersion: cliVersion, configHash: configHash, sourceRoot: environmentSourceRoot, cacheDirectory: testTargetCacheDirectory, diff --git a/Sources/MockingbirdGenerator/Parser/Project/TestTarget.swift b/Sources/MockingbirdGenerator/Parser/Project/TestTarget.swift index 9439750b..4e12398f 100644 --- a/Sources/MockingbirdGenerator/Parser/Project/TestTarget.swift +++ b/Sources/MockingbirdGenerator/Parser/Project/TestTarget.swift @@ -10,11 +10,13 @@ import PathKit public class TestTarget: CodableTarget { public let mockedTypeNames: [Path: Set] + public let projectHash: String public let cliVersion: String public let configHash: String enum CodingKeys: String, CodingKey { case mockedTypeNames + case projectHash case cliVersion case configHash } @@ -22,10 +24,12 @@ public class TestTarget: CodableTarget { public init(from target: T, sourceRoot: Path, mockedTypeNames: [Path: Set], + projectHash: String, cliVersion: String, configHash: String, environment: () -> [String: Any]) throws { self.mockedTypeNames = mockedTypeNames + self.projectHash = projectHash self.cliVersion = cliVersion self.configHash = configHash @@ -41,6 +45,7 @@ public class TestTarget: CodableTarget { let container = try decoder.container(keyedBy: CodingKeys.self) self.mockedTypeNames = try container.decode([Path: Set].self, forKey: .mockedTypeNames) + self.projectHash = try container.decode(String.self, forKey: .projectHash) self.cliVersion = try container.decode(String.self, forKey: .cliVersion) self.configHash = try container.decode(String.self, forKey: .configHash) @@ -51,6 +56,7 @@ public class TestTarget: CodableTarget { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(mockedTypeNames, forKey: .mockedTypeNames) + try container.encode(projectHash, forKey: .projectHash) try container.encode(cliVersion, forKey: .cliVersion) try container.encode(configHash, forKey: .configHash)