BarredEwe / Prefire

🔥 A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests
Apache License 2.0
251 stars 16 forks source link

Support for environment parameters in `prefire.yml` #60

Closed BarredEwe closed 1 month ago

BarredEwe commented 1 month ago

Short description 📝

Support for environment parameters in prefire.yml

test_configuration:
    - test_file_path: ${TARGET_DIR}/PrefireExampleTests/PreviewTests.generated.swift

Solution 📦

You cannot now use environment variables inside the prefire.yml configuration file. This often resulted in an inability to flexibly configure. I decided to add a simple support for this.

Implementation 👩‍💻👨‍💻

I get the data from ProcessInfo.processInfo.environment. Then using a simple search I replace env with data from ProcessInfo:

if let key = value?.findKey(), let envValue = env[key] {
    value = value?.replacingOccurrences(of: "${\(key)}", with: envValue)
}

func findKey() -> String? {
    guard contains("${") else { return nil }

    return components(separatedBy: "${").last?.components(separatedBy: "}").first
}

It's not the fastest solution, but you can speed it up in the future by using index lookup.