Example #1
0
def test_enable_tool_found() -> None:
    """Validates that enabling a check updates ignores in config"""

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

    with util.mod_file(context.config_path):
        runner.invoke(enable, ["check", "r2c.eslint", "curly"], obj=context)
        config = context.config
        assert "curly" not in config["tools"]["r2c.eslint"]["ignore"]
Example #2
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 util.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)
        ig = context.file_ignores
        assert "node_modules/" in ig.patterns
Example #3
0
def test_init_py_only() -> None:
    context = Context(base_path=INTEGRATION / "py-only")
    with util.mod_file(context.config_path):
        context.config_path.unlink()
        CliRunner(mix_stderr=False).invoke(init, obj=context)
        config = context.config

    assert "r2c.eslint" not in config["tools"]
    assert "r2c.flake8" in config["tools"]
    assert "r2c.bandit" in config["tools"]
Example #4
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 util.mod_file(context.config_path):
        config = context.config
        assert "run" not in config["tools"]["r2c.eslint"]

        result = runner.invoke(enable, ["tool", "nonexistent"], obj=context)
        assert result.exit_code == 3
Example #5
0
def test_install_config() -> None:
    """Validates that bento installs a config file if none exists"""
    context = Context(base_path=SIMPLE)
    command = InitCommand(context)
    with util.mod_file(context.config_path):
        context.config_path.unlink()
        command._install_config_if_not_exists()
        cfg = context.config
        assert "r2c.eslint" in cfg["tools"]
        assert "r2c.flake8" in cfg["tools"]
        assert "r2c.bandit" in cfg["tools"]
Example #6
0
def test_disable_tool() -> None:
    """Validates that disable tool correctly modifies config"""
    runner = CliRunner()
    context = Context(base_path=SIMPLE)

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

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=SIMPLE).config
        assert persisted_config["tools"]["r2c.eslint"]["run"] is False
Example #7
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 util.mod_file(context.baseline_file_path):
        context.baseline_file_path.unlink()
        result = runner.invoke(
            check, ["--formatter", "json"], obj=Context(base_path=SIMPLE)
        )
        parsed = json.loads(result.stdout)
        assert len(parsed) == 5  # Archive contains a single whitelisted finding
Example #8
0
def test_enable_tool() -> None:
    """Validates that enable tool works with no run"""
    runner = CliRunner()
    context = Context(base_path=SIMPLE)

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

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

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

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

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

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

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

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=SIMPLE).config
        assert persisted_config["tools"]["r2c.eslint"]["run"]
Example #10
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 util.mod_file(context.config_path):
        config = context.config
        # Test is meaningless if tool is already in config
        assert "r2c.eslint" not in config["tools"]

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

        # Check persists to file (Context reads from file)
        persisted_config = Context(base_path=PY_ONLY).config
        assert persisted_config["tools"]["r2c.eslint"]["run"]
        assert len(persisted_config["tools"]["r2c.eslint"]["ignore"]) > 0
Example #11
0
def test_archive_updates_whitelist() -> None:
    """Validates that archive updates the whitelist file"""

    runner = CliRunner()

    context = Context(INTEGRATION / "simple")

    with util.mod_file(context.baseline_file_path) as whitelist:
        runner.invoke(archive, obj=context)
        yml = bento.result.yml_to_violation_hashes(whitelist)

    expectation = {
        "r2c.bandit": {
            "6f77d9d773cc5248ae20b83f80a7b26a",
            "822c79186b1678f5173e166028576865",
        },
        "r2c.eslint": {"6daebd293be00a3d97e19de4a1a39fa5"},
        "r2c.flake8": {
            "27a91174ddbf5e932a1b2cdbd57da9e0",
            "77e04010d3b0256fd3a434cd00f2c944",
        },
    }

    assert yml == expectation