|
9 | 9 | import XCTest
|
10 | 10 | import ELCLI
|
11 | 11 | import ELFoundation
|
| 12 | +import ELCodable // for JSON |
12 | 13 | @testable import ModuloKit
|
13 | 14 |
|
14 | 15 | class TestDefaults: XCTestCase {
|
15 | 16 | let modulo = Modulo()
|
16 |
| - |
| 17 | + |
| 18 | + // MARK: - Setup |
| 19 | + override func setUp() { |
| 20 | + super.setUp() |
| 21 | + |
| 22 | + moduloReset() |
| 23 | + } |
| 24 | + |
| 25 | + // MARK: - Migration |
| 26 | + func testMigrationFromNoOptionsInModuloFile() { |
| 27 | + let moduleFileJSONDictionary = [ |
| 28 | + "dependencies": [], |
| 29 | + "module": false, |
| 30 | + "name": "best project" |
| 31 | + ] as [String : Any] |
| 32 | + do { |
| 33 | + let moduleSpec = try spec(from: moduleFileJSONDictionary) |
| 34 | + let options = moduleSpec.options |
| 35 | + // validate defaults |
| 36 | + XCTAssertTrue(options.alwaysVerbose == false) |
| 37 | + XCTAssertTrue(options.depdencyInstallationPath == "modules") |
| 38 | + } catch { |
| 39 | + XCTFail("Failed with error \(error)") |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Bad Input |
| 44 | + func testGarbageValuesInModuloFileResultInSaneDefaults() { |
| 45 | + let moduleFileJSONDictionary = [ |
| 46 | + "dependencies": [], |
| 47 | + "module": false, |
| 48 | + "name": "best project", |
| 49 | + "options": [ |
| 50 | + "alwaysVerbose": "lolgarbage", |
| 51 | + "depdencyInstallationPath": ["fart": "toot"], |
| 52 | + "invalid_key": true, |
| 53 | + ] |
| 54 | + ] as [String : Any] |
| 55 | + do { |
| 56 | + let moduleSpec = try spec(from: moduleFileJSONDictionary) |
| 57 | + let options = moduleSpec.options |
| 58 | + // validate defaults since we fed it garbage |
| 59 | + XCTAssertTrue(options.alwaysVerbose == false) |
| 60 | + XCTAssertTrue(options.depdencyInstallationPath == "modules") |
| 61 | + } catch { |
| 62 | + XCTFail("Failed with error \(error)") |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // MARK: - Loaded from module file |
| 67 | + func testOptionsAreParsedFromModuleFile() { |
| 68 | + let directoryPath = "only-the-best-dependencies-live-here" |
| 69 | + let moduleFileJSONDictionary = [ |
| 70 | + "dependencies": [], |
| 71 | + "module": false, |
| 72 | + "name": "best project", |
| 73 | + "options": [ |
| 74 | + "alwaysVerbose": true, |
| 75 | + "depdencyInstallationPath": directoryPath, |
| 76 | + ] |
| 77 | + ] as [String : Any] |
| 78 | + |
| 79 | + do { |
| 80 | + let moduleSpec = try spec(from: moduleFileJSONDictionary) |
| 81 | + let options = moduleSpec.options |
| 82 | + XCTAssertTrue(options.alwaysVerbose == true) |
| 83 | + XCTAssertTrue(options.depdencyInstallationPath == directoryPath) |
| 84 | + } catch { |
| 85 | + XCTFail("Failed with error \(error)") |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // MARK: - CLI API |
| 90 | + func testReadingAllDefaults() { |
| 91 | + _ = Modulo() |
| 92 | + moduloReset() |
| 93 | + |
| 94 | + let result = Modulo.run(["defaults"]) |
| 95 | + |
| 96 | + // ideally we'd capture output (stdout) somehow |
| 97 | + // and verify our output is what we want but since |
| 98 | + // i can't see a nice way to do that with ELCLI |
| 99 | + // we'll just verify success instead. |
| 100 | + |
| 101 | + XCTAssertTrue(result == .success) |
| 102 | + } |
| 103 | + |
| 104 | + func testSettingDefault() { |
| 105 | + _ = Modulo() |
| 106 | + moduloReset() |
| 107 | + XCTAssertFalse(State.instance.options.alwaysVerbose) |
| 108 | + let verboseResult = Modulo.run(["defaults", "--set", "--alwaysVerbose", "true"]) |
| 109 | + XCTAssertTrue(verboseResult == .success) |
| 110 | + XCTAssertTrue(State.instance.options.alwaysVerbose) |
| 111 | + |
| 112 | + let directoryResult = Modulo.run(["defaults", "--set", "--moduleFolder", "bestDIR"]) |
| 113 | + XCTAssertTrue(directoryResult == .success) |
| 114 | + XCTAssertTrue(State.instance.options.depdencyInstallationPath == "bestDIR") |
| 115 | + } |
| 116 | + |
| 117 | + func testFailsIfSettingBadDefault() { |
| 118 | + _ = Modulo() |
| 119 | + moduloReset() |
| 120 | + |
| 121 | + xctAssertThrows({ |
| 122 | + _ = Modulo.run(["defaults", "--set", "--NoTheRightValue", "badValue.BadMe."]) |
| 123 | + }, "Running `defaults --set` with a bad flag/value did not fail") |
| 124 | + } |
| 125 | + |
| 126 | + func testSettingDefaultWithBadValue() { |
| 127 | + _ = Modulo() |
| 128 | + moduloReset() |
| 129 | + State.instance.options.alwaysVerbose = true |
| 130 | + |
| 131 | + let badVerboseResult = Modulo.run(["defaults", "--set", "--alwaysVerbose", "ohSoVerbose"]) |
| 132 | + XCTAssertTrue(badVerboseResult == .success) |
| 133 | + XCTAssertFalse(State.instance.options.alwaysVerbose) |
| 134 | + } |
| 135 | + |
| 136 | + func testSettingWithNoKey() { |
| 137 | + _ = Modulo() |
| 138 | + moduloReset() |
| 139 | + |
| 140 | + let initialVerbose = State.instance.options.alwaysVerbose |
| 141 | + let initialPath = State.instance.options.depdencyInstallationPath |
| 142 | + let result = Modulo.run(["defaults", "--set"]) |
| 143 | + XCTAssertTrue(result == .success, "Even though we set nothing, we shoud succeed with some output for the user") |
| 144 | + XCTAssertTrue(initialVerbose == State.instance.options.alwaysVerbose) |
| 145 | + XCTAssertTrue(initialPath == State.instance.options.depdencyInstallationPath) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// MARK: - Test Helpers |
| 150 | +extension TestDefaults { |
| 151 | + func spec(from dictionary: [String: Any]) throws -> ModuleSpec { |
| 152 | + let moduleFileJSONData = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted) |
| 153 | + guard let moduleFileJSON = JSON(data: moduleFileJSONData) else { |
| 154 | + throw NSError(domain: "TestDefaults", |
| 155 | + code: -1, |
| 156 | + userInfo: [ |
| 157 | + NSLocalizedDescriptionKey: "Failed to create JSON from Data" |
| 158 | + ]) |
| 159 | + } |
| 160 | + return try ModuleSpec.decode(moduleFileJSON) |
| 161 | + } |
17 | 162 | }
|
0 commit comments