Fix failing unit tests (#733)

This commit is contained in:
Farkhod Sadykov
2025-10-30 06:33:20 +03:00
committed by GitHub
parent 9615dfbec8
commit a04167c723
6 changed files with 114 additions and 119 deletions
+1 -1
View File
@@ -35,4 +35,4 @@ jobs:
- name: tests
run: |
export OPENAI_API_KEY=test_api_key
pytest tests/ -p no:warnings
pytest tests/ -p no:warnings -v -s
+55 -55
View File
@@ -56,7 +56,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "Prague" in result.stdout
assert "Prague" in result.output
def test_shell(self):
dict_arguments = {
@@ -65,7 +65,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "git commit" in result.stdout
assert "git commit" in result.output
def test_describe_shell(self):
dict_arguments = {
@@ -74,7 +74,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "lists" in result.stdout.lower()
assert "lists" in result.output.lower()
def test_code(self):
"""
@@ -93,10 +93,10 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
print(result.stdout)
print(result.output)
# Since output will be slightly different, there is no way how to test it precisely.
assert "print" in result.stdout
assert "*" in result.stdout
assert "print" in result.output
assert "*" in result.output
with NamedTemporaryFile("w+", delete=False) as file:
try:
compile(result.output, file.name, "exec")
@@ -124,7 +124,7 @@ class TestShellGpt(TestCase):
dict_arguments["prompt"] = "What is my favorite number + 2?"
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "8" in result.stdout
assert "8" in result.output
dict_arguments["--shell"] = True
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 2
@@ -143,14 +143,14 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "docker run" in result.stdout
assert "-p 80:80" in result.stdout
assert "nginx" in result.stdout
assert "docker run" in result.output
assert "-p 80:80" in result.output
assert "nginx" in result.output
dict_arguments["prompt"] = "Also forward port 443."
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "-p 80:80" in result.stdout
assert "-p 443:443" in result.stdout
assert "-p 80:80" in result.output
assert "-p 443:443" in result.output
dict_arguments["--code"] = True
del dict_arguments["--shell"]
assert "--shell" not in dict_arguments
@@ -167,11 +167,11 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "adds" in result.stdout.lower() or "stages" in result.stdout.lower()
assert "adds" in result.output.lower() or "stages" in result.output.lower()
dict_arguments["prompt"] = "'-A'"
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "all" in result.stdout
assert "all" in result.output
def test_chat_code(self):
chat_name = uuid4()
@@ -182,11 +182,11 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "localhost:80" in result.stdout
assert "localhost:80" in result.output
dict_arguments["prompt"] = "Change port to 443."
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "localhost:443" in result.stdout
assert "localhost:443" in result.output
del dict_arguments["--code"]
assert "--code" not in dict_arguments
dict_arguments["--shell"] = True
@@ -197,7 +197,7 @@ class TestShellGpt(TestCase):
def test_list_chat(self):
result = runner.invoke(app, ["--list-chats"])
assert result.exit_code == 0
assert "test_" in result.stdout
assert "test_" in result.output
def test_show_chat(self):
chat_name = uuid4()
@@ -210,9 +210,9 @@ class TestShellGpt(TestCase):
runner.invoke(app, self.get_arguments(**dict_arguments))
result = runner.invoke(app, ["--show-chat", f"test_{chat_name}"])
assert result.exit_code == 0
assert "Remember my favorite number: 6" in result.stdout
assert "What is my favorite number + 2?" in result.stdout
assert "8" in result.stdout
assert "Remember my favorite number: 6" in result.output
assert "What is my favorite number + 2?" in result.output
assert "8" in result.output
def test_validation_code_shell(self):
dict_arguments = {
@@ -222,7 +222,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 2
assert "Only one of --shell, --describe-shell, and --code" in result.stdout
assert "Only one of --shell, --describe-shell, and --code" in result.output
def test_repl_default(
self,
@@ -240,9 +240,9 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(inputs)
)
assert result.exit_code == 0
assert ">>> Please remember my favorite number: 6" in result.stdout
assert ">>> What is my favorite number + 2?" in result.stdout
assert "8" in result.stdout
assert ">>> Please remember my favorite number: 6" in result.output
assert ">>> What is my favorite number + 2?" in result.output
assert "8" in result.output
def test_repl_multiline(
self,
@@ -263,11 +263,11 @@ class TestShellGpt(TestCase):
)
assert result.exit_code == 0
assert '"""' in result.stdout
assert "Please remember my favorite number: 6" in result.stdout
assert "What is my favorite number + 2?" in result.stdout
assert '"""' in result.stdout
assert "8" in result.stdout
assert '"""' in result.output
assert "Please remember my favorite number: 6" in result.output
assert "What is my favorite number + 2?" in result.output
assert '"""' in result.output
assert "8" in result.output
def test_repl_shell(self):
# Temp chat session from previous test should be overwritten.
@@ -281,11 +281,11 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(inputs)
)
assert result.exit_code == 0
assert "type [e] to execute commands" in result.stdout
assert ">>> What is in current folder?" in result.stdout
assert ">>> Simple sort by name" in result.stdout
assert "ls -la" in result.stdout
assert "sort" in result.stdout
assert "type [e] to execute commands" in result.output
assert ">>> What is in current folder?" in result.output
assert ">>> Simple sort by name" in result.output
assert "ls -la" in result.output
assert "sort" in result.output
chat_storage = cfg.get("CHAT_CACHE_PATH")
tmp_chat = Path(chat_storage) / "temp"
chat_messages = json.loads(tmp_chat.read_text())
@@ -313,8 +313,8 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(inputs)
)
assert result.exit_code == 0
assert "install" in result.stdout.lower()
assert "upgrade" in result.stdout.lower()
assert "install" in result.output.lower()
assert "upgrade" in result.output.lower()
chat_storage = cfg.get("CHAT_CACHE_PATH")
tmp_chat = Path(chat_storage) / "temp"
@@ -338,11 +338,11 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(inputs)
)
assert result.exit_code == 0
assert f">>> {inputs[0]}" in result.stdout
assert "requests.get" in result.stdout
assert "localhost:8080" in result.stdout
assert f">>> {inputs[1]}" in result.stdout
assert "localhost:443" in result.stdout
assert f">>> {inputs[0]}" in result.output
assert "requests.get" in result.output
assert "localhost:8080" in result.output
assert f">>> {inputs[1]}" in result.output
assert "localhost:443" in result.output
chat_storage = cfg.get("CHAT_CACHE_PATH")
tmp_chat = Path(chat_storage) / dict_arguments["--repl"]
@@ -356,8 +356,8 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(new_inputs)
)
# Should include previous chat history.
assert "Chat History" in result.stdout
assert f"user: {inputs[1]}" in result.stdout
assert "Chat History" in result.output
assert f"user: {inputs[1]}" in result.output
def test_zsh_command(self):
"""
@@ -372,12 +372,12 @@ class TestShellGpt(TestCase):
"--shell": True,
}
result = runner.invoke(app, self.get_arguments(**dict_arguments), input="y\n")
stdout = result.stdout.strip()
stdout = result.output.strip()
print(stdout)
# TODO: Fix this test.
# Not sure how os.system pipes the output to stdout,
# but it is not part of the result.stdout.
# assert "command not found" not in result.stdout
# but it is not part of the result.output.
# assert "command not found" not in result.output
# assert "hello world" in stdout.split("\n")[-1]
@patch("sgpt.handlers.handler.Handler.get_completion")
@@ -408,7 +408,7 @@ class TestShellGpt(TestCase):
def test_simple_stdin(self):
result = runner.invoke(app, input="What is the capital of Germany?\n")
assert "Berlin" in result.stdout
assert "Berlin" in result.output
def test_shell_stdin_with_prompt(self):
dict_arguments = {
@@ -417,8 +417,8 @@ class TestShellGpt(TestCase):
}
stdin = "What is in current folder\n"
result = runner.invoke(app, self.get_arguments(**dict_arguments), input=stdin)
assert "ls" in result.stdout
assert "sort" in result.stdout
assert "ls" in result.output
assert "sort" in result.output
def test_role(self):
test_role = Path(cfg.get("ROLE_STORAGE_PATH")) / "json_generator.json"
@@ -440,7 +440,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "json_generator" in result.stdout
assert "json_generator" in result.output
dict_arguments = {
"prompt": "test",
@@ -448,7 +448,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "You are json_generator" in result.stdout
assert "You are json_generator" in result.output
# Test with command line argument prompt.
dict_arguments = {
@@ -457,7 +457,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
generated_json = json.loads(result.stdout)
generated_json = json.loads(result.output)
assert "username" in generated_json
assert "password" in generated_json
assert "email" in generated_json
@@ -470,7 +470,7 @@ class TestShellGpt(TestCase):
stdin = "random username, password, email"
result = runner.invoke(app, self.get_arguments(**dict_arguments), input=stdin)
assert result.exit_code == 0
generated_json = json.loads(result.stdout)
generated_json = json.loads(result.output)
assert "username" in generated_json
assert "password" in generated_json
assert "email" in generated_json
@@ -485,7 +485,7 @@ class TestShellGpt(TestCase):
assert result.exit_code == 0
# Can't really test it since stdin in disable for --shell flag.
# for word in ("prints", "hello", "console"):
# assert word in result.stdout
# assert word in result.output
def test_version(self):
dict_arguments = {
@@ -493,6 +493,6 @@ class TestShellGpt(TestCase):
"--version": True,
}
result = runner.invoke(app, self.get_arguments(**dict_arguments), input="d\n")
assert __version__ in result.stdout
assert __version__ in result.output
# TODO: Implement function call tests.
+13 -13
View File
@@ -18,7 +18,7 @@ def test_code_generation(completion):
completion.assert_called_once_with(**comp_args(role, args["prompt"]))
assert result.exit_code == 0
assert "print('Hello World')" in result.stdout
assert "print('Hello World')" in result.output
@patch("sgpt.printer.TextPrinter.live_print")
@@ -47,8 +47,8 @@ def test_code_generation_stdin(completion):
expected_prompt = f"{stdin}\n\n{args['prompt']}"
completion.assert_called_once_with(**comp_args(role, expected_prompt))
assert result.exit_code == 0
assert "# Hello" in result.stdout
assert "print('Hello')" in result.stdout
assert "# Hello" in result.output
assert "print('Hello')" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -64,14 +64,14 @@ def test_code_chat(completion):
args = {"prompt": "print hello", "--code": True, "--chat": chat_name}
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
assert "print('hello')" in result.stdout
assert "print('hello')" in result.output
assert chat_path.exists()
args["prompt"] = "also print world"
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
assert "print('hello')" in result.stdout
assert "print('world')" in result.stdout
assert "print('hello')" in result.output
assert "print('world')" in result.output
expected_messages = [
{"role": "system", "content": role.role},
@@ -87,7 +87,7 @@ def test_code_chat(completion):
args["--shell"] = True
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
chat_path.unlink()
# TODO: Code chat can be recalled without --code option.
@@ -118,10 +118,10 @@ def test_code_repl(completion):
assert completion.call_count == 2
assert result.exit_code == 0
assert ">>> print hello" in result.stdout
assert "print('hello')" in result.stdout
assert ">>> also print world" in result.stdout
assert "print('world')" in result.stdout
assert ">>> print hello" in result.output
assert "print('hello')" in result.output
assert ">>> also print world" in result.output
assert "print('world')" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -131,7 +131,7 @@ def test_code_and_shell(completion):
completion.assert_not_called()
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -141,4 +141,4 @@ def test_code_and_describe_shell(completion):
completion.assert_not_called()
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
+22 -22
View File
@@ -23,7 +23,7 @@ def test_default(completion):
completion.assert_called_once_with(**comp_args(role, **args))
assert result.exit_code == 0
assert "Prague" in result.stdout
assert "Prague" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -35,7 +35,7 @@ def test_default_stdin(completion):
completion.assert_called_once_with(**comp_args(role, stdin))
assert result.exit_code == 0
assert "Prague" in result.stdout
assert "Prague" in result.output
@patch("rich.console.Console.print")
@@ -85,13 +85,13 @@ def test_default_chat(completion):
args = {"prompt": "my number is 2", "--chat": chat_name}
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
assert "ok" in result.stdout
assert "ok" in result.output
assert chat_path.exists()
args["prompt"] = "my number + 2?"
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
assert "4" in result.stdout
assert "4" in result.output
expected_messages = [
{"role": "system", "content": role.role},
@@ -106,24 +106,24 @@ def test_default_chat(completion):
result = runner.invoke(app, ["--list-chats"])
assert result.exit_code == 0
assert "_test" in result.stdout
assert "_test" in result.output
result = runner.invoke(app, ["--show-chat", chat_name])
assert result.exit_code == 0
assert "my number is 2" in result.stdout
assert "ok" in result.stdout
assert "my number + 2?" in result.stdout
assert "4" in result.stdout
assert "my number is 2" in result.output
assert "ok" in result.output
assert "my number + 2?" in result.output
assert "4" in result.output
args["--shell"] = True
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
args["--code"] = True
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
chat_path.unlink()
@@ -150,10 +150,10 @@ def test_default_repl(completion):
assert completion.call_count == 2
assert result.exit_code == 0
assert ">>> my number is 6" in result.stdout
assert "ok" in result.stdout
assert ">>> my number + 2?" in result.stdout
assert "8" in result.stdout
assert ">>> my number is 6" in result.output
assert "ok" in result.output
assert ">>> my number + 2?" in result.output
assert "8" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -183,11 +183,11 @@ def test_default_repl_stdin(completion):
assert completion.call_count == 2
assert result.exit_code == 0
assert "this is stdin" in result.stdout
assert ">>> prompt" in result.stdout
assert "ok init" in result.stdout
assert ">>> another" in result.stdout
assert "ok another" in result.stdout
assert "this is stdin" in result.output
assert ">>> prompt" in result.output
assert "ok init" in result.output
assert ">>> another" in result.output
assert "ok another" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -212,7 +212,7 @@ def test_llm_options(completion):
)
completion.assert_called_once_with(**expected_args)
assert result.exit_code == 0
assert "Berlin" in result.stdout
assert "Berlin" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -221,7 +221,7 @@ def test_version(completion):
result = runner.invoke(app, cmd_args(**args))
completion.assert_not_called()
assert __version__ in result.stdout
assert __version__ in result.output
@patch("sgpt.printer.TextPrinter.live_print")
+4 -4
View File
@@ -23,13 +23,13 @@ def test_role(completion):
result = runner.invoke(app, cmd_args(**args))
completion.assert_not_called()
assert result.exit_code == 0
assert "json_gen_test" in result.stdout
assert "json_gen_test" in result.output
args = {"--show-role": "json_gen_test"}
result = runner.invoke(app, cmd_args(**args))
completion.assert_not_called()
assert result.exit_code == 0
assert "you are a JSON generator" in result.stdout
assert "you are a JSON generator" in result.output
# Test with argument prompt.
args = {
@@ -40,7 +40,7 @@ def test_role(completion):
role = SystemRole.get("json_gen_test")
completion.assert_called_once_with(**comp_args(role, args["prompt"]))
assert result.exit_code == 0
generated_json = json.loads(result.stdout)
generated_json = json.loads(result.output)
assert "foo" in generated_json
# Test with stdin prompt.
@@ -50,6 +50,6 @@ def test_role(completion):
result = runner.invoke(app, cmd_args(**args), input=stdin)
completion.assert_called_with(**comp_args(role, stdin))
assert result.exit_code == 0
generated_json = json.loads(result.stdout)
generated_json = json.loads(result.output)
assert "foo" in generated_json
path.unlink(missing_ok=True)
+19 -24
View File
@@ -17,9 +17,8 @@ def test_shell(completion):
result = runner.invoke(app, cmd_args(**args))
completion.assert_called_once_with(**comp_args(role, args["prompt"]))
assert result.exit_code == 0
assert "git commit" in result.stdout
assert "[E]xecute, [M]odify, [D]escribe, [A]bort:" in result.stdout
assert "git commit" in result.output
assert "[E]xecute, [M]odify, [D]escribe, [A]bort:" in result.output
@patch("sgpt.printer.TextPrinter.live_print")
@@ -29,9 +28,8 @@ def test_shell_no_markdown(completion, markdown_printer, text_printer):
completion.return_value = mock_comp("git commit -m test")
args = {"prompt": "make a commit using git", "--shell": True, "--md": True}
result = runner.invoke(app, cmd_args(**args))
runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
# Should ignore --md for --shell option and output text without markdown.
markdown_printer.assert_not_called()
text_printer.assert_called()
@@ -48,9 +46,8 @@ def test_shell_stdin(completion):
expected_prompt = f"{stdin}\n\n{args['prompt']}"
completion.assert_called_once_with(**comp_args(role, expected_prompt))
assert result.exit_code == 0
assert "ls -l | sort" in result.stdout
assert "[E]xecute, [M]odify, [D]escribe, [A]bort:" in result.stdout
assert "ls -l | sort" in result.output
assert "[E]xecute, [M]odify, [D]escribe, [A]bort:" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -63,7 +60,7 @@ def test_describe_shell(completion):
completion.assert_called_once_with(**comp_args(role, args["prompt"]))
assert result.exit_code == 0
assert "lists" in result.stdout
assert "lists" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -78,7 +75,7 @@ def test_describe_shell_stdin(completion):
expected_prompt = f"{stdin}"
completion.assert_called_once_with(**comp_args(role, expected_prompt))
assert result.exit_code == 0
assert "lists" in result.stdout
assert "lists" in result.output
@patch("os.system")
@@ -91,8 +88,8 @@ def test_shell_run_description(completion, system):
shell = os.environ.get("SHELL", "/bin/sh")
system.assert_called_once_with(f"{shell} -c 'echo hello'")
assert result.exit_code == 0
assert "echo hello" in result.stdout
assert "prints hello" in result.stdout
assert "echo hello" in result.output
assert "prints hello" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -105,14 +102,12 @@ def test_shell_chat(completion):
args = {"prompt": "list folder", "--shell": True, "--chat": chat_name}
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
assert "ls" in result.stdout
assert "ls" in result.output
assert chat_path.exists()
args["prompt"] = "sort by name"
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 0
assert "ls | sort" in result.stdout
assert "ls | sort" in result.output
expected_messages = [
{"role": "system", "content": role.role},
@@ -128,7 +123,7 @@ def test_shell_chat(completion):
args["--code"] = True
result = runner.invoke(app, cmd_args(**args))
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
chat_path.unlink()
# TODO: Shell chat can be recalled without --shell option.
@@ -160,10 +155,10 @@ def test_shell_repl(completion, mock_system):
assert completion.call_count == 2
assert result.exit_code == 0
assert ">>> list folder" in result.stdout
assert "ls" in result.stdout
assert ">>> sort by name" in result.stdout
assert "ls | sort" in result.stdout
assert ">>> list folder" in result.output
assert "ls" in result.output
assert ">>> sort by name" in result.output
assert "ls | sort" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -173,7 +168,7 @@ def test_shell_and_describe_shell(completion):
completion.assert_not_called()
assert result.exit_code == 2
assert "Error" in result.stdout
assert "Error" in result.output
@patch("sgpt.handlers.handler.completion")
@@ -190,5 +185,5 @@ def test_shell_no_interaction(completion):
completion.assert_called_once_with(**comp_args(role, args["prompt"]))
assert result.exit_code == 0
assert "git commit" in result.stdout
assert "[E]xecute" not in result.stdout
assert "git commit" in result.output
assert "[E]xecute" not in result.output