diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py new file mode 100644 index 0000000..2bc4528 --- /dev/null +++ b/tests/unit/test_config.py @@ -0,0 +1,133 @@ +"""Tests for configuration loading.""" + +import tempfile +from pathlib import Path + +import pytest + +from depcheck.config import Config, load_config, should_ignore_package, should_ignore_file +from depcheck.models import Severity + + +class TestConfig: + """Tests for configuration.""" + + def test_default_config(self): + """Test default configuration values.""" + config = Config() + + assert config.ignore_patterns == [] + assert config.ignore_packages == [] + assert config.fail_level == Severity.MEDIUM + assert config.output_format == "terminal" + assert config.verbose is False + assert config.quiet is False + assert config.include_dev is True + + def test_load_empty_yaml(self): + """Test loading empty YAML config.""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: + f.write("") + f.flush() + + config = load_config(Path(f.name)) + + assert config.ignore_patterns == [] + + def test_load_config_with_ignore_patterns(self): + """Test loading config with ignore patterns.""" + content = """ +ignore_patterns: + - \"test/\" + - \"example/\" +""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: + f.write(content) + f.flush() + + config = load_config(Path(f.name)) + + assert len(config.ignore_patterns) == 2 + assert "test/" in config.ignore_patterns + + def test_load_config_with_fail_level(self): + """Test loading config with fail level.""" + content = """ +fail_level: high +""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: + f.write(content) + f.flush() + + config = load_config(Path(f.name)) + + assert config.fail_level == Severity.HIGH + + def test_invalid_fail_level_uses_default(self): + """Test that invalid fail level uses default.""" + content = """ +fail_level: invalid +""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: + f.write(content) + f.flush() + + config = load_config(Path(f.name)) + + assert config.fail_level == Severity.MEDIUM + + def test_load_config_with_output_settings(self): + """Test loading config with output settings.""" + content = """ +output: + format: json + verbose: true + quiet: false +""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: + f.write(content) + f.flush() + + config = load_config(Path(f.name)) + + assert config.output_format == "json" + assert config.verbose is True + assert config.quiet is False + + def test_load_config_with_ignore_packages(self): + """Test loading config with ignored packages.""" + content = """ +ignore_packages: + - \"@types/*\" + - \"*test*\" +""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: + f.write(content) + f.flush() + + config = load_config(Path(f.name)) + + assert len(config.ignore_packages) == 2 + + def test_should_ignore_package(self): + """Test package ignore checking.""" + config = Config(ignore_packages=["@types/*", "*test*"]) + + assert should_ignore_package(config, "@types/node") is True + assert should_ignore_package(config, "test-utils") is True + assert should_ignore_package(config, "express") is False + + def test_should_ignore_file(self): + """Test file ignore checking.""" + config = Config(ignore_patterns=["test/", "node_modules/"]) + + assert should_ignore_file(config, Path("/project/test/file.txt")) is True + assert should_ignore_file(config, Path("/project/node_modules/pkg/file.txt")) is True + assert should_ignore_file(config, Path("/project/src/file.txt")) is False + + def test_missing_config_returns_defaults(self): + """Test that missing config file returns defaults.""" + config = load_config(Path("/nonexistent/config.yaml")) + + assert config.ignore_patterns == [] + assert config.fail_level == Severity.MEDIUM