Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Merge 1e84691 into 77153c6
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Oct 19, 2019
2 parents 77153c6 + 1e84691 commit f913039
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 57 deletions.
132 changes: 129 additions & 3 deletions test/support/test/on_disk_repo_test_case.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Xgit.Test.OnDiskRepoTestCase do
@moduledoc ~S"""
(Testing only) Test case that sets up a temporary directory with an on-disk repositort.
"""
@moduledoc false
# (Testing only) Test case that sets up a temporary directory with an on-disk repository.
use ExUnit.CaseTemplate

alias Xgit.Repository.OnDisk
Expand Down Expand Up @@ -39,4 +38,131 @@ defmodule Xgit.Test.OnDiskRepoTestCase do

Map.merge(context, %{xgit_path: xgit_path, xgit_repo: xgit_repo})
end

@doc ~S"""
Returns a pre-configured environment for a known author/committer ID and timestamp.
"""
@spec sample_commit_env() :: [{String.t(), String.t()}]
def sample_commit_env do
[
{"GIT_AUTHOR_DATE", "1142878449 +0230"},
{"GIT_COMMITTER_DATE", "1142878449 +0230"},
{"GIT_AUTHOR_EMAIL", "author@example.com"},
{"GIT_COMMITTER_EMAIL", "author@example.com"},
{"GIT_AUTHOR_NAME", "A. U. Thor"},
{"GIT_COMMITTER_NAME", "A. U. Thor"}
]
end

@doc ~S"""
Returns a context with an on-disk repository set up.
This repository has a tree object with one file in it.
Can optionally take a hard-wired path to use instead of the default
temporary directory. Use that when you need to debug a test that is
failing and you want to inspect the repo after the test completes.
"""
@spec setup_with_valid_tree!(path :: Path.t() | nil) :: %{
tmp_dir: Path.t(),
xgit_path: Path.t(),
xgit_repo: Repository.t(),
tree_id: binary()
}
def setup_with_valid_tree!(path \\ nil) do
%{xgit_path: xgit_path} = context = repo!(path)

test_content_path = Temp.path!()
File.write!(test_content_path, "test content\n")

{object_id_str, 0} =
System.cmd(
"git",
[
"hash-object",
"-w",
"--",
test_content_path
],
cd: xgit_path
)

object_id = String.trim(object_id_str)

{_output, 0} =
System.cmd(
"git",
[
"update-index",
"--add",
"--cacheinfo",
"100644",
object_id,
"test"
],
cd: xgit_path
)

{tree_id_str, 0} =
System.cmd(
"git",
[
"write-tree"
],
cd: xgit_path
)

tree_id = String.trim(tree_id_str)

Map.put(context, :tree_id, tree_id)
end

@doc ~S"""
Returns a context with an on-disk repository set up.
This repository has a tree object with one file in it and an
empty commit.
Can optionally take a hard-wired path to use instead of the default
temporary directory. Use that when you need to debug a test that is
failing and you want to inspect the repo after the test completes.
"""
@spec setup_with_valid_parent_commit!(path :: Path.t() | nil) :: %{
tmp_dir: Path.t(),
xgit_path: Path.t(),
xgit_repo: Repository.t(),
tree_id: String.t(),
parent_id: String.t()
}
def setup_with_valid_parent_commit!(path \\ nil) do
%{xgit_path: xgit_path} = context = setup_with_valid_tree!(path)

{empty_tree_id_str, 0} =
System.cmd(
"git",
[
"write-tree"
],
cd: xgit_path
)

empty_tree_id = String.trim(empty_tree_id_str)

{parent_id_str, 0} =
System.cmd(
"git",
[
"commit-tree",
"-m",
"empty",
empty_tree_id
],
cd: xgit_path,
env: sample_commit_env()
)

parent_id = String.trim(parent_id_str)

Map.put(context, :parent_id, parent_id)
end
end
58 changes: 4 additions & 54 deletions test/xgit/plumbing/commit_tree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ defmodule Xgit.Plumbing.CommitTreeTest do
alias Xgit.Core.Object
alias Xgit.Core.PersonIdent
alias Xgit.Plumbing.CommitTree
alias Xgit.Plumbing.HashObject
alias Xgit.Plumbing.UpdateIndex.CacheInfo
alias Xgit.Plumbing.WriteTree
alias Xgit.Repository
alias Xgit.Test.OnDiskRepoTestCase

import FolderDiff

import Xgit.Test.OnDiskRepoTestCase,
only: [setup_with_valid_tree!: 0, setup_with_valid_parent_commit!: 0]

describe "run/2" do
@valid_pi %PersonIdent{
name: "A. U. Thor",
Expand All @@ -20,14 +20,7 @@ defmodule Xgit.Plumbing.CommitTreeTest do
tz_offset: 150
}

@env [
{"GIT_AUTHOR_DATE", "1142878449 +0230"},
{"GIT_COMMITTER_DATE", "1142878449 +0230"},
{"GIT_AUTHOR_EMAIL", "author@example.com"},
{"GIT_COMMITTER_EMAIL", "author@example.com"},
{"GIT_AUTHOR_NAME", "A. U. Thor"},
{"GIT_COMMITTER_NAME", "A. U. Thor"}
]
@env OnDiskRepoTestCase.sample_commit_env()

test "happy path: no parents" do
%{xgit_path: ref_path, tree_id: tree_id} = setup_with_valid_tree!()
Expand Down Expand Up @@ -357,48 +350,5 @@ defmodule Xgit.Plumbing.CommitTreeTest do
committer: Map.put(@valid_pi, :tz_offset, 15_000)
)
end

defp setup_with_valid_tree!(path \\ nil) do
%{xgit_repo: xgit_repo} = context = OnDiskRepoTestCase.repo!(path)

{:ok, object_id} = HashObject.run("test content\n", repo: xgit_repo, write?: true)
:ok = CacheInfo.run(xgit_repo, [{0o100644, object_id, 'test'}])

{:ok, xgit_tree_id} = WriteTree.run(xgit_repo)

Map.put(context, :tree_id, xgit_tree_id)
end

defp setup_with_valid_parent_commit! do
%{xgit_path: xgit_path} = context = setup_with_valid_tree!()

{empty_tree_id_str, 0} =
System.cmd(
"git",
[
"write-tree"
],
cd: xgit_path
)

empty_tree_id = String.trim(empty_tree_id_str)

{parent_id_str, 0} =
System.cmd(
"git",
[
"commit-tree",
"-m",
"empty",
empty_tree_id
],
cd: xgit_path,
env: @env
)

parent_id = String.trim(parent_id_str)

Map.put(context, :parent_id, parent_id)
end
end
end

0 comments on commit f913039

Please sign in to comment.