BarredEwe / Prefire

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

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

Closed BarredEwe closed 5 months ago

BarredEwe commented 5 months 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.