from click.testing import CliRunner from gitignore_generator.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 "list" in result.output assert "interactive" 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 "--ide" in result.output def test_generate_python(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() content = (tmp_path / ".gitignore").read_text() assert "__pycache__" in content def test_generate_multiple_templates(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) runner = CliRunner() result = runner.invoke(main, ["generate", "python", "vscode"]) assert result.exit_code == 0 content = (tmp_path / ".gitignore").read_text() assert "__pycache__" in content assert ".vscode" in content def test_generate_to_stdout(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) runner = CliRunner() result = runner.invoke(main, ["generate", "python", "--output", "-"]) assert result.exit_code == 0 assert "__pycache__" in result.output def test_generate_no_templates(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) runner = CliRunner() result = runner.invoke(main, ["generate"]) assert result.exit_code == 1 assert "No templates specified" in result.output def test_list_templates(self): runner = CliRunner() result = runner.invoke(main, ["list"]) assert result.exit_code == 0 assert "Languages" in result.output assert "python" in result.output def test_list_by_category(self): runner = CliRunner() result = runner.invoke(main, ["list", "--category", "languages"]) assert result.exit_code == 0 assert "python" in result.output assert "IDEs" not in result.output def test_search_templates(self): runner = CliRunner() result = runner.invoke(main, ["search", "python"]) assert result.exit_code == 0 assert "python" in result.output def test_search_no_results(self): runner = CliRunner() result = runner.invoke(main, ["search", "nonexistent12345"]) assert result.exit_code == 0 assert "No templates found" in result.output def test_validate_valid_file(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) gitignore = tmp_path / ".gitignore" gitignore.write_text("__pycache__\n*.pyc") runner = CliRunner() result = runner.invoke(main, ["validate", str(gitignore)]) assert result.exit_code == 0 assert "Valid" in result.output def test_validate_invalid_pattern(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) gitignore = tmp_path / ".gitignore" gitignore.write_text("__pycache__\\n") runner = CliRunner() result = runner.invoke(main, ["validate", str(gitignore)]) assert result.exit_code == 0 assert "Invalid" in result.output def test_interactive(self, tmp_path, monkeypatch, capsys): monkeypatch.chdir(tmp_path) runner = CliRunner() result = runner.invoke(main, ["interactive"], input="1\n1\n\n\nn\n.gitignore\n") assert result.exit_code == 0 def test_template_add(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) template_file = tmp_path / "my_template.gitignore" template_file.write_text("# custom template") runner = CliRunner() result = runner.invoke(main, ["template-add", "custom", str(template_file)]) assert result.exit_code == 0 def test_template_export(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) runner = CliRunner() result = runner.invoke(main, ["template-export", "python", "exported.gitignore"]) assert result.exit_code == 0 assert (tmp_path / "exported.gitignore").exists() def test_info_command(self): runner = CliRunner() result = runner.invoke(main, ["info", "python"]) assert result.exit_code == 0 assert "python" in result.output def test_info_nonexistent(self): runner = CliRunner() result = runner.invoke(main, ["info", "nonexistent12345"]) assert result.exit_code != 0 def test_generate_overwrite(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) gitignore = tmp_path / ".gitignore" gitignore.write_text("# existing content") runner = CliRunner() result = runner.invoke(main, ["generate", "python", "--overwrite"]) assert result.exit_code == 0 content = gitignore.read_text() assert "# existing content" not in content assert "__pycache__" in content def test_generate_append(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) gitignore = tmp_path / ".gitignore" gitignore.write_text("# existing content") runner = CliRunner() result = runner.invoke(main, ["generate", "python"]) assert result.exit_code == 0 content = gitignore.read_text() assert "# existing content" in content assert "__pycache__" in content