Skip to content

Commit ecf2aba

Browse files
author
Philip Sampaio
committed
Add tests covering both ".ps1" and ".bat" on Windows
Also test with ".ps1" on Unix-like SOs when "pwsh" binary is available.
1 parent 6b25505 commit ecf2aba

File tree

2 files changed

+93
-52
lines changed

2 files changed

+93
-52
lines changed

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 79 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ defmodule Retry do
3333
end
3434

3535
defmodule Kernel.CLITest do
36-
use ExUnit.Case, async: true
36+
use ExUnit.Case,
37+
async: true,
38+
parameterize:
39+
if(PathHelpers.windows?(),
40+
do: [%{cli_extension: ".bat"}, %{cli_extension: ".ps1"}],
41+
else: [%{cli_extension: ""}, %{cli_extension: ".ps1"}]
42+
)
3743

3844
import ExUnit.CaptureIO
3945

@@ -62,86 +68,105 @@ defmodule Kernel.CLITest do
6268
end
6369

6470
defmodule Kernel.CLI.ExecutableTest do
65-
use ExUnit.Case, async: true
71+
use ExUnit.Case,
72+
async: true,
73+
parameterize:
74+
if(PathHelpers.windows?(),
75+
do: [%{cli_extension: ".bat"}, %{cli_extension: ".ps1"}],
76+
else:
77+
[%{cli_extension: ""}] ++
78+
if(System.find_executable("pwsh"), do: [%{cli_extension: ".ps1"}], else: [])
79+
)
6680

6781
import Retry
6882

6983
@tag :tmp_dir
7084
test "file smoke test", context do
7185
file = Path.join(context.tmp_dir, "hello_world!.exs")
7286
File.write!(file, "IO.puts :hello_world123")
73-
{output, 0} = System.cmd(elixir_executable(), [file])
87+
{output, 0} = System.cmd(elixir_executable(context.cli_extension), [file])
7488
assert output =~ "hello_world123"
7589
end
7690

77-
test "--eval smoke test" do
78-
{output, 0} = System.cmd(elixir_executable(), ["--eval", "IO.puts :hello_world123"])
91+
test "--eval smoke test", context do
92+
{output, 0} =
93+
System.cmd(elixir_executable(context.cli_extension), ["--eval", "IO.puts :hello_world123"])
94+
7995
assert output =~ "hello_world123"
8096

8197
# Check for -e and exclamation mark handling on Windows
82-
assert {_output, 0} = System.cmd(elixir_executable(), ["-e", "Time.new!(0, 0, 0)"])
98+
assert {_output, 0} =
99+
System.cmd(elixir_executable(context.cli_extension), ["-e", "Time.new!(0, 0, 0)"])
83100

84101
# TODO: remove this once we bump CI to 26.3
85102
unless windows?() and System.otp_release() == "26" do
86103
{output, 0} =
87-
System.cmd(iex_executable(), ["--eval", "IO.puts :hello_world123; System.halt()"])
104+
System.cmd(iex_executable(context.cli_extension), [
105+
"--eval",
106+
"IO.puts :hello_world123; System.halt()"
107+
])
88108

89109
assert output =~ "hello_world123"
90110

91-
{output, 0} = System.cmd(iex_executable(), ["-e", "IO.puts :hello_world123; System.halt()"])
111+
{output, 0} =
112+
System.cmd(iex_executable(context.cli_extension), [
113+
"-e",
114+
"IO.puts :hello_world123; System.halt()"
115+
])
116+
92117
assert output =~ "hello_world123"
93118
end
94119
end
95120

96-
test "--version smoke test" do
97-
output = elixir(~c"--version")
121+
test "--version smoke test", %{cli_extension: cli_extension} do
122+
output = elixir(~c"--version", cli_extension)
98123
assert output =~ "Erlang/OTP #{System.otp_release()}"
99124
assert output =~ "Elixir #{System.version()}"
100125

101-
output = iex(~c"--version")
126+
output = iex(~c"--version", cli_extension)
102127
assert output =~ "Erlang/OTP #{System.otp_release()}"
103128
assert output =~ "IEx #{System.version()}"
104129

105-
output = elixir(~c"--version -e \"IO.puts(:test_output)\"")
130+
output = elixir(~c"--version -e \"IO.puts(:test_output)\"", cli_extension)
106131
assert output =~ "Erlang/OTP #{System.otp_release()}"
107132
assert output =~ "Elixir #{System.version()}"
108133
assert output =~ "Standalone options can't be combined with other options"
109134
end
110135

111-
test "--short-version smoke test" do
112-
output = elixir(~c"--short-version")
136+
test "--short-version smoke test", %{cli_extension: cli_extension} do
137+
output = elixir(~c"--short-version", cli_extension)
113138
assert output =~ System.version()
114139
refute output =~ "Erlang"
115140
end
116141

