diff --git a/lib/k8s/resource.ex b/lib/k8s/resource.ex index 595fb522..db4dfa3b 100644 --- a/lib/k8s/resource.ex +++ b/lib/k8s/resource.ex @@ -143,7 +143,7 @@ defmodule K8s.Resource do def from_file!(path, assigns \\ []) do case from_file(path, assigns) do {:ok, resource} -> resource - {:error, error} -> raise error + error -> raise error end end @@ -254,8 +254,8 @@ defmodule K8s.Resource do rendered = EEx.eval_string(template, assigns) {:ok, rendered} - error -> - error + {:error, reason} -> + %File.Error{reason: reason} end end end diff --git a/test/k8s/resource_test.exs b/test/k8s/resource_test.exs index b7c36642..f4a5b029 100644 --- a/test/k8s/resource_test.exs +++ b/test/k8s/resource_test.exs @@ -3,4 +3,38 @@ defmodule K8s.ResourceTest do doctest K8s.Resource doctest K8s.Resource.FieldAccessors doctest K8s.Resource.Utilization + + @test_file "k8s_tmp_file" + @test_directory "k8s_tmp_dir" + + setup do + on_exit(fn -> + File.rm(@test_file) + File.rmdir(@test_directory) + end) + end + + describe "from_file!/2" do + test "not found" do + assert_raise File.Error, fn -> + K8s.Resource.from_file!("not_found.yaml", []) + end + end + + test "is directory" do + assert_raise File.Error, fn -> + File.mkdir(@test_directory) + K8s.Resource.from_file!("k8s_tmpdir", []) + end + end + + test "has no access to file" do + assert_raise File.Error, fn -> + File.touch(@test_file) + File.chmod(@test_file, 0o000) + + K8s.Resource.from_file!(@test_file, []) + end + end + end end