From 97ad9105d8d151db26839e59c465ea39b9fb59a6 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 1 Jan 2019 16:30:51 +0000 Subject: [PATCH] WIP: Stab at refactoring watcher for testability --- lib/watcher.ex | 21 +++++++++++++-------- mix.exs | 7 +++++-- mix.lock | 5 ++++- test/watcher_test.exs | 13 +++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 test/watcher_test.exs diff --git a/lib/watcher.ex b/lib/watcher.ex index 6839b55a..4a7591cd 100644 --- a/lib/watcher.ex +++ b/lib/watcher.ex @@ -1,26 +1,31 @@ defmodule Watcher do use GenServer - def start_link() do - GenServer.start_link(__MODULE__, dirs: ["lib/koans"]) + @default_arguments %{folder_to_watch: "lib/koans", handler: &Watcher.reload/1} + + def start_link(args \\ %{}) do + state = Map.merge(@default_arguments, args) + GenServer.start_link(__MODULE__, state) end - def init(args) do - {:ok, watcher_pid} = FileSystem.start_link(args) + def init(%{folder_to_watch: dirs} = args) do + {:ok, watcher_pid} = FileSystem.start_link([dirs: [dirs]]) FileSystem.subscribe(watcher_pid) - {:ok, %{watcher_pid: watcher_pid}} + IO.inspect(args) + {:ok, Map.merge(args, %{watcher_pid: watcher_pid})} end - def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do + def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid, handler: file_handler} = state) do + IO.inspect path, events # respond to renamed as well due to that some editors use temporary files for atomic writes (ex: TextMate) if Enum.member?(events, :modified) || Enum.member?(events, :renamed) do - path |> normalize |> reload + path |> normalize |> file_handler.() end {:noreply, state} end - defp reload(file) do + def reload(file) do if Path.extname(file) == ".ex" do try do file diff --git a/mix.exs b/mix.exs index ac553bb0..df249183 100644 --- a/mix.exs +++ b/mix.exs @@ -11,11 +11,14 @@ defmodule Koans.Mixfile do def application do [mod: {ElixirKoans, []}, - applications: [:file_system, :logger]] + applications: [:file_system, :logger, :briefly]] end defp deps do - [{:file_system, "~> 0.2"}] + [ + {:file_system, "~> 0.2"}, + {:briefly, "~> 0.3"} + ] end defp elixirc_path(:test), do: ["lib/", "test/support"] diff --git a/mix.lock b/mix.lock index d7a007ee..eabfd519 100644 --- a/mix.lock +++ b/mix.lock @@ -1 +1,4 @@ -%{"file_system": {:hex, :file_system, "0.2.2", "7f1e9de4746f4eb8a4ca8f2fbab582d84a4e40fa394cce7bfcb068b988625b06", [], [], "hexpm"}} +%{ + "briefly": {:hex, :briefly, "0.3.0", "16e6b76d2070ebc9cbd025fa85cf5dbaf52368c4bd896fb482b5a6b95a540c2f", [:mix], [], "hexpm"}, + "file_system": {:hex, :file_system, "0.2.2", "7f1e9de4746f4eb8a4ca8f2fbab582d84a4e40fa394cce7bfcb068b988625b06", [], [], "hexpm"}, +} diff --git a/test/watcher_test.exs b/test/watcher_test.exs new file mode 100644 index 00000000..67244696 --- /dev/null +++ b/test/watcher_test.exs @@ -0,0 +1,13 @@ +defmodule WatcherTest do + use ExUnit.Case + + test "watches for changes" do + parent = self() + {:ok, path} = Briefly.create(directory: true) + {:ok, pid} = Watcher.start_link(%{folder_to_watch: path, handler: fn(file) -> send parent, {:file, file} end}) + + File.write!(Path.join(path, "test.exs"), "Some Text") + + assert_receive {:file, "foo"}, 1_000 + end +end