Debian Codegraph @

test/cli.fnl

raw
1 (local t (require :test.faith))
2
3 (macro v [form] (view form))
4
5 ;; These are the slowest tests, so for now we just have a basic sanity check
6 ;; to ensure that it compiles and can evaluate math.
7
8 (local test-all? (os.getenv :FNL_TESTALL)) ; set by `make testall`
9
10 (local host-lua (let [long (case _VERSION
11 "Lua 5.1" (if _G.jit :luajit :lua5.1)
12 _ (.. :lua (_VERSION:sub 5)))
13 p (io.popen (.. long " -v"))]
14 (p:read :*all)
15 (if (p:close)
16 long
17 (or (os.getenv "LUA") "lua"))))
18
19 (fn file-exists? [filename]
20 (let [f (io.open filename)]
21 (when f (f:close) true)))
22
23 (λ sh/esc [str] (.. "'" (str:gsub "'" "'\\''") "'"))
24 (λ fennel-cli [...]
25 (let [cmd [host-lua :fennel ...]
26 proc (io.popen (table.concat cmd " "))
27 output (: (proc:read :*a) :gsub "\n$" "")]
28 (values (proc:close) output))) ; proc:close gives exit status on 5.2+
29
30 (λ peval [code ...] (fennel-cli :--eval (sh/esc code) ...))
31
32 (fn test-cli []
33 ;; skip this if we haven't compiled the CLI or on Windows
34 (when (and (file-exists? "fennel") (= "/" (package.config:sub 1 1)))
35 (t.= [true "1\tnil\t2\tnil\tnil"]
36 [(peval (v (values 1 nil 2 nil nil)))])))
37
38 (fn test-lua-flag []
39 ;; skip this when cli is not compiled or not running tests with `make testall`
40 (when (and test-all? (file-exists? "fennel"))
41 (let [;; running io.popen for all 20 combinations of lua versions is slow,
42 ;; so we'll just pick the next one in the list after host-lua
43 lua-exec ((fn pick-lua [lua-vs i lua-v]
44 (if (= host-lua lua-v)
45 (. lua-vs (+ 1 (% i (# lua-vs)))) ; circular next
46 (pick-lua lua-vs (next lua-vs i))))
47 [:lua5.1 :lua5.2 :lua5.3 :lua5.4 :luajit])
48 run #(pick-values 2 (peval $ (: "--lua %q" :format lua-exec)))]
49 (t.= [true lua-exec]
50 [(run (v (case (_VERSION:sub 5)
51 :5.1 (if _G.jit :luajit :lua5.1)
52 v-num (.. :lua v-num))))]
53 (.. "should execute code in Lua runtime: " lua-exec))
54 (let [(success? output) (run (v (do (print :test) (os.exit 1 true))))]
55 (t.= "test" output)
56 ;; pcall in Lua 5.1 doesn't give status with (proc:close)
57 (t.= (if (= _VERSION "Lua 5.1") true nil)
58 success?
59 (.. "errors should cause failing exit status with --lua "
60 lua-exec))))))
61
62 (fn test-lua-plugin []
63 (let [lua-plug :test/plugin/lua-plugin.lua]
64 (t.= [true :s]
65 [(fennel-cli :--plugin lua-plug
66 :-e (sh/esc (v (let [s 1] (is-in-scope s)))))]
67 "lua plugins should be loaded with full compiler env")))
68
69 (fn test-args []
70 (when (and test-all? (file-exists? "fennel"))
71 (t.= [true "-l"] [(peval "(. arg 3)" "-l")])))
72
73 {: test-cli
74 : test-lua-flag
75 : test-lua-plugin
76 : test-args}