Skip to content

Commit 401c530

Browse files
committed
Add tests
1 parent 083f86a commit 401c530

2 files changed

Lines changed: 75 additions & 25 deletions

File tree

lib/mix/tasks/membrane.demo.ex

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ defmodule Mix.Tasks.Membrane.Demo do
4141
list_available_demos()
4242
end
4343

44-
repo_dir = Path.join(Path.expand(opts[:directory] || "."), "membrane_demo")
44+
target_dir = Path.expand(opts[:directory] || ".")
4545

4646
cond do
47-
opts[:all] -> copy_all_demos(repo_dir)
48-
demos_names != [] -> copy_specific_demos(repo_dir, demos_names)
47+
opts[:all] -> copy_all_demos(target_dir)
48+
demos_names != [] -> copy_specific_demos(target_dir, demos_names)
4949
true -> :ok
5050
end
5151
end
@@ -72,59 +72,62 @@ defmodule Mix.Tasks.Membrane.Demo do
7272
|> Enum.map(&String.length(&1["name"]))
7373
|> Enum.max()
7474

75-
demos_info
76-
|> Enum.each(fn %{"name" => name, "description" => description} ->
77-
dots_line = String.duplicate(".", max_name_length - String.length(name) + 3)
78-
Mix.shell().info(" | #{name} #{dots_line} | #{description}")
79-
end)
75+
demos_table =
76+
demos_info
77+
|> Enum.map_join("\n", fn %{"name" => name, "description" => description} ->
78+
dots_line = String.duplicate(".", max_name_length - String.length(name) + 3)
79+
" | #{name} #{dots_line} | #{description}"
80+
end)
81+
82+
Mix.shell().info(demos_table)
8083
end
8184

82-
defp copy_all_demos(repo_dir) do
85+
defp copy_all_demos(target_dir) do
86+
repo_dir = Path.join(target_dir, "membrane_demo")
8387
execute_git_command(["clone", "-q", "--depth", "1", @demos_clone_url, repo_dir])
8488
end
8589

86-
defp copy_specific_demos(repo_dir, demos_names) do
90+
defp copy_specific_demos(target_dir, demos_names) do
91+
repo_dir = Path.join(target_dir, "membrane_demo")
8792
execute_git_command(["clone", "-q", "--depth", "1", "-n", @demos_clone_url, repo_dir])
8893

89-
File.cd!(repo_dir)
90-
9194
Enum.each(demos_names, fn demo_name ->
92-
case copy_demo(demo_name) do
95+
case copy_demo(target_dir, demo_name) do
9396
:error ->
9497
Mix.shell().error(
9598
"No demo named #{demo_name}, run this task with -l to list all available demos"
9699
)
97100

98101
:ok ->
99-
Mix.shell().info(
100-
"`#{demo_name}` demo created at #{Path.join(Path.dirname(repo_dir), demo_name)}"
101-
)
102+
Mix.shell().info("`#{demo_name}` demo created at #{Path.join(target_dir, demo_name)}")
102103
end
103104
end)
104105

105106
File.rm_rf!(repo_dir)
106107
end
107108

108-
defp copy_demo(demo_name) do
109+
defp copy_demo(target_dir, demo_name) do
110+
repo_dir = Path.join(target_dir, "membrane_demo")
111+
109112
Enum.reduce_while(@demos_search_list, :error, fn demo_dir, _status ->
110-
demo_path = Path.join(demo_dir, demo_name)
113+
demo_path = Path.join([repo_dir, demo_dir, demo_name])
111114

112-
execute_git_command(["sparse-checkout", "set", demo_path])
113-
execute_git_command(["checkout"])
115+
execute_git_command(["sparse-checkout", "set", Path.join(demo_dir, demo_name)], repo_dir)
116+
execute_git_command(["checkout"], repo_dir)
114117

115-
case File.cp_r(demo_path, Path.join("..", Path.basename(demo_path))) do
118+
case File.cp_r(demo_path, Path.join(target_dir, Path.basename(demo_path))) do
116119
{:ok, _files} -> {:halt, :ok}
117120
{:error, :enoent, _dir} -> {:cont, :error}
118121
end
119122
end)
120123
end
121124

122-
defp execute_git_command(argv) do
123-
case System.cmd("git", argv, stderr_to_stdout: true) do
124-
{0, _output} ->
125+
defp execute_git_command(argv, dir \\ ".") do
126+
case System.cmd("git", argv, stderr_to_stdout: true, cd: dir) do
127+
{_output, 0} ->
125128
:ok
126129

127-
{exit_code, output} ->
130+
{output, exit_code} ->
128131
Mix.shell().error("""
129132
Git command `git #{Enum.join(argv, " ")} exited with code #{exit_code}. Output:
130133
#{output}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defmodule Mix.Tasks.Membrane.DemoTest do
2+
use ExUnit.Case
3+
alias Mix.Tasks.Membrane.Demo
4+
5+
setup do
6+
Mix.shell(Mix.Shell.Process)
7+
end
8+
9+
describe "All listed demos are pulled" do
10+
@tag :tmp_dir
11+
test "individually", %{tmp_dir: tmp} do
12+
Demo.run(["-l"])
13+
14+
get_available_demos_list()
15+
|> Enum.each(fn demo ->
16+
Demo.run(["-d", tmp, demo])
17+
18+
receive do
19+
msg -> assert {:mix_shell, :info, _message} = msg
20+
end
21+
end)
22+
end
23+
24+
@tag :tmp_dir
25+
test "all at once", %{tmp_dir: tmp} do
26+
demos_list = get_available_demos_list()
27+
Demo.run(["-d", tmp] ++ demos_list)
28+
29+
Enum.each(demos_list, fn _demo ->
30+
receive do
31+
msg -> assert {:mix_shell, :info, _message} = msg
32+
end
33+
end)
34+
end
35+
end
36+
37+
defp get_available_demos_list() do
38+
Demo.run(["-l"])
39+
40+
receive do
41+
{:mix_shell, :info, [table]} ->
42+
table
43+
|> String.split("\n")
44+
|> Enum.map(&Regex.named_captures(~r/\| (?<name>\w+)/, &1)["name"])
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)