diff --git a/lib/http_server_fixture/data/test-data.json b/lib/http_server_fixture/data/test-data.json deleted file mode 100644 index faf97bb..0000000 --- a/lib/http_server_fixture/data/test-data.json +++ /dev/null @@ -1 +0,0 @@ -{"1":{"task":"a new task"},"2":{"task":"a new task"},"3":{"task":"a new task"}} \ No newline at end of file diff --git a/lib/http_server_test_fixture/mock_data/test-data.json b/lib/http_server_test_fixture/mock_data/test-data.json deleted file mode 100644 index 5e47cbc..0000000 --- a/lib/http_server_test_fixture/mock_data/test-data.json +++ /dev/null @@ -1 +0,0 @@ -{"1":{"todo1":"Act"},"2":{"todo2":"Arrange"},"3":{"todo3":"Assert"}} \ No newline at end of file diff --git a/lib/to_do/db.ex b/lib/to_do/db.ex index afd45b6..b635079 100644 --- a/lib/to_do/db.ex +++ b/lib/to_do/db.ex @@ -1,7 +1,12 @@ defmodule ToDo.DB do @file_path Application.compile_env(:http_server, :file_path, "lib/to_do/data/to_dos.json") - def all, do: File.read!(@file_path) |> JSON.decode!() + def all do + case File.exists?(@file_path) do + true -> File.read!(@file_path) |> JSON.decode!() + false -> %{} + end + end def save(data), do: File.write(@file_path, JSON.encode!(data), [:read, :write]) diff --git a/test/http_server_spec/features/support/env.rb b/test/http_server_spec/features/support/env.rb index 50c31ab..a846ad2 100644 --- a/test/http_server_spec/features/support/env.rb +++ b/test/http_server_spec/features/support/env.rb @@ -6,3 +6,10 @@ HOSTNAME = yaml["server"]["hostname"] PORT = yaml["server"]["port"] PROTOCOL = yaml["server"]["protocol"] + +Spinach.hooks.after_run do |status| + pn = File.expand_path('../../../../lib/http_server_fixture/data/test-data.json', __dir__) + File.delete(pn) if File.exist?(pn) + file_deleted = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ntest-data file deleted\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + puts file_deleted unless File.exist?(pn) +end diff --git a/test/http_to_do_test.exs b/test/http_to_do_test.exs index a1c2b04..7d1c8b0 100644 --- a/test/http_to_do_test.exs +++ b/test/http_to_do_test.exs @@ -1,6 +1,6 @@ defmodule HTTPServerTest.ToDo do alias ToDo.API - use ExUnit.Case, async: true + use ExUnit.Case, async: false doctest HTTPServer @file_path Application.compile_env( @@ -11,7 +11,9 @@ defmodule HTTPServerTest.ToDo do setup do act_arrange_test_todo = JSON.encode!(%{"1" => %{"todo1" => "Act"}, "2" => %{"todo2" => "Arrange"}}) - File.write!(@file_path, act_arrange_test_todo) + File.write(@file_path, act_arrange_test_todo) + on_exit(fn -> if File.exists?(@file_path), do: File.rm!(@file_path) end) + :ok end test "Can write \"{\"todo3\":\"Assert\"}\" to data file without overwriting previous data" do