Skip to content

Commit

Permalink
Merge pull request #195 from nappex/parse-working-dir
Browse files Browse the repository at this point in the history
Parse working_dir switch
  • Loading branch information
Glutexo committed May 10, 2024
2 parents 96c2bd6 + 4120a34 commit 0d890b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
17 changes: 12 additions & 5 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ defmodule Onigumo.CLI do
}

def main(argv) do
case OptionParser.parse(argv, strict: []) do
{[], [component], []} ->
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))
root_path = File.cwd!()
module.main(root_path)
working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!())
module.main(working_dir)

_ ->
usage_message()
Expand All @@ -19,11 +23,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:
-C, --working-dir <dir>\tChange working dir to <dir> before running
""")
end
end
31 changes: 28 additions & 3 deletions test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ defmodule OnigumoCLITest do
"uploader"
]

@invalid_switches [
"--invalid",
"-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
Expand All @@ -23,18 +33,33 @@ 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 #{inspect(switch)}") do
assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end)
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)
expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end)

File.cd(tmp_dir)
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
expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end)

assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir
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
end

defp usage_message_printed?(function) do
output = capture_io(function)
String.starts_with?(output, "Usage: onigumo ")
Expand Down

0 comments on commit 0d890b9

Please sign in to comment.