Skip to content

Commit

Permalink
Invalidate test target cache on project changes (#161)
Browse files Browse the repository at this point in the history
Adding or removing test files were previously not causing the test
target cache to invalidate (unlike the source target cache), which was
causing thunk stubs to not generate for newly referenced mocked types.
  • Loading branch information
andrewchang-bird authored Jul 30, 2020
1 parent 2d3df5a commit cf77e50
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
11 changes: 10 additions & 1 deletion Sources/MockingbirdCli/Interface/Generator+PruningPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -95,6 +96,7 @@ extension Generator {
target = try TestTarget(from: pipelineTarget,
sourceRoot: sourceRoot,
mockedTypeNames: mockedTypeNames,
projectHash: projectHash,
cliVersion: cliVersion,
configHash: configHash,
environment: environment)
Expand All @@ -106,6 +108,7 @@ extension Generator {
target = try TestTarget(from: pipelineTarget,
sourceRoot: sourceRoot,
mockedTypeNames: mockedTypeNames,
projectHash: projectHash,
cliVersion: cliVersion,
configHash: configHash,
environment: environment)
Expand All @@ -119,6 +122,7 @@ extension Generator {
}

func findCachedTestTarget(for targetName: String,
projectHash: String,
cliVersion: String,
configHash: String,
cacheDirectory: Path,
Expand All @@ -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
Expand Down
27 changes: 15 additions & 12 deletions Sources/MockingbirdCli/Interface/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions Sources/MockingbirdGenerator/Parser/Project/TestTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ import PathKit

public class TestTarget: CodableTarget {
public let mockedTypeNames: [Path: Set<String>]
public let projectHash: String
public let cliVersion: String
public let configHash: String

enum CodingKeys: String, CodingKey {
case mockedTypeNames
case projectHash
case cliVersion
case configHash
}

public init<T: Target>(from target: T,
sourceRoot: Path,
mockedTypeNames: [Path: Set<String>],
projectHash: String,
cliVersion: String,
configHash: String,
environment: () -> [String: Any]) throws {
self.mockedTypeNames = mockedTypeNames
self.projectHash = projectHash
self.cliVersion = cliVersion
self.configHash = configHash

Expand All @@ -41,6 +45,7 @@ public class TestTarget: CodableTarget {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.mockedTypeNames = try container.decode([Path: Set<String>].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)

Expand All @@ -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)

Expand Down

0 comments on commit cf77e50

Please sign in to comment.