Skip to content

Commit fa39f57

Browse files
committed
Add tests for config dir
1 parent 39dca82 commit fa39f57

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
158158
* Return the URL and token from the CLI config.
159159
*/
160160
@JvmStatic
161-
fun readConfig(): Pair<String?, String?> {
162-
val configDir = getConfigDir()
161+
fun readConfig(env: Environment = Environment()): Pair<String?, String?> {
162+
val configDir = getConfigDir(env)
163163
CoderWorkspacesStepView.logger.info("Reading config from $configDir")
164164
return try {
165165
val url = Files.readString(configDir.resolve("url"))
@@ -174,24 +174,34 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
174174
* Return the config directory used by the CLI.
175175
*/
176176
@JvmStatic
177-
fun getConfigDir(): Path {
178-
var dir = System.getenv("CODER_CONFIG_DIR")
177+
fun getConfigDir(env: Environment = Environment()): Path {
178+
var dir = env.get("CODER_CONFIG_DIR")
179179
if (!dir.isNullOrBlank()) {
180180
return Path.of(dir)
181181
}
182182
// The Coder CLI uses https://github.com/kirsle/configdir so this should
183183
// match how it behaves.
184184
return when (getOS()) {
185-
OS.WINDOWS -> Paths.get(System.getenv("APPDATA"), "coderv2")
186-
OS.MAC -> Paths.get(System.getenv("HOME"), "Library/Application Support/coderv2")
185+
OS.WINDOWS -> Paths.get(env.get("APPDATA"), "coderv2")
186+
OS.MAC -> Paths.get(env.get("HOME"), "Library/Application Support/coderv2")
187187
else -> {
188-
dir = System.getenv("XDG_CONFIG_HOME")
188+
dir = env.get("XDG_CONFIG_HOME")
189189
if (!dir.isNullOrBlank()) {
190190
return Paths.get(dir, "coderv2")
191191
}
192-
return Paths.get(System.getenv("HOME"), ".config/coderv2")
192+
return Paths.get(env.get("HOME"), ".config/coderv2")
193193
}
194194
}
195195
}
196196
}
197197
}
198+
199+
class Environment(private val env: Map<String, String> = emptyMap()) {
200+
fun get(name: String): String? {
201+
val e = env[name]
202+
if (e != null) {
203+
return e
204+
}
205+
return System.getenv(name)
206+
}
207+
}

src/test/groovy/CoderCLIManagerTest.groovy

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coder.gateway.sdk
22

3+
import spock.lang.Requires
34
import spock.lang.Unroll
45

56
import java.nio.file.Files
@@ -70,4 +71,60 @@ class CoderCLIManagerTest extends spock.lang.Specification {
7071
!downloaded
7172
ccm.localBinaryPath.toFile().readLines() == ["cli"]
7273
}
74+
75+
/**
76+
* Get a config dir using default environment variable values.
77+
*/
78+
def configDir(Map<String, String> env = emptyMap()) {
79+
return CoderCLIManager.getConfigDir(new Environment([
80+
"APPDATA" : "/tmp/coder-gateway-test/appdata",
81+
"HOME" : "/tmp/coder-gateway-test/home",
82+
"XDG_CONFIG_HOME" : "/tmp/coder-gateway-test/xdg",
83+
"CODER_CONFIG_DIR": "",
84+
] + env))
85+
}
86+
87+
// Mostly just a sanity check to make sure the default System.getenv runs
88+
// without throwing any errors.
89+
def "gets config dir"() {
90+
when:
91+
def dir = CoderCLIManager.getConfigDir(new Environment([
92+
"CODER_CONFIG_DIR": "",
93+
]))
94+
95+
then:
96+
dir.toString().contains("coderv2")
97+
}
98+
99+
def "gets config dir from CODER_CONFIG_DIR"() {
100+
expect:
101+
Path.of(path) == configDir(env)
102+
103+
where:
104+
env || path
105+
["CODER_CONFIG_DIR": "/tmp/coder-gateway-test/conf"] || "/tmp/coder-gateway-test/conf"
106+
}
107+
108+
@Requires({ os.linux })
109+
def "gets config dir from XDG or HOME"() {
110+
expect:
111+
Path.of(path) == configDir(env)
112+
113+
where:
114+
env || path
115+
[:] || "/tmp/coder-gateway-test/xdg/coderv2"
116+
["XDG_CONFIG_HOME": ""] || "/tmp/coder-gateway-test/home/.config/coderv2"
117+
}
118+
119+
@Requires({ os.macOs })
120+
def "gets config dir from HOME"() {
121+
expect:
122+
Path.of("/tmp/coder-gateway-test/home/Library/Application Support/coderv2") == configDir()
123+
}
124+
125+
@Requires({ os.windows })
126+
def "gets config dir from APPDATA"() {
127+
expect:
128+
Path.of("/tmp/coder-gateway-test/appdata/coderv2") == configDir()
129+
}
73130
}

0 commit comments

Comments
 (0)