Ejemplo n.º 1
0
def test_archive_updates_whitelist() -> None:
    """Validates that archive updates the whitelist file"""

    runner = CliRunner()

    context = Context(INTEGRATION / "simple")

    with mod_file(context.baseline_file_path) as whitelist:
        runner.invoke(archive,
                      obj=context,
                      args=["--all", str(context.base_path)])
        yml = bento.result.json_to_violation_hashes(whitelist)

    expectation = {
        "bandit": {
            "6f77d9d773cc5248ae20b83f80a7b26a",
            "e540c501c568dad8d9e2e00abba5740f",
        },
        "eslint": {"6daebd293be00a3d97e19de4a1a39fa5"},
        "flake8": {
            "23d898269aae05ed6897cf56dfbd3cde",
            "b849b45f8a969cc5eb46e6ea76d7e809",
        },
    }

    assert yml == expectation
Ejemplo n.º 2
0
def test_no_install_empty_project() -> None:
    """Validates that bento does installs a config on an empty project"""
    context = Context(base_path=INTEGRATION / "none", is_init=True)
    command = InitCommand(context)
    with mod_file(context.config_path):
        context.config_path.unlink()
        assert not context.config_path.exists()
        command._install_config_if_not_exists()
        assert len(context.config["tools"]) == 0
Ejemplo n.º 3
0
def test_install_ignore_in_repo() -> None:
    """Validates that bento installs an ignore file if none exists"""
    context = Context(base_path=SIMPLE, is_init=True)
    command = InitCommand(context)
    with mod_file(context.ignore_file_path):
        context.ignore_file_path.unlink()
        command._install_ignore_if_not_exists()
        context = Context(base_path=SIMPLE, is_init=True)
        assert context.ignore_file_path.exists()
Ejemplo n.º 4
0
def test_init_py_only() -> None:
    context = Context(base_path=INTEGRATION / "py-only", is_init=True)
    with mod_file(context.config_path):
        context.config_path.unlink()
        CliRunner(mix_stderr=False).invoke(init, obj=context)
        config = context.config

    assert "eslint" not in config["tools"]
    assert "flake8" in config["tools"]
    assert "bandit" in config["tools"]
Ejemplo n.º 5
0
def test_enable_tool_found() -> None:
    """Validates that enabling a check updates ignores in config"""

    runner = CliRunner()
    context = Context(base_path=SIMPLE)

    with mod_file(context.config_path):
        runner.invoke(enable, ["check", "eslint", "curly"], obj=context)
        config = context.config
        assert "curly" not in config["tools"]["eslint"]["ignore"]
Ejemplo n.º 6
0
def test_install_config() -> None:
    """Validates that bento installs a config file if none exists"""
    context = Context(base_path=SIMPLE, is_init=True)
    command = InitCommand(context)
    with mod_file(context.config_path):
        context.config_path.unlink()
        command._install_config_if_not_exists()
        cfg = context.config
        assert "eslint" in cfg["tools"]
        assert "flake8" in cfg["tools"]
        assert "bandit" in cfg["tools"]
Ejemplo n.º 7
0
def test_enable_invalid_tool() -> None:
    """Validates that enable tool exits when invalid tool is passed"""
    runner = CliRunner()
    context = Context(base_path=SIMPLE)

    with mod_file(context.config_path):
        config = context.config
        assert "run" not in config["tools"]["eslint"]

        result = runner.invoke(enable, ["tool", "nonexistent"], obj=context)
        assert result.exception.code == 3
Ejemplo n.º 8
0
def test_disable_tool() -> None:
    """Validates that disable tool correctly modifies config"""
    runner = CliRunner()
    context = Context(base_path=SIMPLE)

    with mod_file(context.config_path):
        runner.invoke(disable, ["tool", "eslint"], obj=context)
        config = context.config
        assert config["tools"]["eslint"]["run"] is False

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=SIMPLE).config
        assert persisted_config["tools"]["eslint"]["run"] is False
Ejemplo n.º 9
0
def test_enable_tool() -> None:
    """Validates that enable tool works with no run"""
    runner = CliRunner()
    context = Context(base_path=SIMPLE)

    with mod_file(context.config_path):
        config = context.config
        assert "run" not in config["tools"]["eslint"]

        runner.invoke(enable, ["tool", "eslint"], obj=context)

        context = Context(base_path=SIMPLE)
        assert config["tools"]["eslint"]["run"]

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=SIMPLE).config
        assert persisted_config["tools"]["eslint"]["run"]
Ejemplo n.º 10
0
def test_disable_then_enable_tool() -> None:
    """Validates that enable tool works after previously disabling"""
    runner = CliRunner()
    context = Context(base_path=SIMPLE)

    with mod_file(context.config_path):
        config = context.config

        runner.invoke(disable, ["tool", "eslint"], obj=context)
        assert config["tools"]["eslint"]["run"] is False

        runner.invoke(enable, ["tool", "eslint"], obj=context)
        assert config["tools"]["eslint"]["run"]

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=SIMPLE).config
        assert persisted_config["tools"]["eslint"]["run"]
Ejemplo n.º 11
0
def test_enable_default_ignores() -> None:
    """Validates that enable tool not in config uses default ignores"""
    runner = CliRunner()
    context = Context(base_path=PY_ONLY)

    with mod_file(context.config_path):
        config = context.config
        # Test is meaningless if tool is already in config
        assert "eslint" not in config["tools"]

        runner.invoke(enable, ["tool", "eslint"], obj=context)
        assert config["tools"]["eslint"]["run"]
        assert len(config["tools"]["eslint"]["ignore"]) > 0

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=PY_ONLY).config
        assert persisted_config["tools"]["eslint"]["run"]
        assert len(persisted_config["tools"]["eslint"]["ignore"]) > 0
Ejemplo n.º 12
0
def test_check_no_archive() -> None:
    """Validates that check operates without an archive file"""

    runner = CliRunner(mix_stderr=False)
    context = Context(base_path=SIMPLE)
    context.cache.wipe()

    with mod_file(context.baseline_file_path):
        context.baseline_file_path.unlink()
        result = runner.invoke(
            check,
            ["--formatter", "json", "--all",
             str(SIMPLE)],
            obj=Context(base_path=SIMPLE),
        )
        parsed = json.loads(result.stdout)
        assert len(
            parsed) == 5  # Archive contains a single whitelisted finding
Ejemplo n.º 13
0
def test_check_compare_to_head_diffs(monkeypatch: MonkeyPatch) -> None:
    """Validates that check shows issues in staged changes"""
    monkeypatch.chdir(SIMPLE)
    runner = CliRunner(mix_stderr=False)
    Context(SIMPLE).cache.wipe()

    edited = SIMPLE / "bar.py"

    with mod_file(edited):
        with edited.open("a") as stream:
            stream.write("\nprint({'a': 2}).has_key('b')")

        subprocess.run(["git", "add", str(edited)], check=True)
        result = runner.invoke(
            check, ["--formatter", "json", str(SIMPLE)],
            obj=Context(base_path=SIMPLE))
        subprocess.run(["git", "reset", "HEAD", str(edited)])

    parsed = json.loads(result.stdout)
    assert [p["check_id"] for p in parsed] == ["deprecated-has-key"]