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

Commit.from_object/1: Add tests for interop with command-line git. #206

Merged
merged 2 commits into from Oct 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 69 additions & 2 deletions test/xgit/core/commit_test.exs
Expand Up @@ -7,9 +7,13 @@ defmodule Xgit.Core.CommitTest do
alias Xgit.GitInitTestCase
alias Xgit.Repository
alias Xgit.Repository.OnDisk
alias Xgit.Test.OnDiskRepoTestCase

import FolderDiff

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

@valid_pi %PersonIdent{
name: "A. U. Thor",
email: "author@example.com",
Expand All @@ -24,6 +28,8 @@ defmodule Xgit.Core.CommitTest do
tz_offset: 150
}

@env OnDiskRepoTestCase.sample_commit_env()

describe "valid?/1" do
test "valid: no parent" do
assert Commit.valid?(%Commit{
Expand Down Expand Up @@ -214,8 +220,69 @@ defmodule Xgit.Core.CommitTest do
end

describe "from_object/1" do
# TO DO: Test ability to read commits generated by command-line git.
# https://github.com/elixir-git/xgit/issues/203
test "command-line interop: no parents" do
%{xgit_path: path, xgit_repo: repo, tree_id: tree_id} = setup_with_valid_tree!()

assert {commit_id_str, 0} =
System.cmd("git", ["commit-tree", tree_id, "-m", "xxx"], cd: path, env: @env)

commit_id = String.trim(commit_id_str)

{:ok, commit_object} = Repository.get_object(repo, commit_id)

assert {:ok,
%Xgit.Core.Commit{
author: %Xgit.Core.PersonIdent{
email: "author@example.com",
name: "A. U. Thor",
tz_offset: 150,
when: 1_142_878_449
},
committer: %Xgit.Core.PersonIdent{
email: "author@example.com",
name: "A. U. Thor",
tz_offset: 150,
when: 1_142_878_449
},
message: 'xxx\n',
parents: [],
tree: "3e69f02f3247843b482cc99872683692999f6703"
}} = Commit.from_object(commit_object)
end

test "command-line interop: one parent" do
%{xgit_path: path, xgit_repo: repo, tree_id: tree_id, parent_id: parent_id} =
setup_with_valid_parent_commit!()

assert {commit_id_str, 0} =
System.cmd("git", ["commit-tree", tree_id, "-m", "mumble", "-p", parent_id],
cd: path,
env: @env
)

commit_id = String.trim(commit_id_str)

{:ok, commit_object} = Repository.get_object(repo, commit_id)

assert {:ok,
%Xgit.Core.Commit{
author: %Xgit.Core.PersonIdent{
email: "author@example.com",
name: "A. U. Thor",
tz_offset: 150,
when: 1_142_878_449
},
committer: %Xgit.Core.PersonIdent{
email: "author@example.com",
name: "A. U. Thor",
tz_offset: 150,
when: 1_142_878_449
},
message: 'mumble\n',
parents: [^parent_id],
tree: "3e69f02f3247843b482cc99872683692999f6703"
}} = Commit.from_object(commit_object)
end

test "valid: no parent" do
assert {:ok,
Expand Down