### Prefire Configuration Example Source: https://github.com/barredewe/prefire/blob/main/README.md Example of a `.prefire.yml` file for configuring Prefire. Refer to the Configuration guide for detailed options. ```yaml test_configuration: target: MyApp playbook_configuration: preview_default_enabled: true ``` -------------------------------- ### Install Prefire CLI using Homebrew Source: https://github.com/barredewe/prefire/blob/main/Documentation/Installation.md Install the Prefire command-line interface using Homebrew by tapping the repository and installing the package. ```bash brew tap barredewe/prefire brew install prefire ``` -------------------------------- ### Prefire YAML Configuration Example Source: https://github.com/barredewe/prefire/blob/main/Documentation/Configuration.md This snippet shows a comprehensive example of a .prefire.yml file, demonstrating configurations for both 'test_configuration' and 'playbook_configuration'. It covers settings for target, paths, templates, devices, OS versions, snapshotting, sources, and imports. ```yaml test_configuration: target: PrefireExample test_target_path: ${PROJECT_DIR}/Tests test_file_path: PrefireExampleTests/PreviewTests.generated.swift template_file_path: CustomPreviewTests.stencil simulator_device: "iPhone15,2" required_os: 16 preview_default_enabled: true use_grouped_snapshots: true split_snapshot_directories: false sources: - ${PROJECT_DIR}/Sources/ snapshot_devices: - iPhone 14 - iPad imports: - UIKit - SwiftUI testable_imports: - Prefire playbook_configuration: preview_default_enabled: true template_file_path: CustomModels.stencil imports: - UIKit - Foundation testable_imports: - SwiftUI ``` -------------------------------- ### Install Prefire CLI with Mint Source: https://github.com/barredewe/prefire/blob/main/README.md Install the Prefire command-line interface using Mint for easy access to its features. The installed binary is self-contained. ```sh mint install BarredEwe/Prefire ``` -------------------------------- ### SwiftUI Preview Example Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md A simple SwiftUI preview definition that Prefire can process. This example shows a basic button within a preview. ```swift #Preview("Default") { Button("Submit") { } } ``` -------------------------------- ### Prefire CLI Usage Examples Source: https://github.com/barredewe/prefire/blob/main/Documentation/Installation.md Basic commands for using the Prefire CLI to generate tests or playbook components. ```bash prefire tests prefire playbook ``` -------------------------------- ### Prefire YAML Configuration Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This YAML configuration file defines Prefire's testing setup, including the main target, test target path, test file path, and the template file to use. It also specifies any necessary imports. ```yaml test_configuration: target: MyApp test_target_path: ${PROJECT_DIR}/MyAppTests test_file_path: MyAppTests/PreviewTests.generated.swift template_file_path: MyAppTests/MinimalPreviewTests.stencil imports: - UIKit ``` -------------------------------- ### Write SwiftUI Preview Source: https://github.com/barredewe/prefire/blob/main/README.md Define your UI components using SwiftUI's `#Preview` macro. Prefire will automatically generate snapshots for these previews when tests are run. ```swift #Preview { Button("Submit") } ``` -------------------------------- ### Implement PrefireProvider Protocol Source: https://github.com/barredewe/prefire/blob/main/README.md To generate tests and playbook views, conform your preview struct to the PrefireProvider protocol. ```swift struct Text_Previews: PreviewProvider, PrefireProvider { static var previews: some View { ... } } ``` -------------------------------- ### Generate Prefire Tests and Playbooks Source: https://github.com/barredewe/prefire/blob/main/README.md Use these commands to generate snapshot tests or playbook models. Run with --help for more options. ```bash # Generate snapshot tests prefire tests # Generate playbook models prefire playbook ``` -------------------------------- ### Prefire CLI Help Command Source: https://github.com/barredewe/prefire/blob/main/Documentation/Installation.md Use the --help flag with any Prefire CLI command to view available options and usage details. ```bash prefire tests --help prefire playbook --help ``` -------------------------------- ### Generate Preview Test Cases with Stencil Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Iterates over `previewsMacrosDict` to generate Swift test functions for each macro model. Use `|indent:N` to align the Swift body within the generated closure. ```stencil {% for macroModel in argument.previewsMacrosDict %} func test_{{ macroModel.componentTestName }}_Preview() { // ... {{ macroModel.body|indent:12 }} // ... } {% endfor %} ``` -------------------------------- ### Display Playbook View Components Source: https://github.com/barredewe/prefire/blob/main/README.md Use `PlaybookView` with `isComponent: true` to display a list of all available views. ```swift import Prefire struct ContentView: View { var body: some View { PlaybookView(isComponent: true, previewModels: PreviewModels.models) } } ``` -------------------------------- ### Enable Package Plugin Fingerprint Validation for CI Source: https://github.com/barredewe/prefire/blob/main/README.md To run Prefire via Continuous Integration (CI), you need to configure permissions by enabling this setting. ```bash defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES ``` -------------------------------- ### Configure SPM Plugins for Targets Source: https://github.com/barredewe/prefire/blob/main/Documentation/Installation.md Add Prefire plugins to your Swift package targets for Playbook or Snapshot Tests. ```swift .target( plugins: [ // For Playbook (Demo) view .plugin(name: "PrefirePlaybookPlugin", package: "Prefire") ] ), .testTarget( plugins: [ // For Snapshot Tests .plugin(name: "PrefireTestsPlugin", package: "Prefire") ] ) ``` -------------------------------- ### Customize Snapshot Parameters Source: https://github.com/barredewe/prefire/blob/main/README.md Set delay, precision, and perceptualPrecision for snapshots. Use this when fine-tuning snapshot quality and timing. ```swift .snapshot(delay: 0.3, precision: 0.95, perceptualPrecision: 0.98) ``` ```swift static var previews: some View { TestView() .snapshot(delay: 0.3, precision: 0.95, perceptualPrecision: 0.98) } ``` -------------------------------- ### Snapshot Devices Array Parsing Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This stencil code demonstrates how to split a pipe-delimited string of snapshot devices into a Swift array. It handles cases where no devices are specified. ```stencil private let snapshotDevices:[String]{% if argument.snapshotDevices %} = {{ argument.snapshotDevices|split:"|" }}{% else %} = []{% endif %} ``` -------------------------------- ### Configure Custom Template Path in .prefire.yml Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Specify a custom Stencil template file path for either test or playbook configurations within your .prefire.yml file. The path is resolved relative to the target directory. ```yaml # .prefire.yml test_configuration: target: MyApp test_target_path: ${PROJECT_DIR}/MyAppTests test_file_path: MyAppTests/PreviewTests.generated.swift template_file_path: MyAppTests/CustomPreviewTests.stencil playbook_configuration: target: ${PROJECT_DIR}/MyApp template_file_path: MyApp/CustomPreviewModels.stencil ``` -------------------------------- ### Connect Previews in a Flow Source: https://github.com/barredewe/prefire/blob/main/README.md Group related previews together to represent a user story or flow. Useful for demonstrating multi-step user interactions like authentication. ```swift .previewUserStory(.auth) ``` ```swift static var previews: some View { PrefireView() .previewUserStory(.auth) } ``` ```swift static var previews: some View { AuthView() .previewUserStory(.auth) } ``` -------------------------------- ### Add Prefire to Project (SPM) Source: https://github.com/barredewe/prefire/blob/main/README.md Integrate Prefire into your project using Swift Package Manager by adding the Prefire and swift-snapshot-testing packages to your dependencies. ```swift // Package.swift dependencies: [ .package(url: "https://github.com/BarredEwe/Prefire.git", from: "5.4.0") .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.0"), ], .testTarget( dependencies: [ .product(name: "Prefire", package: "Prefire"), .product(name: "SnapshotTesting", package: "swift-snapshot-testing"), ], plugins: [ .plugin(name: "PrefireTestsPlugin", package: "Prefire") ] ) ``` -------------------------------- ### Enable Prefire for a Preview Source: https://github.com/barredewe/prefire/blob/main/README.md When `preview_default_enabled` is set to false, use the `.prefireEnabled()` modifier to include a #Preview in tests. ```swift #Preview { Text("") .prefireEnabled() } ``` -------------------------------- ### Generated Prefire Test File Skeleton Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This is a skeleton of a Swift test file generated by Prefire based on the provided Stencil template and SwiftUI preview. It includes necessary imports and a test case for the preview. ```swift import XCTest import SwiftUI import Prefire import UIKit @testable import MyApp import SnapshotTesting @MainActor class PreviewTests: XCTestCase { private let deviceConfig: DeviceConfig = ViewImageConfig.iPhoneX.deviceConfig func test_Default_Preview() { let prefireSnapshot = PrefireSnapshot( { Button("Submit") { } }, name: "Default", isScreen: false, device: deviceConfig ) if let failure = assertSnapshot(of: prefireSnapshot.loadViewWithPreferences().0, as: .image) { XCTFail(failure) } } } ``` -------------------------------- ### Guard for Macro Previews Dictionary Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This conditional block ensures that the subsequent code for rendering `#Preview` macros is only executed if the `previewsMacrosDict` argument is present. This prevents issues with empty macro dictionaries. ```stencil {% if argument.previewsMacrosDict %} {% endif %} ``` -------------------------------- ### Minimal Preview Tests Stencil Template Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This Stencil template is used to generate leaner test files for Prefire, allowing for custom imports and testable targets. It's designed to be used with the `.prefire.yml` configuration. ```stencil // swiftlint:disable all // swiftformat:disable all import XCTest import SwiftUI import Prefire {% for import in argument.imports %} import {{ import }} {% endfor %} {% if argument.mainTarget %} @testable import {{ argument.mainTarget }} {% endif %} {% for import in argument.testableImports %} @testable import {{ import }} {% endfor %} import SnapshotTesting @MainActor class {PREVIEW_FILE_NAME}Tests: XCTestCase { private let deviceConfig: DeviceConfig = ViewImageConfig.iPhoneX.deviceConfig {% if argument.previewsMacrosDict %} {% for macroModel in argument.previewsMacrosDict %} func test_{{ macroModel.componentTestName }}_Preview() { let prefireSnapshot = PrefireSnapshot( { {{ macroModel.body|indent:12 }} }, name: "{{ macroModel.displayName }}", isScreen: {% if macroModel.isScreen == 1 %}true{% else %}false{% endif %}, device: deviceConfig ) if let failure = assertSnapshot(of: prefireSnapshot.loadViewWithPreferences().0, as: .image) { XCTFail(failure) } } {% endfor %} {% endif %} } ``` -------------------------------- ### Provide Default Value Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `default:VALUE` filter to provide a default `VALUE` when a variable is missing. ```Liquid {{ argument.simulatorDevice|default:nil }} ``` -------------------------------- ### Add Prefire as SPM Dependency Source: https://github.com/barredewe/prefire/blob/main/Documentation/Installation.md Add Prefire to your project's dependencies in the Package.swift manifest. ```swift dependencies: [ .package(url: "https://github.com/BarredEwe/Prefire", from: "4.0.0") ] ``` -------------------------------- ### Generating SwiftUI PreviewWrapper with Properties Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This template block generates a SwiftUI View called PreviewWrapper. It dynamically includes properties and embeds the body, with indentation applied to the body content. ```stencil {% if macroModel.properties %} struct PreviewWrapper{{ macroModel.componentTestName }}: SwiftUI.View { {{ macroModel.properties }} var body: some View { {{ macroModel.body|indent:12 }} } } {% endif %} ``` -------------------------------- ### Mark UI State for Previews Source: https://github.com/barredewe/prefire/blob/main/README.md Assign a specific state to a preview, such as loading or error. This helps in visualizing different states of a view within the preview canvas. ```swift .previewState(.loading) ``` ```swift static var previews: some View { TestView("Default") TestView("Loading") .previewState(.loading) } ``` -------------------------------- ### Indent String Lines Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `indent:N` filter to indent every line of the input string by `N` spaces. ```Liquid {{ macroModel.body|indent:12 }} ``` -------------------------------- ### Exclude PreviewModels with Xcode Source: https://github.com/barredewe/prefire/blob/main/README.md Configure Xcode build settings to exclude PreviewModels from release builds by adding PLAYBOOK_DISABLED to SWIFT_ACTIVE_COMPILATION_CONDITIONS. ```bash SWIFT_ACTIVE_COMPILATION_CONDITIONS = PLAYBOOK_DISABLED; ``` -------------------------------- ### Exclude PreviewModels with Swift Package Manager Source: https://github.com/barredewe/prefire/blob/main/README.md Configure Swift Package Manager to exclude PreviewModels from release builds by defining PLAYBOOK_DISABLED. ```swift swiftSettings: [ .define("PLAYBOOK_DISABLED", .when(configuration: .release)), ] ``` -------------------------------- ### Iterate PrefireProvider Types in Stencil Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This Stencil template iterates over types that implement or are based on `PrefireProvider` to generate test functions. It filters types and transforms their names for test function naming. ```stencil {% for type in types.types where type.implements.PrefireProvider or type.based.PrefireProvider or type|annotated:"PrefireProvider" %} func test_{{ type.name|lowerFirstLetter|replace:"_Previews", "" }}() { for preview in {{ type.name }}._allPreviews { // ... } } {% endfor %} ``` -------------------------------- ### Conditional File Path in Template Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This snippet shows how to conditionally include a static file path string. It's guarded by a check to see if a file argument is provided. ```stencil {% if argument.file %} private var file: StaticString { .init(stringLiteral: "{{ argument.file }}") } {% endif %} ``` -------------------------------- ### Split String into Array Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `split:SEP` filter to split a string by a separator `SEP` and return a Swift array literal. ```Liquid {{ argument.snapshotDevices|split:"|" }} ``` -------------------------------- ### Class Name Placeholder in Tests Template Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md This placeholder is used in the tests template to dynamically insert the class name. It ensures the same template works for different snapshot grouping modes. ```stencil @MainActor class {PREVIEW_FILE_NAME}Tests: XCTestCase, Sendable { ``` -------------------------------- ### Ignore Prefire for a Preview Source: https://github.com/barredewe/prefire/blob/main/README.md Use the `.prefireIgnored()` modifier on a #Preview block to prevent Prefire from processing it. ```swift #Preview { Text("") .prefireIgnored() } ``` -------------------------------- ### Check for Last Loop Iteration Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `forloop.last` variable, which is `true` on the last iteration of a `{% for %}` loop. Useful for controlling separators. ```Liquid {%- if not forloop.last %} {% endif %} ``` -------------------------------- ### Disable Sandbox for Xcode File Generation Source: https://github.com/barredewe/prefire/blob/main/README.md If Xcode is unable to generate tests in a custom path, disable the sandbox for file generation by running this command in your terminal. ```bash defaults write com.apple.dt.Xcode IDEPackageSupportDisablePluginExecutionSandbox -bool YES ``` -------------------------------- ### Filter Types by Annotation Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `annotated:NAME` filter within a `{% for %}` loop to filter types that are annotated with `NAME`. ```Liquid {% for type in types.types where type|annotated:"PrefireProvider" %} ``` -------------------------------- ### Replace Substring in String Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `replace:OLD,NEW` filter to replace all occurrences of a literal substring `OLD` with `NEW`. ```Liquid {{ type.name|replace:"_Previews", "" }} ``` -------------------------------- ### Lowercase First Letter of String Source: https://github.com/barredewe/prefire/blob/main/Documentation/Templates.md Use the `lowerFirstLetter` filter to lowercase the first character of a string. Useful for generating test function names. ```Liquid {{ type.name|lowerFirstLetter }} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.