Skip to content

Commit

Permalink
Merge pull request #42 from stefanceriu/stefan/37
Browse files Browse the repository at this point in the history
Fixes #37 - Build correct config file paths, update tests
  • Loading branch information
BarredEwe authored Mar 11, 2024
2 parents 7c70aa3 + 982b207 commit bf7fa45
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Binaries/PrefireBinary.artifactbundle/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"artifacts": {
"PrefireBinary": {
"type": "executable",
"version": "2.0.3",
"version": "2.0.4",
"variants": [
{
"path": "prefire-2.0.3-macos/bin/prefire",
"path": "prefire-2.0.4-macos/bin/prefire",
"supportedTriples": ["x86_64-apple-macosx", "arm64-apple-macosx"]
},
]
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ struct GeneratedTestsOptions {
self.sourcery = sourcery
self.target = config?.tests.target ?? target
self.testTarget = testTarget
if let template = config?.tests.template, let testTargetURL = testTargetPath.flatMap(URL.init(string:)) {
self.template = testTargetURL.deletingLastPathComponent().appending(path: template).absoluteString

if let template = config?.tests.template, let testTargetPath {
let testTargetURL = URL(filePath: testTargetPath)
let templateURL = testTargetURL.appending(path: template)
self.template = templateURL.absoluteURL.path()
} else {
self.template = template
}

self.sources = sources
self.output = config?.tests.testFilePath ?? output
prefireEnabledMarker = config?.tests.previewDefaultEnabled ?? true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension Prefire {
struct Version: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Display the current version of Prefire")

static var value: String = "2.0.3"
static var value: String = "2.0.4"

func run() throws {
print(Self.value)
Expand Down
5 changes: 2 additions & 3 deletions PrefireExecutable/Sources/prefire/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Foundation

private enum Constants {
static let separtor = ":"
static let fileMark = "file://"
}

struct Config {
Expand Down Expand Up @@ -124,8 +123,8 @@ extension Config {
let possibleConfigPaths = ConfigPathBuilder.possibleConfigPaths(for: configPath, testTargetPath: testTargetPath)

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

if verbose {
Expand Down
38 changes: 17 additions & 21 deletions PrefireExecutable/Sources/prefire/Config/ConfigPathBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@ enum ConfigPathBuilder {
/// - Returns: An array of possible paths for the configuration file
static func possibleConfigPaths(for configPath: String?, testTargetPath: String?) -> [String] {
var possibleConfigPaths = [String]()

if var configPath = configPath {
// Check if the provided path is absolute and remove leading slash if so
if configPath.hasPrefix("/") {
configPath.removeFirst()
}
if configPath.hasSuffix("/") {
configPath.removeLast()

for path in [testTargetPath, configPath] {
guard let path else {
continue
}

possibleConfigPaths.append(configPath)
possibleConfigPaths.append(FileManager.default.currentDirectoryPath + "/\(configPath)")

if !configPath.hasSuffix(configFileName) {
possibleConfigPaths = possibleConfigPaths.map { $0 + "/\(configFileName)" }

// This will build the absolute path relative to the current directory
var configURL = URL(filePath: path)

// Add the config file name if not provided
if configURL.lastPathComponent != configFileName {
configURL.append(component: configFileName)
}
} else {
// Add the default path if no specific one is provided
possibleConfigPaths.append(FileManager.default.currentDirectoryPath + "/\(configFileName)")
}

if let testTargetPath {
possibleConfigPaths.insert(testTargetPath + "/\(configFileName)", at: 0)

possibleConfigPaths.append(configURL.absoluteURL.path())
}

// Add the default path
let configURL = URL(filePath: configFileName) // Relative to the current directory
possibleConfigPaths.append(configURL.absoluteURL.path())

return possibleConfigPaths
}
Expand Down
31 changes: 13 additions & 18 deletions PrefireExecutable/Tests/PrefireTests/ConfigPathBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConfigPathBuilderTests: XCTestCase {
let result = ConfigPathBuilder.possibleConfigPaths(for: nil, testTargetPath: testTargetPath)

let expectableResult = [
testTargetPath + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(testTargetPath)" + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configFileName)",
]

Expand All @@ -32,52 +32,47 @@ class ConfigPathBuilderTests: XCTestCase {
let result = ConfigPathBuilder.possibleConfigPaths(for: configPath, testTargetPath: testTargetPath)

let expectableResult = [
testTargetPath + "/\(configFileName)",
configPath,
FileManager.default.currentDirectoryPath + "/\(testTargetPath)" + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configPath)",
FileManager.default.currentDirectoryPath + "/\(configFileName)"
]

XCTAssertEqual(result, expectableResult)
}

func test_possibleConfigPathsWithConfigSeparated() {
var configPath = "/Tests/" + configFileName
let configPath = "Tests/" + configFileName
let result = ConfigPathBuilder.possibleConfigPaths(for: configPath, testTargetPath: testTargetPath)

configPath.removeFirst()
let expectableResult = [
testTargetPath + "/\(configFileName)",
configPath,
FileManager.default.currentDirectoryPath + "/\(testTargetPath)" + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configPath)",
FileManager.default.currentDirectoryPath + "/\(configFileName)"
]

XCTAssertEqual(result, expectableResult)
}

func test_possibleConfigPathsWithConfigNoFileName() {
var configPath = "/Tests/"
let configPath = "Tests/"
let result = ConfigPathBuilder.possibleConfigPaths(for: configPath, testTargetPath: testTargetPath)

configPath.removeFirst()
configPath.removeLast()
let expectableResult = [
testTargetPath + "/\(configFileName)",
configPath + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configPath)/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(testTargetPath)" + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configPath)" + "\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configFileName)"
]

XCTAssertEqual(result, expectableResult)
}

func test_possibleConfigPathsWithConfigNoFileNameNoTestingTarget() {
var configPath = "/Tests/"
let configPath = "Tests/"
let result = ConfigPathBuilder.possibleConfigPaths(for: configPath, testTargetPath: nil)

configPath.removeFirst()
configPath.removeLast()
let expectableResult = [
configPath + "/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configPath)/\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configPath)" + "\(configFileName)",
FileManager.default.currentDirectoryPath + "/\(configFileName)"
]

XCTAssertEqual(result, expectableResult)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ test_configuration:
- template_file_path: CustomPreviewTests.stencil
- simulator_device: "iPhone15,2"
- required_os: 16
- snapshot_devices: [iPhone 14, iPad]
- snapshot_devices:
- iPhone 14
- iPad
- preview_default_enabled: true
- imports:
- UIKit
Expand Down

0 comments on commit bf7fa45

Please sign in to comment.