Skip to content

Commit

Permalink
Test for running tool with actual CLI parsing
Browse files Browse the repository at this point in the history
Also tests the `-C` option
  • Loading branch information
lbonn committed Oct 31, 2019
1 parent df3a9a2 commit 53042d3
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions test_git_archive_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import pycodestyle
import pytest

import git_archive_all
from git_archive_all import GitArchiver


Expand Down Expand Up @@ -140,6 +141,36 @@ def archive(self, path):
a.create(path)


def make_expected_tree(contents):
e = {}

for k, v in contents.items():
if v.kind == 'file' and not v.excluded:
e[k] = v.contents
elif v.kind in ('dir', 'submodule') and not v.excluded:
for nested_k, nested_v in make_expected_tree(v.contents).items():
e[as_posix(os.path.join(k, nested_k))] = nested_v

return e


def make_actual_tree(tar_file):
a = {}

for m in tar_file.getmembers():
if m.isfile():
name = m.name

if sys.version_info < (3,):
name = m.name.decode('utf-8')

a[name] = tar_file.extractfile(m).read().decode()
else:
raise NotImplementedError

return a


base = {
'app': DirRecord({
'__init__.py': FileRecord('#Beautiful is better than ugly.'),
Expand Down Expand Up @@ -294,36 +325,30 @@ def test_ignore(contents, tmpdir, git_env, monkeypatch):
repo.archive(repo_tar_path)
repo_tar = TarFile(repo_tar_path, format=PAX_FORMAT, encoding='utf-8')

def make_expected(contents):
e = {}
expected = make_expected_tree(contents)
actual = make_actual_tree(repo_tar)

for k, v in contents.items():
if v.kind == 'file' and not v.excluded:
e[k] = v.contents
elif v.kind in ('dir', 'submodule') and not v.excluded:
for nested_k, nested_v in make_expected(v.contents).items():
e[as_posix(os.path.join(k, nested_k))] = nested_v

return e
assert actual == expected

def make_actual(tar_file):
a = {}

for m in tar_file.getmembers():
if m.isfile():
name = m.name
def test_cli(tmpdir, git_env, monkeypatch):
contents = base

if sys.version_info < (3,):
name = m.name.decode('utf-8')
for name, value in git_env.items():
monkeypatch.setenv(name, value)

a[name] = tar_file.extractfile(m).read().decode()
else:
raise NotImplementedError
repo_path = os.path.join(str(tmpdir), 'repo')
repo = Repo(repo_path)
repo.init()
repo.add_dir('.', contents)
repo.commit('init')

return a
repo_tar_path = os.path.join(str(tmpdir), 'repo.tar')
git_archive_all.main(['git_archive_all.py', '--prefix', '', '-C', repo_path, repo_tar_path])
repo_tar = TarFile(repo_tar_path, format=PAX_FORMAT, encoding='utf-8')

expected = make_expected(contents)
actual = make_actual(repo_tar)
expected = make_expected_tree(contents)
actual = make_actual_tree(repo_tar)

assert actual == expected

Expand Down

0 comments on commit 53042d3

Please sign in to comment.