Skip to content

Commit 9686fee

Browse files
committed
Add tests for config dir
1 parent aa64025 commit 9686fee

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

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)