117-
stderr_test "--help smoke test" do
118-
output = elixir(~c"--help")
142+
stderr_test "--help smoke test", %{cli_extension: cli_extension} do
143+
output = elixir(~c"--help", cli_extension)
119144
assert output =~ "Usage: elixir"
120145
end
121146

122-
stderr_test "combining --help results in error" do
123-
output = elixir(~c"-e 1 --help")
147+
stderr_test "combining --help results in error", %{cli_extension: cli_extension} do
148+
output = elixir(~c"-e 1 --help", cli_extension)
124149
assert output =~ "--help : Standalone options can't be combined with other options"
125150

126-
output = elixir(~c"--help -e 1")
151+
output = elixir(~c"--help -e 1", cli_extension)
127152
assert output =~ "--help : Standalone options can't be combined with other options"
128153
end
129154

130-
stderr_test "combining --short-version results in error" do
131-
output = elixir(~c"--short-version -e 1")
155+
stderr_test "combining --short-version results in error", %{cli_extension: cli_extension} do
156+
output = elixir(~c"--short-version -e 1", cli_extension)
132157
assert output =~ "--short-version : Standalone options can't be combined with other options"
133158

134-
output = elixir(~c"-e 1 --short-version")
159+
output = elixir(~c"-e 1 --short-version", cli_extension)
135160
assert output =~ "--short-version : Standalone options can't be combined with other options"
136161
end
137162

138-
test "parses paths" do
163+
test "parses paths", %{cli_extension: cli_extension} do
139164
root = fixture_path("../../..") |> to_charlist
140165

141166
args =
142167
~c"-pa \"#{root}/*\" -pz \"#{root}/lib/*\" -e \"IO.inspect(:code.get_path(), limit: :infinity)\""
143168

144-
list = elixir(args)
169+
list = elixir(args, cli_extension)
145170
{path, _} = Code.eval_string(list, [])
146171

147172
# pa
@@ -153,25 +178,31 @@ defmodule Kernel.CLI.ExecutableTest do
153178
assert to_charlist(Path.expand(~c"lib/list", root)) in path
154179
end
155180

156-
stderr_test "formats errors" do
157-
assert String.starts_with?(elixir(~c"-e \":erlang.throw 1\""), "** (throw) 1")
181+
stderr_test "formats errors", %{cli_extension: cli_extension} do
182+
assert String.starts_with?(elixir(~c"-e \":erlang.throw 1\"", cli_extension), "** (throw) 1")
158183

159184
assert String.starts_with?(
160-
elixir(~c"-e \":erlang.error 1\""),
185+
elixir(~c"-e \":erlang.error 1\"", cli_extension),
161186
"** (ErlangError) Erlang error: 1"
162187
)
163188

164-
assert String.starts_with?(elixir(~c"-e \"1 +\""), "** (TokenMissingError)")
189+
assert String.starts_with?(elixir(~c"-e \"1 +\"", cli_extension), "** (TokenMissingError)")
165190

166-
assert elixir(~c"-e \"Task.async(fn -> raise ArgumentError end) |> Task.await\"") =~
191+
assert elixir(
192+
~c"-e \"Task.async(fn -> raise ArgumentError end) |> Task.await\"",
193+
cli_extension
194+
) =~
167195
"an exception was raised:\n ** (ArgumentError) argument error"
168196

169-
assert elixir(~c"-e \"IO.puts(Process.flag(:trap_exit, false)); exit({:shutdown, 1})\"") ==
197+
assert elixir(
198+
~c"-e \"IO.puts(Process.flag(:trap_exit, false)); exit({:shutdown, 1})\"",
199+
cli_extension
200+
) ==
170201
"false\n"
171202
end
172203

173-
stderr_test "blames exceptions" do
174-
error = elixir(~c"-e \"Access.fetch :foo, :bar\"")
204+
stderr_test "blames exceptions", %{cli_extension: cli_extension} do
205+
error = elixir(~c"-e \"Access.fetch :foo, :bar\"", cli_extension)
175206
assert error =~ "** (FunctionClauseError) no function clause matching in Access.fetch/2"
176207
assert error =~ "The following arguments were given to Access.fetch/2"
177208
assert error =~ ":foo"
@@ -238,7 +269,15 @@ defmodule Kernel.CLI.RPCTest do
238269
end
239270

240271
defmodule Kernel.CLI.CompileTest do
241-
use ExUnit.Case, async: true
272+
use ExUnit.Case,
273+
async: true,
274+
parameterize:
275+
if(PathHelpers.windows?(),
276+
do: [%{cli_extension: ".bat"}, %{cli_extension: ".ps1"}],
277+
else:
278+
[%{cli_extension: ""}] ++
279+
if(System.find_executable("pwsh"), do: [%{cli_extension: ".ps1"}], else: [])
280+
)
242281

