From 301fd250e078d0d51b1fdddf298c0e6615b189fb Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 15 Jan 2023 14:17:27 +0100 Subject: [PATCH 01/53] Add test to invoke onigumo from CLI --- test/onigumo_cli_test.exs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/onigumo_cli_test.exs diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs new file mode 100644 index 0000000..536455f --- /dev/null +++ b/test/onigumo_cli_test.exs @@ -0,0 +1,11 @@ +defmodule OnigumoCLITest do + use ExUnit.Case + + + describe("Onigumo.CLI.main/1") do + test("run Onigumo.CLI.main") do + Onigumo.CLI.main(["arg1"]) + end + end +end + From 84364526033e3fc113a428bbaf99ba16c21c92f2 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 May 2023 19:00:58 +0200 Subject: [PATCH 02/53] Move private function body and prepare_response - These functions was moved because we need them in more tests then just one --- mix.exs | 7 ++++++- test/onigumo_downloader_test.exs | 29 +++++++++-------------------- test/support/http_test_util.ex | 13 +++++++++++++ 3 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 test/support/http_test_util.ex diff --git a/mix.exs b/mix.exs index 77e7906..8cc9d4b 100644 --- a/mix.exs +++ b/mix.exs @@ -8,7 +8,8 @@ defmodule Onigumo.MixProject do elixir: "~> 1.10", start_permanent: Mix.env() == :prod, deps: deps(), - escript: escript() + escript: escript(), + elixirc_paths: elixirc_paths(Mix.env()), ] end @@ -37,4 +38,8 @@ defmodule Onigumo.MixProject do main_module: Onigumo.CLI ] end + + defp elixirc_paths(:test), do: ["lib", "test/support"] + defp elixirc_paths(_), do: [ "lib" ] + end diff --git a/test/onigumo_downloader_test.exs b/test/onigumo_downloader_test.exs index 8d04baf..2639984 100644 --- a/test/onigumo_downloader_test.exs +++ b/test/onigumo_downloader_test.exs @@ -14,7 +14,7 @@ defmodule OnigumoDownloaderTest do @tag :tmp_dir test("run Downloader", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) - expect(HTTPoisonMock, :get!, length(@urls), &prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpTestUtil.prepare_response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) @@ -30,7 +30,7 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.create_download_stream/1") do @tag :tmp_dir test("download URLs from the input file with a created stream", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :get!, length(@urls), &prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpTestUtil.prepare_response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) @@ -46,7 +46,7 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.download_url/2") do @tag :tmp_dir test("download a URL", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :get!, &prepare_response/1) + expect(HTTPoisonMock, :get!, &HttpTestUtil.prepare_response/1) input_url = Enum.at(@urls, 0) Onigumo.Downloader.download_url(input_url, tmp_dir) @@ -54,18 +54,18 @@ defmodule OnigumoDownloaderTest do output_file_name = Onigumo.Downloader.create_file_name(input_url) output_path = Path.join(tmp_dir, output_file_name) read_output = File.read!(output_path) - expected_output = body(input_url) + expected_output = HttpTestUtil.body(input_url) assert(read_output == expected_output) end end describe("Onigumo.Downloader.get_url/1") do test("get response by HTTP request") do - expect(HTTPoisonMock, :get!, &prepare_response/1) + expect(HTTPoisonMock, :get!, &HttpTestUtil.prepare_response/1) url = Enum.at(@urls, 0) get_response = Onigumo.Downloader.get_url(url) - expected_response = prepare_response(url) + expected_response = HttpTestUtil.prepare_response(url) assert(get_response == expected_response) end end @@ -73,9 +73,9 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.get_body/1") do test("extract body from URL response") do url = Enum.at(@urls, 0) - response = prepare_response(url) + response = HttpTestUtil.prepare_response(url) get_body = Onigumo.Downloader.get_body(response) - expected_body = body(url) + expected_body = HttpTestUtil.body(url) assert(get_body == expected_body) end end @@ -121,27 +121,16 @@ defmodule OnigumoDownloaderTest do end end - defp prepare_response(url) do - %HTTPoison.Response{ - status_code: 200, - body: body(url) - } - end - defp prepare_input(urls) do Enum.map(urls, &(&1 <> "\n")) |> Enum.join() end - defp body(url) do - "Body from: #{url}\n" - end - defp assert_downloaded(url, tmp_dir) do file_name = Onigumo.Downloader.create_file_name(url) output_path = Path.join(tmp_dir, file_name) read_output = File.read!(output_path) - expected_output = body(url) + expected_output = HttpTestUtil.body(url) assert(read_output == expected_output) end end diff --git a/test/support/http_test_util.ex b/test/support/http_test_util.ex new file mode 100644 index 0000000..01628c2 --- /dev/null +++ b/test/support/http_test_util.ex @@ -0,0 +1,13 @@ +defmodule HttpTestUtil do + + def prepare_response(url) do + %HTTPoison.Response{ + status_code: 200, + body: body(url) + } + end + + def body(url) do + "Body from: #{url}\n" + end +end From e807364585d0641680254c1f2f79cf43bc6acb36 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 May 2023 19:08:07 +0200 Subject: [PATCH 03/53] Rename file from http_util to http - when we move the file to folder support util in name is extra. The mean of util is include in folder name support --- test/support/{http_test_util.ex => http.ex} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/support/{http_test_util.ex => http.ex} (100%) diff --git a/test/support/http_test_util.ex b/test/support/http.ex similarity index 100% rename from test/support/http_test_util.ex rename to test/support/http.ex From 16ead5368bdc49e2af992366dc50762773e4309d Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 May 2023 19:50:49 +0200 Subject: [PATCH 04/53] Rename module HttpTestUtil to HttpSupport --- test/onigumo_downloader_test.exs | 18 +++++++++--------- test/support/http.ex | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/test/onigumo_downloader_test.exs b/test/onigumo_downloader_test.exs index 2639984..59fb11b 100644 --- a/test/onigumo_downloader_test.exs +++ b/test/onigumo_downloader_test.exs @@ -14,7 +14,7 @@ defmodule OnigumoDownloaderTest do @tag :tmp_dir test("run Downloader", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) - expect(HTTPoisonMock, :get!, length(@urls), &HttpTestUtil.prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.prepare_response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) @@ -30,7 +30,7 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.create_download_stream/1") do @tag :tmp_dir test("download URLs from the input file with a created stream", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :get!, length(@urls), &HttpTestUtil.prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.prepare_response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) @@ -46,7 +46,7 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.download_url/2") do @tag :tmp_dir test("download a URL", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :get!, &HttpTestUtil.prepare_response/1) + expect(HTTPoisonMock, :get!, &HttpSupport.prepare_response/1) input_url = Enum.at(@urls, 0) Onigumo.Downloader.download_url(input_url, tmp_dir) @@ -54,18 +54,18 @@ defmodule OnigumoDownloaderTest do output_file_name = Onigumo.Downloader.create_file_name(input_url) output_path = Path.join(tmp_dir, output_file_name) read_output = File.read!(output_path) - expected_output = HttpTestUtil.body(input_url) + expected_output = HttpSupport.body(input_url) assert(read_output == expected_output) end end describe("Onigumo.Downloader.get_url/1") do test("get response by HTTP request") do - expect(HTTPoisonMock, :get!, &HttpTestUtil.prepare_response/1) + expect(HTTPoisonMock, :get!, &HttpSupport.prepare_response/1) url = Enum.at(@urls, 0) get_response = Onigumo.Downloader.get_url(url) - expected_response = HttpTestUtil.prepare_response(url) + expected_response = HttpSupport.prepare_response(url) assert(get_response == expected_response) end end @@ -73,9 +73,9 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.get_body/1") do test("extract body from URL response") do url = Enum.at(@urls, 0) - response = HttpTestUtil.prepare_response(url) + response = HttpSupport.prepare_response(url) get_body = Onigumo.Downloader.get_body(response) - expected_body = HttpTestUtil.body(url) + expected_body = HttpSupport.body(url) assert(get_body == expected_body) end end @@ -130,7 +130,7 @@ defmodule OnigumoDownloaderTest do file_name = Onigumo.Downloader.create_file_name(url) output_path = Path.join(tmp_dir, file_name) read_output = File.read!(output_path) - expected_output = HttpTestUtil.body(url) + expected_output = HttpSupport.body(url) assert(read_output == expected_output) end end diff --git a/test/support/http.ex b/test/support/http.ex index 01628c2..0f49c1d 100644 --- a/test/support/http.ex +++ b/test/support/http.ex @@ -1,4 +1,4 @@ -defmodule HttpTestUtil do +defmodule HttpSupport do def prepare_response(url) do %HTTPoison.Response{ @@ -10,4 +10,5 @@ defmodule HttpTestUtil do def body(url) do "Body from: #{url}\n" end + end From b083ba72f2ca75355662d2d5de33d56ba41013ec Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 May 2023 19:51:36 +0200 Subject: [PATCH 05/53] Move prepare input func to support --- test/onigumo_downloader_test.exs | 11 +++-------- test/support/input.ex | 8 ++++++++ 2 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 test/support/input.ex diff --git a/test/onigumo_downloader_test.exs b/test/onigumo_downloader_test.exs index 59fb11b..f8c2033 100644 --- a/test/onigumo_downloader_test.exs +++ b/test/onigumo_downloader_test.exs @@ -18,7 +18,7 @@ defmodule OnigumoDownloaderTest do input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = prepare_input(@urls) + input_file_content = InputSupport.prepare(@urls) File.write!(input_path_tmp, input_file_content) Onigumo.Downloader.main(tmp_dir) @@ -34,7 +34,7 @@ defmodule OnigumoDownloaderTest do input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = prepare_input(@urls) + input_file_content = InputSupport.prepare(@urls) File.write!(input_path_tmp, input_file_content) Onigumo.Downloader.create_download_stream(tmp_dir) |> Stream.run() @@ -101,7 +101,7 @@ defmodule OnigumoDownloaderTest do input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = prepare_input(input_urls) + input_file_content = InputSupport.prepare(input_urls) File.write!(input_path_tmp, input_file_content) loaded_urls = Onigumo.Downloader.load_urls(tmp_dir) |> Enum.to_list() @@ -121,11 +121,6 @@ defmodule OnigumoDownloaderTest do end end - defp prepare_input(urls) do - Enum.map(urls, &(&1 <> "\n")) - |> Enum.join() - end - defp assert_downloaded(url, tmp_dir) do file_name = Onigumo.Downloader.create_file_name(url) output_path = Path.join(tmp_dir, file_name) diff --git a/test/support/input.ex b/test/support/input.ex new file mode 100644 index 0000000..84d92c4 --- /dev/null +++ b/test/support/input.ex @@ -0,0 +1,8 @@ +defmodule InputSupport do + + def prepare(urls) do + Enum.map(urls, &(&1 <> "\n")) + |> Enum.join() + end + +end From 71262f7b9efe7ae744cb1eb01210444280638213 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 May 2023 19:54:15 +0200 Subject: [PATCH 06/53] Test CLI with input for 'Downloader' --- test/onigumo_cli_test.exs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 536455f..f8e8a23 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -1,11 +1,26 @@ defmodule OnigumoCLITest do use ExUnit.Case + import Mox + @urls [ + "http://onigumo.local/hello.html", + "http://onigumo.local/bye.html" + ] describe("Onigumo.CLI.main/1") do - test("run Onigumo.CLI.main") do - Onigumo.CLI.main(["arg1"]) + @tag :tmp_dir + test("run Onigumo.CLI.main", %{tmp_dir: tmp_dir}) do + expect(HTTPoisonMock, :start, fn -> nil end) + expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.prepare_response/1) + + input_path_env = Application.get_env(:onigumo, :input_path) + input_path_tmp = Path.join(tmp_dir, input_path_env) + input_file_content = InputSupport.prepare(@urls) + File.write!(input_path_tmp, input_file_content) + File.cd(tmp_dir) + Onigumo.CLI.main(["Downloader"]) end end + end From fc5380f902c2ba6a0341111de8b6cbbc5fd7f54c Mon Sep 17 00:00:00 2001 From: dstroch Date: Tue, 23 May 2023 11:54:13 +0200 Subject: [PATCH 07/53] Format the code with mix --- mix.exs | 5 ++--- test/onigumo_cli_test.exs | 2 -- test/support/http.ex | 2 -- test/support/input.ex | 2 -- 4 files changed, 2 insertions(+), 9 deletions(-) diff --git a/mix.exs b/mix.exs index 8cc9d4b..a2239b2 100644 --- a/mix.exs +++ b/mix.exs @@ -9,7 +9,7 @@ defmodule Onigumo.MixProject do start_permanent: Mix.env() == :prod, deps: deps(), escript: escript(), - elixirc_paths: elixirc_paths(Mix.env()), + elixirc_paths: elixirc_paths(Mix.env()) ] end @@ -40,6 +40,5 @@ defmodule Onigumo.MixProject do end defp elixirc_paths(:test), do: ["lib", "test/support"] - defp elixirc_paths(_), do: [ "lib" ] - + defp elixirc_paths(_), do: ["lib"] end diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index f8e8a23..5cc2a17 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -21,6 +21,4 @@ defmodule OnigumoCLITest do Onigumo.CLI.main(["Downloader"]) end end - end - diff --git a/test/support/http.ex b/test/support/http.ex index 0f49c1d..c2e0821 100644 --- a/test/support/http.ex +++ b/test/support/http.ex @@ -1,5 +1,4 @@ defmodule HttpSupport do - def prepare_response(url) do %HTTPoison.Response{ status_code: 200, @@ -10,5 +9,4 @@ defmodule HttpSupport do def body(url) do "Body from: #{url}\n" end - end diff --git a/test/support/input.ex b/test/support/input.ex index 84d92c4..155a3bb 100644 --- a/test/support/input.ex +++ b/test/support/input.ex @@ -1,8 +1,6 @@ defmodule InputSupport do - def prepare(urls) do Enum.map(urls, &(&1 <> "\n")) |> Enum.join() end - end From 3d4cca5b8493a458f3ad99dd01ed465140a1a229 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 9 Jun 2023 11:23:29 +0200 Subject: [PATCH 08/53] Parse switches output and component from CLI - parse output path and component to run as switch -o and --component --- lib/cli.ex | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0a0f638..1e3b05d 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,7 +1,28 @@ defmodule Onigumo.CLI do - def main([component]) do - module = Module.safe_concat("Onigumo", component) - root_path = File.cwd!() - module.main(root_path) + def main(argv) do + {parsed_switches, _, _} = + argv + |> parse_argv + + module = + parsed_switches + |> Keyword.get(:component, "Downloader") + |> safe_concat() + + parsed_switches + |> Keyword.get(:output, File.cwd!()) + |> module.main() + end + + defp parse_argv(argv) do + argv + |> OptionParser.parse( + aliases: [o: :output], + strict: [component: :string, output: :string] + ) + end + + defp safe_concat(component) do + Module.safe_concat("Onigumo", component) end end From bec0fe63c882c42ea7780c1bb8e246b2a9666e3e Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 9 Jun 2023 11:27:19 +0200 Subject: [PATCH 09/53] Fix formatting --- lib/cli.ex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 1e3b05d..0cdfe76 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -15,11 +15,11 @@ defmodule Onigumo.CLI do end defp parse_argv(argv) do - argv - |> OptionParser.parse( - aliases: [o: :output], - strict: [component: :string, output: :string] - ) + argv + |> OptionParser.parse( + aliases: [o: :output], + strict: [component: :string, output: :string] + ) end defp safe_concat(component) do From 6b380c7a1cb0c0de33c77e5ce385fe422e0df680 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 29 Jul 2023 20:30:03 +0200 Subject: [PATCH 10/53] Remove prepare in the name of support functions --- test/onigumo_cli_test.exs | 4 ++-- test/onigumo_downloader_test.exs | 18 +++++++++--------- test/support/http.ex | 2 +- test/support/input.ex | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 5cc2a17..56db75a 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -11,11 +11,11 @@ defmodule OnigumoCLITest do @tag :tmp_dir test("run Onigumo.CLI.main", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) - expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = InputSupport.prepare(@urls) + input_file_content = InputSupport.url_list(@urls) File.write!(input_path_tmp, input_file_content) File.cd(tmp_dir) Onigumo.CLI.main(["Downloader"]) diff --git a/test/onigumo_downloader_test.exs b/test/onigumo_downloader_test.exs index f8c2033..abc334a 100644 --- a/test/onigumo_downloader_test.exs +++ b/test/onigumo_downloader_test.exs @@ -14,11 +14,11 @@ defmodule OnigumoDownloaderTest do @tag :tmp_dir test("run Downloader", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) - expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = InputSupport.prepare(@urls) + input_file_content = InputSupport.url_list(@urls) File.write!(input_path_tmp, input_file_content) Onigumo.Downloader.main(tmp_dir) @@ -30,11 +30,11 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.create_download_stream/1") do @tag :tmp_dir test("download URLs from the input file with a created stream", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.prepare_response/1) + expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = InputSupport.prepare(@urls) + input_file_content = InputSupport.url_list(@urls) File.write!(input_path_tmp, input_file_content) Onigumo.Downloader.create_download_stream(tmp_dir) |> Stream.run() @@ -46,7 +46,7 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.download_url/2") do @tag :tmp_dir test("download a URL", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :get!, &HttpSupport.prepare_response/1) + expect(HTTPoisonMock, :get!, &HttpSupport.response/1) input_url = Enum.at(@urls, 0) Onigumo.Downloader.download_url(input_url, tmp_dir) @@ -61,11 +61,11 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.get_url/1") do test("get response by HTTP request") do - expect(HTTPoisonMock, :get!, &HttpSupport.prepare_response/1) + expect(HTTPoisonMock, :get!, &HttpSupport.response/1) url = Enum.at(@urls, 0) get_response = Onigumo.Downloader.get_url(url) - expected_response = HttpSupport.prepare_response(url) + expected_response = HttpSupport.response(url) assert(get_response == expected_response) end end @@ -73,7 +73,7 @@ defmodule OnigumoDownloaderTest do describe("Onigumo.Downloader.get_body/1") do test("extract body from URL response") do url = Enum.at(@urls, 0) - response = HttpSupport.prepare_response(url) + response = HttpSupport.response(url) get_body = Onigumo.Downloader.get_body(response) expected_body = HttpSupport.body(url) assert(get_body == expected_body) @@ -101,7 +101,7 @@ defmodule OnigumoDownloaderTest do input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = InputSupport.prepare(input_urls) + input_file_content = InputSupport.url_list(input_urls) File.write!(input_path_tmp, input_file_content) loaded_urls = Onigumo.Downloader.load_urls(tmp_dir) |> Enum.to_list() diff --git a/test/support/http.ex b/test/support/http.ex index c2e0821..6a0b61c 100644 --- a/test/support/http.ex +++ b/test/support/http.ex @@ -1,5 +1,5 @@ defmodule HttpSupport do - def prepare_response(url) do + def response(url) do %HTTPoison.Response{ status_code: 200, body: body(url) diff --git a/test/support/input.ex b/test/support/input.ex index 155a3bb..a73a4fa 100644 --- a/test/support/input.ex +++ b/test/support/input.ex @@ -1,5 +1,5 @@ defmodule InputSupport do - def prepare(urls) do + def url_list(urls) do Enum.map(urls, &(&1 <> "\n")) |> Enum.join() end From 0a1a093d025a427f95c63d424974c0fc1cc29f3f Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 1 Oct 2023 19:33:27 +0200 Subject: [PATCH 11/53] Load default elixirc_paths, then add extra paths to default --- mix.exs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mix.exs b/mix.exs index a2239b2..0acdce5 100644 --- a/mix.exs +++ b/mix.exs @@ -39,6 +39,10 @@ defmodule Onigumo.MixProject do ] end - defp elixirc_paths(:test), do: ["lib", "test/support"] - defp elixirc_paths(_), do: ["lib"] + defp elixirc_paths(:test), do: elixirc_paths_default() ++ ["test/support"] + + defp elixirc_paths(_), do: elixirc_paths_default() + + defp elixirc_paths_default(), do: Mix.Project.config()[:elixirc_paths] + end From 39583f79b2bd7c9cda712f35eba306177884deb3 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 1 Oct 2023 20:24:43 +0200 Subject: [PATCH 12/53] Fix formatting --- mix.exs | 1 - 1 file changed, 1 deletion(-) diff --git a/mix.exs b/mix.exs index 0acdce5..687fa2f 100644 --- a/mix.exs +++ b/mix.exs @@ -44,5 +44,4 @@ defmodule Onigumo.MixProject do defp elixirc_paths(_), do: elixirc_paths_default() defp elixirc_paths_default(), do: Mix.Project.config()[:elixirc_paths] - end From 63d2f1230ae394b4b5db18b644d894475d180766 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 1 Oct 2023 20:30:21 +0200 Subject: [PATCH 13/53] Reduce calling Mix.env() to just once --- mix.exs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mix.exs b/mix.exs index 687fa2f..2e64791 100644 --- a/mix.exs +++ b/mix.exs @@ -2,14 +2,16 @@ defmodule Onigumo.MixProject do use Mix.Project def project do + env = Mix.env() + [ app: :onigumo, version: "0.1.0", elixir: "~> 1.10", - start_permanent: Mix.env() == :prod, + start_permanent: env == :prod, deps: deps(), escript: escript(), - elixirc_paths: elixirc_paths(Mix.env()) + elixirc_paths: elixirc_paths(env) ] end From fb6665f4bc44e1b791a357f2a6e0220bb192f3fb Mon Sep 17 00:00:00 2001 From: dstroch Date: Tue, 3 Oct 2023 10:36:19 +0200 Subject: [PATCH 14/53] Use OptionParser to parse component we have to specify option strict: with empty list, otherwise we get an error from elixir: "warning: not passing the :switches or :strict option to OptionParser is deprecate" --- lib/cli.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0a0f638..738c52e 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,6 +1,7 @@ defmodule Onigumo.CLI do - def main([component]) do - module = Module.safe_concat("Onigumo", component) + def main(argv) do + {_parsed, args, _invalid} = OptionParser.parse(argv, strict: []) + root_path = File.cwd!() module.main(root_path) end From 9f789ca8d8ef3e586218f348b9280230c2e388bd Mon Sep 17 00:00:00 2001 From: dstroch Date: Tue, 3 Oct 2023 10:38:50 +0200 Subject: [PATCH 15/53] Create module from parsed component parse component as first position argument from CLI. Rest of positional arguments are unused. This goal is achieved by pattern matching of private func get_component. If no positional argument is specified then "Downloader" must be used. --- lib/cli.ex | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/cli.ex b/lib/cli.ex index 738c52e..e9a1f50 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -2,7 +2,19 @@ defmodule Onigumo.CLI do def main(argv) do {_parsed, args, _invalid} = OptionParser.parse(argv, strict: []) + module = + args + |> get_component + |> safe_concat + root_path = File.cwd!() module.main(root_path) end + + defp get_component([]), do: "Downloader" + defp get_component([component | _]), do: component + + defp safe_concat(component) do + Module.safe_concat("Onigumo", component) + end end From cc58c7ff1d1185c27cb088ceb2c9e008309eea23 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 7 Oct 2023 18:38:54 +0200 Subject: [PATCH 16/53] Use OptionParser parse component --- lib/cli.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0a0f638..0ea12a9 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,5 +1,6 @@ defmodule Onigumo.CLI do - def main([component]) do + def main(argv) do + {[], [component]} = OptionParser.parse!(argv, strict: []) module = Module.safe_concat("Onigumo", component) root_path = File.cwd!() module.main(root_path) From 1aab464b138dffad952c6ddd7a5f23f36de69e06 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 7 Oct 2023 19:12:03 +0200 Subject: [PATCH 17/53] Test cases when CLI raise an error --- test/onigumo_cli_test.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 56db75a..bc82eb5 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -20,5 +20,17 @@ defmodule OnigumoCLITest do File.cd(tmp_dir) Onigumo.CLI.main(["Downloader"]) end + + test("Onigumo CLI with invalid component name") do + assert_raise(ArgumentError, fn -> Onigumo.CLI.main(["invalid_name"]) end) + end + + test("Onigumo CLI with no component") do + assert_raise(FunctionClauseError, fn -> Onigumo.CLI.main([]) end) + end + + test("Onigumo CLI with more than one args ") do + assert_raise(FunctionClauseError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) + end end end From 48cac75ba2c5731720e2f40193543ecfe2111384 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 7 Oct 2023 19:23:58 +0200 Subject: [PATCH 18/53] Improve tests names --- test/onigumo_cli_test.exs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index bc82eb5..37160c2 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -9,7 +9,7 @@ defmodule OnigumoCLITest do describe("Onigumo.CLI.main/1") do @tag :tmp_dir - test("run Onigumo.CLI.main", %{tmp_dir: tmp_dir}) do + test("run CLI with 'Downloader' argument", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) @@ -21,15 +21,15 @@ defmodule OnigumoCLITest do Onigumo.CLI.main(["Downloader"]) end - test("Onigumo CLI with invalid component name") do - assert_raise(ArgumentError, fn -> Onigumo.CLI.main(["invalid_name"]) end) + test("run CLI with unknown argument") do + assert_raise(ArgumentError, fn -> Onigumo.CLI.main(["Uploader"]) end) end - test("Onigumo CLI with no component") do + test("run CLI with no arguments") do assert_raise(FunctionClauseError, fn -> Onigumo.CLI.main([]) end) end - test("Onigumo CLI with more than one args ") do + test("run CLI with more than one argument") do assert_raise(FunctionClauseError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end end From 1574c96b73fc8df552e889a8228c5743f899eadb Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 7 Oct 2023 19:46:28 +0200 Subject: [PATCH 19/53] Update test for pattern matching --- test/onigumo_cli_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 37160c2..b79f8a4 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -26,11 +26,11 @@ defmodule OnigumoCLITest do end test("run CLI with no arguments") do - assert_raise(FunctionClauseError, fn -> Onigumo.CLI.main([]) end) + assert_raise(MatchError, fn -> Onigumo.CLI.main([]) end) end test("run CLI with more than one argument") do - assert_raise(FunctionClauseError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) + assert_raise(MatchError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end end end From be21ca1151ac398a427608a6e3e00fef5ca2eb98 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 7 Oct 2023 19:49:07 +0200 Subject: [PATCH 20/53] Test error is raised by OptionParser.parse! if invalid switch is passed --- test/onigumo_cli_test.exs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index b79f8a4..ba1a5c4 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -32,5 +32,9 @@ defmodule OnigumoCLITest do test("run CLI with more than one argument") do assert_raise(MatchError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end + + test("run CLI with invalid switch") do + assert_raise(OptionParser.ParseError, fn -> Onigumo.CLI.main(["--help"]) end) + end end end From d6232f57d31413a25f6150fd6c1584652ee8d37c Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 15 Oct 2023 18:45:38 +0200 Subject: [PATCH 21/53] Name of test should be consistent --- test/onigumo_cli_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index ba1a5c4..da24245 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -21,7 +21,7 @@ defmodule OnigumoCLITest do Onigumo.CLI.main(["Downloader"]) end - test("run CLI with unknown argument") do + test("run CLI with invalid argument") do assert_raise(ArgumentError, fn -> Onigumo.CLI.main(["Uploader"]) end) end From 56acd648a98d805197cdd6f4c0d593ff12e724c8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 15 Oct 2023 19:01:35 +0200 Subject: [PATCH 22/53] Parse working_dir switch --- lib/cli.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0ea12a9..ad9fde3 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,8 +1,10 @@ defmodule Onigumo.CLI do def main(argv) do - {[], [component]} = OptionParser.parse!(argv, strict: []) + {parsed_switches, [component]} = OptionParser.parse!(argv, strict: [working_dir: :string]) module = Module.safe_concat("Onigumo", component) - root_path = File.cwd!() - module.main(root_path) + + parsed_switches + |> Keyword.get(:working_dir, File.cwd!()) + |> module.main end end From fffc5d32db229d1cda7e41bba1d90c183c3796c9 Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 21 Oct 2023 21:01:31 +0200 Subject: [PATCH 23/53] Use Map instead of Module.safe_concat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CLI argument doesn’t need to be tightly coupled to the implementation detail of components being Elixir modules. ArgumentError raised by Module.safe_concat was only a symptom of that. Replaced with a mapping of string arguments (component names) to Elixir modules. As a result MatchError is raised when unknown component is passed, instead of a rather cryptic ArgumentError from Module.safe_concat. --- lib/cli.ex | 6 +++++- test/onigumo_cli_test.exs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0ea12a9..1e2553a 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,7 +1,11 @@ defmodule Onigumo.CLI do + @components %{ + :Downloader => Onigumo.Downloader + } + def main(argv) do {[], [component]} = OptionParser.parse!(argv, strict: []) - module = Module.safe_concat("Onigumo", component) + {:ok, module} = Map.fetch(@components, String.to_atom(component)) root_path = File.cwd!() module.main(root_path) end diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index da24245..9a54cd9 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -22,7 +22,7 @@ defmodule OnigumoCLITest do end test("run CLI with invalid argument") do - assert_raise(ArgumentError, fn -> Onigumo.CLI.main(["Uploader"]) end) + assert_raise(MatchError, fn -> Onigumo.CLI.main(["Uploader"]) end) end test("run CLI with no arguments") do From 8f52b2314040cf3c8962e1a77446f8a8eaa86d2c Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 4 Nov 2023 19:33:28 +0100 Subject: [PATCH 24/53] Introduce Onigumo.Component behavior All components are going to have a common behavior. Both the CLI and a possible future Supervisor are going to treat components as blackboxes using their interface. --- lib/onigumo/component.ex | 4 ++++ lib/onigumo/downloader.ex | 1 + 2 files changed, 5 insertions(+) create mode 100644 lib/onigumo/component.ex diff --git a/lib/onigumo/component.ex b/lib/onigumo/component.ex new file mode 100644 index 0000000..f706af2 --- /dev/null +++ b/lib/onigumo/component.ex @@ -0,0 +1,4 @@ +defmodule Onigumo.Component do + @doc "Runs the component." + @callback main(root_path :: String.t()) :: :ok +end diff --git a/lib/onigumo/downloader.ex b/lib/onigumo/downloader.ex index fa0606b..1464489 100644 --- a/lib/onigumo/downloader.ex +++ b/lib/onigumo/downloader.ex @@ -2,6 +2,7 @@ defmodule Onigumo.Downloader do @moduledoc """ Web scraper """ + @behaviour Onigumo.Component def main(root_path) do http_client().start() From 76d8c2c1314889938ba824f78d886f9d3ffe9b32 Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 4 Nov 2023 19:36:03 +0100 Subject: [PATCH 25/53] Use @impl to mark behavior functions --- lib/onigumo/downloader.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/onigumo/downloader.ex b/lib/onigumo/downloader.ex index 1464489..326bb84 100644 --- a/lib/onigumo/downloader.ex +++ b/lib/onigumo/downloader.ex @@ -4,6 +4,7 @@ defmodule Onigumo.Downloader do """ @behaviour Onigumo.Component + @impl true def main(root_path) do http_client().start() From d2919a901273d9a5dd8d0c0a79f44f80a2b9f360 Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 4 Nov 2023 19:46:18 +0100 Subject: [PATCH 26/53] Declare @impl explicitly It is considered a good practice to mark the concrete behavior that a method belongs to. --- lib/onigumo/downloader.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onigumo/downloader.ex b/lib/onigumo/downloader.ex index 326bb84..bf9ddf7 100644 --- a/lib/onigumo/downloader.ex +++ b/lib/onigumo/downloader.ex @@ -4,7 +4,7 @@ defmodule Onigumo.Downloader do """ @behaviour Onigumo.Component - @impl true + @impl Onigumo.Component def main(root_path) do http_client().start() From 96031e8d1f7a45ab7063c85ffee7c536e29e72ba Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 4 Nov 2023 20:34:52 +0100 Subject: [PATCH 27/53] Change component name to lowercase Capitalized component names were only a remnant of camel-cased elixir modules. Now, with component names being separate, there is no need to follow this convention. Lower-case arguments are the easiest way to interact with an application. --- lib/cli.ex | 2 +- test/onigumo_cli_test.exs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 1e2553a..e9d0c8e 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,6 +1,6 @@ defmodule Onigumo.CLI do @components %{ - :Downloader => Onigumo.Downloader + :downloader => Onigumo.Downloader } def main(argv) do diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 9a54cd9..55d4f44 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -9,7 +9,7 @@ defmodule OnigumoCLITest do describe("Onigumo.CLI.main/1") do @tag :tmp_dir - test("run CLI with 'Downloader' argument", %{tmp_dir: tmp_dir}) do + test("run CLI with 'downloader' argument", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) @@ -18,7 +18,7 @@ defmodule OnigumoCLITest do input_file_content = InputSupport.url_list(@urls) File.write!(input_path_tmp, input_file_content) File.cd(tmp_dir) - Onigumo.CLI.main(["Downloader"]) + Onigumo.CLI.main(["downloader"]) end test("run CLI with invalid argument") do From 643c10c797f255fa4f32846d1cd1725b75ff1092 Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 4 Nov 2023 20:55:08 +0100 Subject: [PATCH 28/53] Parametrize invalid argument test Added more tests for an invalid component argument: * Downloader with a capital D to test that the component namae is case-sensitive and lowercase. * uploader to test a completely unknown component, but in a valid (lowercase) format. --- test/onigumo_cli_test.exs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 55d4f44..a8a193a 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -7,6 +7,11 @@ defmodule OnigumoCLITest do "http://onigumo.local/bye.html" ] + @invalid_arguments [ + "Downloader", + "uploader" + ] + describe("Onigumo.CLI.main/1") do @tag :tmp_dir test("run CLI with 'downloader' argument", %{tmp_dir: tmp_dir}) do @@ -21,8 +26,10 @@ defmodule OnigumoCLITest do Onigumo.CLI.main(["downloader"]) end - test("run CLI with invalid argument") do - assert_raise(MatchError, fn -> Onigumo.CLI.main(["Uploader"]) end) + for argument <- @invalid_arguments do + test("run CLI with invalid argument #{inspect(argument)}") do + assert_raise(MatchError, fn -> Onigumo.CLI.main([unquote(argument)]) end) + end end test("run CLI with no arguments") do From c364f58f7cddaec400a2daec3a3eb9d9fcd6c0a5 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 25 Nov 2023 22:18:14 +0100 Subject: [PATCH 29/53] Add usage message --- lib/cli.ex | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/cli.ex b/lib/cli.ex index e9d0c8e..36fbedb 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -8,5 +8,10 @@ defmodule Onigumo.CLI do {:ok, module} = Map.fetch(@components, String.to_atom(component)) root_path = File.cwd!() module.main(root_path) + + defp usage_message() do + IO.puts("Usage: ./onigumo [COMPONENT]\n") + IO.puts("\tSimple program that retrieve http web content in structured data.\n") + IO.puts("COMPONENT\tonigumo component to run, availables #{inspect Map.keys(@components)}") end end From 31d4f1ef033b8e41dab1c4c8a9defa5fcf105192 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 25 Nov 2023 22:19:21 +0100 Subject: [PATCH 30/53] Show usage message if user insert invalid cli arg --- lib/cli.ex | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 36fbedb..21914f7 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,14 +4,28 @@ defmodule Onigumo.CLI do } def main(argv) do - {[], [component]} = OptionParser.parse!(argv, strict: []) - {:ok, module} = Map.fetch(@components, String.to_atom(component)) - root_path = File.cwd!() - module.main(root_path) + try do + {[], [component]} = OptionParser.parse!(argv, strict: []) + component + rescue + _ in [OptionParser.ParseError, MatchError] -> usage_message() + else + component -> + {:ok, module} = Map.fetch(@components, String.to_atom(component)) + root_path = File.cwd!() + module.main(root_path) + end + end defp usage_message() do - IO.puts("Usage: ./onigumo [COMPONENT]\n") - IO.puts("\tSimple program that retrieve http web content in structured data.\n") - IO.puts("COMPONENT\tonigumo component to run, availables #{inspect Map.keys(@components)}") + components = Enum.join(Map.keys(@components), ", ") + + IO.puts(""" + Usage: onigumo [COMPONENT] + + Simple program that retrieves HTTP web content as structured data. + + COMPONENT\tOnigumo component to run, available: #{components} + """) end end From 58e365d6a2404b79e2d729ac70f2dc750e9ee72c Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 26 Jan 2024 19:16:46 +0100 Subject: [PATCH 31/53] Fix tests --- test/onigumo_cli_test.exs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index a8a193a..d388889 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -1,5 +1,6 @@ defmodule OnigumoCLITest do use ExUnit.Case + import ExUnit.CaptureIO import Mox @urls [ @@ -33,15 +34,20 @@ defmodule OnigumoCLITest do end test("run CLI with no arguments") do - assert_raise(MatchError, fn -> Onigumo.CLI.main([]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main([]) end) end test("run CLI with more than one argument") do - assert_raise(MatchError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end test("run CLI with invalid switch") do - assert_raise(OptionParser.ParseError, fn -> Onigumo.CLI.main(["--help"]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main(["--invalid"]) end) + end + + defp usage_message_printed?(function) do + output = capture_io(function) + String.starts_with?(output, "Usage: onigumo ") end end end From 7f9db4befc7c6c3b4e5b9c5b56a24f050270601d Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 24 Feb 2024 20:07:24 +0100 Subject: [PATCH 32/53] Bump ssl_verify_fun to 1.1.7 To fix mix compile on some machines. --- mix.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.lock b/mix.lock index 628ea6a..4ef7828 100644 --- a/mix.lock +++ b/mix.lock @@ -9,6 +9,6 @@ "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, "mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"}, "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, } From d6394fcc06421edfd58ecabda7cc48eee1ad35e1 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 24 Feb 2024 20:43:10 +0100 Subject: [PATCH 33/53] Do not rescue matchError for positional argument --- lib/cli.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 9033521..4703ce2 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -5,15 +5,17 @@ defmodule Onigumo.CLI do def main(argv) do try do - {[], [component], []} = OptionParser.parse(argv, strict: []) - component + {[], argv, []} = OptionParser.parse(argv, strict: []) + argv rescue MatchError -> usage_message() else - component -> + [component] -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) root_path = File.cwd!() module.main(root_path) + _ -> + usage_message() end end From 445b8675134b722d86a5a3b6fab102fa79ad0e2d Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 24 Feb 2024 21:16:39 +0100 Subject: [PATCH 34/53] Use case instead of MatchErrror Instead of pattern matching causing a MatchError, use case directly matching pattern for the happy path and for the invalid argument path. --- lib/cli.ex | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 4703ce2..53e2f6f 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,16 +4,12 @@ defmodule Onigumo.CLI do } def main(argv) do - try do - {[], argv, []} = OptionParser.parse(argv, strict: []) - argv - rescue - MatchError -> usage_message() - else - [component] -> + case OptionParser.parse(argv, strict: []) do + {[], [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) root_path = File.cwd!() module.main(root_path) + _ -> usage_message() end From 1963a9b22c9f5023bcd6d6c99b0cebe93b4eefc5 Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 24 Feb 2024 21:39:56 +0100 Subject: [PATCH 35/53] Upgrade dependencies Bumped all mix.lock dependencies to their newest versions. --- mix.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mix.lock b/mix.lock index 4ef7828..53afdb2 100644 --- a/mix.lock +++ b/mix.lock @@ -1,14 +1,14 @@ %{ - "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, - "floki": {:hex, :floki, "0.32.1", "dfe3b8db3b793939c264e6f785bca01753d17318d144bd44b407fb3493acaa87", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "d4b91c713e4a784a3f7b1e3cc016eefc619f6b1c3898464222867cafd3c681a3"}, - "hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"}, + "certifi": {:hex, :certifi, "2.12.0", "2d1cca2ec95f59643862af91f001478c9863c2ac9cb6e2f89780bfd8de987329", [:rebar3], [], "hexpm", "ee68d85df22e554040cdb4be100f33873ac6051387baf6a8f6ce82272340ff1c"}, + "floki": {:hex, :floki, "0.35.4", "cc947b446024732c07274ac656600c5c4dc014caa1f8fb2dfff93d275b83890d", [:mix], [], "hexpm", "27fa185d3469bd8fc5947ef0f8d5c4e47f0af02eb6b070b63c868f69e3af0204"}, + "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~>2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, "html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"}, "httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, - "mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"}, - "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, + "mox": {:hex, :mox, "1.1.0", "0f5e399649ce9ab7602f72e718305c0f9cdc351190f72844599545e4996af73c", [:mix], [], "hexpm", "d44474c50be02d5b72131070281a5d3895c0e7a95c780e90bc0cfe712f633a13"}, + "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, } From 3b30377856dce84f6891e070b16d075701d155a8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 24 Feb 2024 22:13:24 +0100 Subject: [PATCH 36/53] Parse a new switch --working-dir from CLI --working-dir is switch which allow user use different working directory than only current working directory --- lib/cli.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 53e2f6f..92af5fc 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,10 +4,10 @@ defmodule Onigumo.CLI do } def main(argv) do - case OptionParser.parse(argv, strict: []) do - {[], [component], []} -> + case OptionParser.parse(argv, strict: [working_dir: :string]) do + {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) - root_path = File.cwd!() + root_path = Keyword.get(parsed_switches, :working_dir, File.cwd!()) module.main(root_path) _ -> From b0a4b892157c5d255621926742c54cb79e310793 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 16 Mar 2024 15:39:15 +0100 Subject: [PATCH 37/53] Rename variable 'root_path' to 'working_dir' - working_dir is more precise naming for the intend purpose, - working_dir is also name of the switch we use on CLI --- lib/cli.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 92af5fc..21e6c8b 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -7,8 +7,8 @@ defmodule Onigumo.CLI do case OptionParser.parse(argv, strict: [working_dir: :string]) do {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) - root_path = Keyword.get(parsed_switches, :working_dir, File.cwd!()) - module.main(root_path) + working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) + module.main(working_dir) _ -> usage_message() From 4a11e4ecee3e7858bfa208450006583b3ca2cecf Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 16 Mar 2024 16:04:03 +0100 Subject: [PATCH 38/53] Update help message with usage of --working-dir --- lib/cli.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index 21e6c8b..245c162 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -19,11 +19,14 @@ defmodule Onigumo.CLI do components = Enum.join(Map.keys(@components), ", ") IO.puts(""" - Usage: onigumo [COMPONENT] + Usage: onigumo [OPTION]... [COMPONENT] Simple program that retrieves HTTP web content as structured data. COMPONENT\tOnigumo component to run, available: #{components} + + OPTIONS: + --working-dir=DIR\tChange to before processing remaining files """) end end From 02c0b2cf32114aec65ee1d7b91eccb4c609466c8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 16 Mar 2024 16:21:19 +0100 Subject: [PATCH 39/53] Add shorthand version for switch --working-dir --- lib/cli.ex | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 245c162..683940b 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,7 +4,11 @@ defmodule Onigumo.CLI do } def main(argv) do - case OptionParser.parse(argv, strict: [working_dir: :string]) do + case OptionParser.parse( + argv, + aliases: [C: :working_dir], + strict: [working_dir: :string] + ) do {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) @@ -26,7 +30,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - --working-dir=DIR\tChange to before processing remaining files + -C, --working-dir=DIR\tChange to before processing remaining files """) end end From c8d714d41fdf1eea956e6f3f476fdc908c749690 Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 6 Apr 2024 20:51:56 +0200 Subject: [PATCH 40/53] Mock Onigumo.Downloader in CLI tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Onigumo.Component is a behavior that can be mocked. CLI tests don’t verify behavior of concrete components. Defined a mock for the Downlaoader components so the CLI tests only verifies its call, not its behavior. Defined the expect mock to return the passed working directory to test it’s set properly. --- config/dev.exs | 1 + config/test.exs | 1 + lib/cli.ex | 2 +- test/onigumo_cli_test.exs | 4 +++- test/test_helper.exs | 1 + 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index 4a906a5..82fae0c 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,3 +1,4 @@ import Config config(:onigumo, :http_client, HTTPoison) +config(:onigumo, :downloader, Onigumo.Downloader) diff --git a/config/test.exs b/config/test.exs index acade56..995249c 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,3 +1,4 @@ import Config config(:onigumo, :http_client, HTTPoisonMock) +config(:onigumo, :downloader, OnigumoDownloaderMock) diff --git a/lib/cli.ex b/lib/cli.ex index 53e2f6f..0e91033 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,6 +1,6 @@ defmodule Onigumo.CLI do @components %{ - :downloader => Onigumo.Downloader + :downloader => Application.compile_env(:onigumo, :downloader) } def main(argv) do diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index d388889..4d05313 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -18,13 +18,15 @@ defmodule OnigumoCLITest do test("run CLI with 'downloader' argument", %{tmp_dir: tmp_dir}) do expect(HTTPoisonMock, :start, fn -> nil end) expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) input_path_env = Application.get_env(:onigumo, :input_path) input_path_tmp = Path.join(tmp_dir, input_path_env) input_file_content = InputSupport.url_list(@urls) File.write!(input_path_tmp, input_file_content) File.cd(tmp_dir) - Onigumo.CLI.main(["downloader"]) + + assert Onigumo.CLI.main(["downloader"]) == tmp_dir end for argument <- @invalid_arguments do diff --git a/test/test_helper.exs b/test/test_helper.exs index e6511e3..e16a0ae 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,3 +1,4 @@ ExUnit.start() Mox.defmock(HTTPoisonMock, for: HTTPoison.Base) +Mox.defmock(OnigumoDownloaderMock, for: Onigumo.Component) From b0b2f5031825a668707b072e63bf18bae097eb6d Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 6 Apr 2024 21:29:25 +0200 Subject: [PATCH 41/53] Test CLI default working dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Onigumo.Downloader is now mocked, thus it’s not necessary to verify its inner workings. Testing only its call, mocking the behavior, it’s no longer required to assume what’s happening inside the module and at the same time, it became possible to check the call arugments. The root_path argument can be now checked to be set to the current working directory. --- test/onigumo_cli_test.exs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 4d05313..fd9dd68 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -14,21 +14,6 @@ defmodule OnigumoCLITest do ] describe("Onigumo.CLI.main/1") do - @tag :tmp_dir - test("run CLI with 'downloader' argument", %{tmp_dir: tmp_dir}) do - expect(HTTPoisonMock, :start, fn -> nil end) - expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1) - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - - input_path_env = Application.get_env(:onigumo, :input_path) - input_path_tmp = Path.join(tmp_dir, input_path_env) - input_file_content = InputSupport.url_list(@urls) - File.write!(input_path_tmp, input_file_content) - File.cd(tmp_dir) - - assert Onigumo.CLI.main(["downloader"]) == tmp_dir - end - for argument <- @invalid_arguments do test("run CLI with invalid argument #{inspect(argument)}") do assert_raise(MatchError, fn -> Onigumo.CLI.main([unquote(argument)]) end) @@ -47,6 +32,14 @@ defmodule OnigumoCLITest do assert usage_message_printed?(fn -> Onigumo.CLI.main(["--invalid"]) end) end + @tag :tmp_dir + test("run CLI with 'downloader' argument passing cwd", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + + File.cd(tmp_dir) + assert Onigumo.CLI.main(["downloader"]) == tmp_dir + end + defp usage_message_printed?(function) do output = capture_io(function) String.starts_with?(output, "Usage: onigumo ") From 945287220f3414b629814288220be288e5b5a615 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 6 Apr 2024 22:25:02 +0200 Subject: [PATCH 42/53] Add tests for working_dir switch usage --- test/onigumo_cli_test.exs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index fd9dd68..9d1cfb8 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,6 +13,11 @@ defmodule OnigumoCLITest do "uploader" ] + @invalid_switches [ + "--invalid", + "-c" + ] + describe("Onigumo.CLI.main/1") do for argument <- @invalid_arguments do test("run CLI with invalid argument #{inspect(argument)}") do @@ -28,8 +33,10 @@ defmodule OnigumoCLITest do assert usage_message_printed?(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end - test("run CLI with invalid switch") do - assert usage_message_printed?(fn -> Onigumo.CLI.main(["--invalid"]) end) + for switch <- @invalid_switches do + test("run CLI with invalid switch #{switch}") do + assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) + end end @tag :tmp_dir @@ -40,6 +47,20 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader"]) == tmp_dir end + @tag :tmp_dir + test("run CLI 'downloader' with '--working-dir' switch", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + + assert Onigumo.CLI.main(["downloader", "--working-dir", tmp_dir]) == tmp_dir + end + + @tag :tmp_dir + test("run CLI 'downloader' with '-C' switch", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + + assert Onigumo.CLI.main(["downloader", "-C", tmp_dir]) == tmp_dir + end + defp usage_message_printed?(function) do output = capture_io(function) String.starts_with?(output, "Usage: onigumo ") From 56834021d22c0d07b1c6da6f99951bb019eac93c Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 6 Apr 2024 22:41:02 +0200 Subject: [PATCH 43/53] Remove unused @urls @urls module variable was only needed for the actual downloader workings. Now it being mocked, the acutual URLs are irrelevant. --- test/onigumo_cli_test.exs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index fd9dd68..abf9d96 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -3,11 +3,6 @@ defmodule OnigumoCLITest do import ExUnit.CaptureIO import Mox - @urls [ - "http://onigumo.local/hello.html", - "http://onigumo.local/bye.html" - ] - @invalid_arguments [ "Downloader", "uploader" From 0988b1305028d223471de2e09264c8e3e6217690 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 15:51:33 +0200 Subject: [PATCH 44/53] Improve usage message for working_dir switch --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index c48e588..cdc5a0a 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -30,7 +30,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - -C, --working-dir=DIR\tChange to before processing remaining files + -C, --working-dir \tChange to before running """) end end From a5e9edc1a14a50199756f3da7963176f518bf2cc Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:18:54 +0200 Subject: [PATCH 45/53] Parametrize tests for working_dir switch with long and short --- test/onigumo_cli_test.exs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index f6686ee..e1e5b88 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,6 +13,11 @@ defmodule OnigumoCLITest do "-c" ] + @working_dir_switches [ + "--working-dir", + "-C" + ] + describe("Onigumo.CLI.main/1") do for argument <- @invalid_arguments do test("run CLI with invalid argument #{inspect(argument)}") do @@ -42,18 +47,14 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader"]) == tmp_dir end - @tag :tmp_dir - test("run CLI 'downloader' with '--working-dir' switch", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - assert Onigumo.CLI.main(["downloader", "--working-dir", tmp_dir]) == tmp_dir - end + for switch <- @working_dir_switches do + @tag :tmp_dir + test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - @tag :tmp_dir - test("run CLI 'downloader' with '-C' switch", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - - assert Onigumo.CLI.main(["downloader", "-C", tmp_dir]) == tmp_dir + assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir + end end defp usage_message_printed?(function) do From 9fbdbafda1b845f56d04126ad2129b043f7bf6b6 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:20:14 +0200 Subject: [PATCH 46/53] Be consistent with naming of variables in tests with the code --- test/onigumo_cli_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index e1e5b88..1467d36 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -41,7 +41,7 @@ defmodule OnigumoCLITest do @tag :tmp_dir test("run CLI with 'downloader' argument passing cwd", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) File.cd(tmp_dir) assert Onigumo.CLI.main(["downloader"]) == tmp_dir @@ -51,7 +51,7 @@ defmodule OnigumoCLITest do for switch <- @working_dir_switches do @tag :tmp_dir test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir end From c73a42fa5d36cb39890a58850f23eeb207237ca8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:21:58 +0200 Subject: [PATCH 47/53] Use inspect to print string in all descriptions of tests --- test/onigumo_cli_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 1467d36..5ae87cb 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -34,7 +34,7 @@ defmodule OnigumoCLITest do end for switch <- @invalid_switches do - test("run CLI with invalid switch #{switch}") do + test("run CLI with invalid switch #{inspect(switch)}") do assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) end end From 47e06ed305aef9a2270ff3d3ba81cd067c1670ef Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:35:06 +0200 Subject: [PATCH 48/53] Test switch --working-dir without value --- test/onigumo_cli_test.exs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 5ae87cb..3b9dbd4 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -57,6 +57,12 @@ defmodule OnigumoCLITest do end end + test("run CLI 'downloader' with '--working-dir' without any value") do + assert usage_message_printed?( + fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end + ) + end + defp usage_message_printed?(function) do output = capture_io(function) String.starts_with?(output, "Usage: onigumo ") From b3c5bf34346d875e3c2ed5dee116fbe3154bb338 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:51:14 +0200 Subject: [PATCH 49/53] Test switch --working-dir with value '.' --- test/onigumo_cli_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 3b9dbd4..2b00099 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -61,6 +61,13 @@ defmodule OnigumoCLITest do assert usage_message_printed?( fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end ) + + @tag :tmp_dir + test("run CLI 'downloader' with '--working-dir' and value '.'", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) + + File.cd(tmp_dir) + assert Onigumo.CLI.main(["downloader", "--working-dir", "."]) == "." end defp usage_message_printed?(function) do From f883c28bb7929f26325d83ee5015f0ad5331c40d Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:51:43 +0200 Subject: [PATCH 50/53] Fix formatting --- test/onigumo_cli_test.exs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 2b00099..67e4fee 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -47,7 +47,6 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader"]) == tmp_dir end - for switch <- @working_dir_switches do @tag :tmp_dir test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do @@ -58,9 +57,8 @@ defmodule OnigumoCLITest do end test("run CLI 'downloader' with '--working-dir' without any value") do - assert usage_message_printed?( - fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end - ) + assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end) + end @tag :tmp_dir test("run CLI 'downloader' with '--working-dir' and value '.'", %{tmp_dir: tmp_dir}) do From e57bfa706f83a16f119a86c0209d393fc53d1756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20=C5=A0troch?= <45515588+nappex@users.noreply.github.com> Date: Fri, 10 May 2024 19:07:23 +0200 Subject: [PATCH 51/53] Update CLI help message for working dir Co-authored-by: Glutexo --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index cdc5a0a..6a76a11 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -30,7 +30,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - -C, --working-dir \tChange to before running + -C, --working-dir \tChange working dir to before running """) end end From e227fb2855c3da61260660c9fc5e83de213755dd Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 10 May 2024 19:21:36 +0200 Subject: [PATCH 52/53] Parametrize all tests with working-dir switch --- test/onigumo_cli_test.exs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 67e4fee..d500cff 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -54,18 +54,18 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir end - end - test("run CLI 'downloader' with '--working-dir' without any value") do - assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end) - end + test("run CLI 'downloader' with #{inspect(switch)} without any value") do + assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", unquote(switch)]) end) + end - @tag :tmp_dir - test("run CLI 'downloader' with '--working-dir' and value '.'", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) + @tag :tmp_dir + test("run CLI 'downloader' with #{inspect(switch)} and value '.'", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) - File.cd(tmp_dir) - assert Onigumo.CLI.main(["downloader", "--working-dir", "."]) == "." + File.cd(tmp_dir) + assert Onigumo.CLI.main(["downloader", unquote(switch), "."]) == "." + end end defp usage_message_printed?(function) do From 4120a34026927c9752a771ae98250615ace61734 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 10 May 2024 19:26:22 +0200 Subject: [PATCH 53/53] Remove test with no additional value --- test/onigumo_cli_test.exs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index d500cff..c0d51ba 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -58,14 +58,6 @@ defmodule OnigumoCLITest do test("run CLI 'downloader' with #{inspect(switch)} without any value") do assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", unquote(switch)]) end) end - - @tag :tmp_dir - test("run CLI 'downloader' with #{inspect(switch)} and value '.'", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) - - File.cd(tmp_dir) - assert Onigumo.CLI.main(["downloader", unquote(switch), "."]) == "." - end end defp usage_message_printed?(function) do