Skip to content

Commit 009ba24

Browse files
committed
Add tests for CommandLine
1 parent 39234f8 commit 009ba24

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

test/command_line_test.exs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
defmodule ElixirScript.CommandLineTest do
2+
use ExUnit.Case, async: false
3+
4+
alias ElixirScript.CommandLine
5+
6+
describe "parse_args!/1" do
7+
@script "IO.puts('Hello, world!')"
8+
@default_parsed_args %CommandLine.ParsedArgs{debug?: false, help?: false, script: nil}
9+
10+
test "returns default ParsedArgs when no arguments are provided" do
11+
args = []
12+
assert CommandLine.parse_args!(args) == @default_parsed_args
13+
end
14+
15+
test "parses --script argument correctly" do
16+
args = ["--script", @script]
17+
expected = %{@default_parsed_args | script: @script}
18+
assert CommandLine.parse_args!(args) == expected
19+
end
20+
21+
test "parses -s (script alias) argument correctly" do
22+
args = ["-s", @script]
23+
expected = %{@default_parsed_args | script: @script}
24+
assert CommandLine.parse_args!(args) == expected
25+
end
26+
27+
test "parses --debug argument correctly" do
28+
args = ["--debug"]
29+
expected = %{@default_parsed_args | debug?: true}
30+
assert CommandLine.parse_args!(args) == expected
31+
end
32+
33+
test "parses -d (debug alias) argument correctly" do
34+
args = ["-d"]
35+
expected = %{@default_parsed_args | debug?: true}
36+
assert CommandLine.parse_args!(args) == expected
37+
end
38+
39+
test "parses --help argument correctly" do
40+
args = ["--help"]
41+
expected = %{@default_parsed_args | help?: true}
42+
assert CommandLine.parse_args!(args) == expected
43+
end
44+
45+
test "parses -h (help alias) argument correctly" do
46+
args = ["-h"]
47+
expected = %{@default_parsed_args | help?: true}
48+
assert CommandLine.parse_args!(args) == expected
49+
end
50+
51+
test "falls back to environment variables when no arguments are given" do
52+
safe_put_env("INPUT_SCRIPT", @script)
53+
safe_put_env("INPUT_DEBUG", "true")
54+
55+
args = []
56+
expected = %CommandLine.ParsedArgs{debug?: true, help?: false, script: @script}
57+
58+
assert CommandLine.parse_args!(args) == expected
59+
end
60+
61+
test "gives precedence to command-line arguments over environment variables" do
62+
safe_put_env("INPUT_SCRIPT", "Env script")
63+
64+
args = ["--script", @script]
65+
expected = %CommandLine.ParsedArgs{debug?: false, help?: false, script: @script}
66+
67+
assert CommandLine.parse_args!(args) == expected
68+
end
69+
end
70+
71+
def safe_put_env(varname, value) do
72+
original_value = System.get_env(varname)
73+
System.put_env(varname, value)
74+
75+
on_exit(fn ->
76+
if original_value,
77+
do: System.put_env(varname, original_value),
78+
else: System.delete_env(varname)
79+
end)
80+
end
81+
end

0 commit comments

Comments
 (0)