2019-01-06 19:26:08 +00:00
|
|
|
//
|
|
|
|
// Bundle+JSON.swift
|
2024-10-01 18:05:41 +00:00
|
|
|
// masTests
|
2019-01-06 19:26:08 +00:00
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 1/5/19.
|
|
|
|
// Copyright © 2019 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2020-09-06 20:15:02 +00:00
|
|
|
extension Data {
|
|
|
|
/// Unsafe initializer for loading data from string paths.
|
|
|
|
/// - Parameter file: Relative path within the JSON folder
|
|
|
|
init(from fileName: String) {
|
|
|
|
let fileURL = Bundle.url(for: fileName)!
|
|
|
|
try! self.init(contentsOf: fileURL, options: .mappedIfSafe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 19:26:08 +00:00
|
|
|
extension Bundle {
|
|
|
|
/// Locates a JSON response file from the test bundle.
|
|
|
|
///
|
|
|
|
/// - Parameter fileName: Name of file to locate.
|
|
|
|
/// - Returns: URL to file.
|
2020-09-06 20:15:02 +00:00
|
|
|
static func url(for fileName: String) -> URL? {
|
2021-04-26 23:44:17 +00:00
|
|
|
// The Swift Package Manager places resources in a separate bundle from the executable.
|
2021-04-28 21:56:24 +00:00
|
|
|
// https://forums.swift.org/t/swift-5-3-spm-resources-in-tests-uses-wrong-bundle-path/37051
|
|
|
|
let bundleURL = Bundle(for: NetworkSessionMock.self)
|
|
|
|
.bundleURL
|
|
|
|
.deletingLastPathComponent()
|
2024-10-01 18:05:41 +00:00
|
|
|
.appendingPathComponent("mas_masTests.bundle")
|
2021-04-28 21:56:24 +00:00
|
|
|
guard let bundle = Bundle(url: bundleURL),
|
|
|
|
let url = bundle.url(for: fileName)
|
|
|
|
else {
|
|
|
|
fatalError("Unable to load file \(fileName)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return url
|
2019-01-06 19:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a URL for a file in the JSON directory of the current bundle.
|
|
|
|
///
|
|
|
|
/// - Parameter fileName: Name of file to locate.
|
|
|
|
/// - Returns: URL to file.
|
2021-04-28 21:56:24 +00:00
|
|
|
private func url(for fileName: String) -> URL? {
|
|
|
|
url(
|
|
|
|
forResource: fileName.fileNameWithoutExtension,
|
|
|
|
withExtension: fileName.fileExtension,
|
|
|
|
subdirectory: "JSON"
|
|
|
|
)
|
2019-01-06 19:26:08 +00:00
|
|
|
}
|
|
|
|
}
|