Skip to content

Commit 10fe47b

Browse files
committed
Add tests for config dir
1 parent ac9d363 commit 10fe47b

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
@@ -160,8 +160,8 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
160160
* Return the URL and token from the CLI config.
161161
*/
162162
@JvmStatic
163-
fun readConfig(): Pair<String?, String?> {
164-
val configDir = getConfigDir()
163+
fun readConfig(env: Environment = Environment()): Pair<String?, String?> {
164+
val configDir = getConfigDir(env)
165165
CoderWorkspacesStepView.logger.info("Reading config from $configDir")
166166
return try {
167167
val url = Files.readString(configDir.resolve("url"))
@@ -176,24 +176,34 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
176176
* Return the config directory used by the CLI.
177177
*/
178178
@JvmStatic
179-
fun getConfigDir(): Path {
180-
var dir = System.getenv("CODER_CONFIG_DIR")
179+
fun getConfigDir(env: Environment = Environment()): Path {
180+
var dir = env.get("CODER_CONFIG_DIR")
181181
if (!dir.isNullOrBlank()) {
182182
return Path.of(dir)
183183
}
184184
// The Coder CLI uses https://github.com/kirsle/configdir so this should
185185
// match how it behaves.
186186
return when (getOS()) {
187-
OS.WINDOWS -> Paths.get(System.getenv("APPDATA"), "coderv2")
188-
OS.MAC -> Paths.get(System.getenv("HOME"), "Library/Application Support/coderv2")
187+
OS.WINDOWS -> Paths.get(env.get("APPDATA"), "coderv2")
188+
OS.MAC -> Paths.get(env.get("HOME"), "Library/Application Support/coderv2")
189189
else -> {
190-
dir = System.getenv("XDG_CONFIG_HOME")
190+
dir = env.get("XDG_CONFIG_HOME")
191191
if (!dir.isNullOrBlank()) {
192192
return Paths.get(dir, "coderv2")
193193
}
194-
return Paths.get(System.getenv("HOME"), ".config/coderv2")
194+
return Paths.get(env.get("HOME"), ".config/coderv2")
195195
}
196196
}
197197
}
198198
}
199199
}
200+
201+
class Environment(private val env: Map<String, String> = emptyMap()) {
202+
fun get(name: String): String? {
203+
val e = env[name]
204+
if (e != null) {
205+
return e
206+
}
207+
return System.getenv(name)
208+
}
209+
}

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 = [:]) {
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)