diff --git a/git_authorship/cli.py b/git_authorship/cli.py index 0bfa5d5..f7f01f6 100644 --- a/git_authorship/cli.py +++ b/git_authorship/cli.py @@ -34,21 +34,26 @@ def parse_args(argv=None) -> Args: return Args(args.location, args.clone_to, args.branch) -def ensure_cloned_and_pulled(location: str, clone_to: str): - log.info(f"Cloning {location} to {clone_to}") - if not Path(clone_to).exists(): - Repo.clone_from(location, clone_to) - else: - Repo(clone_to).git.pull() +def clone_and_checkout(args: Args): + log.info(f"Cloning {args.location} @ {args.branch} to {args.clone_to}") - return Repo(clone_to) + if not Path(args.clone_to).exists(): + Repo.clone_from(args.location, args.clone_to) + repo = Repo(args.clone_to) -def main(argv=None): - args = parse_args(argv) - repo = ensure_cloned_and_pulled(args.location, args.clone_to) if args.branch: repo.git.checkout(args.branch) + + return repo + + +def run(args: Args): + repo = clone_and_checkout(args) repo_authorship = authorship.for_repo(repo) export.as_treemap(repo_authorship) export.as_json(repo_authorship) + + +def main(argv=None): + run(parse_args(argv)) diff --git a/poetry.lock b/poetry.lock index e6a1960..db02b82 100644 --- a/poetry.lock +++ b/poetry.lock @@ -379,6 +379,20 @@ tomli = {version = ">=1", markers = "python_version < \"3.11\""} [package.extras] dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] +[[package]] +name = "pytest-snapshot" +version = "0.9.0" +description = "A plugin for snapshot testing with pytest." +optional = false +python-versions = ">=3.5" +files = [ + {file = "pytest-snapshot-0.9.0.tar.gz", hash = "sha256:c7013c3abc3e860f9feff899f8b4debe3708650d8d8242a61bf2625ff64db7f3"}, + {file = "pytest_snapshot-0.9.0-py3-none-any.whl", hash = "sha256:4b9fe1c21c868fe53a545e4e3184d36bc1c88946e3f5c1d9dd676962a9b3d4ab"}, +] + +[package.dependencies] +pytest = ">=3.0.0" + [[package]] name = "pyyaml" version = "6.0" @@ -539,4 +553,4 @@ test = ["covdefaults (>=2.3)", "coverage (>=7.2.3)", "coverage-enable-subprocess [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "44dda767c2fe59d8785baf5010d490a16ad994eddf87228daf11f7f965ad6ab3" +content-hash = "333330f53c39f83880549f2c4334552062a619fcd2957f3e31757d5a8d2c5635" diff --git a/pyproject.toml b/pyproject.toml index 6ac3ad8..df0e1a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ mypy = "^1.13" pre-commit = "^3.5.0" pytest = "^8.3.3" liccheck = "^0.9.2" +pytest-snapshot = "^0.9.0" [tool.liccheck] authorized_licenses = [ diff --git a/test/snapshots/test_e2e/test_full_workflow/authorship.json b/test/snapshots/test_e2e/test_full_workflow/authorship.json new file mode 100644 index 0000000..6d13ca2 --- /dev/null +++ b/test/snapshots/test_e2e/test_full_workflow/authorship.json @@ -0,0 +1 @@ +{".": {"Joseph Hale ": 241532, "Joseph Hale <47901316+thehale@users.noreply.github.com>": 74, "dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>": 116}, "git_authorship": {"Joseph Hale ": 171}, "git_authorship/__main__.py": {"Joseph Hale ": 166}, "git_authorship/__init__.py": {"Joseph Hale ": 5}, ".gitignore": {"Joseph Hale ": 169}, "LICENSE": {"Joseph Hale ": 373}, "README.md": {"Joseph Hale ": 139, "Joseph Hale <47901316+thehale@users.noreply.github.com>": 5}, ".devcontainer": {"Joseph Hale ": 41}, ".devcontainer/devcontainer.json": {"Joseph Hale ": 32}, ".devcontainer/Dockerfile": {"Joseph Hale ": 9}, "pyproject.toml": {"Joseph Hale ": 45, "dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>": 7}, "docs": {"Joseph Hale ": 240056}, "docs/git-authorship-demo-cubingjs.gif": {"Joseph Hale ": 238279}, "docs/git-authorship-social-preview.svg": {"Joseph Hale ": 1111}, "docs/git-authorship-social-preview.png": {"Joseph Hale ": 666}, "config": {"Joseph Hale ": 12}, "config/.gitignore": {"Joseph Hale ": 8}, "config/pseudonyms.txt.dist": {"Joseph Hale ": 2}, "config/author-licenses.txt.dist": {"Joseph Hale ": 2}, "poetry.lock": {"dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>": 109, "Joseph Hale ": 433}, ".github": {"Joseph Hale <47901316+thehale@users.noreply.github.com>": 69, "Joseph Hale ": 13}, ".github/dependabot.yml": {"Joseph Hale <47901316+thehale@users.noreply.github.com>": 22}, ".github/workflows": {"Joseph Hale <47901316+thehale@users.noreply.github.com>": 47, "Joseph Hale ": 13}, ".github/workflows/auto-merge-dependabot.yml": {"Joseph Hale <47901316+thehale@users.noreply.github.com>": 23}, ".github/workflows/precommit.yml": {"Joseph Hale <47901316+thehale@users.noreply.github.com>": 24, "Joseph Hale ": 13}, "test": {"Joseph Hale ": 9}, "test/test_authorship_analyzer.py": {"Joseph Hale ": 9}, ".vscode": {"Joseph Hale ": 16}, ".vscode/launch.json": {"Joseph Hale ": 16}, "Makefile": {"Joseph Hale ": 13}, ".pre-commit-config.yaml": {"Joseph Hale ": 42}} \ No newline at end of file diff --git a/test/test_e2e.py b/test/test_e2e.py new file mode 100644 index 0000000..6a5e89a --- /dev/null +++ b/test/test_e2e.py @@ -0,0 +1,15 @@ +from git_authorship.cli import Args +from git_authorship.cli import run + + +def test_full_workflow(snapshot): + run( + Args( + location="https://github.com/thehale/git-authorship", + clone_to="./build/test/e2e", + branch="b655cc6c634a52660d3d2e87f9978343c92aa998", + ) + ) + + with open("build/authorship.json", "r") as f: + snapshot.assert_match(f.read(), "authorship.json")