/
system_test.exs
242 lines (192 loc) · 6.98 KB
/
system_test.exs
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
Code.require_file("test_helper.exs", __DIR__)
defmodule SystemTest do
use ExUnit.Case
import PathHelpers
test "build_info/0" do
build_info = System.build_info()
assert is_map(build_info)
assert is_binary(build_info[:build])
assert is_binary(build_info[:date])
assert is_binary(build_info[:revision])
assert is_binary(build_info[:version])
assert is_binary(build_info[:otp_release])
if build_info[:revision] != "" do
assert String.length(build_info[:revision]) == 7
end
version_file = Path.join([__DIR__, "../../../..", "VERSION"]) |> Path.expand()
{:ok, version} = File.read(version_file)
assert build_info[:version] == String.trim(version)
assert build_info[:build] =~ "compiled with Erlang/OTP"
end
test "user_home/0" do
assert is_binary(System.user_home())
assert is_binary(System.user_home!())
end
test "tmp_dir/0" do
assert is_binary(System.tmp_dir())
assert is_binary(System.tmp_dir!())
end
test "endianness/0" do
assert System.endianness() in [:little, :big]
assert System.endianness() == System.compiled_endianness()
end
test "pid/0" do
assert is_binary(System.pid())
end
test "argv/0" do
list = elixir('-e "IO.inspect System.argv" -- -o opt arg1 arg2 --long-opt 10')
{args, _} = Code.eval_string(list, [])
assert args == ["-o", "opt", "arg1", "arg2", "--long-opt", "10"]
end
@test_var "SYSTEM_ELIXIR_ENV_TEST_VAR"
test "*_env/*" do
assert System.get_env(@test_var) == nil
assert System.get_env(@test_var, "SAMPLE") == "SAMPLE"
assert System.fetch_env(@test_var) == :error
message = "could not fetch environment variable #{inspect(@test_var)} because it is not set"
assert_raise ArgumentError, message, fn -> System.fetch_env!(@test_var) end
System.put_env(@test_var, "SAMPLE")
assert System.get_env(@test_var) == "SAMPLE"
assert System.get_env()[@test_var] == "SAMPLE"
assert System.fetch_env(@test_var) == {:ok, "SAMPLE"}
assert System.fetch_env!(@test_var) == "SAMPLE"
System.delete_env(@test_var)
assert System.get_env(@test_var) == nil
System.put_env(%{@test_var => "OTHER_SAMPLE"})
assert System.get_env(@test_var) == "OTHER_SAMPLE"
assert_raise ArgumentError, ~r[cannot execute System.put_env/2 for key with \"=\"], fn ->
System.put_env("FOO=BAR", "BAZ")
end
end
test "cmd/2 raises for null bytes" do
assert_raise ArgumentError, ~r"cannot execute System.cmd/3 for program with null byte", fn ->
System.cmd("null\0byte", [])
end
end
test "cmd/3 raises with non-binary arguments" do
assert_raise ArgumentError, ~r"all arguments for System.cmd/3 must be binaries", fn ->
System.cmd("ls", ['/usr'])
end
end
describe "Windows" do
@describetag :windows
test "cmd/2" do
assert {"hello\r\n", 0} = System.cmd("cmd", ~w[/c echo hello])
end
test "cmd/3 (with options)" do
assert {["hello\r\n"], 0} =
System.cmd(
"cmd",
~w[/c echo hello],
into: [],
cd: File.cwd!(),
env: %{"foo" => "bar", "baz" => nil},
arg0: "echo",
stderr_to_stdout: true,
parallelism: true
)
end
@echo "echo-elixir-test"
test "cmd/2 with absolute and relative paths" do
echo = tmp_path(@echo)
File.mkdir_p!(Path.dirname(echo))
File.cp!(System.find_executable("cmd"), echo)
File.cd!(Path.dirname(echo), fn ->
# There is a bug in OTP where find_executable is finding
# entries on the current directory. If this is the case,
# we should avoid the assertion below.
unless System.find_executable(@echo) do
assert :enoent = catch_error(System.cmd(@echo, ~w[/c echo hello]))
end
assert {"hello\r\n", 0} =
System.cmd(Path.join(File.cwd!(), @echo), ~w[/c echo hello], [{:arg0, "echo"}])
end)
end
end
describe "Unix" do
@describetag :unix
test "cmd/2" do
assert {"hello\n", 0} = System.cmd("echo", ["hello"])
end
test "cmd/3 (with options)" do
opts = [
into: [],
cd: File.cwd!(),
env: %{"foo" => "bar", "baz" => nil},
arg0: "echo",
stderr_to_stdout: true,
parallelism: true
]
assert {["hello\n"], 0} = System.cmd("echo", ["hello"], opts)
end
@echo "echo-elixir-test"
test "cmd/2 with absolute and relative paths" do
echo = tmp_path(@echo)
File.mkdir_p!(Path.dirname(echo))
File.cp!(System.find_executable("echo"), echo)
File.cd!(Path.dirname(echo), fn ->
# There is a bug in OTP where find_executable is finding
# entries on the current directory. If this is the case,
# we should avoid the assertion below.
unless System.find_executable(@echo) do
assert :enoent = catch_error(System.cmd(@echo, ["hello"]))
end
assert {"hello\n", 0} =
System.cmd(Path.join(File.cwd!(), @echo), ["hello"], [{:arg0, "echo"}])
end)
end
end
test "find_executable/1" do
assert System.find_executable("erl")
assert is_binary(System.find_executable("erl"))
assert !System.find_executable("does-not-really-exist-from-elixir")
message = ~r"cannot execute System.find_executable/1 for program with null byte"
assert_raise ArgumentError, message, fn ->
System.find_executable("null\0byte")
end
end
test "monotonic_time/0" do
assert is_integer(System.monotonic_time())
end
test "monotonic_time/1" do
assert is_integer(System.monotonic_time(:nanosecond))
assert abs(System.monotonic_time(:microsecond)) < abs(System.monotonic_time(:nanosecond))
end
test "system_time/0" do
assert is_integer(System.system_time())
end
test "system_time/1" do
assert is_integer(System.system_time(:nanosecond))
assert abs(System.system_time(:microsecond)) < abs(System.system_time(:nanosecond))
end
test "time_offset/0 and time_offset/1" do
assert is_integer(System.time_offset())
assert is_integer(System.time_offset(:second))
end
test "os_time/0" do
assert is_integer(System.os_time())
end
test "os_time/1" do
assert is_integer(System.os_time(:nanosecond))
assert abs(System.os_time(:microsecond)) < abs(System.os_time(:nanosecond))
end
test "unique_integer/0 and unique_integer/1" do
assert is_integer(System.unique_integer())
assert System.unique_integer([:positive]) > 0
assert System.unique_integer([:positive, :monotonic]) <
System.unique_integer([:positive, :monotonic])
end
test "convert_time_unit/3" do
time = System.monotonic_time(:nanosecond)
assert abs(System.convert_time_unit(time, :nanosecond, :microsecond)) < abs(time)
end
test "schedulers/0" do
assert System.schedulers() >= 1
end
test "schedulers_online/0" do
assert System.schedulers_online() >= 1
end
test "otp_release/0" do
assert is_binary(System.otp_release())
end
end