Skip to content

Commit 155aa5e

Browse files
authored
Fixing cfg value source printing
Fixes #5
2 parents 2337c3f + c523226 commit 155aa5e

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

webtau-groovy/src/main/groovy/com/twosigma/webtau/cli/WebTauTestCliConfig.groovy

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,38 @@
1616

1717
package com.twosigma.webtau.cli
1818

19+
import com.twosigma.webtau.cfg.ConfigValue
1920
import com.twosigma.webtau.console.ConsoleOutputs
2021
import com.twosigma.webtau.console.ansi.Color
2122
import com.twosigma.webtau.cfg.WebTauConfig
2223
import org.apache.commons.cli.*
2324

2425
import java.nio.file.Files
2526
import java.nio.file.Path
26-
import java.nio.file.Paths
2727

2828
class WebTauTestCliConfig {
2929
private static final String CLI_SOURCE = "command line argument"
3030
private static final String CFG_SOURCE = "config file"
3131

32-
private WebTauConfig cfg = WebTauConfig.INSTANCE
32+
private final WebTauConfig cfg
3333

3434
private final ExitHandler exitHandler
3535

3636
private List<String> testFiles
37-
private String env
3837
private Path configPath
3938
private CommandLine commandLine
4039
private ConfigObject configObject
4140

4241
WebTauTestCliConfig(String... args) {
43-
this({ System.exit(it) }, args)
42+
this(WebTauConfig.INSTANCE, { System.exit(it) }, args)
4443
}
4544

46-
WebTauTestCliConfig(ExitHandler exitHandler, String... args) {
45+
WebTauTestCliConfig(WebTauConfig cfg, String... args) {
46+
this(cfg, { System.exit(it) }, args)
47+
}
48+
49+
WebTauTestCliConfig(WebTauConfig cfg, ExitHandler exitHandler, String... args) {
50+
this.cfg = cfg
4751
this.exitHandler = exitHandler
4852
parseArgs(args)
4953
}
@@ -90,12 +94,14 @@ class WebTauTestCliConfig {
9094
}
9195

9296
testFiles = new ArrayList<>(commandLine.argList)
93-
Path workingDir = Paths.get(cliValue(cfg.getWorkingDirConfigName(), ""))
94-
configPath = workingDir.resolve(cliValue("config", "test.cfg"))
9597

96-
def envCliValue = cliValue(cfg.envConfigValue.key, "local")
97-
cfg.acceptConfigValues(CLI_SOURCE, [(cfg.envConfigValue.key): envCliValue,
98-
(cfg.workingDirConfigValue.key): workingDir])
98+
setValueFromCliIfPresent(cfg.workingDirConfigValue)
99+
Path workingDir = cfg.workingDir
100+
101+
setValueFromCliIfPresent(cfg.configFileName)
102+
configPath = workingDir.resolve(cfg.configFileName.asString)
103+
104+
setValueFromCliIfPresent(cfg.envConfigValue)
99105
}
100106

101107
private static CommandLine createCommandLine(String[] args, Options options) {
@@ -107,9 +113,10 @@ class WebTauTestCliConfig {
107113
}
108114
}
109115

110-
private def cliValue(String name, defaultValue) {
111-
return commandLine.hasOption(name) ? commandLine.getOptionValue(name) :
112-
defaultValue
116+
private def setValueFromCliIfPresent(ConfigValue configValue) {
117+
if (commandLine.hasOption(configValue.key)) {
118+
configValue.set(CLI_SOURCE, commandLine.getOptionValue(configValue.key))
119+
}
113120
}
114121

115122
private Options createOptions() {

webtau-groovy/src/test/groovy/com/twosigma/webtau/cli/WebTauTestCliConfigTest.groovy

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ import java.nio.file.Paths
2424

2525
class WebTauTestCliConfigTest {
2626
private static def groovy = GroovyStandaloneEngine.createWithoutDelegating(Paths.get(""), [])
27-
private static final cfg = WebTauConfig.INSTANCE
27+
private final cfg = new WebTauConfig()
2828

2929
@Test
30-
void "should use default environment values when evn is not specified"() {
31-
def cliConfig = new WebTauTestCliConfig("--config=src/test/resources/test.cfg", "testFile")
30+
void "should use default environment values when env is not specified"() {
31+
def cliConfig = new WebTauTestCliConfig(cfg, "--config=src/test/resources/test.cfg", "testFile")
3232
cliConfig.parseConfig(groovy)
3333

3434
cfg.baseUrl.should == "http://localhost:8180"
3535
}
3636

3737
@Test
38-
void "should use environment specific values when evn is specified"() {
39-
def cliConfig = new WebTauTestCliConfig("--config=src/test/resources/test.cfg", "--env=dev", "testFile")
38+
void "should use environment specific values when env is specified"() {
39+
def cliConfig = new WebTauTestCliConfig(cfg, "--config=src/test/resources/test.cfg", "--env=dev", "testFile")
4040
cliConfig.parseConfig(groovy)
4141

4242
cfg.baseUrl.should == "http://dev.host:8080"
@@ -45,8 +45,33 @@ class WebTauTestCliConfigTest {
4545
@Test
4646
void "should exit if only config file provided"() {
4747
Integer retCode
48-
new WebTauTestCliConfig({ retCode = it }, "--config=src/test/resources/test.cfg", "--env=dev")
48+
new WebTauTestCliConfig(cfg, { retCode = it }, "--config=src/test/resources/test.cfg", "--env=dev")
4949

5050
retCode.should == 1
5151
}
52+
53+
@Test
54+
void "should set command line args source when env is specified"() {
55+
def cliConfig = new WebTauTestCliConfig(cfg, "--config=src/test/resources/test.cfg", "--env=dev", "testFile")
56+
cliConfig.parseConfig(groovy)
57+
58+
cfg.envConfigValue.source.should == "command line argument"
59+
}
60+
61+
@Test
62+
void "should set default source when env is not specified"() {
63+
def cliConfig = new WebTauTestCliConfig(cfg, "--config=src/test/resources/test.cfg", "testFile")
64+
cliConfig.parseConfig(groovy)
65+
66+
cfg.envConfigValue.source.should == "default"
67+
}
68+
69+
@Test
70+
void "should set default source and value when cfg is not specified"() {
71+
def cliConfig = new WebTauTestCliConfig(cfg, "testFile")
72+
cliConfig.parseConfig(groovy)
73+
74+
cfg.configFileName.asString.should == "test.cfg"
75+
cfg.configFileName.source.should == "default"
76+
}
5277
}

webtau/src/main/java/com/twosigma/webtau/cfg/WebTauConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public ConfigValue getEnvConfigValue() {
103103
return env;
104104
}
105105

106+
public ConfigValue getConfigFileName() {
107+
return config;
108+
}
109+
106110
public ConfigValue getWorkingDirConfigValue() {
107111
return workingDir;
108112
}

0 commit comments

Comments
 (0)