Skip to content

Commit

Permalink
Add options to print the project's current checksum (#54)
Browse files Browse the repository at this point in the history
This command lets users get the current checksum of the project without
performing any operations. This can be useful to generate a cache key in CI
platforms to bundle databases dumps with other artifacts like coverage
reports.
  • Loading branch information
marcgibbons authored Jun 17, 2024
1 parent a34f31c commit 6556254
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ options:
--location LOCATION
--pytest
--depth DEPTH
--checksum Prints the current checksum and exits.
-v {0,1,2,3}
```

Expand Down
14 changes: 14 additions & 0 deletions django_migrations_ci/management/commands/migrateci.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def add_arguments(self, parser):
)
parser.add_argument("--depth", type=int, default=settings.depth)
parser.add_argument("--reuse-db", action="store_true", default=False)
parser.add_argument(
"--checksum",
dest="only_print_checksum",
action="store_true",
default=False,
help="Prints the current checksum and exits."
)

def _setup_logging(self):
lib_logger = logging.getLogger("django_migrations_ci")
Expand All @@ -54,6 +61,7 @@ def handle(
depth,
verbosity,
reuse_db,
only_print_checksum,
**options,
):
self._setup_logging()
Expand Down Expand Up @@ -87,7 +95,13 @@ def handle(

current_checksum = None
checksums = django.hash_files(depth)

for cached_checksum in checksums:
# If run with --checksum, print the current checksum and exit.
if only_print_checksum:
self.stdout.write(cached_checksum)
return 0

# Current checksum is the first result returned from hash_files.
if current_checksum is None:
current_checksum = cached_checksum
Expand Down
10 changes: 10 additions & 0 deletions tests/test_migrateci_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def cli(
depth=None,
reuse_db=False,
verbosity=None,
checksum=None,
):
args = ["manage.py", "migrateci"]
if parallel is not None:
Expand All @@ -51,6 +52,8 @@ def cli(
args.append(f"--depth={depth}")
if reuse_db:
args.append("--reuse-db")
if checksum:
args.append("--checksum")

execute_from_command_line(args)

Expand Down Expand Up @@ -133,6 +136,13 @@ def test_migrateci_reuse_db(mocker, tmpdir):
load_spy.assert_not_called()


def test_checksum(capsys, mocker, tmpdir):
cli(location=tmpdir, checksum=True)
result = capsys.readouterr()
assert result.out.strip() == CHECKSUM_0002
assert not result.err


class StorageSpy(FileSystemStorage):
initialized_with: ClassVar = []

Expand Down

0 comments on commit 6556254

Please sign in to comment.