-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathcmd_spec.moon
More file actions
341 lines (256 loc) · 9.49 KB
/
cmd_spec.moon
File metadata and controls
341 lines (256 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
nginx = require "lapis.cmd.nginx"
describe "lapis.cmd.nginx", ->
local snapshot
before_each ->
snapshot = assert\snapshot!
stub(os, "exit").invokes (status) ->
error "os.exit was called unexpectedly: #{exit_code}"
after_each ->
snapshot\revert!
it "should compile config", ->
tpl = [[
hello: ${{some_var}}]]
compiled = nginx.compile_config tpl, { some_var: "what's up" }
assert.same [[
env LAPIS_ENVIRONMENT;
hello: what's up]], compiled
it "looks up nested config values with dot syntax", ->
tpl = [[
nested: ${{outer.inner}}]]
compiled = nginx.compile_config tpl, { outer: { inner: "value" } }
assert.same [[
env LAPIS_ENVIRONMENT;
nested: value]], compiled
it "compies config with variable that doesn't exist", ->
tpl = [[
hello: ${{oops_var}}]]
compiled = nginx.compile_config tpl, { some_var: "what's up" }
assert.same [[
env LAPIS_ENVIRONMENT;
hello: ${{oops_var}}]], compiled
it "should compile postgres connect string", ->
tpl = [[
pg-connect: ${{pg postgres}}]]
compiled = nginx.compile_config tpl, {
postgres: "postgres://pg_user:user_password@127.0.0.1/my_database"
}
assert.same [[
env LAPIS_ENVIRONMENT;
pg-connect: 127.0.0.1 dbname=my_database user=pg_user password=user_password]], compiled
it "should compile postgres connect table", ->
tpl = [[
pg-connect: ${{pg postgres}}]]
compiled = nginx.compile_config tpl, {
postgres: {
host: "example.com:1234"
user: "leafo"
password: "thepass"
database: "hello"
}
}
assert.same [[
env LAPIS_ENVIRONMENT;
pg-connect: example.com:1234 dbname=hello user=leafo password=thepass]], compiled
it "should read environment variable", ->
unless pcall -> require "posix"
pending "luaposix is required for cmd.nginx specs"
return
posix = require "posix"
val = "hi there #{os.time!}"
posix.setenv "LAPIS_COOL", val
compiled = nginx.compile_config "thing: ${{cool}}"
assert.same "env LAPIS_ENVIRONMENT;\nthing: #{val}", compiled
it "should compile etlua config", ->
tpl = [[
hello: <%- some_var %>]]
compiled = nginx.compile_etlua_config tpl, { some_var: "what's up" }
assert.same [[
env LAPIS_ENVIRONMENT;
hello: what's up]], compiled
it "should read environment variable in etlua config", ->
unless pcall -> require "posix"
pending "luaposix is required for cmd.nginx specs"
return
posix = require "posix"
val = "hi there #{os.time!}"
posix.setenv "LAPIS_COOL", val
compiled = nginx.compile_etlua_config "thing: <%- cool %>"
assert.same "env LAPIS_ENVIRONMENT;\nthing: #{val}", compiled
describe "lapis.cmd.actions", ->
import get_command, command_runner, execute from require "lapis.cmd.actions"
it "builds the command parser", ->
parser = command_runner\build_parser!
assert.same {false, "a command is required"}, { parser\pparse {} }
it "gets built in action", ->
command = get_command "new"
assert.same "new", command.name
it "gets nil for invalid action", ->
command = get_command "wazzupf2323"
assert.same nil, command
it "executes help", ->
local exit_status
stub(os, "exit").invokes (status) ->
exit_status = status
coroutine.yield "os.exit"
output = {}
s_print = stub(_G, "print").invokes (...) ->
table.insert output, table.concat {...}, "\t"
assert.same "os.exit", coroutine.wrap(-> execute {"help"})!
print\revert!
output = table.concat output, "\n"
assert.same 0, exit_status
assert output\match "Options:"
describe "lapis.cmd.actions.execute", ->
import join, shell_escape from require "lapis.cmd.path"
local cmd
local old_dir, new_dir, old_package_path
lfs = require "lfs"
before_each ->
cmd = require "lapis.cmd.actions"
-- replace the annotated path with silent one
cmd.command_runner.path = require "lapis.cmd.path"
old_dir = lfs.currentdir!
old_package_path = package.path
package.path ..= ";#{old_dir}/?.lua"
new_dir = join old_dir, "spec_tmp_app"
assert lfs.mkdir new_dir
assert lfs.chdir new_dir
after_each ->
package.path = old_package_path
assert lfs.chdir old_dir
os.execute "rm -r '#{shell_escape new_dir}'"
list_files = (dir, accum={}, prefix="") ->
for f in lfs.dir dir
continue if f\match "^%.*$"
relative_name = join prefix, f
if "directory" == lfs.attributes relative_name, "mode"
list_files join(dir, f), accum, relative_name
else
table.insert accum, relative_name
accum
assert_files = (files) ->
have_files = list_files lfs.currentdir!
table.sort files
table.sort have_files
assert.same files, have_files
describe "debug", ->
it "gets default environment with no overrides", ->
res = cmd.execute { "debug" }
assert.same "test", res.environment
it "environment with --environment", ->
res = cmd.execute { "debug", "--environment", "cool" }
assert.same "cool", res.environment
res = cmd.execute { "--environment", "cool", "debug" }
assert.same "cool", res.environment
it "environment with arg", ->
res = cmd.execute { "debug", "wow" }
assert.same "wow", res.environment
it "fails with double env", ->
assert.has_error(
-> cmd.execute { "--environment=umm", "debug", "wow" }
"You tried to set the environment twice. Use either --environment or the environment argument, not both"
)
describe "new", ->
before_each ->
stub(require("lapis.cmd.nginx"), "find_nginx").returns true
it "lapis new", ->
cmd.execute { "new" }
assert_files {
"app.lua", "config.lua", "mime.types", "models.lua", "nginx.conf"
}
it "lapis new --moonscript", ->
cmd.execute { "new", "--moonscript" }
assert_files {
"app.moon", "config.moon", "mime.types", "models.moon", "nginx.conf"
}
it "fails if files already exist", ->
cmd.execute { "new" }
assert.has_error ->
cmd.execute { "new" }
it "lapis new --cqueues", ->
-- --forsce to bypass the module dependency check
cmd.execute { "new", "--cqueues", "--force" }
assert_files { "app.lua", "config.lua", "models.lua" }
it "lapis new --etlua-config", ->
cmd.execute { "new", "--etlua-config" }
assert_files {
"app.lua", "config.lua", "mime.types", "models.lua", "nginx.conf.etlua"
}
it "lapis new --tup", ->
cmd.execute { "new", "--tup" }
assert_files {
"app.lua", "config.lua", "mime.types", "models.lua", "nginx.conf", "Tupfile", "Tuprules.tup"
}
it "lapis new --git", ->
cmd.execute { "new", "--git" }
assert_files {
"app.lua", "config.lua", "mime.types", "models.lua", "nginx.conf", ".gitignore"
}
describe "build", ->
it "lapis build", ->
cmd.execute { "new" }
cmd.execute { "build" }
assert_files {
"app.lua", "config.lua", "mime.types", "models.lua", "nginx.conf", "nginx.conf.compiled"
}
describe "generate", ->
it "lapis generate model things", ->
cmd.execute { "generate", "model", "things" }
assert_files { "models/things.lua" }
it "lapis generate model --moonscript things", ->
cmd.execute { "generate", "model", "things", "--moonscript" }
assert_files { "models/things.moon" }
it "lapis generate spec models.things", ->
cmd.execute { "generate", "spec", "models.things" }
assert_files { "spec/models/things_spec.lua" }
it "lapis generate spec models.things --moonscript", ->
cmd.execute { "generate", "spec", "models.things", "--moonscript" }
assert_files { "spec/models/things_spec.moon" }
it "lapis generate migration in lua", ->
cmd.execute { "generate", "migration", "--lua" }
cmd.execute { "generate", "migration", "--lua" } -- appends a new migration
assert_files { "migrations.lua" }
-- load the file to ensure it's valid Lua syntax
assert loadfile("migrations.lua")
it "lapis generate migration in lua", ->
cmd.execute { "generate", "migration", "--moon" }
cmd.execute { "generate", "migration", "--moon" } -- appends a new migration
assert_files { "migrations.moon" }
-- load file to ensure it's valid moonscript -> lua syntax
assert require("moonscript.base").loadfile "migrations.moon"
it "lapis generate rockspec", ->
cmd.execute { "generate", "rockspec" }
cmd.execute { "generate", "rockspec", "--moon", "--sqlite", "--version-name=dev-2", "--app-name=lapis-thing" }
assert_files {
"lapis-thing-dev-2.rockspec"
"spec-tmp-app-dev-1.rockspec"
}
-- verify that they are valid lua
assert loadfile("lapis-thing-dev-2.rockspec")
assert loadfile("spec-tmp-app-dev-1.rockspec")
describe "lapis.cmd.util", ->
it "columnizes", ->
import columnize from require "lapis.cmd.util"
columnize {
{"hello", "here is some info"}
{"what is going on", "this is going to be a lot of text so it wraps around the end"}
{"this is something", "not so much here"}
{"else", "yeah yeah yeah not so much okay goodbye"}
}
it "parses flags", ->
import parse_flags from require "lapis.cmd.util"
flags, args = parse_flags { "hello", "--world", "-h=1", "yeah" }
assert.same {
h: "1"
world: true
}, flags
assert.same {
"hello"
"yeah"
}, args
flags, args = parse_flags { "new", "dad" }
assert.same {}, flags
assert.same {
"new"
"dad"
}, args