Example #1
0
    def _primer_test(package: PackageToLint,
                     caplog: LogCaptureFixture) -> None:
        """Runs pylint over external packages to check for crashes and fatal messages.

        We only check for crashes (bit-encoded exit code 32) and fatal messages
        (bit-encoded exit code 1). We assume that these external repositories do not
        have any fatal errors in their code so that any fatal errors are pylint false
        positives
        """
        caplog.set_level(logging.INFO)
        package.lazy_clone()

        try:
            # We want to test all the code we can
            enables = ["--enable-all-extensions", "--enable=all"]
            # Duplicate code takes too long and is relatively safe
            disables = ["--disable=duplicate-code"]
            command = ["pylint"] + enables + disables + package.pylint_args
            logging.info("Launching primer:\n%s", " ".join(command))
            subprocess.run(command, check=True)
        except subprocess.CalledProcessError as ex:
            msg = f"Encountered {{}} during primer test for {package}"
            assert ex.returncode != 32, msg.format("a crash")
            assert ex.returncode % 2 == 0, msg.format(
                "a message of category 'fatal'")
Example #2
0
def test_filter_unallowed_hashes__log_message_with_match(
    caplog: pytest.LogCaptureFixture, ) -> None:
    caplog.set_level(logging.DEBUG)

    # Test 1 match, 2 non-matches, 3 no hashes so all 3 values will be
    # different.
    candidates = [
        make_mock_candidate("1.0"),
        make_mock_candidate("1.1", ),
        make_mock_candidate("1.2", ),
        make_mock_candidate("1.3", hex_digest=(64 * "a")),
        make_mock_candidate("1.4", hex_digest=(64 * "b")),
        make_mock_candidate("1.5", hex_digest=(64 * "c")),
    ]
    hashes_data = {
        "sha256": [64 * "a", 64 * "d"],
    }
    hashes = Hashes(hashes_data)
    actual = filter_unallowed_hashes(
        candidates,
        hashes=hashes,
        project_name="my-project",
    )
    assert len(actual) == 4

    expected_message = (
        "Checked 6 links for project 'my-project' against 2 hashes "
        "(1 matches, 3 no digest): discarding 2 non-matches:\n"
        "  https://example.com/pkg-1.4.tar.gz#sha256="
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
        "  https://example.com/pkg-1.5.tar.gz#sha256="
        "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
    check_caplog(caplog, "DEBUG", expected_message)
Example #3
0
def test_filter_unallowed_hashes__log_message_with_no_match(
    caplog: pytest.LogCaptureFixture,
) -> None:
    caplog.set_level(logging.DEBUG)

    candidates = [
        make_mock_candidate("1.0"),
        make_mock_candidate("1.1", hex_digest=(64 * "b")),
        make_mock_candidate("1.2", hex_digest=(64 * "c")),
    ]
    hashes_data = {
        "sha256": [64 * "a", 64 * "d"],
    }
    hashes = Hashes(hashes_data)
    actual = filter_unallowed_hashes(
        candidates,
        hashes=hashes,
        project_name="my-project",
    )
    assert len(actual) == 3

    expected_message = (
        "Checked 3 links for project 'my-project' against 2 hashes "
        "(0 matches, 1 no digest): discarding no candidates"
    )
    check_caplog(caplog, "DEBUG", expected_message)