BarredEwe / Prefire

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

Regression when specifying a `template_file_path` #37

Closed stefanceriu closed 8 months ago

stefanceriu commented 9 months ago

Context 🕵️‍♀️

Setup the Prefire example project to use a - template_file_path: PreviewTests.stencil, where PreviewTests.stencil is just a copy of the default template next to PreFireExample.xcodeproj

What 🌱

Specifying a template_file_path doesn't seem to be working anymore resulting in a does not exist or is not readable error

Proposal 🎉

Let's try to fix it maybe 😁

stefanceriu commented 9 months ago

Okay, so the problem seems to be that the Prefire binary isn't aware of what the right path should be. I've managed to get it working here piggybacking on the config file URL but perhaps you have an idea for a cleaner solution.

BarredEwe commented 9 months ago

Fixed in: https://github.com/BarredEwe/Prefire/pull/39

stefanceriu commented 8 months ago

We need to reopen this because it still doesn't work for swfit packages e.g. https://github.com/element-hq/compound-ios/pull/61 and neither does my original fix from here so I'm not entirely sure what the problem is.

We were relying on it to set default perceptual precission to 0.98 and I was able to work around it by specifying .snapshot(perceptualPrecision: 0.98) on the previews themselves.

stefanceriu commented 8 months ago

This might help, the configuration isn't picked up when running directly through xcodebuild either

xcodebuild -project 'PreFireExample.xcodeproj' -scheme 'PrefireExample' -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 15,OS=17.2' test

stefanceriu commented 8 months ago

I got it working by using URL(filePath:) to build correct file paths but it's clear that the ConfigPathBuilder needs tweaking. I would suggest rewriting it to use URls instead of direct string manipulation.

     static func load(from configPath: String?, testTargetPath: String?, verbose: Bool) -> Config? {
-        let possibleConfigPaths = ConfigPathBuilder.possibleConfigPaths(for: configPath, testTargetPath: testTargetPath)
+        var possibleConfigPaths = [String]()
+
+        if let configPath {
+            possibleConfigPaths = [URL(filePath: configPath).appending(path: ".prefire.yml").path()]
+        }

         for path in possibleConfigPaths {
-            guard let configUrl = URL(string: Constants.fileMark + path),
-                  FileManager.default.fileExists(atPath: configUrl.path),
+
+            let configUrl = URL(filePath: path)
+
+            print("🟢 Checking url: \(configUrl)")
+
+            guard FileManager.default.fileExists(atPath: configUrl.path),
                   let configDataString = try? String(contentsOf: configUrl, encoding: .utf8) else { continue }

             if verbose {
🟢 Checking url: file:///Users/stefanceriu/Developer/Prefire/Example/.prefire.yml
🟢 The '.prefire' file is used on the path: /Users/stefanceriu/Developer/Prefire/Example/.prefire.yml

vs what the current version does

🟡 The '.prefire' file was not found by paths:
  - /Users/stefanceriu/Developer/Prefire/Example/PrefireExampleTests/.prefire.yml
  - Users/stefanceriu/Developer/Prefire/Example/.prefire.yml
  - /Users/stefanceriu/Developer/Prefire/Example/Users/stefanceriu/Developer/Prefire/Example/.prefire.yml