From 01254f2f901c62df45a7e85821560e1aaa164579 Mon Sep 17 00:00:00 2001 From: Ramon Snir Date: Sat, 18 Jul 2015 14:24:15 +0300 Subject: [PATCH] add Geef.Config module for Elixir --- lib/geef.ex | 17 ++++++++++------- lib/geef/config.ex | 25 +++++++++++++++++++++++++ lib/geef/repository.ex | 2 ++ src/geef_config.erl | 9 ++++++++- test/config_test.exs | 30 ++++++++++++++++++++++++++++++ test/test_helper.exs | 2 +- 6 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 lib/geef/config.ex create mode 100644 test/config_test.exs diff --git a/lib/geef.ex b/lib/geef.ex index bfd3017..90745b5 100644 --- a/lib/geef.ex +++ b/lib/geef.ex @@ -2,17 +2,20 @@ defmodule Geef do defmacro __using__(_) do quote do - alias Geef.Repository + alias Geef.Blob + alias Geef.Commit + alias Geef.Config + alias Geef.Index + alias Geef.Index.Tree + alias Geef.Object alias Geef.Odb alias Geef.Oid alias Geef.Reference - alias Geef.Object - alias Geef.Commit - alias Geef.Tree - alias Geef.TreeEntry - alias Geef.Blob - alias Geef.Tag + alias Geef.Repository alias Geef.Signature + alias Geef.Tree + alias Geef.Tag + alias Geef.TreeEntry end end diff --git a/lib/geef/config.ex b/lib/geef/config.ex new file mode 100644 index 0000000..fb5a236 --- /dev/null +++ b/lib/geef/config.ex @@ -0,0 +1,25 @@ +defmodule Geef.Config do + import Geef + alias Geef.Repository + + def stop(config), do: :geef_config.stop(config) + + def open(repo) when is_pid(repo), do: Repository.config(repo) + def open(path), do: :geef_config.open(path) + def open!(arg), do: open(arg) |> assert_ok + + def set(config, key, value) do + :geef_config.set(config, make_config_key(key), value) + end + + def get_bool(config, key) do + :geef_config.get_bool(config, make_config_key(key)) + end + + def get_string(config, key) do + :geef_config.get_string(config, make_config_key(key)) + end + + defp make_config_key(key) when is_binary(key), do: String.to_char_list(key) + defp make_config_key(key), do: key +end diff --git a/lib/geef/repository.ex b/lib/geef/repository.ex index 91000bc..ef6b189 100644 --- a/lib/geef/repository.ex +++ b/lib/geef/repository.ex @@ -15,7 +15,9 @@ defmodule Geef.Repository do def discover(path), do: :geef_repo.discover(path) def bare?(repo), do: :geef_repo.is_bare(repo) + def gitdir(repo), do: :geef_repo.path(repo) def workdir(repo), do: :geef_repo.workdir(repo) + def config(repo), do: :geef_repo.config(repo) def reference_names(repo), do: :geef_repo.references(repo) end diff --git a/src/geef_config.erl b/src/geef_config.erl index ff13767..e60831d 100644 --- a/src/geef_config.erl +++ b/src/geef_config.erl @@ -16,6 +16,7 @@ -export([set/3]). -export([get_bool/2]). -export([get_string/2]). +-export([stop/1]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -60,6 +61,9 @@ get_bool(Pid, Name) -> get_string(Pid, Name) -> gen_server:call(Pid, {get_string, Name}). +stop(Pid) -> + gen_server:call(Pid, stop). + %%%=================================================================== %%% gen_server callbacks %%%=================================================================== @@ -96,7 +100,10 @@ handle_call({set_string, Name, Val}, _From, State = #state{handle=Handle}) -> handle_call({get_string, Name}, _From, State = #state{handle=Handle}) -> Reply = geef_nif:config_get_string(Handle, Name), - {reply, Reply, State}. + {reply, Reply, State}; + +handle_call(stop, _From, State) -> + {stop, normal, ok, State}. %%-------------------------------------------------------------------- %% @private diff --git a/test/config_test.exs b/test/config_test.exs new file mode 100644 index 0000000..f12511b --- /dev/null +++ b/test/config_test.exs @@ -0,0 +1,30 @@ +defmodule ConfigTest do + use ExUnit.Case + use Geef + import RepoHelpers + + + setup do + {repo, path} = tmp_bare + Process.link(repo) + {:ok, config} = Repository.config(repo) + on_exit(fn -> File.rm_rf!(path) end) + {:ok, [config: config, path: path]} + end + + test "write and get a boolean", meta do + config = meta[:config] + var = "core.logallrefupdates" + assert :ok = Config.set(config, var, true) + assert {:ok, true} = Config.get_bool(config, var) + end + + test "write and get a string", meta do + config = meta[:config] + var = "user.name" + val = "Random J. Hacker" + assert :ok = Config.set(config, var, val) + assert {:ok, ^val} = Config.get_string(config, var) + end + +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 19b1813..0217d3c 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -2,7 +2,7 @@ ExUnit.start defmodule RepoHelpers do def tmp_bare do - {a, b, c} = :erlang.now() + {a, b, c} = :erlang.timestamp() n = node() dir = :io_lib.format("geef-~p~p~p~p.git", [n, a, b, c]) path = Path.join(System.tmp_dir!, dir)