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