379 lines
11 KiB
Python
379 lines
11 KiB
Python
"""
|
|
Functional tests for ``git-gamo-report`` (gamo.report).
|
|
|
|
The installed entry point is exercised against a real local ``git``
|
|
worktree. Records are written as JSON files into a ``mirror`` directory;
|
|
the command must emit a markdown table with one row per record, sorted
|
|
by most recently mirrored first.
|
|
"""
|
|
|
|
import json
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from subprocess import CompletedProcess
|
|
|
|
import humanize
|
|
import pytest
|
|
|
|
from gamo.tests.conftest import GitEnv, entry_point
|
|
|
|
ENTRY_POINT = 'git-gamo-report'
|
|
|
|
HEADER = '| Repository | Last mirrored | Last modified | Versions | Keys | Missing |'
|
|
SEPARATOR = '| --- | --- | --- | --- | --- | --- |'
|
|
|
|
|
|
def make_record(
|
|
*,
|
|
name: str,
|
|
target: str,
|
|
source_last_modified: str | None = None,
|
|
last_success: str | None = None,
|
|
n_tags: int | None = None,
|
|
enabled: bool | None = None,
|
|
auto_disabled: bool | None = None,
|
|
keys_total_n: int | None = None,
|
|
keys_total_bytesize: int | None = None,
|
|
keys_missing_n: int | None = None,
|
|
keys_missing_bytesize: int | None = None,
|
|
) -> dict:
|
|
"""Build a production-shaped record for the report command."""
|
|
record: dict = {
|
|
'id': f'mirror/{name}',
|
|
'source': {'url': f'https://example.com/{name}-source.git'},
|
|
'target': {'url': target},
|
|
}
|
|
if enabled is not None:
|
|
record['enabled'] = enabled
|
|
if auto_disabled is not None:
|
|
record['schedule'] = {'auto_disabled': auto_disabled}
|
|
state: dict = {}
|
|
if source_last_modified is not None:
|
|
state['source_last_modified'] = source_last_modified
|
|
if last_success is not None:
|
|
state['last_success'] = last_success
|
|
if n_tags is not None:
|
|
state['n_tags'] = n_tags
|
|
if keys_total_n is not None:
|
|
state['keys_total_n'] = keys_total_n
|
|
if keys_total_bytesize is not None:
|
|
state['keys_total_bytesize'] = keys_total_bytesize
|
|
if keys_missing_n is not None:
|
|
state['keys_missing_n'] = keys_missing_n
|
|
if keys_missing_bytesize is not None:
|
|
state['keys_missing_bytesize'] = keys_missing_bytesize
|
|
if state:
|
|
record['state'] = state
|
|
return record
|
|
|
|
|
|
@dataclass
|
|
class ReportEnv:
|
|
entry: str
|
|
base: GitEnv
|
|
|
|
@property
|
|
def work(self) -> Path:
|
|
return self.base.work
|
|
|
|
def write(self, records: dict[str, dict], name: str = 'mirror') -> Path:
|
|
directory = self.work / name
|
|
directory.mkdir(parents=True, exist_ok=True)
|
|
for fname, record in records.items():
|
|
(directory / fname).write_text(json.dumps(record), encoding='utf-8')
|
|
return directory
|
|
|
|
def run(self, *args: str) -> CompletedProcess[str]:
|
|
return self.base.run(self.entry, *args)
|
|
|
|
|
|
@pytest.fixture
|
|
def report(git_env: GitEnv) -> ReportEnv:
|
|
git_env.git('init', '-q', '-b', 'main', str(git_env.work))
|
|
return ReportEnv(entry=entry_point(ENTRY_POINT), base=git_env)
|
|
|
|
|
|
def _rows(result: CompletedProcess[str]) -> list[list[str]]:
|
|
lines = result.stdout.splitlines()
|
|
assert lines[0] == HEADER
|
|
assert lines[1] == SEPARATOR
|
|
return [
|
|
[cell.strip() for cell in line.strip().strip('|').split('|')]
|
|
for line in lines[2:]
|
|
]
|
|
|
|
|
|
def test_table_header_and_separator(report: ReportEnv) -> None:
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://example.com/org/a.git',
|
|
source_last_modified='2026-11-07T12:34:56Z',
|
|
n_tags=2,
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert len(_rows(result)) == 1
|
|
|
|
|
|
def test_row_content_and_date_format(report: ReportEnv) -> None:
|
|
# the two dates differ, so a swapped or duplicated column is caught
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://example.com/org/a.git',
|
|
last_success='2026-11-07T06:00:00Z',
|
|
source_last_modified='2026-11-06T12:34:56Z',
|
|
n_tags=2,
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert _rows(result) == [
|
|
[
|
|
'[a](https://example.com/org/a)',
|
|
'2026-11-07',
|
|
'2026-11-06',
|
|
'[2](https://example.com/org/a/tags)',
|
|
'-',
|
|
'-',
|
|
],
|
|
]
|
|
|
|
|
|
def test_sorted_most_recently_mirrored_first_never_mirrored_last(
|
|
report: ReportEnv,
|
|
) -> None:
|
|
report.write(
|
|
{
|
|
# last_success dates are the reverse of the source_last_modified
|
|
# dates: sorting must use last_success, not source_last_modified
|
|
'a_old.json': make_record(
|
|
name='old',
|
|
target='https://example.com/org/old.git',
|
|
last_success='2026-11-07T00:00:00Z',
|
|
source_last_modified='2026-11-01T00:00:00Z',
|
|
n_tags=1,
|
|
),
|
|
'b_new.json': make_record(
|
|
name='new',
|
|
target='https://example.com/org/new.git',
|
|
last_success='2026-11-01T00:00:00Z',
|
|
source_last_modified='2026-11-07T00:00:00Z',
|
|
n_tags=2,
|
|
),
|
|
# never mirrored: no state at all, must sort last
|
|
'c_never.json': make_record(
|
|
name='never',
|
|
target='https://example.com/org/never.git',
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert [row[0] for row in _rows(result)] == [
|
|
'[old](https://example.com/org/old)',
|
|
'[new](https://example.com/org/new)',
|
|
'[never](https://example.com/org/never)',
|
|
]
|
|
|
|
|
|
def test_tie_broken_by_name_ascending(report: ReportEnv) -> None:
|
|
same = '2026-06-01T00:00:00Z'
|
|
report.write(
|
|
{
|
|
# the filenames suggest the opposite order; sorting must use
|
|
# the repository name. last_success is tied; the differing
|
|
# source_last_modified dates must not affect the order
|
|
'a_zeta.json': make_record(
|
|
name='zeta',
|
|
target='https://example.com/org/zeta.git',
|
|
last_success=same,
|
|
source_last_modified='2026-05-01T00:00:00Z',
|
|
n_tags=1,
|
|
),
|
|
'b_alpha.json': make_record(
|
|
name='alpha',
|
|
target='https://example.com/org/alpha.git',
|
|
last_success=same,
|
|
source_last_modified='2026-07-01T00:00:00Z',
|
|
n_tags=2,
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert [row[0] for row in _rows(result)] == [
|
|
'[alpha](https://example.com/org/alpha)',
|
|
'[zeta](https://example.com/org/zeta)',
|
|
]
|
|
|
|
|
|
def test_url_derivation_for_https_and_scp_like_targets(report: ReportEnv) -> None:
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://forge.example.org/org/a.git',
|
|
source_last_modified='2026-01-02T00:00:00Z',
|
|
n_tags=3,
|
|
),
|
|
'b.json': make_record(
|
|
name='b',
|
|
target='git@forge.example.org:org/b.git',
|
|
source_last_modified='2026-01-01T00:00:00Z',
|
|
n_tags=0,
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
rows = _rows(result)
|
|
assert rows[0] == [
|
|
'[a](https://forge.example.org/org/a)',
|
|
'-',
|
|
'2026-01-02',
|
|
'[3](https://forge.example.org/org/a/tags)',
|
|
'-',
|
|
'-',
|
|
]
|
|
# the scp-like target URL is derived as https, and a tag count of 0
|
|
# is still linked
|
|
assert rows[1] == [
|
|
'[b](https://forge.example.org/org/b)',
|
|
'-',
|
|
'2026-01-01',
|
|
'[0](https://forge.example.org/org/b/tags)',
|
|
'-',
|
|
'-',
|
|
]
|
|
|
|
|
|
def test_missing_state_fields_render_as_dash(report: ReportEnv) -> None:
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://example.com/org/a.git',
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert _rows(result) == [
|
|
['[a](https://example.com/org/a)', '-', '-', '-', '-', '-'],
|
|
]
|
|
|
|
|
|
def test_key_stats_rendered_with_humansize(report: ReportEnv) -> None:
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://example.com/org/a.git',
|
|
source_last_modified='2026-11-07T00:00:00Z',
|
|
n_tags=1,
|
|
keys_total_n=10,
|
|
keys_total_bytesize=1234567,
|
|
keys_missing_n=2,
|
|
keys_missing_bytesize=65536,
|
|
),
|
|
# known key counts but unknown sizes must render as '?'
|
|
'b.json': make_record(
|
|
name='b',
|
|
target='https://example.com/org/b.git',
|
|
source_last_modified='2026-11-06T00:00:00Z',
|
|
n_tags=1,
|
|
keys_total_n=3,
|
|
keys_missing_n=1,
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert _rows(result) == [
|
|
[
|
|
'[a](https://example.com/org/a)',
|
|
'-',
|
|
'2026-11-07',
|
|
'[1](https://example.com/org/a/tags)',
|
|
f'10@{humanize.naturalsize(1234567)}',
|
|
f'2@{humanize.naturalsize(65536)}',
|
|
],
|
|
[
|
|
'[b](https://example.com/org/b)',
|
|
'-',
|
|
'2026-11-06',
|
|
'[1](https://example.com/org/b/tags)',
|
|
'3@?',
|
|
'1@?',
|
|
],
|
|
]
|
|
|
|
|
|
def test_disabled_and_auto_disabled_records_are_included(
|
|
report: ReportEnv,
|
|
) -> None:
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://example.com/org/a.git',
|
|
source_last_modified='2026-03-01T00:00:00Z',
|
|
n_tags=1,
|
|
enabled=False,
|
|
),
|
|
'b.json': make_record(
|
|
name='b',
|
|
target='https://example.com/org/b.git',
|
|
source_last_modified='2026-02-01T00:00:00Z',
|
|
n_tags=2,
|
|
auto_disabled=True,
|
|
),
|
|
},
|
|
)
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert [row[0] for row in _rows(result)] == [
|
|
'[a](https://example.com/org/a)',
|
|
'[b](https://example.com/org/b)',
|
|
]
|
|
|
|
|
|
def test_custom_mirror_dir(report: ReportEnv) -> None:
|
|
report.write(
|
|
{
|
|
'a.json': make_record(
|
|
name='a',
|
|
target='https://example.com/org/a.git',
|
|
source_last_modified='2026-01-01T00:00:00Z',
|
|
n_tags=1,
|
|
),
|
|
},
|
|
name='custom',
|
|
)
|
|
|
|
result = report.run('--mirror', 'custom')
|
|
assert result.returncode == 0, result.stderr
|
|
assert len(_rows(result)) == 1
|
|
|
|
|
|
def test_empty_mirror_emits_header_only(report: ReportEnv) -> None:
|
|
report.write({})
|
|
|
|
result = report.run()
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout == f'{HEADER}\n{SEPARATOR}\n'
|