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

Introduce a Parser module stub #146

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import Config

config(:onigumo, :http_client, HTTPoison)
config(:onigumo, :downloader, Onigumo.Downloader)
config(:onigumo, :parser, Onigumo.Parser)
3 changes: 2 additions & 1 deletion lib/cli.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Onigumo.CLI do
@components %{
:downloader => Application.compile_env(:onigumo, :downloader)
:downloader => Application.compile_env(:onigumo, :downloader),
:parser => Application.compile_env(:onigumo, :parser)
}

def main(argv) do
Expand Down
26 changes: 26 additions & 0 deletions lib/onigumo/parser.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Onigumo.Parser do
@moduledoc """
Web scraper
"""
@behaviour Onigumo.Component

@impl Onigumo.Component
def main(root_path) do
root_path
|> list_downloaded()
|> Enum.map(&IO.puts(&1))

:ok
end

def list_downloaded(path) do
path
|> File.ls!()
|> Enum.filter(&is_downloaded(&1))
end

def is_downloaded(path) do
suffix = Application.get_env(:onigumo, :downloaded_suffix)
Path.extname(path) == suffix
end
end
72 changes: 72 additions & 0 deletions test/onigumo_parser_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
defmodule OnigumoParserTest do
use ExUnit.Case
import Mox

@suffixes [
".json",
""
]

@files [
{[], []},
{["first", "second"], []},
{["first"], ["second"]},
{[], ["first", "second"]},
]

setup(:verify_on_exit!)

describe("Onigumo.Parser.main/1") do
@tag :tmp_dir
test("run Parser", %{tmp_dir: tmp_dir}) do

Check failure on line 21 in test/onigumo_parser_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test Onigumo.Parser.main/1 run Parser (OnigumoParserTest)
_result = Onigumo.Parser.main(tmp_dir)
assert(false) # TODO: Do!
end

@tag :tmp_dir
test("return :ok", %{tmp_dir: tmp_dir}) do
result = Onigumo.Parser.main(tmp_dir)
assert(result == :ok)
end
end

describe("Onigumo.Parser.list_downloaded/1") do
for {expected, unexpected} <- @files do
@tag :tmp_dir
test("list a directory expecting #{inspect(expected)} and not expecting #{inspect(unexpected)}", %{tmp_dir: tmp_dir}) do
suffix = Application.get_env(:onigumo, :downloaded_suffix)

expected = Enum.map(unquote(expected), fn file ->
file <> suffix
end)

Enum.map(expected ++ unquote(unexpected), fn file ->
path = Path.join(tmp_dir, file)
File.write!(path, "")
end)

result = Onigumo.Parser.list_downloaded(tmp_dir)
assert(MapSet.new(result) == MapSet.new(expected))
end
end
end

describe("Onigumo.Parser.is_downloaded/1") do
test("recognize a downloaded file") do
suffix = Application.get_env(:onigumo, :downloaded_suffix)
path = "/test_dir/test_file#{suffix}"

result = Onigumo.Parser.is_downloaded(path)
assert(result == true)
end

for suffix <- @suffixes do
test("do not recognize another file #{inspect(suffix)}") do
path = "/test_dir/test_file#{unquote(suffix)}"

result = Onigumo.Parser.is_downloaded(path)
assert(result == false)
end
end
end
end
Loading