Skip to content

Commit

Permalink
refactor: Improve e2e test performance
Browse files Browse the repository at this point in the history
I don't need to download and clone an entire respository (though it's a nice test).
Using a locally created repository instead makes the test easier to review and faster to execute.
  • Loading branch information
thehale committed Jan 11, 2025
1 parent 4692927 commit b094bd2
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Generated Project Files
repo
tmp

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
7 changes: 6 additions & 1 deletion git_authorship/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import shutil
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
from typing import Optional
from typing import Union

from git import Repo

Expand Down Expand Up @@ -90,7 +92,10 @@ def clone_and_checkout(args: Args):
return repo


def run(args: Args):
def run(args: Union[Args, Iterable[str]]):
if isinstance(args, Iterable):
args = parse_args(args)

repo = clone_and_checkout(args)
repo_authorship = authorship.for_repo(
repo,
Expand Down
Empty file added test/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion test/fixtures/licensing.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Joseph Hale <[email protected]>,MPL-2.0
Alice <[email protected]>,MPL-2.0
56 changes: 56 additions & 0 deletions test/fixtures/tmp_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pathlib import Path
from typing import List
from typing import Tuple
from typing import Union

from git import Actor
from git import Repo

Message = str
AuthorName = str
AuthorEmail = str

CommitMessage = Tuple[Message, AuthorName, AuthorEmail]

FilePath = str
FileContent = Union[List[str], str]
Change = Tuple[FilePath, FileContent]


class TemporaryRepository:
def __init__(self, directory: str):
self._repo = Repo.init(directory)
self._index = self._repo.index

@property
def dir(self):
return self._repo.working_dir

@property
def branch(self) -> str:
return self._repo.active_branch.name

def set_file(self, path: str, content: Union[str, List[str]]):
"""
Updates a file in the repository to the given content, creating it if necessary.
"""
if isinstance(content, list):
content = "\n".join(content)

with open(Path(self.dir) / path, "w") as f:
f.write(content)

self._index.add(path)

def append_file(self, path: str, content: Union[str, List[str]]):
if isinstance(content, list):
content = "\n".join(content)

with open(Path(self.dir) / path, "a") as f:
f.write(content)

self._index.add(path)

def commit(self, message: str, author_name: str, author_email: str):
author = Actor(author_name, author_email)
self._index.commit(message, author=author)
40 changes: 0 additions & 40 deletions test/snapshots/test_e2e/test_full_workflow/authorship.csv

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions test/snapshots/test_e2e/test_typical_workflow/authorship.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
path,author,lines,license
.,Alice <[email protected]>,1,
.,Bob <[email protected]>,1,
greeting.txt,Alice <[email protected]>,1,
greeting.txt,Bob <[email protected]>,1,
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
path,author,lines,license
.,Alice <[email protected]>,1,MPL-2.0
.,Bob <[email protected]>,1,
greeting.txt,Alice <[email protected]>,1,MPL-2.0
greeting.txt,Bob <[email protected]>,1,
51 changes: 30 additions & 21 deletions test/test_e2e.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
from git_authorship.cli import Args
from tempfile import TemporaryDirectory
from test.fixtures.tmp_repo import TemporaryRepository

import pytest

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",
author_licenses_path=None,
use_cache=False,
)
)
@pytest.fixture
def repo():
with TemporaryDirectory() as d:
repo = TemporaryRepository(d)

repo.set_file("greeting.txt", "Hello, world!\n")
repo.commit("Initial commit", "Alice", "[email protected]")

repo.append_file("greeting.txt", "Excited to be here!\n")
repo.commit("Second commit", "Bob", "[email protected]")

yield repo


def test_typical_workflow(snapshot, repo: TemporaryRepository):
run([repo.dir, "--clone-to", "./tmp", "--no-cache"])

with open("build/authorship.csv", "r") as f:
snapshot.assert_match(f.read(), "authorship.csv")


def test_full_workflow_with_author_licenses(snapshot):
run(
Args(
location="https://github.com/thehale/git-authorship",
clone_to="./build/test/e2e",
branch="b655cc6c634a52660d3d2e87f9978343c92aa998",
author_licenses_path="./test/fixtures/licensing.csv",
use_cache=False,
)
)
def test_workflow_with_author_licenses(snapshot, repo: TemporaryRepository):
# fmt: off
run([
repo.dir,
"--author-licenses", "./test/fixtures/licensing.csv",
"--clone-to", "./tmp",
"--no-cache",
])
# fmt: on

with open("build/authorship.csv", "r") as f:
snapshot.assert_match(f.read(), "authorship.csv")

0 comments on commit b094bd2

Please sign in to comment.