Skip to content
This repository has been archived by the owner on Mar 25, 2023. It is now read-only.

Commit

Permalink
add additional GitMock functions (add & commit) for #28
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed May 26, 2022
1 parent 4bb3624 commit ed45fa5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
40 changes: 39 additions & 1 deletion lib/git_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ defmodule Gitea.GitMock do
if String.contains?(url, "error") do
{:error, %Git.Error{message: "git clone error (mock)"}}
else
{:ok, %Git.Repository{path: Gitea.Helpers.local_repo_path("test-org", "test-repo")}}
# copy the contents of /test-repo into /tmp/test-repo when mock:true
path = Gitea.Helpers.local_repo_path("test-org", "test-repo")
submodule = Path.join([File.cwd!(), "test-repo"]) |> Path.expand()
# Useful.empty_dir_contents(Envar.get("GIT_TEMP_DIR_PATH"))
File.mkdir_p(path)
File.cp_r(Path.join([submodule, ".git"]), path)
File.copy(Path.join([submodule, "README.md"]), path)
{:ok, %Git.Repository{path: path}}
end
end
end
Expand All @@ -52,4 +59,35 @@ defmodule Gitea.GitMock do
repo_name = Gitea.Helpers.get_repo_name_from_url(repo_path <> ".git")
{:ok, "To ssh://gitea-server.fly.dev:10022/myorg/#{repo_name}.git\n"}
end

@doc """
`commit/2` (mock) simulate a commit and always returns {:ok, commit_info}
## Examples
iex> GitMock.commit("my-repo", ["any", "args"])
{:ok, "[master dc5b0d4] test msg\n Author: Al Ex <[email protected]>\n 1 file changed, 1 insertion(+)\n"}
"""
@spec commit(Git.Repository.t(), [any]) :: {:ok, any}
def commit(repo, _args) do
IO.inspect(repo)

if String.contains?(repo.path, "error") do
{:error, %Git.Error{message: "git clone error (mock)"}}
else
{:ok,
"[master dc5b0d4] test msg\n Author: Al Ex <[email protected]>\n 1 file changed, 1 insertion(+)\n"}
end
end

@doc """
`add/2` (mock) simulate a commit and always returns {:ok, any}
## Examples
iex> GitMock.add("my-repo", ["."])
{:ok, "any string"}
"""
@spec add(String.t(), [any]) :: {:ok, any}
def add(_repo_path, _args) do
{:ok, "totes always works"}
end
end
4 changes: 2 additions & 2 deletions lib/gitea.ex
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ defmodule Gitea do
def commit(org_name, repo_name, params) do
repo = local_git_repo(org_name, repo_name)

with {:ok, _output} <- Git.add(repo, ["."]),
with {:ok, _output} <- inject_git().add(repo, ["."]),
{:ok, commit_output} <-
Git.commit(repo, [
inject_git().commit(repo, [
"-m",
params.message,
~s(--author="#{params.full_name} <#{params.email}>")
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule Gitea.Helpers do
def local_repo_path(org, repo) do
# coveralls-ignore-start
if @mock do
if String.contains?(repo, "no-repo") do
if String.contains?(repo, "no-repo") || String.contains?(repo, "error") do
# in branch test we need to simulate a full path not a test-repo one ...
Path.join([temp_dir(git_dir()), org, repo]) |> Path.expand()
else
Expand Down
2 changes: 2 additions & 0 deletions test/gitea_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ defmodule GiteaTest do
org_name = "myorg"
repo_name = create_test_git_repo(org_name)
file_name = "README.md"

text = "text #{repo_name}"

assert {:ok, _path} = Gitea.local_file_write_text(org_name, repo_name, file_name, text)
Expand Down Expand Up @@ -201,6 +202,7 @@ defmodule GiteaTest do
email: "[email protected]"
})

IO.inspect(msg)
assert String.contains?(msg, "test msg")
assert String.contains?(msg, "1 file changed, 1 insertion(+)")

Expand Down

0 comments on commit ed45fa5

Please sign in to comment.