1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00
Files
app/phpmon/Common/Testables/TestableConfiguration.swift
2023-03-03 16:49:38 +01:00

73 lines
2.4 KiB
Swift

//
// TestableConfiguration.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 16/10/2022.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
public struct TestableConfiguration: Codable {
var architecture: String
var filesystem: [String: FakeFile]
var shellOutput: [String: BatchFakeShellOutput]
var commandOutput: [String: String]
var preferenceOverrides: [PreferenceName: Bool]
func apply() {
Log.separator()
Log.info("USING TESTABLE CONFIGURATION...")
Homebrew.fake = true
Log.separator()
Log.info("Applying fake shell...")
ActiveShell.useTestable(shellOutput)
Log.info("Applying fake filesystem...")
ActiveFileSystem.useTestable(filesystem)
Log.info("Applying fake commands...")
ActiveCommand.useTestable(commandOutput)
Log.info("Applying fake scanner...")
ValetScanner.useFake()
Log.info("Applying fake services manager...")
ServicesManager.useFake()
Log.info("Applying fake Valet domain interactor...")
ValetInteractor.useFake()
Log.info("Applying temporary preference overrides...")
preferenceOverrides.forEach { (key: PreferenceName, value: Any?) in
Preferences.shared.cachedPreferences[key] = value
}
}
func toJson(pretty: Bool = false) -> String {
let data = try! JSONEncoder().encode(self)
if pretty {
return data.prettyPrintedJSONString! as String
}
return String(data: data, encoding: .utf8)!
}
static func loadFrom(path: String) -> TestableConfiguration {
let url = URL(fileURLWithPath: path.replacingTildeWithHomeDirectory)
if !FileManager.default.fileExists(atPath: url.path) {
/*
You will need to run the `TestableConfigurationTest` test,
which will generate two configuration files you can use.
*/
fatalError("Error: the expected configuration file at \(url.path) is missing!")
}
/*
If the decoder below fails to decode the configuration file,
the configuration may have been updated.
In that case, you will need to run the test (see above) again.
*/
return try! JSONDecoder().decode(
TestableConfiguration.self,
from: try! String(contentsOf: url, encoding: .utf8).data(using: .utf8)!
)
}
}