Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ To show the hidden results again, delete `slither.db.json`.

### Configuration File

Some options can be set through a json configuration file. By default, `slither.config.json` is used if present (it can be changed through `--config-file file.config.json`).
Some options can be set through a json configuration file. By default, `slither.config.json` is used if present, with `slither.conf.json` accepted as an alternative (it can be changed through `--config-file file.config.json`).

Options passed via the CLI have priority over options set in the configuration file.

Expand Down
2 changes: 1 addition & 1 deletion slither/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def parse_args(

group_misc.add_argument(
"--config-file",
help="Provide a config file (default: slither.config.json)",
help="Provide a config file (default: slither.config.json or slither.conf.json)",
action="store",
dest="config_file",
default=None,
Expand Down
4 changes: 2 additions & 2 deletions slither/tools/slither_format/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def parse_args() -> argparse.Namespace:

parser.add_argument(
"--config-file",
help="Provide a config file (default: slither.config.json)",
help="Provide a config file (default: slither.config.json or slither.conf.json)",
action="store",
dest="config_file",
default="slither.config.json",
default=None,
)

group_detector = parser.add_argument_group("Detectors")
Expand Down
12 changes: 8 additions & 4 deletions slither/utils/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ class FailOnLevel(enum.Enum):
}


DEFAULT_CONFIG_FILENAMES = ("slither.config.json", "slither.conf.json")


def read_config_file(args: argparse.Namespace) -> None:
# No config file was provided as an argument
if args.config_file is None:
# Check whether the default config file is present
if os.path.exists("slither.config.json"):
# The default file exists, use it
args.config_file = "slither.config.json"
# Check whether a default config file is present
for candidate in DEFAULT_CONFIG_FILENAMES:
if os.path.exists(candidate):
args.config_file = candidate
break
else:
return

Expand Down
54 changes: 54 additions & 0 deletions tests/unit/utils/test_command_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Tests for slither/utils/command_line.py"""

import argparse
import json
import os

import pytest

from slither.utils.command_line import DEFAULT_CONFIG_FILENAMES, read_config_file


@pytest.fixture
def in_tmp_cwd(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return tmp_path


def _write_json(path, data):
path.write_text(json.dumps(data))


def test_read_config_file_autodetects_config_json(in_tmp_cwd):
_write_json(in_tmp_cwd / "slither.config.json", {"exclude_low": True})
args = argparse.Namespace(config_file=None, exclude_low=False)
read_config_file(args)
assert args.config_file == "slither.config.json"
assert args.exclude_low is True


def test_read_config_file_autodetects_conf_json(in_tmp_cwd):
_write_json(in_tmp_cwd / "slither.conf.json", {"exclude_low": True})
args = argparse.Namespace(config_file=None, exclude_low=False)
read_config_file(args)
assert args.config_file == "slither.conf.json"
assert args.exclude_low is True


def test_read_config_file_prefers_config_over_conf(in_tmp_cwd):
_write_json(in_tmp_cwd / "slither.config.json", {"exclude_low": True})
_write_json(in_tmp_cwd / "slither.conf.json", {"exclude_low": False})
args = argparse.Namespace(config_file=None, exclude_low=False)
read_config_file(args)
assert args.config_file == "slither.config.json"
assert args.exclude_low is True


def test_read_config_file_no_default_present(in_tmp_cwd):
args = argparse.Namespace(config_file=None)
read_config_file(args)
assert args.config_file is None


def test_default_filenames_constant():
assert DEFAULT_CONFIG_FILENAMES == ("slither.config.json", "slither.conf.json")