Skip to content

Commit

Permalink
♻️ Introduce Storage to act as Git Config a façade
Browse files Browse the repository at this point in the history
Move Actions.add/1 to Storage abstraction
  • Loading branch information
Amadeu 🦄 Lindo 🌈 committed Sep 23, 2020
1 parent 708511f commit f84a7dc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/git_pair.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule GitPair do
Documentation for `GitPair`.
"""

@version Mix.Project.config[:version]
@version Mix.Project.config()[:version]

def version(), do: @version
end
5 changes: 0 additions & 5 deletions lib/git_pair/actions.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
defmodule GitPair.Actions do
@git_config "config"
@key "pair.coauthor"

@success_exit_status 0

@commit_msg_hook_content """
#!/bin/sh
set -e
Expand Down
20 changes: 20 additions & 0 deletions lib/storage.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule GitPair.Storage do
@git_config "config"
@key "pair"
@success_exit_status 0

def add([identifier, email]) do
run(["--add", "#{@key}.#{identifier}.identifier", identifier])
run(["--add", "#{@key}.#{identifier}.email", email])

{:ok, nil}
end

def run(command) do
runner().cmd("git", [@git_config | command])
end

def runner() do
Application.get_env(:git_pair, :command_runner, System)
end
end
13 changes: 12 additions & 1 deletion test/actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule GitPair.ActionsTest do

setup :verify_on_exit!

test ".add calls git config add command" do
test ".add calls git config add command passing username" do
expect(SystemMock, :cmd, fn _cmd, _options ->
{"", 0}
end)
Expand All @@ -19,6 +19,17 @@ defmodule GitPair.ActionsTest do
assert message == "User fake-user added"
end

test ".add calls git config add command passing identifier and email" do
expect(SystemMock, :cmd, fn _cmd, _options ->
{"", 0}
end)

{result, message} = Actions.add(["fake-user", "[email protected]"])

assert result == :ok
assert message == "User fake-user ([email protected]) added"
end

test ".rm calls git config unset command" do
expect(SystemMock, :cmd, fn _cmd, _options ->
{"", 0}
Expand Down
30 changes: 30 additions & 0 deletions test/storage_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule GitPair.StorageTest do
use ExUnit.Case, async: true

import Mox

alias GitPair.Storage
alias GitPair.SystemMock

setup :verify_on_exit!

describe "add/2" do
test "stores user with identifier and email" do
command_prefix = ["config", "--add"]
expect(SystemMock, :cmd, fn _cmd, options ->
assert options == command_prefix ++ ["pair.fake-user.identifier", "fake-user"]
{"", 0}
end)

expect(SystemMock, :cmd, fn _cmd, options ->
assert options == command_prefix ++ ["pair.fake-user.email", "[email protected]"]
{"", 0}
end)

{result, message} = Storage.add(["fake-user", "[email protected]"])

assert result == :ok
assert message == nil
end
end
end

0 comments on commit f84a7dc

Please sign in to comment.