From faa286cf685ada39be33cc3687fe40349a78e9ed Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 03:34:05 +0000 Subject: [PATCH] Add test files for CLI, config, detector, and generator modules --- tests/test_cli.py | 173 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..6e5d9b1 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,173 @@ +from click.testing import CliRunner +from src.cli import main + + +class TestCLI: + def test_main_version(self): + runner = CliRunner() + result = runner.invoke(main, ["--version"]) + assert result.exit_code == 0 + assert "gitignore-generator" in result.output + + def test_main_help(self): + runner = CliRunner() + result = runner.invoke(main, ["--help"]) + assert result.exit_code == 0 + assert "generate" in result.output + assert "detect" in result.output + assert "list" in result.output + assert "wizard" in result.output + + def test_generate_help(self): + runner = CliRunner() + result = runner.invoke(main, ["generate", "--help"]) + assert result.exit_code == 0 + assert "--output" in result.output + assert "--language" in result.output + assert "--framework" in result.output + assert "--ide" in result.output + + def test_generate_python_stdout(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "python", "--stdout"]) + assert result.exit_code == 0 + assert "__pycache__" in result.output + + def test_generate_multiple_types(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke( + main, ["generate", "python", "vscode", "--stdout"] + ) + assert result.exit_code == 0 + assert "__pycache__" in result.output + assert ".vscode" in result.output + + def test_generate_to_file(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "python"]) + assert result.exit_code == 0 + assert (tmp_path / ".gitignore").exists() + + def test_generate_append_to_existing(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + (tmp_path / ".gitignore").write_text("# existing content\n") + runner = CliRunner() + result = runner.invoke(main, ["generate", "python"]) + assert result.exit_code == 0 + content = (tmp_path / ".gitignore").read_text() + assert "# existing content" in content + assert "__pycache__" in content + + def test_generate_overwrite(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + (tmp_path / ".gitignore").write_text("# existing content\n") + runner = CliRunner() + result = runner.invoke(main, ["generate", "python", "--overwrite"]) + assert result.exit_code == 0 + content = (tmp_path / ".gitignore").read_text() + assert "# existing content" not in content + assert "__pycache__" in content + + def test_detect_help(self): + runner = CliRunner() + result = runner.invoke(main, ["detect", "--help"]) + assert result.exit_code == 0 + + def test_detect_no_project(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["detect"]) + assert result.exit_code == 0 + assert "No known project type detected" in result.output + + def test_detect_python_project(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + (tmp_path / "requirements.txt").touch() + runner = CliRunner() + result = runner.invoke(main, ["detect"]) + assert result.exit_code == 0 + assert "python" in result.output + + def test_list_help(self): + runner = CliRunner() + result = runner.invoke(main, ["list", "--help"]) + assert result.exit_code == 0 + + def test_list_all(self): + runner = CliRunner() + result = runner.invoke(main, ["list"]) + assert result.exit_code == 0 + assert "Language" in result.output + assert "Framework" in result.output + assert "IDE" in result.output + + def test_list_by_category(self): + runner = CliRunner() + result = runner.invoke(main, ["list", "--category", "language"]) + assert result.exit_code == 0 + assert "python" in result.output + assert "javascript" in result.output + + def test_generate_no_types_no_detection(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate"]) + assert result.exit_code == 1 + assert "No project type detected" in result.output + + def test_generate_language_option(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "--language", "python"]) + assert result.exit_code == 0 + assert "__pycache__" in (tmp_path / ".gitignore").read_text() + + def test_generate_framework_option(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "--framework", "django"]) + assert result.exit_code == 0 + + def test_generate_ide_option(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "--ide", "vscode"]) + assert result.exit_code == 0 + assert ".vscode" in (tmp_path / ".gitignore").read_text() + + def test_generate_os_option(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "--os", "macos"]) + assert result.exit_code == 0 + + def test_generate_tools_option(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "--tools", "docker"]) + assert result.exit_code == 0 + + def test_generate_invalid_type(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + runner = CliRunner() + result = runner.invoke(main, ["generate", "invalid_type"]) + assert result.exit_code == 2 + + def test_detect_specific_path(self, tmp_path, monkeypatch): + (tmp_path / "requirements.txt").touch() + runner = CliRunner() + result = runner.invoke(main, ["detect", str(tmp_path)]) + assert result.exit_code == 0 + assert "python" in result.output + + def test_detect_invalid_path(self): + runner = CliRunner() + result = runner.invoke(main, ["detect", "/nonexistent/path"]) + assert result.exit_code != 0 + + +import pytest +from pathlib import Path