From 6ea694b7594799c5d4aabeff432c37c1e09c4f70 Mon Sep 17 00:00:00 2001 From: woops Date: Sat, 5 Mar 2022 16:26:22 +0100 Subject: [PATCH 1/6] raise File.Error when file does not exist in from_file!/2 --- lib/k8s/resource.ex | 3 ++- test/k8s/resource_test.exs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/k8s/resource.ex b/lib/k8s/resource.ex index 595fb522..d6a6d1e6 100644 --- a/lib/k8s/resource.ex +++ b/lib/k8s/resource.ex @@ -141,8 +141,9 @@ defmodule K8s.Resource do """ @spec from_file!(binary, keyword | nil) :: map | no_return def from_file!(path, assigns \\ []) do - case from_file(path, assigns) do + case from_file(path, assigns) |> IO.inspect do {:ok, resource} -> resource + {:error, :enoent} -> raise File.Error {:error, error} -> raise error end end diff --git a/test/k8s/resource_test.exs b/test/k8s/resource_test.exs index b7c36642..0922b1b5 100644 --- a/test/k8s/resource_test.exs +++ b/test/k8s/resource_test.exs @@ -3,4 +3,13 @@ defmodule K8s.ResourceTest do doctest K8s.Resource doctest K8s.Resource.FieldAccessors doctest K8s.Resource.Utilization + + + describe "from_file!/2" do + test "not found" do + assert_raise File.Error, fn -> + K8s.Resource.from_file!("not_found.yaml", []) + end + end + end end From a29da3ef7ce76413aab2e6ada572ad2a0dc09151 Mon Sep 17 00:00:00 2001 From: woops Date: Sat, 5 Mar 2022 16:27:16 +0100 Subject: [PATCH 2/6] remove inspect --- lib/k8s/resource.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/k8s/resource.ex b/lib/k8s/resource.ex index d6a6d1e6..4d6a89bf 100644 --- a/lib/k8s/resource.ex +++ b/lib/k8s/resource.ex @@ -141,7 +141,7 @@ defmodule K8s.Resource do """ @spec from_file!(binary, keyword | nil) :: map | no_return def from_file!(path, assigns \\ []) do - case from_file(path, assigns) |> IO.inspect do + case from_file(path, assigns) do {:ok, resource} -> resource {:error, :enoent} -> raise File.Error {:error, error} -> raise error From 512878b30c80cd6e3c4184f6947369c14d70d141 Mon Sep 17 00:00:00 2001 From: woops Date: Sat, 5 Mar 2022 22:43:10 +0100 Subject: [PATCH 3/6] fix formatting --- lib/k8s/client/dynamic_http_provider.ex | 1 - test/k8s/resource_test.exs | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/k8s/client/dynamic_http_provider.ex b/lib/k8s/client/dynamic_http_provider.ex index ec561c67..5d4bc12e 100644 --- a/lib/k8s/client/dynamic_http_provider.ex +++ b/lib/k8s/client/dynamic_http_provider.ex @@ -29,7 +29,6 @@ defmodule K8s.Client.DynamicHTTPProvider do }} | {:ok, any} - @doc "See `K8s.Client.HTTPProvider.handle_response/1`" defdelegate handle_response(resp), to: K8s.Client.HTTPProvider diff --git a/test/k8s/resource_test.exs b/test/k8s/resource_test.exs index 0922b1b5..4208ef13 100644 --- a/test/k8s/resource_test.exs +++ b/test/k8s/resource_test.exs @@ -4,7 +4,6 @@ defmodule K8s.ResourceTest do doctest K8s.Resource.FieldAccessors doctest K8s.Resource.Utilization - describe "from_file!/2" do test "not found" do assert_raise File.Error, fn -> From 72c1a0c403511f7b95508553e0a475a92b2cc337 Mon Sep 17 00:00:00 2001 From: gpopides Date: Sun, 6 Mar 2022 20:30:31 +0100 Subject: [PATCH 4/6] return %File.Error{reason: reason} from render --- lib/k8s/resource.ex | 7 +++---- test/k8s/resource_test.exs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/k8s/resource.ex b/lib/k8s/resource.ex index 4d6a89bf..db4dfa3b 100644 --- a/lib/k8s/resource.ex +++ b/lib/k8s/resource.ex @@ -143,8 +143,7 @@ defmodule K8s.Resource do def from_file!(path, assigns \\ []) do case from_file(path, assigns) do {:ok, resource} -> resource - {:error, :enoent} -> raise File.Error - {:error, error} -> raise error + error -> raise error end end @@ -255,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 4208ef13..f4a5b029 100644 --- a/test/k8s/resource_test.exs +++ b/test/k8s/resource_test.exs @@ -4,11 +4,37 @@ defmodule K8s.ResourceTest do 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 From 9f9ea32e73a19d1ff347feb26c59cb61facdbb7c Mon Sep 17 00:00:00 2001 From: woops Date: Sat, 5 Mar 2022 22:43:10 +0100 Subject: [PATCH 5/6] fix formatting --- lib/k8s/client/dynamic_http_provider.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/k8s/client/dynamic_http_provider.ex b/lib/k8s/client/dynamic_http_provider.ex index ec561c67..5d4bc12e 100644 --- a/lib/k8s/client/dynamic_http_provider.ex +++ b/lib/k8s/client/dynamic_http_provider.ex @@ -29,7 +29,6 @@ defmodule K8s.Client.DynamicHTTPProvider do }} | {:ok, any} - @doc "See `K8s.Client.HTTPProvider.handle_response/1`" defdelegate handle_response(resp), to: K8s.Client.HTTPProvider From a2d95c4f25532d4405f07e5c615c89d9f89bc609 Mon Sep 17 00:00:00 2001 From: gpopides Date: Sun, 6 Mar 2022 22:44:00 +0100 Subject: [PATCH 6/6] delete unrelated spec after rebase --- lib/k8s/client/dynamic_http_provider.ex | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/k8s/client/dynamic_http_provider.ex b/lib/k8s/client/dynamic_http_provider.ex index 5d4bc12e..ead0f2ef 100644 --- a/lib/k8s/client/dynamic_http_provider.ex +++ b/lib/k8s/client/dynamic_http_provider.ex @@ -17,18 +17,6 @@ defmodule K8s.Client.DynamicHTTPProvider do defdelegate headers(method, request_options), to: K8s.Client.HTTPProvider @impl true - @spec handle_response( - {:error, HTTPoison.Error.t()} - | {:ok, - %{:__struct__ => HTTPoison.AsyncResponse | HTTPoison.Response, optional(any) => any}} - ) :: - {:error, - %{ - :__struct__ => HTTPoison.Error | HTTPoison.Response | K8s.Client.APIError, - optional(any) => any - }} - | {:ok, any} - @doc "See `K8s.Client.HTTPProvider.handle_response/1`" defdelegate handle_response(resp), to: K8s.Client.HTTPProvider