Skip to content

Refactor escript tests to use less modules #13732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 51 additions & 126 deletions lib/mix/test/mix/tasks/escript_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,10 @@ defmodule Mix.Tasks.EscriptTest do
end
end

defmodule EscriptWithDebugInfo do
def project do
[
app: :escript_test_with_debug_info,
version: "0.0.1",
escript: [main_module: EscriptTest, strip_beams: false]
]
end
end

defmodule EscriptWithPath do
def project do
[
app: :escript_test_with_path,
version: "0.0.1",
escript: [
app: nil,
embed_elixir: true,
main_module: EscriptTest,
path: Path.join("ebin", "escript_test_with_path")
]
]
end
end

defmodule EscriptWithDeps do
def project do
[
app: :escript_test_with_deps,
version: "0.0.1",
escript: [main_module: EscriptTest],
deps: [{:ok, path: fixture_path("deps_status/deps/ok")}]
]
end
end

defmodule EscriptErlangWithDeps do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this one, since it also defines application.

def project do
[
app: :escript_test_erlang_with_deps,
app: :escript_test,
version: "0.0.1",
language: :erlang,
escript: [main_module: :escript_test],
Expand All @@ -65,57 +29,18 @@ defmodule Mix.Tasks.EscriptTest do
end
end

defmodule EscriptErlangMainModule do
def project do
[
app: :escript_test_erlang_main_module,
version: "0.0.1",
escript: [main_module: :escript_test]
]
end
end

defmodule EscriptWithUnknownMainModule do
def project do
[
app: :escript_test_with_unknown_main_module,
version: "0.0.1",
escript: [main_module: BogusEscriptTest]
]
end
end

defmodule EscriptConsolidated do
def project do
[
app: :escript_test_consolidated,
build_embedded: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this to the default test, since we expect consolidation to happen by default. Also, I don't think :build_embedded is supposed to do anything here.

version: "0.0.1",
escript: [main_module: EscriptTest]
]
end
end

defmodule EscriptWithPrivs do
def project do
[
app: :escript_test_with_priv,
version: "0.0.1",
escript: [main_module: EscriptTest, include_priv_for: [:escript_test_with_priv, :ok]],
deps: [{:ok, path: fixture_path("deps_status/deps/ok")}]
]
end
end

test "generate escript" do
in_fixture("escript_test", fn ->
Mix.Project.push(Escript)
push_project_with_config(Escript)

Mix.Tasks.Escript.Build.run([])
assert_received {:mix_shell, :info, ["Generated escript escript_test with MIX_ENV=dev"]}
assert System.cmd("escript", ["escript_test"]) == {"TEST\n", 0}
assert count_abstract_code("escript_test") == 0

# Consolidates protocols
assert System.cmd("escript", ["escript_test", "--protocol", "Enumerable"]) == {"true\n", 0}

# Each app has a distinct, valid path
assert System.cmd("escript", ["escript_test", "--app-paths"]) == {"{true, true, true}\n", 0}

Expand All @@ -127,7 +52,7 @@ defmodule Mix.Tasks.EscriptTest do

test "generate escript with --no-compile option" do
in_fixture("escript_test", fn ->
Mix.Project.push(Escript)
push_project_with_config(Escript)

Mix.Tasks.Compile.run([])
purge([EscriptTest])
Expand All @@ -139,7 +64,7 @@ defmodule Mix.Tasks.EscriptTest do

test "generate escript with compile config" do
in_fixture("escript_test", fn ->
Mix.Project.push(Escript)
push_project_with_config(Escript)

File.mkdir_p!("config")

Expand All @@ -156,7 +81,7 @@ defmodule Mix.Tasks.EscriptTest do

test "generate escript with runtime config" do
in_fixture("escript_test", fn ->
Mix.Project.push(Escript)
push_project_with_config(Escript)

File.mkdir_p!("config")

Expand All @@ -183,15 +108,15 @@ defmodule Mix.Tasks.EscriptTest do

test "generate escript with debug information" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptWithDebugInfo)
push_project_with_config(Escript, escript: [main_module: EscriptTest, strip_beams: false])

Mix.Tasks.Escript.Build.run([])

msg = "Generated escript escript_test_with_debug_info with MIX_ENV=dev"
msg = "Generated escript escript_test with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^msg]}

assert System.cmd("escript", ["escript_test_with_debug_info"]) == {"TEST\n", 0}
assert count_abstract_code("escript_test_with_debug_info") > 0
assert System.cmd("escript", ["escript_test"]) == {"TEST\n", 0}
assert count_abstract_code("escript_test") > 0
end)
end

Expand Down Expand Up @@ -226,46 +151,55 @@ defmodule Mix.Tasks.EscriptTest do

test "generate escript with path" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptWithPath)
push_project_with_config(Escript,
escript: [
app: nil,
embed_elixir: true,
main_module: EscriptTest,
path: Path.join("ebin", "escript_test")
]
)

Mix.Tasks.Escript.Build.run([])

message = "Generated escript ebin/escript_test_with_path with MIX_ENV=dev"
message = "Generated escript ebin/escript_test with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^message]}

assert System.cmd("escript", ["ebin/escript_test_with_path"]) == {"TEST\n", 0}
assert System.cmd("escript", ["ebin/escript_test"]) == {"TEST\n", 0}
end)
end

test "generate escript with deps" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptWithDeps)
push_project_with_config(Escript,
escript: [main_module: EscriptTest],
deps: [{:ok, path: fixture_path("deps_status/deps/ok")}]
)