243282
import Retry
244283
@moduletag :tmp_dir
@@ -250,7 +289,7 @@ defmodule Kernel.CLI.CompileTest do
250289
end
251290

252291
test "compiles code", context do
253-
assert elixirc(~c"#{context.fixture} -o #{context.tmp_dir}") == ""
292+
assert elixirc(~c"#{context.fixture} -o #{context.tmp_dir}", context.cli_extension) == ""
254293
assert File.regular?(context.beam_file_path)
255294

256295
# Assert that the module is loaded into memory with the proper destination for the BEAM file.
@@ -267,7 +306,7 @@ defmodule Kernel.CLI.CompileTest do
267306
try do
268307
fixture = String.replace(context.fixture, "/", "\\")
269308
tmp_dir_path = String.replace(context.tmp_dir, "/", "\\")
270-
assert elixirc(~c"#{fixture} -o #{tmp_dir_path}") == ""
309+
assert elixirc(~c"#{fixture} -o #{tmp_dir_path}", context.cli_extension) == ""
271310
assert File.regular?(context[:beam_file_path])
272311

273312
# Assert that the module is loaded into memory with the proper destination for the BEAM file.
@@ -283,7 +322,9 @@ defmodule Kernel.CLI.CompileTest do
283322
end
284323

285324
stderr_test "fails on missing patterns", context do
286-
output = elixirc(~c"#{context.fixture} non_existing.ex -o #{context.tmp_dir}")
325+
output =
326+
elixirc(~c"#{context.fixture} non_existing.ex -o #{context.tmp_dir}", context.cli_extension)
327+
287328
assert output =~ "non_existing.ex"
288329
refute output =~ "compile_sample.ex"
289330
refute File.exists?(context.beam_file_path)
@@ -292,7 +333,7 @@ defmodule Kernel.CLI.CompileTest do
292333
stderr_test "fails on missing write access to .beam file", context do
293334
compilation_args = ~c"#{context.fixture} -o #{context.tmp_dir}"
294335

295-
assert elixirc(compilation_args) == ""
336+
assert elixirc(compilation_args, context.cli_extension) == ""
296337
assert File.regular?(context.beam_file_path)
297338

298339
# Set the .beam file to read-only
@@ -301,7 +342,7 @@ defmodule Kernel.CLI.CompileTest do
301342

302343
# Can only assert when read-only applies to the user
303344
if access != :read_write do
304-
output = elixirc(compilation_args)
345+
output = elixirc(compilation_args, context.cli_extension)
305346

306347
expected =
307348
"(File.Error) could not write to file #{inspect(context.beam_file_path)}: permission denied"

lib/elixir/test/elixir/test_helper.exs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ defmodule PathHelpers do
2323
Path.join(tmp_path(), extra)
2424
end
2525

26-
def elixir(args) do
27-
run_cmd(elixir_executable(), args)
26+
def elixir(args, executable_extension \\ "") do
27+
run_cmd(elixir_executable(executable_extension), args)
2828
end
2929

30-
def elixir_executable do
31-
executable_path("elixir")
30+
def elixir_executable(extension \\ "") do
31+
executable_path("elixir", extension)
3232
end
3333

34-
def elixirc(args) do
35-
run_cmd(elixirc_executable(), args)
34+
def elixirc(args, executable_extension \\ "") do
35+
run_cmd(elixirc_executable(executable_extension), args)
3636
end
3737

38-
def elixirc_executable do
39-
executable_path("elixirc")
38+
def elixirc_executable(extension \\ "") do
39+
executable_path("elixirc", extension)
4040
end
4141

42-
def iex(args) do
43-
run_cmd(iex_executable(), args)
42+
def iex(args, executable_extension \\ "") do
43+
run_cmd(iex_executable(executable_extension), args)
4444
end
4545

46-
def iex_executable do
47-
executable_path("iex")
46+
def iex_executable(extension \\ "") do
47+
executable_path("iex", extension)
4848
end
4949

5050
def write_beam({:module, name, bin, _} = res) do
@@ -60,8 +60,8 @@ defmodule PathHelpers do
6060
|> :unicode.characters_to_binary()
6161
end
6262

63-
defp executable_path(name) do
64-
Path.expand("../../../../bin/#{name}#{executable_extension()}", __DIR__)
63+
defp executable_path(name, extension) do
64+
Path.expand("../../../../bin/#{name}#{extension}", __DIR__)
6565
end
6666

6767
if match?({:win32, _}, :os.type()) do

0 commit comments

Comments
 (0)