from bundlewrap.exceptions import BundleError
from bundlewrap.utils.text import bold, green, yellow
from bundlewrap.utils.ui import io


def test(repo, **kwargs):
    for node in repo.nodes:
        if not node.has_bundle('apt'):
            continue

        spread = node.metadata.get('apt/unattended-upgrades/spread_in_group', None)
        if spread is None:
            continue

        for rnode in repo.nodes_in_group(spread):
            rspread = rnode.metadata.get('apt/unattended-upgrades/spread_in_group', None)

            if spread != rspread:
                raise BundleError(f'{node.name} sets apt/unattended-upgrades/spread_in_group to "{spread}", but node {rnode.name} in that group does set "{rspread}"!')

        io.stdout('{x} {node}  apt/unattended-upgrades/spread_in_group matches'.format(
            x=green("✓"),
            node=bold(node.name),
        ))
