Skip to content

Commit 8858ca3

Browse files
committed
Move tests for cli all into one place
1 parent 671ddfa commit 8858ca3

File tree

2 files changed

+85
-87
lines changed

2 files changed

+85
-87
lines changed

tests/test_cli.py

Lines changed: 85 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
from aicodebot import version as aicodebot_version
22
from aicodebot.cli import cli
33
from aicodebot.config import read_config
4+
from aicodebot.helpers import create_and_write_file
45
from aicodebot.prompts import DEFAULT_PERSONALITY
6+
from git import Repo
57
from pathlib import Path
68
import os, pytest
79

810

9-
def test_version(cli_runner):
10-
result = cli_runner.invoke(cli, ["-V"])
11+
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
12+
def test_alignment(cli_runner):
13+
result = cli_runner.invoke(cli, ["alignment", "-t", "50"])
1114
assert result.exit_code == 0, f"Output: {result.output}"
12-
assert aicodebot_version in result.output
1315

1416

15-
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
16-
def test_fun_fact(cli_runner):
17-
result = cli_runner.invoke(cli, ["fun-fact", "-t", "50"])
18-
assert result.exit_code == 0, f"Output: {result.output}"
17+
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="skipping live tests without an api key.")
18+
def test_configure(cli_runner, tmp_path, monkeypatch):
19+
key = os.getenv("OPENAI_API_KEY")
1920

21+
temp_config_file = Path(tmp_path / ".aicodebot.test.yaml")
22+
# set aicodebot_config_file to the temp config file
23+
monkeypatch.setenv("AICODEBOT_CONFIG_FILE", str(temp_config_file))
2024

21-
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
22-
def test_alignment(cli_runner):
23-
result = cli_runner.invoke(cli, ["alignment", "-t", "50"])
24-
assert result.exit_code == 0, f"Output: {result.output}"
25+
assert read_config() is None
26+
27+
# run the setup command, should work with the env var set
28+
result = cli_runner.invoke(cli, ["configure"])
29+
30+
# check if the command was successful
31+
assert result.exit_code == 0, f"output: {result.output}"
32+
# check if the config file was created
33+
assert Path(temp_config_file).exists()
34+
35+
# load the config file
36+
config_data = read_config()
37+
# check if the config file contains the correct data
38+
assert config_data["openai_api_key"] == key
39+
assert config_data["personality"] == DEFAULT_PERSONALITY.name
40+
41+
# remove the config file
42+
Path.unlink(temp_config_file)
43+
assert read_config() is None
44+
45+
# now unset the env var and run the command again with it passed as a flag
46+
monkeypatch.setenv("OPENAI_API_KEY", "")
47+
assert not os.getenv("OPENAI_API_KEY")
48+
49+
result = cli_runner.invoke(cli, ["configure", "--openai-api-key", key])
50+
assert result.exit_code == 0, f"output: {result.output}"
51+
52+
# load the config file
53+
config_data = read_config()
54+
# check if the config file contains the correct data
55+
assert config_data["openai_api_key"] == key
56+
assert config_data["personality"] == DEFAULT_PERSONALITY.name
2557

2658

2759
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
@@ -40,55 +72,60 @@ def test_debug_failure(cli_runner):
4072

4173

4274
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
43-
def test_sidekick(cli_runner):
44-
# Define a mock request and file context
45-
mock_request = "What is 3 + 2? Just give me the answer, nothing else. Use a number, not text"
46-
mock_files = [".gitignore"]
75+
def test_fun_fact(cli_runner):
76+
result = cli_runner.invoke(cli, ["fun-fact", "-t", "50"])
77+
assert result.exit_code == 0, f"Output: {result.output}"
4778

48-
# Invoke the sidekick command
49-
result = cli_runner.invoke(cli, ["sidekick", "-t", "100", "--request", mock_request] + mock_files)
5079

51-
assert result.exit_code == 0, f"Output: {result.output}"
52-
assert "5" in result.output
80+
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
81+
def test_commit(cli_runner, temp_git_repo):
82+
with cli_runner.isolated_filesystem():
83+
os.chdir(temp_git_repo.working_dir) # change to the temporary repo directory
84+
create_and_write_file("test.txt", "This is a test file.")
85+
result = cli_runner.invoke(cli, ["commit", "-y"])
86+
assert result.exit_code == 0
87+
assert "The following files will be committed:\ntest.txt" in result.output
88+
89+
# Check the last commit message in the repository
90+
repo = Repo(temp_git_repo.working_dir)
91+
last_commit_message = repo.head.commit.message
92+
assert len(last_commit_message) > 10
5393

5494

5595
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
56-
def test_configure(cli_runner, tmp_path, monkeypatch):
57-
key = os.getenv("OPENAI_API_KEY")
96+
def test_review(cli_runner, temp_git_repo):
97+
with cli_runner.isolated_filesystem():
98+
os.chdir(temp_git_repo.working_dir) # change to the temporary repo directory
5899

59-
temp_config_file = Path(tmp_path / ".aicodebot.test.yaml")
60-
# set AICODEBOT_CONFIG_FILE to the temp config file
61-
monkeypatch.setenv("AICODEBOT_CONFIG_FILE", str(temp_config_file))
100+
# Add a new file
101+
create_and_write_file("test.txt", "Adding a new line.")
62102

63-
assert read_config() is None
103+
repo = Repo(temp_git_repo.working_dir)
104+
# Stage the new file
105+
repo.git.add("test.txt")
64106

65-
# Run the setup command, should work with the env var set
66-
result = cli_runner.invoke(cli, ["configure"])
107+
# Run the review command
108+
result = cli_runner.invoke(cli, ["review"])
67109

68-
# Check if the command was successful
69-
assert result.exit_code == 0, f"Output: {result.output}"
70-
# Check if the config file was created
71-
assert Path(temp_config_file).exists()
110+
# Check that the review command ran successfully
111+
assert result.exit_code == 0
112+
assert len(result.output) > 20
72113

73-
# Load the config file
74-
config_data = read_config()
75-
# Check if the config file contains the correct data
76-
assert config_data["openai_api_key"] == key
77-
assert config_data["personality"] == "Her"
78114

79-
# Remove the config file
80-
Path.unlink(temp_config_file)
81-
assert read_config() is None
115+
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
116+
def test_sidekick(cli_runner):
117+
# Define a mock request and file context
118+
mock_request = "What is 3 + 2? Just give me the answer, nothing else. Use a number, not text"
119+
mock_files = [".gitignore"]
82120

83-
# Now unset the env var and run the command again with it passed as a flag
84-
monkeypatch.setenv("OPENAI_API_KEY", "")
85-
assert os.getenv("OPENAI_API_KEY") == ""
121+
# Invoke the sidekick command
122+
result = cli_runner.invoke(cli, ["sidekick", "-t", "100", "--request", mock_request] + mock_files)
86123

87-
result = cli_runner.invoke(cli, ["configure", "--openai-api-key", key])
88124
assert result.exit_code == 0, f"Output: {result.output}"
125+
assert "5" in result.output
89126

90-
# Load the config file
91-
config_data = read_config()
92-
# Check if the config file contains the correct data
93-
assert config_data["openai_api_key"] == key
94-
assert config_data["personality"] == DEFAULT_PERSONALITY.name
127+
128+
def test_version(cli_runner):
129+
result = cli_runner.invoke(cli, ["-V"])
130+
assert result.exit_code == 0, f"output: {result.output}"
131+
assert aicodebot_version in result.output

tests/test_workflow.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)