Skip to content

Commit

Permalink
Merge pull request #15 from dmiedema/ModuloFileSettings
Browse files Browse the repository at this point in the history
Improve `defaults` command
  • Loading branch information
woolie authored Sep 27, 2018
2 parents 106f7a4 + ada862d commit b401f94
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ModuloKit/Commands/AddCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class AddCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

open func configureOptions() {
Expand Down
28 changes: 16 additions & 12 deletions ModuloKit/Commands/DefaultsCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class DefaultsCommand: NSObject, Command {

public var failOnUnrecognizedOptions: Bool { return true }

public var verbose: Bool = State.instance.options.verboseOutput
public var verbose: Bool = State.instance.options.alwaysVerbose
public var quiet: Bool = false

public func execute(_ otherParams: Array<String>?) -> Int {
Expand All @@ -58,23 +58,27 @@ open class DefaultsCommand: NSObject, Command {
newValue = false
}

spec.options.verboseOutput = newValue
State.instance.options.verboseOutput = newValue
spec.options.alwaysVerbose = newValue
State.instance.options.alwaysVerbose = newValue
}
if let moduleFolderPath = moduleFolderPath,
!moduleFolderPath.isEmpty {
spec.options.depdencyInstallationPath = moduleFolderPath
State.instance.options.depdencyInstallationPath = moduleFolderPath
}

if !toggleVerbose && moduleFolderPath == nil {
writeln(.stderr, """
When `--set` is passed its assumed you want to set a default.
Please specify one of the options
--alwaysVerbose
--moduleFolder
""")
}
spec.save()
} else {
if toggleVerbose {
writeln(.stdout, "VerboseOutput - \(spec.options.verboseOutput)")
}
if moduleFolderPath != nil {
writeln(.stdout, "depdencyInstallationPath - \(spec.options.depdencyInstallationPath)")
}

writeln(.stdout, "alwaysVerbose - \(spec.options.alwaysVerbose)")
writeln(.stdout, "depdencyInstallationPath - \(spec.options.depdencyInstallationPath)")
}

return ErrorCode.success.rawValue
Expand All @@ -85,9 +89,9 @@ open class DefaultsCommand: NSObject, Command {
self.setValue = true
}

addOptionValue(["--verboseOutput"],
addOptionValue(["--alwaysVerbose"],
usage: "specify `verbose` for all commands that are run",
valueSignature: "<[true|false}>") { (option, value) in
valueSignature: "<[true|false]>") { (option, value) in
self.toggleVerbose = true
self.verboseValue = value
}
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/InitCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ open class InitCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

open func configureOptions() {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/MapCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class MapCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

fileprivate var simple = false
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/RemoveCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class RemoveCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

open func configureOptions() {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/SetCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class SetCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

// subcommands
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/StatusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class StatusCommand: NSObject, Command {
open var failOnUnrecognizedOptions: Bool { return true }
open var ignoreMain: Bool = false

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

open func configureOptions() {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/UpdateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ open class UpdateCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = State.instance.options.verboseOutput
open var verbose: Bool = State.instance.options.alwaysVerbose
open var quiet: Bool = false

open func configureOptions() {
Expand Down
6 changes: 3 additions & 3 deletions ModuloKit/Specs/OptionsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import Foundation

public struct OptionsSpec {
/// Should we have `verbose` on all commands
var verboseOutput: Bool = false
var alwaysVerbose: Bool = false
/// Path to store our 'modules'/dependencies in
var depdencyInstallationPath: String = "modules"
}

extension OptionsSpec: ELDecodable {
public static func decode(_ json: JSON?) throws -> OptionsSpec {
return try OptionsSpec(
verboseOutput: json ==> "verboseOutput",
alwaysVerbose: json ==> "alwaysVerbose",
depdencyInstallationPath: json ==> "depdencyInstallationPath"
)
}
Expand All @@ -31,7 +31,7 @@ extension OptionsSpec: ELDecodable {
extension OptionsSpec: ELEncodable {
public func encode() throws -> JSON {
return try encodeToJSON([
"verboseOutput" <== verboseOutput,
"alwaysVerbose" <== alwaysVerbose,
"depdencyInstallationPath" <== depdencyInstallationPath
])
}
Expand Down
147 changes: 146 additions & 1 deletion ModuloKitTests/TestDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,154 @@
import XCTest
import ELCLI
import ELFoundation
import ELCodable // for JSON
@testable import ModuloKit

class TestDefaults: XCTestCase {
let modulo = Modulo()


// MARK: - Setup
override func setUp() {
super.setUp()

moduloReset()
}

// MARK: - Migration
func testMigrationFromNoOptionsInModuloFile() {
let moduleFileJSONDictionary = [
"dependencies": [],
"module": false,
"name": "best project"
] as [String : Any]
do {
let moduleSpec = try spec(from: moduleFileJSONDictionary)
let options = moduleSpec.options
// validate defaults
XCTAssertTrue(options.alwaysVerbose == false)
XCTAssertTrue(options.depdencyInstallationPath == "modules")
} catch {
XCTFail("Failed with error \(error)")
}
}

// MARK: - Bad Input
func testGarbageValuesInModuloFileResultInSaneDefaults() {
let moduleFileJSONDictionary = [
"dependencies": [],
"module": false,
"name": "best project",
"options": [
"alwaysVerbose": "lolgarbage",
"depdencyInstallationPath": ["fart": "toot"],
"invalid_key": true,
]
] as [String : Any]
do {
let moduleSpec = try spec(from: moduleFileJSONDictionary)
let options = moduleSpec.options
// validate defaults since we fed it garbage
XCTAssertTrue(options.alwaysVerbose == false)
XCTAssertTrue(options.depdencyInstallationPath == "modules")
} catch {
XCTFail("Failed with error \(error)")
}
}

// MARK: - Loaded from module file
func testOptionsAreParsedFromModuleFile() {
let directoryPath = "only-the-best-dependencies-live-here"
let moduleFileJSONDictionary = [
"dependencies": [],
"module": false,
"name": "best project",
"options": [
"alwaysVerbose": true,
"depdencyInstallationPath": directoryPath,
]
] as [String : Any]

do {
let moduleSpec = try spec(from: moduleFileJSONDictionary)
let options = moduleSpec.options
XCTAssertTrue(options.alwaysVerbose == true)
XCTAssertTrue(options.depdencyInstallationPath == directoryPath)
} catch {
XCTFail("Failed with error \(error)")
}
}

// MARK: - CLI API
func testReadingAllDefaults() {
_ = Modulo()
moduloReset()

let result = Modulo.run(["defaults"])

// ideally we'd capture output (stdout) somehow
// and verify our output is what we want but since
// i can't see a nice way to do that with ELCLI
// we'll just verify success instead.

XCTAssertTrue(result == .success)
}

func testSettingDefault() {
_ = Modulo()
moduloReset()
XCTAssertFalse(State.instance.options.alwaysVerbose)
let verboseResult = Modulo.run(["defaults", "--set", "--alwaysVerbose", "true"])
XCTAssertTrue(verboseResult == .success)
XCTAssertTrue(State.instance.options.alwaysVerbose)

let directoryResult = Modulo.run(["defaults", "--set", "--moduleFolder", "bestDIR"])
XCTAssertTrue(directoryResult == .success)
XCTAssertTrue(State.instance.options.depdencyInstallationPath == "bestDIR")
}

func testFailsIfSettingBadDefault() {
_ = Modulo()
moduloReset()

xctAssertThrows({
_ = Modulo.run(["defaults", "--set", "--NoTheRightValue", "badValue.BadMe."])
}, "Running `defaults --set` with a bad flag/value did not fail")
}

func testSettingDefaultWithBadValue() {
_ = Modulo()
moduloReset()
State.instance.options.alwaysVerbose = true

let badVerboseResult = Modulo.run(["defaults", "--set", "--alwaysVerbose", "ohSoVerbose"])
XCTAssertTrue(badVerboseResult == .success)
XCTAssertFalse(State.instance.options.alwaysVerbose)
}

func testSettingWithNoKey() {
_ = Modulo()
moduloReset()

let initialVerbose = State.instance.options.alwaysVerbose
let initialPath = State.instance.options.depdencyInstallationPath
let result = Modulo.run(["defaults", "--set"])
XCTAssertTrue(result == .success, "Even though we set nothing, we shoud succeed with some output for the user")
XCTAssertTrue(initialVerbose == State.instance.options.alwaysVerbose)
XCTAssertTrue(initialPath == State.instance.options.depdencyInstallationPath)
}
}

// MARK: - Test Helpers
extension TestDefaults {
func spec(from dictionary: [String: Any]) throws -> ModuleSpec {
let moduleFileJSONData = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted)
guard let moduleFileJSON = JSON(data: moduleFileJSONData) else {
throw NSError(domain: "TestDefaults",
code: -1,
userInfo: [
NSLocalizedDescriptionKey: "Failed to create JSON from Data"
])
}
return try ModuleSpec.decode(moduleFileJSON)
}
}
6 changes: 5 additions & 1 deletion modulo.xcodeproj/xcshareddata/xcschemes/modulo.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@
argument = "update --all --host github.corp.ebay.com"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "defaults --set --alwaysVerbose true"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "update --verbose"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "update"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
<AdditionalOptions>
Expand Down

0 comments on commit b401f94

Please sign in to comment.