Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor Actions to accept email for GitLab #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import Config
# config :logger, level: :info
#

config :git_pair,
storage: GitPair.Storage

config :git_pair,
command_runner: System

Expand Down
3 changes: 3 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ import Config

config :git_pair,
command_runner: GitPair.SystemMock

config :git_pair,
storage: GitPair.StorageMock
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
27 changes: 23 additions & 4 deletions lib/git_pair/actions.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule GitPair.Actions do
@git_config "config"
@key "pair.coauthor"

@key "pair"
@success_exit_status 0

@commit_msg_hook_content """
Expand All @@ -14,6 +13,8 @@ defmodule GitPair.Actions do

@commit_msg_hook_path "./.git/hooks/commit-msg"

alias GitPair.Storage

def init() do
File.mkdir_p!(Path.dirname(@commit_msg_hook_path))

Expand All @@ -33,10 +34,16 @@ defmodule GitPair.Actions do
end
end

def add([username, email]) do
{result, user_data} = storage().add([username, email])

output(result, "User #{user_data[:identifier]} (#{user_data[:email]}) added")
end

def add(username) do
result = command("--add", username)
{result, user_data} = storage().add(username)

output(result, "User #{username} added")
output(result, "User #{user_data[:identifier]} (#{user_data[:email]}) added")
end

def rm(username) do
Expand Down Expand Up @@ -89,6 +96,14 @@ defmodule GitPair.Actions do
|> Enum.join("\n"))
end

defp output(:ok, message) do
{:ok, message}
end

defp output(:error, _message) do
{:error, "Failed to execute command"}
end

defp output({"", @success_exit_status}, message) do
{:ok, message}
end
Expand Down Expand Up @@ -122,6 +137,10 @@ defmodule GitPair.Actions do
{:error, "Unsuported multiple users"}
end

def storage() do
Application.get_env(:git_pair, :storage, Storage)
end

def command_runner() do
Application.get_env(:git_pair, :command_runner, System)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/git_pair/behaviours/storage_behaviour.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule GitPair.StorageBehaviour do
@moduledoc false

@callback add(String.t()) :: {atom(), list()}
@callback add(list(String.t())) :: {atom(), list()}
end
28 changes: 28 additions & 0 deletions lib/storage.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule GitPair.Storage do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules should have a @moduledoc tag.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will do it later.

@git_config "config"
@key "pair"
@github_noreply_email "@users.noreply.github.com"

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

{:ok,
[
identifier: identifier,
email: email
]}
end

def add(identifier) do
add([identifier, identifier <> @github_noreply_email])
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
30 changes: 25 additions & 5 deletions test/actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@ defmodule GitPair.ActionsTest do
import Mox

alias GitPair.Actions
alias GitPair.StorageMock
wevtimoteo marked this conversation as resolved.
Show resolved Hide resolved
alias GitPair.SystemMock

setup :verify_on_exit!

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

{result, message} = Actions.add(["fake_user"])

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

test ".add calls git config add command passing identifier and email" do
expect(StorageMock, :add, fn [identifier, email] ->
{:ok,
[
identifier: identifier,
email: "[email protected]"
]}
end)

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

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

test ".rm calls git config unset command" do
Expand Down
62 changes: 62 additions & 0 deletions test/storage_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule GitPair.StorageTest do
use ExUnit.Case, async: true

import Mox

alias GitPair.Storage
alias GitPair.SystemMock

setup :verify_on_exit!

describe "add/1" do
test "store user with identifier with GitHub no-reply 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, user_data} = Storage.add("fake_user")

assert result == :ok

assert user_data == [
identifier: "fake_user",
email: "[email protected]"
]
end
end

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, user_data} = Storage.add(["fake_user", "[email protected]"])

assert result == :ok

assert user_data == [
identifier: "fake_user",
email: "[email protected]"
]
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Mox.defmock(GitPair.SystemMock, for: GitPair.SystemBehaviour)
Mox.defmock(GitPair.StorageMock, for: GitPair.StorageBehaviour)

ExUnit.start()