From c177e29899f589d75302d7fbd2f184e563e63ef8 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 13 May 2023 07:09:03 -0700 Subject: [PATCH] aider: Replaced usage of /dev/null with prompt_toolkit's input mocking functions in tests. # Context: USER: instead of using /dev/null to send no output in the main tests, can we use the prompt_toolkit input mocking functions? ASSISTANT: Yes, we can use the `prompt_toolkit.input` module to mock the input in the tests. Here's how to modify the `tests/test_main.py` file: ```python tests/test_main.py <<<<<<< ORIGINAL import os import sys import tempfile from unittest import TestCase from aider.main import main import subprocess ======= import os import tempfile from unittest import TestCase from aider.main import main import subprocess from prompt_toolkit.input import create_pipe_input from prompt_toolkit.output import DummyOutput >>>>>>> UPDATED ``` Now, update the tests to use the `create_pipe_input` and `DummyOutput`: ```python tests/test_main.py <<<<<<< ORIGINAL with open(os.devnull, "r") as dev_null: save_stdin = sys.stdin sys.stdin = dev_null main([]) sys.stdin = save_stdin ======= pipe_input = create_pipe_input() save_stdin = sys.stdin sys.stdin = pipe_input main([], input=pipe_input, output=DummyOutput()) sys.stdin = save_stdin pipe_input.close() >>>>>>> UPDATED ``` ```python tests/test_main.py <<<<<<< ORIGINAL with open(os.devnull, "r") as dev_null: save_stdin = sys.stdin sys.stdin = dev_null main(["foo.txt"]) sys.stdin = save_stdin self.assertTrue(os.path.exists("foo.txt")) ======= pipe_input = create_pipe_input() save_stdin = sys.stdin sys.stdin = pipe_input main(["foo.txt"], input=pipe_input, output=DummyOutput()) sys.stdin = save_stdin pipe_input.close() self.assertTrue(os.path.exists("foo.txt")) >>>>>>> UPDATED ``` ```python tests/test_main.py <<<<<<< ORIGINAL with open(os.devnull, "r") as dev_null: save_stdin = sys.stdin sys.stdin = dev_null main(["--yes", "foo.txt"]) sys.stdin = save_stdin self.assertTrue(os.path.exists("foo.txt")) ======= pipe_input = create_pipe_input() save_stdin = sys.stdin sys.stdin = pipe_input main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput()) sys.stdin = save_stdin pipe_input.close() self.assertTrue(os.path.exists("foo.txt")) >>>>>>> UPDATED ``` These changes will replace the usage of `/dev/null` with `prompt_toolkit`'s input mocking functions. --- tests/test_main.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 6aa05e97d..b21639aff 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,38 +1,42 @@ import os -import sys import tempfile from unittest import TestCase from aider.main import main import subprocess +from prompt_toolkit.input import create_pipe_input +from prompt_toolkit.output import DummyOutput class TestMain(TestCase): def test_main_with_empty_dir_no_files_on_command(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) - with open(os.devnull, "r") as dev_null: - save_stdin = sys.stdin - sys.stdin = dev_null - main([]) - sys.stdin = save_stdin + pipe_input = create_pipe_input() + save_stdin = sys.stdin + sys.stdin = pipe_input + main([], input=pipe_input, output=DummyOutput()) + sys.stdin = save_stdin + pipe_input.close() def test_main_with_empty_dir_new_file(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) - with open(os.devnull, "r") as dev_null: - save_stdin = sys.stdin - sys.stdin = dev_null - main(["foo.txt"]) - sys.stdin = save_stdin - self.assertTrue(os.path.exists("foo.txt")) + pipe_input = create_pipe_input() + save_stdin = sys.stdin + sys.stdin = pipe_input + main(["foo.txt"], input=pipe_input, output=DummyOutput()) + sys.stdin = save_stdin + pipe_input.close() + self.assertTrue(os.path.exists("foo.txt")) def test_main_with_empty_git_dir_new_file(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) subprocess.run(["git", "init"], cwd=temp_dir) - with open(os.devnull, "r") as dev_null: - save_stdin = sys.stdin - sys.stdin = dev_null - main(["--yes", "foo.txt"]) - sys.stdin = save_stdin - self.assertTrue(os.path.exists("foo.txt")) + pipe_input = create_pipe_input() + save_stdin = sys.stdin + sys.stdin = pipe_input + main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput()) + sys.stdin = save_stdin + pipe_input.close() + self.assertTrue(os.path.exists("foo.txt"))