Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse working_dir switch #195

Merged
merged 24 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3d4cca5
Parse switches output and component from CLI
nappex Jun 9, 2023
bec0fe6
Fix formatting
nappex Jun 9, 2023
6d4929d
Merge branch 'master' into cli-work-dir
nappex Oct 3, 2023
56acd64
Parse working_dir switch
nappex Oct 15, 2023
31ff6d7
Merge branch 'master' into cli-work-dir
nappex Feb 24, 2024
3b30377
Parse a new switch --working-dir from CLI
nappex Feb 24, 2024
4e5fa37
Merge branch 'master' into parse-working-dir
Glutexo Mar 16, 2024
25c5b16
Merge branch 'cli-work-dir' into parse-working-dir
nappex Mar 16, 2024
b0a4b89
Rename variable 'root_path' to 'working_dir'
nappex Mar 16, 2024
4a11e4e
Update help message with usage of --working-dir
nappex Mar 16, 2024
02c0b2c
Add shorthand version for switch --working-dir
nappex Mar 16, 2024
e8467f0
Merge branch 'master' into parse-working-dir
nappex Apr 6, 2024
9452872
Add tests for working_dir switch usage
nappex Apr 6, 2024
d6a9b96
Merge branch 'master' into parse-working-dir
nappex Apr 7, 2024
0988b13
Improve usage message for working_dir switch
nappex Apr 20, 2024
a5e9edc
Parametrize tests for working_dir switch with long and short
nappex Apr 20, 2024
9fbdbaf
Be consistent with naming of variables in tests with the code
nappex Apr 20, 2024
c73a42f
Use inspect to print string in all descriptions of tests
nappex Apr 20, 2024
47e06ed
Test switch --working-dir without value
nappex Apr 20, 2024
b3c5bf3
Test switch --working-dir with value '.'
nappex Apr 20, 2024
f883c28
Fix formatting
nappex Apr 20, 2024
e57bfa7
Update CLI help message for working dir
nappex May 10, 2024
e227fb2
Parametrize all tests with working-dir switch
nappex May 10, 2024
4120a34
Remove test with no additional value
nappex May 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
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!())
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
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"
]
Glutexo marked this conversation as resolved.
Show resolved Hide resolved

@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
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
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
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
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

Glutexo marked this conversation as resolved.
Show resolved Hide resolved
defp usage_message_printed?(function) do
output = capture_io(function)
String.starts_with?(output, "Usage: onigumo ")
Expand Down
Loading