@@ -8,11 +8,38 @@ import java.nio.file.Path
8
8
9
9
@Unroll
10
10
class CoderCLIManagerTest extends spock.lang. Specification {
11
- // TODO: Probably not good to depend on dev.coder.com being up...should use
12
- // a mock? Or spin up a Coder deployment in CI?
11
+ /**
12
+ * Create a CLI manager pointing to the URL in the environment or to the
13
+ default URLs.
14
+ *
15
+ * @TODO: Implement a mock.
16
+ */
17
+ def createCLIManager (Boolean alternate = false ) {
18
+ var url = System . getenv(" CODER_GATEWAY_TEST_DEPLOYMENT" )
19
+ if (url == null ) {
20
+ url = " https://dev.coder.com"
21
+ }
22
+ if (alternate) {
23
+ url = System . getenv(" CODER_GATEWAY_TEST_DEPLOYMENT_ALT" )
24
+ if (url == null ) {
25
+ url = " https://oss.demo.coder.com"
26
+ }
27
+ }
28
+ var tmpdir = Path . of(System . getProperty(" java.io.tmpdir" )). resolve(" coder-gateway-test" )
29
+ return new CoderCLIManager (new URL (url), tmpdir)
30
+ }
31
+
32
+ def " defaults to a sub-directory in the data directory" () {
33
+ given :
34
+ def ccm = new CoderCLIManager (new URL (" https://test.coder.invalid" ))
35
+
36
+ expect :
37
+ ccm. localBinaryPath. getParent() == CoderCLIManager . getDataDir(). resolve(" test.coder.invalid" )
38
+ }
39
+
13
40
def " downloads a working cli" () {
14
41
given :
15
- def ccm = new CoderCLIManager ( new URL ( " https://dev.coder.com " ) )
42
+ def ccm = createCLIManager( )
16
43
ccm. localBinaryPath. getParent(). toFile(). deleteDir()
17
44
18
45
when :
@@ -25,7 +52,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
25
52
26
53
def " overwrites cli if incorrect version" () {
27
54
given :
28
- def ccm = new CoderCLIManager ( new URL ( " https://dev.coder.com " ) )
55
+ def ccm = createCLIManager( )
29
56
Files . createDirectories(ccm. localBinaryPath. getParent())
30
57
ccm. localBinaryPath. toFile(). write(" cli" )
31
58
@@ -39,7 +66,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
39
66
40
67
def " skips cli download if it already exists" () {
41
68
given :
42
- def ccm = new CoderCLIManager ( new URL ( " https://dev.coder.com " ) )
69
+ def ccm = createCLIManager( )
43
70
44
71
when :
45
72
ccm. downloadCLI()
@@ -52,8 +79,8 @@ class CoderCLIManagerTest extends spock.lang.Specification {
52
79
53
80
def " does not clobber other deployments" () {
54
81
given :
55
- def ccm1 = new CoderCLIManager ( new URL ( " https://oss.demo.coder.com " ) )
56
- def ccm2 = new CoderCLIManager ( new URL ( " https://dev.coder.com " ) )
82
+ def ccm1 = createCLIManager( true )
83
+ def ccm2 = createCLIManager( )
57
84
58
85
when :
59
86
ccm1. downloadCLI()
@@ -63,25 +90,27 @@ class CoderCLIManagerTest extends spock.lang.Specification {
63
90
ccm1. localBinaryPath != ccm2. localBinaryPath
64
91
}
65
92
93
+ def testEnv = [
94
+ " APPDATA" : " /tmp/coder-gateway-test/appdata" ,
95
+ " LOCALAPPDATA" : " /tmp/coder-gateway-test/localappdata" ,
96
+ " HOME" : " /tmp/coder-gateway-test/home" ,
97
+ " XDG_CONFIG_HOME" : " /tmp/coder-gateway-test/xdg-config" ,
98
+ " XDG_DATA_HOME" : " /tmp/coder-gateway-test/xdg-data" ,
99
+ " CODER_CONFIG_DIR" : " " ,
100
+ ]
101
+
66
102
/**
67
103
* Get a config dir using default environment variable values.
68
104
*/
69
105
def configDir (Map<String , String > env = [:]) {
70
- return CoderCLIManager . getConfigDir(new Environment ([
71
- " APPDATA" : " /tmp/coder-gateway-test/appdata" ,
72
- " HOME" : " /tmp/coder-gateway-test/home" ,
73
- " XDG_CONFIG_HOME" : " /tmp/coder-gateway-test/xdg" ,
74
- " CODER_CONFIG_DIR" : " " ,
75
- ] + env))
106
+ return CoderCLIManager . getConfigDir(new Environment (testEnv + env))
76
107
}
77
108
78
109
// Mostly just a sanity check to make sure the default System.getenv runs
79
110
// without throwing any errors.
80
111
def " gets config dir" () {
81
112
when :
82
- def dir = CoderCLIManager . getConfigDir(new Environment ([
83
- " CODER_CONFIG_DIR" : " " ,
84
- ]))
113
+ def dir = CoderCLIManager . getConfigDir()
85
114
86
115
then :
87
116
dir. toString(). contains(" coderv2" )
@@ -97,13 +126,13 @@ class CoderCLIManagerTest extends spock.lang.Specification {
97
126
}
98
127
99
128
@Requires ({ os.linux })
100
- def " gets config dir from XDG or HOME" () {
129
+ def " gets config dir from XDG_CONFIG_HOME or HOME" () {
101
130
expect :
102
131
Path . of(path) == configDir(env)
103
132
104
133
where :
105
134
env || path
106
- [:] || " /tmp/coder-gateway-test/xdg/coderv2"
135
+ [:] || " /tmp/coder-gateway-test/xdg-config /coderv2"
107
136
[" XDG_CONFIG_HOME" : " " ] || " /tmp/coder-gateway-test/home/.config/coderv2"
108
137
}
109
138
@@ -118,4 +147,43 @@ class CoderCLIManagerTest extends spock.lang.Specification {
118
147
expect :
119
148
Path . of(" /tmp/coder-gateway-test/appdata/coderv2" ) == configDir()
120
149
}
150
+
151
+ /**
152
+ * Get a data dir using default environment variable values.
153
+ */
154
+ def dataDir (Map<String , String > env = [:]) {
155
+ return CoderCLIManager . getDataDir(new Environment (testEnv + env))
156
+ }
157
+ // Mostly just a sanity check to make sure the default System.getenv runs
158
+ // without throwing any errors.
159
+ def " gets data dir" () {
160
+ when :
161
+ def dir = CoderCLIManager . getDataDir()
162
+
163
+ then :
164
+ dir. toString(). contains(" coder-gateway" )
165
+ }
166
+
167
+ @Requires ({ os.linux })
168
+ def " gets data dir from XDG_DATA_HOME or HOME" () {
169
+ expect :
170
+ Path . of(path) == dataDir(env)
171
+
172
+ where :
173
+ env || path
174
+ [:] || " /tmp/coder-gateway-test/xdg-data/coder-gateway"
175
+ [" XDG_DATA_HOME" : " " ] || " /tmp/coder-gateway-test/home/.local/share/coder-gateway"
176
+ }
177
+
178
+ @Requires ({ os.macOs })
179
+ def " gets data dir from HOME" () {
180
+ expect :
181
+ Path . of(" /tmp/coder-gateway-test/home/Library/Application Support/coder-gateway" ) == dataDir()
182
+ }
183
+
184
+ @Requires ({ os.windows })
185
+ def " gets data dir from LOCALAPPDATA" () {
186
+ expect :
187
+ Path . of(" /tmp/coder-gateway-test/localappdata/coder-gateway" ) == dataDir()
188
+ }
121
189
}
0 commit comments