1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
defmodule Hex.Netrc.CacheTest do
use HexTest.Case, async: false
alias Hex.Netrc.Cache
setup do
# Restart Hex between tests to get a fresh cache.
Application.stop(:hex)
:ok = Application.start(:hex)
end
test "fetch/1 fails on non-existent file" do
in_tmp(fn ->
assert {:error, :enoent} = Cache.fetch(".netrc")
end)
end
test "fetch/1 remembers parse errors" do
in_tmp(fn ->
File.write!(".netrc", "")
assert {:error, :parse} = Cache.fetch(".netrc")
File.rm!(".netrc")
assert {:error, :parse} = Cache.fetch(".netrc")
end)
end
test "fetch/1 succeeds on simple file" do
in_tmp(fn ->
data = """
machine foo.example.com
login john
password bar
"""
parsed = %{
"foo.example.com" => %{
username: "john",
password: "bar"
}
}
File.write!(".netrc", data)
assert {:ok, ^parsed} = Cache.fetch(".netrc")
end)
end
test "fetch/1 remembers successful parses" do
in_tmp(fn ->
data = """
machine foo.example.com
login john
password bar
"""
parsed = %{
"foo.example.com" => %{
username: "john",
password: "bar"
}
}
File.write!(".netrc", data)
assert {:ok, ^parsed} = Cache.fetch(".netrc")
File.rm!(".netrc")
assert {:ok, ^parsed} = Cache.fetch(".netrc")
end)
end
end
|