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

Commit

Permalink
Adapt some of git's tests for git init. (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Jan 11, 2020
1 parent f903161 commit 8975777
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion test/xgit/repository/on_disk/create_test.exs
Original file line number Diff line number Diff line change
@@ -1,22 +1,89 @@
defmodule Xgit.Repository.OnDisk.CreateTest do
use Bitwise
use ExUnit.Case, async: true

alias Xgit.Repository.OnDisk
alias Xgit.Test.OnDiskRepoTestCase
alias Xgit.Test.TempDirTestCase

import FolderDiff

describe "create/1" do
test "happy path matches command-line git" do
%{xgit_path: ref} = OnDiskRepoTestCase.repo!()
%{xgit_path: xgit_root} = OnDiskRepoTestCase.repo!()
%{tmp_dir: xgit_root} = TempDirTestCase.tmp_dir!()

xgit = Path.join(xgit_root, "repo")

assert :ok = OnDisk.create(xgit)
assert_folders_are_equal(ref, xgit)
end

test ".git/objects should be empty after git init in an empty repo" do
# Adapted from git t0000-basic.sh
%{tmp_dir: xgit_root} = TempDirTestCase.tmp_dir!()

xgit = Path.join(xgit_root, "repo")
assert :ok = OnDisk.create(xgit)

assert {"", 0} = System.cmd("find", [".git/objects", "-type", "f"], cd: xgit)
end

test ".git/objects should have 3 subdirectories" do
# Adapted from git t0000-basic.sh

%{tmp_dir: xgit_root} = TempDirTestCase.tmp_dir!()

xgit = Path.join(xgit_root, "repo")
assert :ok = OnDisk.create(xgit)

assert {dirs_str, 0} = System.cmd("find", [".git/objects", "-type", "d"], cd: xgit)

dirs =
dirs_str
|> String.split("\n", trim: true)
|> Enum.sort()

assert dirs == [".git/objects", ".git/objects/info", ".git/objects/pack"]
end

defp check_config(path) do
assert File.dir?(path)
assert File.dir?(Path.join(path, ".git"))
assert File.regular?(Path.join(path, ".git/config"))
assert File.dir?(Path.join(path, ".git/refs"))

refute executable?(Path.join(path, ".git/config"))

# bare=$(cd "$1" && git config --bool core.bare)
# worktree=$(cd "$1" && git config core.worktree) ||
# worktree=unset

# test "$bare" = "$2" && test "$worktree" = "$3" || {
# echo "expected bare=$2 worktree=$3"
# echo " got bare=$bare worktree=$worktree"
# return 1
# }
end

defp executable?(path) do
case File.lstat(path) do
{:ok, %File.Stat{mode: mode}} -> (mode &&& 0o100) == 0o100
_ -> false
end
end

test "plain" do
# Adapted from git t0001-init.sh

%{tmp_dir: xgit_root} = TempDirTestCase.tmp_dir!()

xgit = Path.join(xgit_root, "repo")
assert :ok = OnDisk.create(xgit)

check_config(xgit)
end

test "error: no work_dir" do
assert_raise FunctionClauseError, fn ->
OnDisk.create(nil)
Expand Down

0 comments on commit 8975777

Please sign in to comment.