From 53042d32529e3a104f1b9ebc2645bd80503b9edc Mon Sep 17 00:00:00 2001 From: Laurent Bonnans Date: Thu, 31 Oct 2019 14:58:31 +0100 Subject: [PATCH] Test for running tool with actual CLI parsing Also tests the `-C` option --- test_git_archive_all.py | 71 ++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/test_git_archive_all.py b/test_git_archive_all.py index 13bae77..9d3f09a 100644 --- a/test_git_archive_all.py +++ b/test_git_archive_all.py @@ -14,6 +14,7 @@ import pycodestyle import pytest +import git_archive_all from git_archive_all import GitArchiver @@ -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.'), @@ -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