Skip to content

Commit

Permalink
Merge branch 'master' into parse-working-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutexo committed Mar 16, 2024
2 parents 56acd64 + 32ec35a commit 81546fb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
30 changes: 25 additions & 5 deletions lib/cli.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
defmodule Onigumo.CLI do
@components %{
:downloader => Onigumo.Downloader
}

def main(argv) do
{parsed_switches, [component]} = OptionParser.parse!(argv, strict: [working_dir: :string])
module = Module.safe_concat("Onigumo", component)
case OptionParser.parse(argv, strict: []) do
{parsed_switches, [component], [working_dir: :string]} ->
{:ok, module} = Map.fetch(@components, String.to_atom(component))
parsed_switches
|> Keyword.get(:working_dir, File.cwd!())
|> module.main

_ ->
usage_message()
end
end

defp usage_message() do
components = Enum.join(Map.keys(@components), ", ")

IO.puts("""
Usage: onigumo [COMPONENT]
Simple program that retrieves HTTP web content as structured data.
parsed_switches
|> Keyword.get(:working_dir, File.cwd!())
|> module.main
COMPONENT\tOnigumo component to run, available: #{components}
""")
end
end
4 changes: 4 additions & 0 deletions lib/onigumo/component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Onigumo.Component do
@doc "Runs the component."
@callback main(root_path :: String.t()) :: :ok
end
2 changes: 2 additions & 0 deletions lib/onigumo/downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ defmodule Onigumo.Downloader do
@moduledoc """
Web scraper
"""
@behaviour Onigumo.Component

@impl Onigumo.Component
def main(root_path) do
http_client().start()

Expand Down
12 changes: 6 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -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"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"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"},
}
27 changes: 20 additions & 7 deletions test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
defmodule OnigumoCLITest do
use ExUnit.Case
import ExUnit.CaptureIO
import Mox

@urls [
"http://onigumo.local/hello.html",
"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
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)

Expand All @@ -18,23 +24,30 @@ 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
assert_raise(ArgumentError, fn -> Onigumo.CLI.main(["Uploader"]) end)
for argument <- @invalid_arguments do
test("run CLI with invalid argument #{inspect(argument)}") do

Check failure on line 31 in test/onigumo_cli_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test Onigumo.CLI.main/1 run CLI with invalid argument "Downloader" (OnigumoCLITest)

Check failure on line 31 in test/onigumo_cli_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test Onigumo.CLI.main/1 run CLI with invalid argument "uploader" (OnigumoCLITest)
assert_raise(MatchError, fn -> Onigumo.CLI.main([unquote(argument)]) end)
end
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

0 comments on commit 81546fb

Please sign in to comment.