From 8d611fb5d7eb91edcfcb0b9aaba9941b2ecf8b2b Mon Sep 17 00:00:00 2001 From: Andrew Chang Date: Fri, 24 Dec 2021 04:28:52 -1000 Subject: [PATCH] Show help message no mockable types are generated --- .../Handlers/Generator+Pipeline.swift | 1 + .../Generator/Operations/FileGenerator.swift | 22 ++++++++++++++++--- .../Operations/GenerateFileOperation.swift | 3 +++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Sources/MockingbirdCli/Interface/Handlers/Generator+Pipeline.swift b/Sources/MockingbirdCli/Interface/Handlers/Generator+Pipeline.swift index 9d610c82..a5d969dc 100644 --- a/Sources/MockingbirdCli/Interface/Handlers/Generator+Pipeline.swift +++ b/Sources/MockingbirdCli/Interface/Handlers/Generator+Pipeline.swift @@ -89,6 +89,7 @@ extension Generator { findMockedTypesResult: findMockedTypesOperation?.result, config: GenerateFileConfig( moduleName: moduleName, + testTargetName: config.environmentTargetName, outputPath: outputPath, header: config.header, compilationCondition: config.compilationCondition, diff --git a/Sources/MockingbirdGenerator/Generator/Operations/FileGenerator.swift b/Sources/MockingbirdGenerator/Generator/Operations/FileGenerator.swift index 3f1a0bb0..eebac36e 100644 --- a/Sources/MockingbirdGenerator/Generator/Operations/FileGenerator.swift +++ b/Sources/MockingbirdGenerator/Generator/Operations/FileGenerator.swift @@ -90,8 +90,14 @@ class FileGenerator { } private func generateFileBody() -> PartialFileContent { - guard !mockableTypes.isEmpty else { return PartialFileContent(contents: "") } - let operations = mockableTypes + guard !mockableTypes.isEmpty else { + return PartialFileContent(contents: """ + // No mockable types in \(singleQuoted: config.moduleName). Check that the module's + // source files have classes or protocols that are not private and not marked as final. + """) + } + + let availableMockableTypes = mockableTypes .filter({ mockableType in switch config.pruningMethod { case .omit: @@ -100,6 +106,16 @@ class FileGenerator { case .disable, .stub: return true } }) + guard !availableMockableTypes.isEmpty else { + let testTarget = config.testTargetName?.singleQuoted ?? "your test target" + return PartialFileContent(contents: """ + // No mocks used in \(testTarget). Mockingbird is configured to + // only generate mocks for types that are explicitly initialized in your tests with + // `mock(SomeType.self)`. For more information, see 'Thunk Pruning' in the README. + """) + } + + let operations = availableMockableTypes .sorted(by: <) .flatMap({ mockableType -> [RenderTemplateOperation] in let mockableTypeTemplate = MockableTypeTemplate(mockableType: mockableType, @@ -137,7 +153,7 @@ class FileGenerator { substructure: [generateFileHeader(), generateFileBody(), generateFileFooter()].filter({ !$0.isEmpty }), - delimiter: "\n", + delimiter: "\n\n", footer: "\n") } diff --git a/Sources/MockingbirdGenerator/Generator/Operations/GenerateFileOperation.swift b/Sources/MockingbirdGenerator/Generator/Operations/GenerateFileOperation.swift index 4b349e53..0ea07360 100644 --- a/Sources/MockingbirdGenerator/Generator/Operations/GenerateFileOperation.swift +++ b/Sources/MockingbirdGenerator/Generator/Operations/GenerateFileOperation.swift @@ -12,6 +12,7 @@ import os.log public struct GenerateFileConfig { let moduleName: String + let testTargetName: String? let outputPath: Path let header: [String] let compilationCondition: String? @@ -20,6 +21,7 @@ public struct GenerateFileConfig { let pruningMethod: PruningMethod public init(moduleName: String, + testTargetName: String?, outputPath: Path, header: [String], compilationCondition: String?, @@ -27,6 +29,7 @@ public struct GenerateFileConfig { disableSwiftlint: Bool, pruningMethod: PruningMethod) { self.moduleName = moduleName + self.testTargetName = testTargetName self.outputPath = outputPath self.header = header self.compilationCondition = compilationCondition