Mix.Tasks.Escript.Build.run([])

message = "Generated escript escript_test_with_deps with MIX_ENV=dev"
message = "Generated escript escript_test with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^message]}

assert System.cmd("escript", ["escript_test_with_deps"]) == {"TEST\n", 0}
assert System.cmd("escript", ["escript_test"]) == {"TEST\n", 0}

# Does not include priv for deps by default
assert System.cmd("escript", ["escript_test_with_deps", "--list-priv", "ok"]) ==
{":error\n", 0}
assert System.cmd("escript", ["escript_test", "--list-priv", "ok"]) == {":error\n", 0}
end)
after
purge([Ok.MixProject])
end

test "generate escript with Erlang and deps" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptErlangWithDeps)
push_project_with_config(EscriptErlangWithDeps)

Mix.Tasks.Escript.Build.run([])

message = "Generated escript escript_test_erlang_with_deps with MIX_ENV=dev"
message = "Generated escript escript_test with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^message]}

assert System.cmd("escript", ["escript_test_erlang_with_deps", "arg1", "arg2"]) ==
assert System.cmd("escript", ["escript_test", "arg1", "arg2"]) ==
{~s(["arg1","arg2"]), 0}
end)
after
Expand All @@ -274,50 +208,36 @@ defmodule Mix.Tasks.EscriptTest do

test "generate escript with Erlang main module" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptErlangMainModule)
push_project_with_config(Escript, escript: [main_module: :escript_test])

Mix.Tasks.Escript.Build.run([])

message = "Generated escript escript_test_erlang_main_module with MIX_ENV=dev"
message = "Generated escript escript_test with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^message]}

assert System.cmd("escript", ["escript_test_erlang_main_module", "arg1", "arg2"]) ==
assert System.cmd("escript", ["escript_test", "arg1", "arg2"]) ==
{~s([<<"arg1">>,<<"arg2">>]), 0}
end)
after
purge([Ok.MixProject])
end

test "generate escript with consolidated protocols" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptConsolidated)

Mix.Tasks.Escript.Build.run([])

message = "Generated escript escript_test_consolidated with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^message]}

assert System.cmd("escript", ["escript_test_consolidated", "--protocol", "Enumerable"]) ==
{"true\n", 0}
end)
end

test "generate escript with priv" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptWithPrivs)
push_project_with_config(Escript,
escript: [main_module: EscriptTest, include_priv_for: [:escript_test, :ok]],
deps: [{:ok, path: fixture_path("deps_status/deps/ok")}]
)

Mix.Tasks.Escript.Build.run([])

message = "Generated escript escript_test_with_priv with MIX_ENV=dev"
message = "Generated escript escript_test with MIX_ENV=dev"
assert_received {:mix_shell, :info, [^message]}

assert System.cmd("escript", [
"escript_test_with_priv",
"--list-priv",
"escript_test_with_priv"
]) == {~s/{:ok, [~c"hello"]}\n/, 0}
assert System.cmd("escript", ["escript_test", "--list-priv", "escript_test"]) ==
{~s/{:ok, [~c"hello"]}\n/, 0}

assert System.cmd("escript", ["escript_test_with_priv", "--list-priv", "ok"]) ==
assert System.cmd("escript", ["escript_test", "--list-priv", "ok"]) ==
{~s/{:ok, [~c"sample"]}\n/, 0}
end)
end
Expand All @@ -326,7 +246,7 @@ defmodule Mix.Tasks.EscriptTest do
File.rm_rf!(tmp_path(".mix/escripts"))

in_fixture("escript_test", fn ->
Mix.Project.push(Escript)
push_project_with_config(Escript)

# check that no escripts are installed
Mix.Tasks.Escript.run([])
Expand Down Expand Up @@ -362,7 +282,7 @@ defmodule Mix.Tasks.EscriptTest do
File.rm_rf!(tmp_path(".mix/escripts"))

in_fixture("escript_test", fn ->
Mix.Project.push(Escript)
push_project_with_config(Escript)

Mix.Tasks.Escript.Install.run(["--force"])

Expand Down Expand Up @@ -392,7 +312,7 @@ defmodule Mix.Tasks.EscriptTest do

test "escript invalid main module" do
in_fixture("escript_test", fn ->
Mix.Project.push(EscriptWithUnknownMainModule)
push_project_with_config(Escript, escript: [main_module: BogusEscriptTest])

message =
"Could not generate escript, module Elixir.BogusEscriptTest defined as :main_module could not be loaded"
Expand Down Expand Up @@ -446,4 +366,9 @@ defmodule Mix.Tasks.EscriptTest do
after
purge([GitRepo, GitRepo.MixProject])
end

defp push_project_with_config(module, config \\ []) do
Mix.ProjectStack.post_config(config)
Mix.Project.push(module)
end
end
Loading