Skip to content

Commit

Permalink
Merge pull request #159 from nappex/cli
Browse files Browse the repository at this point in the history
Add test to invoke onigumo from CLI
  • Loading branch information
Glutexo authored Oct 1, 2023
2 parents dc0b3fc + 63d2f12 commit a8425aa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 30 deletions.
13 changes: 11 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +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()
escript: escript(),
elixirc_paths: elixirc_paths(env)
]
end

Expand Down Expand Up @@ -37,4 +40,10 @@ defmodule Onigumo.MixProject do
main_module: Onigumo.CLI
]
end

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
24 changes: 24 additions & 0 deletions test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
@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.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.url_list(@urls)
File.write!(input_path_tmp, input_file_content)
File.cd(tmp_dir)
Onigumo.CLI.main(["Downloader"])
end
end
end
40 changes: 12 additions & 28 deletions test/onigumo_downloader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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), &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 = prepare_input(@urls)
input_file_content = InputSupport.url_list(@urls)
File.write!(input_path_tmp, input_file_content)

Onigumo.Downloader.main(tmp_dir)
Expand All @@ -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), &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 = prepare_input(@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()
Expand All @@ -46,36 +46,36 @@ 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!, &HttpSupport.response/1)

input_url = Enum.at(@urls, 0)
Onigumo.Downloader.download_url(input_url, tmp_dir)

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 = 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!, &prepare_response/1)
expect(HTTPoisonMock, :get!, &HttpSupport.response/1)

url = Enum.at(@urls, 0)
get_response = Onigumo.Downloader.get_url(url)
expected_response = prepare_response(url)
expected_response = HttpSupport.response(url)
assert(get_response == expected_response)
end
end

describe("Onigumo.Downloader.get_body/1") do
test("extract body from URL response") do
url = Enum.at(@urls, 0)
response = prepare_response(url)
response = HttpSupport.response(url)
get_body = Onigumo.Downloader.get_body(response)
expected_body = body(url)
expected_body = HttpSupport.body(url)
assert(get_body == expected_body)
end
end
Expand All @@ -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.url_list(input_urls)
File.write!(input_path_tmp, input_file_content)

loaded_urls = Onigumo.Downloader.load_urls(tmp_dir) |> Enum.to_list()
Expand All @@ -124,27 +124,11 @@ 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 = HttpSupport.body(url)
assert(read_output == expected_output)
end
end
12 changes: 12 additions & 0 deletions test/support/http.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule HttpSupport do
def response(url) do
%HTTPoison.Response{
status_code: 200,
body: body(url)
}
end

def body(url) do
"Body from: #{url}\n"
end
end
6 changes: 6 additions & 0 deletions test/support/input.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule InputSupport do
def url_list(urls) do
Enum.map(urls, &(&1 <> "\n"))
|> Enum.join()
end
end

0 comments on commit a8425aa

Please sign in to comment.