Skip to content

Commit

Permalink
Integration test for escript building #384
Browse files Browse the repository at this point in the history
Will it work on Windows? Maybe?
  • Loading branch information
PragTob committed Nov 8, 2023
1 parent 7cb1b5d commit a893bda
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/benchee/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ defmodule Benchee.System do
[path]
|> Protocol.extract_protocols()
|> Enum.all?(&Protocol.consolidated?/1)

_error ->
IO.puts("Could not check if protocols are consolidated. Running as escript? Defaulting to they are consolidated.")
IO.puts(
"Could not check if protocols are consolidated. Running as escript? Defaulting to they are consolidated."
)

true
end
end
Expand Down
14 changes: 8 additions & 6 deletions test/benchee/system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,21 @@ defmodule Benchee.SystemTest do

describe "all_protocols_consolidated?/1" do
test "normally it just works and is true for Bechee and does not log a warning" do
warning = capture_io(fn ->
assert true == all_protocols_consolidated?()
end)
warning =
capture_io(fn ->
assert true == all_protocols_consolidated?()
end)

assert warning == ""
end

test "when it borks out it warns and defaults to true, see #384" do
fake_lib_dir = fn _, _ -> {:error, :bad_name} end

warning = capture_io(fn ->
assert true == all_protocols_consolidated?(fake_lib_dir)
end)
warning =
capture_io(fn ->
assert true == all_protocols_consolidated?(fake_lib_dir)
end)

assert warning =~ ~r/not.*check.*protocol.*consolidat/i
end
Expand Down
12 changes: 12 additions & 0 deletions test/benchee_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,18 @@ defmodule BencheeTest do
end
end

describe "escript building" do
@sample_project_directory Path.expand("fixtures/escript", __DIR__)
test "benchee can be built into and used as an escript" do
File.cd!(@sample_project_directory)
# we don't match the exit_status right now to get better error messages potentially
{output, exit_status} = System.cmd("bash", ["test.sh"])

readme_sample_asserts(output)
assert exit_status == 0
end
end

@slower_regex "\\s+- \\d+\\.\\d+x slower \\+\\d+(\\.\\d+)?.+"
defp readme_sample_asserts(output, tag_string \\ "") do
assert output =~ "warmup: 5 ms"
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/escript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
escript-*.tar

# Temporary files, for example, from tests.
/tmp/

/escript
21 changes: 21 additions & 0 deletions test/fixtures/escript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Escript

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `escript` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:escript, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/escript>.

17 changes: 17 additions & 0 deletions test/fixtures/escript/lib/escript.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Escript do
# is just testdummy
@moduledoc false
def main(_args \\ []) do
list = Enum.to_list(1..10_000)
map_fun = fn i -> [i, i * i] end

Benchee.run(
%{
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end
},
time: 0.01,
warmup: 0.005
)
end
end
30 changes: 30 additions & 0 deletions test/fixtures/escript/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Escript.MixProject do
use Mix.Project

def project do
[
app: :escript,
version: "0.1.0",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps(),
escript: [main_module: Escript]
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:benchee, "~> 1.0", path: "../../.."}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
4 changes: 4 additions & 0 deletions test/fixtures/escript/mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%{
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
}
1 change: 1 addition & 0 deletions test/fixtures/escript/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mix escript.build && ./escript

0 comments on commit a893bda

Please sign in to comment.