#!/usr/bin/env python3
"""Exits with failure message if, for any single class, coverage goes down.

Also exits with a failure message if a class appears in the old coverage report
but not in the new one -- for example, because the class was deleted or renamed,
or because a module was not analyzed (it failed to build, or it was omitted from
the Jacoco report).  Pass --ignore-removed-classes to permit such classes, which
is appropriate when the code change intentionally deletes or renames a class.

Usage: jacoco-coverage-ratchet [--ignore-removed-classes] OLD.csv NEW.csv

Arguments: two Jacoco coverage reports (.csv files).
"""

# import argparse
import csv
import sys
from pathlib import Path

PROGRAM = Path(__file__).name

DEBUG = False


def main():
    """Exit with failure message if, for any single class, coverage goes down.

    Also exit with a failure message if a class is in the old coverage report but
    not in the new one, unless --ignore-removed-classes was passed.
    """
    args = sys.argv[1:]
    ignore_removed_classes = "--ignore-removed-classes" in args
    if ignore_removed_classes:
        args = [arg for arg in args if arg != "--ignore-removed-classes"]
    num_args = len(args)
    if num_args != 2:
        print(f"{PROGRAM} received {num_args} arguments, expected 2: {args}")
        sys.exit(2)

    old_coverage = read_jacoco_csv_to_map(args[0])
    new_coverage = read_jacoco_csv_to_map(args[1])

    failed = False

    # A class that vanished from the new report is not measured at all, so no
    # ratio comparison below would notice it.  Treat that as a ratchet failure.
    if not ignore_removed_classes:
        for fq_classname in sorted(old_coverage.keys() - new_coverage.keys()):
            print(f"Class {fq_classname} is in the old coverage report but not in the new one")
            failed = True

    for fq_classname, new_instruction_cov in sorted(new_coverage.items()):
        if fq_classname not in old_coverage:
            continue
        old_instruction_cov = old_coverage[fq_classname]
        if old_instruction_cov == new_instruction_cov:
            continue
        if DEBUG:
            print(fq_classname, old_instruction_cov, new_instruction_cov, file=sys.stderr)
        if not check_coverage_ratio(fq_classname, old_instruction_cov, new_instruction_cov):
            failed = True

    if failed:
        sys.exit(1)


def read_jacoco_csv_to_map(filename: str) -> dict[str, tuple[int, int]]:
    """Read a Jacoco file.

    Returns:
        a map from class name to (instruction_missed, instruction_covered).
    """
    result = {}
    with Path.open(Path(filename)) as csvfile:
        dict_reader = csv.DictReader(csvfile)
        for row in dict_reader:
            # Include GROUP (the Maven/Gradle module) in the key.  Without it,
            # two modules that share a fully-qualified class name would collide
            # and one module's coverage would be silently dropped.
            fq_classname = row["GROUP"] + ":" + row["PACKAGE"] + "." + row["CLASS"]
            result[fq_classname] = (int(row["INSTRUCTION_MISSED"]), int(row["INSTRUCTION_COVERED"]))
    return result


def check_coverage_ratio(
    class_name: str, old_pair: tuple[int, int], new_pair: tuple[int, int]
) -> bool:
    """Return false if the coverage decreased, true otherwise.

    Returns:
         false if the coverage decreased, true otherwise.
    """
    old_missed = old_pair[0]
    old_covered = old_pair[1]
    old_denominator = old_covered + old_missed
    if old_denominator == 0:
        return True
    old_ratio = old_covered / old_denominator
    new_missed = new_pair[0]
    new_covered = new_pair[1]
    new_denominator = new_covered + new_missed
    if new_denominator == 0:
        return True
    new_ratio = new_covered / new_denominator
    if new_ratio < old_ratio:
        print(
            f"Coverage of {class_name} fell "
            f"from {old_covered}/{old_denominator} "
            f"to {new_covered}/{new_denominator}"
        )
        return False
    return True


if __name__ == "__main__":
    main()
