Commit graph

1214 commits

Author SHA1 Message Date
Paul Gauthier
bcf258f1c6 aider: Modified test to check if coder.abs_fnames contains both files.
# Context:
USER: don't compare the result string. check that coder.abs_fnames now contains both files
ASSISTANT: To update the test to check if `coder.abs_fnames` contains both files, modify the test as follows:

```python
tests/test_coder.py
<<<<<<< ORIGINAL
        # Check if the result is as expected
        self.assertEqual(result, "Added files: file1.txt, file2.py")
=======
        # Check if coder.abs_fnames contains both files
        expected_files = {os.path.abspath("file1.txt"), os.path.abspath("file2.py")}
        self.assertEqual(coder.abs_fnames, expected_files)
>>>>>>> UPDATED
```

This change will ensure that the test checks if `coder.abs_fnames` contains both files instead of comparing the result string.
2023-05-27 08:28:31 -07:00
Paul Gauthier
4ac2143cc2 Initialized Coder object with mocked IO and mocked repo, and updated coder.repo instead of coder.root. 2023-05-27 08:27:45 -07:00
Paul Gauthier
0fc40df8e6 aider: Replaced manual setting of abs_fnames with a mocked git.Repo object that has its ls_files method return the desired file paths.
# Context:
USER: don't set the files in abs_fnames. patch self.repo.git.ls_files to return them
ASSISTANT: To patch `self.repo.git.ls_files` to return the files in `abs_fnames`, we will modify the `test_check_for_file_mentions` function in the `tests/test_coder.py` file.

```python
tests/test_coder.py
<<<<<<< ORIGINAL
        # Initialize the Coder object with the mocked IO
        coder = Coder(io=mock_io, openai_api_key="fake_key")

        # Add sample files to the Coder object
        coder.abs_fnames = {
            "/path/to/repo/file1.txt",
            "/path/to/repo/file2.py",
        }
        coder.root = "/path/to/repo"
=======
        # Mock the git repo
        mock_repo = MagicMock()
        mock_repo.git.ls_files.return_value = "file1.txt\nfile2.py"

        # Initialize the Coder object with the mocked IO and mocked repo
        coder = Coder(io=mock_io, openai_api_key="fake_key", repo=mock_repo)
        coder.root = "/path/to/repo"
>>>>>>> UPDATED
```

This change replaces the manual setting of `abs_fnames` with a mocked `git.Repo` object that has its `ls_files` method return the desired file paths.
2023-05-27 08:26:16 -07:00
Paul Gauthier
475f85bcc3 aider: Added a test function for the check_for_file_mentions method in tests/test_coder.py.
# Context:
USER: write a test for check_for_file_mentions
ASSISTANT: To write a test for the `check_for_file_mentions` method, we will create a new test function in the `tests/test_coder.py` file. Here's the test function:

```python
tests/test_coder.py
<<<<<<< ORIGINAL
=======
import unittest
from unittest.mock import MagicMock
from aider.coder import Coder

class TestCoder(unittest.TestCase):
    def test_check_for_file_mentions(self):
        # Mock the IO object
        mock_io = MagicMock()
        mock_io.get_input.return_value = "Please check file1.txt and file2.py"
        mock_io.confirm_ask.return_value = True

        # Initialize the Coder object with the mocked IO
        coder = Coder(io=mock_io, openai_api_key="fake_key")

        # Add sample files to the Coder object
        coder.abs_fnames = {
            "/path/to/repo/file1.txt",
            "/path/to/repo/file2.py",
        }
        coder.root = "/path/to/repo"

        # Call the check_for_file_mentions method
        result = coder.check_for_file_mentions("Please check file1.txt and file2.py")

        # Check if the result is as expected
        self.assertEqual(result, "Added files: file1.txt, file2.py")

if __name__ == "__main__":
    unittest.main()
>>>>>>> UPDATED
```

This test function creates a `Coder` object with a mocked IO, adds sample files to the object, and then calls the `check_for_file_mentions` method with a sample input. It checks if the returned result is as expected.
2023-05-27 08:23:51 -07:00
Paul Gauthier
dd3bdfbe15 Added new files to the git repo: tests/test_coder.py 2023-05-27 08:22:37 -07:00
Paul Gauthier
dbb4870021 if the user mentions a file, add it to the chat 2023-05-27 08:11:41 -07:00
Paul Gauthier
090b46983c Explain /help on launch 2023-05-27 07:51:56 -07:00
Paul Gauthier
e0de81fea6 copy 2023-05-27 07:42:21 -07:00
Paul Gauthier
976c5c8e74 Handle missing api key gracefully 2023-05-27 06:35:50 -07:00
Paul Gauthier
b0d860bcf4 Handle missing api key gracefully 2023-05-27 06:35:37 -07:00
Paul Gauthier
3f5e4130ea fixed test 2023-05-27 06:25:16 -07:00
Paul Gauthier
82e7dcb7ba updated usage 2023-05-27 06:24:22 -07:00
Paul Gauthier
9832807814 aider: Added custom exception MissingAPIKeyError to Coder class to raise an error when no OpenAI API key is provided.
# Context:
USER: what's a good exception to raise in coder if no api key
ASSISTANT: A good exception to raise in this case would be a custom exception, such as `MissingAPIKeyError`. You can define this custom exception by subclassing the built-in `ValueError` class. Here's how to define and raise the custom exception:

```python
aider/coder.py
<<<<<<< ORIGINAL
class Coder:
    abs_fnames = None
    repo = None
    last_aider_commit_hash = None
    last_asked_for_commit_time = 0

    def __init__(
        self,
        io,
        main_model="gpt-4",
        fnames=None,
        pretty=True,
        show_diffs=False,
        auto_commits=True,
        dirty_commits=True,
        dry_run=False,
        use_ctags=False,
        verbose=False,
        openai_api_key=None,
    ):
        if openai_api_key:
            openai.api_key = openai_api_key
        else:
            io.tool_error('No OpenAI API key provided.')
=======
class MissingAPIKeyError(ValueError):
    pass

class Coder:
    abs_fnames = None
    repo = None
    last_aider_commit_hash = None
    last_asked_for_commit_time = 0

    def __init__(
        self,
        io,
        main_model="gpt-4",
        fnames=None,
        pretty=True,
        show_diffs=False,
        auto_commits=True,
        dirty_commits=True,
        dry_run=False,
        use_ctags=False,
        verbose=False,
        openai_api_key=None,
    ):
        if openai_api_key:
            openai.api_key = openai_api_key
        else:
            raise MissingAPIKeyError('No OpenAI API key provided.')
>>>>>>> UPDATED
```

This custom exception `MissingAPIKeyError` will provide a more specific and informative error message when the API key is not provided.
2023-05-27 06:21:06 -07:00
Paul Gauthier
abf73d00a2 aider: Added --openai-api-key argument to the command-line parser, passed it to the Coder class, and set the openai.api_key accordingly.
# Context:
USER: add a new arg --openai-api-key. pass it to Coder
ASSISTANT: I will add a new argument `--openai-api-key` and pass it to the `Coder` class. Here are the changes:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--no-dirty-commits",
        action="store_false",
        dest="dirty_commits",
        help="Disable dirty commit of changes",
    )
=======
    parser.add_argument(
        "--no-dirty-commits",
        action="store_false",
        dest="dirty_commits",
        help="Disable dirty commit of changes",
    )
    parser.add_argument(
        "--openai-api-key",
        metavar="OPENAI_API_KEY",
        help="Specify the OpenAI API key",
    )
>>>>>>> UPDATED
```

```python
aider/main.py
<<<<<<< ORIGINAL
    coder = Coder(
        io,
        main_model=args.model,
        fnames=args.files,
        pretty=args.pretty,
        show_diffs=args.show_diffs,
        auto_commits=args.auto_commits,
        dirty_commits=args.dirty_commits,
        dry_run=args.dry_run,
        use_ctags=args.ctags,
        verbose=args.verbose,
    )
=======
    coder = Coder(
        io,
        main_model=args.model,
        fnames=args.files,
        pretty=args.pretty,
        show_diffs=args.show_diffs,
        auto_commits=args.auto_commits,
        dirty_commits=args.dirty_commits,
        dry_run=args.dry_run,
        use_ctags=args.ctags,
        verbose=args.verbose,
        openai_api_key=args.openai_api_key,
    )
>>>>>>> UPDATED
```

```python
aider/coder.py
<<<<<<< ORIGINAL
openai.api_key = os.getenv("OPENAI_API_KEY")

class Coder:
    abs_fnames = None
    repo = None
    last_aider_commit_hash = None
    last_asked_for_commit_time = 0

    def __init__(
        self,
        io,
        main_model="gpt-4",
        fnames=None,
        pretty=True,
        show_diffs=False,
        auto_commits=True,
        dirty_commits=True,
        dry_run=False,
        use_ctags=False,
        verbose=False,
    ):
=======
class Coder:
    abs_fnames = None
    repo = None
    last_aider_commit_hash = None
    last_asked_for_commit_time = 0

    def __init__(
        self,
        io,
        main_model="gpt-4",
        fnames=None,
        pretty=True,
        show_diffs=False,
        auto_commits=True,
        dirty_commits=True,
        dry_run=False,
        use_ctags=False,
        verbose=False,
        openai_api_key=None,
    ):
        if openai_api_key:
            openai.api_key = openai_api_key
        else:
            openai.api_key = os.getenv("OPENAI_API_KEY")
>>>>>>> UPDATED
```

These changes add the `--openai-api-key` argument to the command-line parser, pass it to the `Coder` class, and set the `openai.api_key` accordingly.
2023-05-27 06:17:11 -07:00
Paul Gauthier
139a141be9 removed dotenv 2023-05-27 06:13:34 -07:00
Paul Gauthier
1fb1765b38 drop dotenv 2023-05-27 06:06:51 -07:00
Paul Gauthier
3e99b4f45c moved test_main_pretty_from_env to end of file 2023-05-27 05:20:10 -07:00
Paul Gauthier
89c412ccfe aider: Added a new test method test_main_pretty_from_env to check if the pretty option is set to False by creating a temporary directory, writing a .env file with AIDER_PRETTY=false, and then calling main with no arguments.
# Context:
USER: add a test like test_main_args. it should test pretty by making a .env file with AIDER_PRETTY=false and then call main with no args.
ASSISTANT: To add a new test for the `pretty` option, you can create a new test method in the `TestMain` class in the `tests/test_main.py` file. Here's the code for the new test:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_args(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)

            with patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"])
                _, kwargs = MockCoder.call_args
                assert kwargs["auto_commits"] is False
=======
    def test_main_args(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)

            with patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"])
                _, kwargs = MockCoder.call_args
                assert kwargs["auto_commits"] is False

    def test_main_pretty_from_env(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            with open(".env", "w") as env_file:
                env_file.write("AIDER_PRETTY=false\n")

            with patch("aider.main.Coder") as MockCoder:
                main([])
                _, kwargs = MockCoder.call_args
                assert kwargs["pretty"] is False
>>>>>>> UPDATED
```

This new test method, `test_main_pretty_from_env`, creates a temporary directory, writes a `.env` file with `AIDER_PRETTY=false`, and then calls `main` with no arguments. It checks if the `pretty` option is set to `False` as expected.
2023-05-27 05:18:26 -07:00
Paul Gauthier
e02b612126 aider: Added functionality to load .env file from git_root if available.
# Context:
USER: load_dotenv from the git_root if avail
ASSISTANT: To load the `.env` file from the `git_root` if available, you can modify the `load_dotenv()` function call in `aider/main.py`. Here's the change:

```python
aider/main.py
<<<<<<< ORIGINAL
    load_dotenv()
=======
    if git_root:
        load_dotenv(os.path.join(git_root, ".env"))
    else:
        load_dotenv()
>>>>>>> UPDATED
```

This change checks if `git_root` is available and loads the `.env` file from the `git_root` directory. If `git_root` is not available, it falls back to the default behavior of loading the `.env` file from the current working directory.
2023-05-27 05:15:05 -07:00
Paul Gauthier
a1ff34d084 updated usage 2023-05-27 05:13:27 -07:00
Paul Gauthier
4c76a8a242 Added --dirty-commits 2023-05-27 05:12:50 -07:00
Paul Gauthier
a2fa4f2929 aider: Added --dirty-commits argument to the argument parser in aider/main.py.
# Context:
USER: add --dirty-commits too
ASSISTANT: To add the `--dirty-commits` argument, we need to modify the `aider/main.py` file. Here are the changes:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--no-dirty-commits",
        action="store_false",
        dest="dirty_commits",
        help="Disable dirty commit of changes",
    )
=======
    parser.add_argument(
        "--no-dirty-commits",
        action="store_false",
        dest="dirty_commits",
        help="Disable dirty commit of changes",
    )
    parser.add_argument(
        "--dirty-commits",
        action="store_true",
        dest="dirty_commits",
        help="Enable dirty commit of changes",
    )
>>>>>>> UPDATED
```

These changes add the `--dirty-commits` argument to the argument parser in `aider/main.py`.
2023-05-27 05:10:23 -07:00
Paul Gauthier
ed1a886a4f aider: Added --no-dirty-commits argument to the argument parser in aider/main.py, passed it to the Coder class, and set the self.dirty_commits attribute in the Coder class in aider/coder.py.
# Context:
USER: add a new arg like --no-auto-commits, but called --no-dirty-commits. pass it to Coder and set self.dirty_commits
ASSISTANT: To add the `--no-dirty-commits` argument, we need to modify the `aider/main.py` file and pass the new argument to the `Coder` class in the `aider/coder.py` file.

Here are the changes:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--no-auto-commits",
        action="store_false",
        dest="auto_commits",
        help="Disable auto commit of changes",
    )
=======
    parser.add_argument(
        "--no-auto-commits",
        action="store_false",
        dest="auto_commits",
        help="Disable auto commit of changes",
    )
    parser.add_argument(
        "--no-dirty-commits",
        action="store_false",
        dest="dirty_commits",
        help="Disable dirty commit of changes",
    )
>>>>>>> UPDATED
```

```python
aider/main.py
<<<<<<< ORIGINAL
    coder = Coder(
        io,
        main_model=args.model,
        fnames=args.files,
        pretty=args.pretty,
        show_diffs=args.show_diffs,
        auto_commits=args.auto_commits,
        dry_run=args.dry_run,
        use_ctags=args.ctags,
        verbose=args.verbose,
    )
=======
    coder = Coder(
        io,
        main_model=args.model,
        fnames=args.files,
        pretty=args.pretty,
        show_diffs=args.show_diffs,
        auto_commits=args.auto_commits,
        dirty_commits=args.dirty_commits,
        dry_run=args.dry_run,
        use_ctags=args.ctags,
        verbose=args.verbose,
    )
>>>>>>> UPDATED
```

```python
aider/coder.py
<<<<<<< ORIGINAL
    def __init__(
        self,
        io,
        main_model="gpt-4",
        fnames=None,
        pretty=True,
        show_diffs=False,
        auto_commits=True,
        dry_run=False,
        use_ctags=False,
        verbose=False,
    ):
        self.verbose = verbose
        self.abs_fnames = set()
        self.cur_messages = []
        self.done_messages = []

        self.io = io
        self.auto_commits = auto_commits
        self.dry_run = dry_run
=======
    def __init__(
        self,
        io,
        main_model="gpt-4",
        fnames=None,
        pretty=True,
        show_diffs=False,
        auto_commits=True,
        dirty_commits=True,
        dry_run=False,
        use_ctags=False,
        verbose=False,
    ):
        self.verbose = verbose
        self.abs_fnames = set()
        self.cur_messages = []
        self.done_messages = []

        self.io = io
        self.auto_commits = auto_commits
        self.dirty_commits = dirty_commits
        self.dry_run = dry_run
>>>>>>> UPDATED
```

These changes add the `--no-dirty-commits` argument to the argument parser in `aider/main.py`, pass it to the `Coder` class, and set the `self.dirty_commits` attribute in the `Coder` class in `aider/coder.py`.
2023-05-27 05:09:28 -07:00
Paul Gauthier
a24e150e49 more tests, including --pretty 2023-05-27 05:06:36 -07:00
Paul Gauthier
24e78229b2 cleaned up --no-autocommit test 2023-05-27 05:03:30 -07:00
Paul Gauthier
2bb1c34bd5 aider: Updated test_main_with_no_auto_commits to only check if auto_commits is set to False.
# Context:
USER: in test_main_with_empty_dir_new_file can we *just* check the auto_commits=False?
ASSISTANT: Sure, I'll update the `test_main_with_empty_dir_new_file` test to check if `auto_commits` is set to `False`.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_empty_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            main(["foo.txt"], input=pipe_input, output=DummyOutput())
            pipe_input.close()
            self.assertTrue(os.path.exists("foo.txt"))
=======
    def test_main_with_empty_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["foo.txt"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock
^C KeyboardInterrupt
USER: in test_main_with_no_auto_commits can we *just* check the auto_commits=False?
ASSISTANT: Sure, I'll update the `test_main_with_no_auto_commits` test to only check if `auto_commits` is set to `False`.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,
                    verbose=unittest.mock.ANY,
                )
            pipe_input.close()
=======
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,

^C KeyboardInterrupt
USER: i want to avoid listing all the other args and ANY
ASSISTANT: I understand. I'll update the `test_main_with_no_auto_commits` test to only check if `auto_commits` is set to `False` without listing all the other arguments.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,
                    verbose=unittest.mock.ANY,
                )
            pipe_input.close()
=======
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                _, kwargs = MockCoder.call_args
                assert kwargs["auto_commits"] is False
            pipe_input.close()
>>>>>>> UPDATED
```

This change will make the test only check if `auto_commits` is set to `False` without listing all the other arguments.
2023-05-27 05:00:50 -07:00
Paul Gauthier
01963c28f8 aider: Added a test which runs main with --no-auto-commits and patches Coder with a mock to see that it gets called with auto_commits=False.
# Context:
USER: add a test which runs main with --no-auto-commits and patches Coder with a mock to see that it gets called with auto_commits=False
ASSISTANT: To add the test, you need to modify the `tests/test_main.py` file. Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
class TestMain(TestCase):
    def test_main_with_empty_dir_no_files_on_command(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            main([], input=pipe_input, output=DummyOutput())
            pipe_input.close()

    def test_main_with_empty_dir_new_file(self):
=======
class TestMain(TestCase):
    def test_main_with_empty_dir_no_files_on_command(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            main([], input=pipe_input, output=DummyOutput())
            pipe_input.close()

    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,
                    verbose=unittest.mock.ANY,
                )
            pipe_input.close()

    def test_main_with_empty_dir_new_file(self):
>>>>>>> UPDATED
```

This new test case `test_main_with_no_auto_commits` creates a temporary directory, changes the current working directory to it, and then runs the `main` function with the `--no-auto-commits` flag. It also patches the `Coder` class with a mock to check if it gets called with `auto_commits=False`.
2023-05-27 04:56:32 -07:00
Paul Gauthier
a9cc332e2d Removed test for disabling auto commits. 2023-05-27 04:54:55 -07:00
Paul Gauthier
d240e5ebc0 Show options on -v 2023-05-27 04:49:16 -07:00
Paul Gauthier
ed70a32671 Added import for MagicMock in test_main.py. 2023-05-27 04:39:47 -07:00
Paul Gauthier
10dc4f6557 aider: Replaced unittest.mock with MagicMock in test_main_no_auto_commits.
# Context:
USER: I ran this command:

 pytest tests/test_main.py

And got this output:

============================= test session starts ==============================
platform darwin -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/gauthier/Projects/aider
collected 4 items

tests/test_main.py F...                                                  [100%]

=================================== FAILURES ===================================
______________________ TestMain.test_main_no_auto_commits ______________________

self = <tests.test_main.TestMain testMethod=test_main_no_auto_commits>

    def test_main_no_auto_commits(self):
>       with unittest.mock.patch("aider.main.Coder") as MockCoder:
E       AttributeError: module 'unittest' has no attribute 'mock'

tests/test_main.py:38: AttributeError
=========================== short test summary info ============================
FAILED tests/test_main.py::TestMain::test_main_no_auto_commits - AttributeErr...
========================= 1 failed, 3 passed in 1.32s ==========================

ASSISTANT: Apologies for the oversight. It seems that the `unittest.mock` module is not available in your Python version. You can use the `mock` module instead, which is available as a separate package. First, you need to install the `mock` package using pip:

```
pip install mock
```

Then, update the import statement in the `tests/test_main.py` file to use the `mock` package:

```python
tests/test_main.py
<<<<<<< ORIGINAL
import os
import tempfile
import unittest
from unittest import TestCase
from aider.main import main
=======

^C KeyboardInterrupt
USER: would magicmock work?
ASSISTANT: Yes, `MagicMock` can be used in this case. It is a part of the `unittest.mock` module in Python 3.3 and later. Since you are using Python 3.11.3, you can use `MagicMock` directly from the `unittest.mock` module.

First, update the import statement in the `tests/test_main.py` file to import `MagicMock`:

```python
tests/test_main.py
<<<<<<< ORIGINAL
import os
import tempfile
import unittest
from unittest import TestCase
from aider.main import main
=======
import os
import tempfile
import unittest
from unittest import TestCase, MagicMock
from aider.main import main
>>>>>>> UPDATED
```

Then, update the test function `test_main_no_auto_commits` to use `MagicMock` instead of `unittest.mock.patch`:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_no_auto_commits(self):
        with unittest.mock.patch("aider.main.Coder") as MockCoder:
            with tempfile.TemporaryDirectory() as temp_dir:
                os.chdir(temp_dir)
                subprocess.run(["git", "init"], cwd=temp_dir)
                pipe_input = create_input(StringIO(""))
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                pipe_input.close()

            mock_coder_instance = MockCoder.return_value
            self.assertFalse(mock_coder_instance.auto_commits)
=======
    def test_main_no_auto_commits(self):
        with MagicMock() as MockCoder:
            with tempfile.TemporaryDirectory() as temp_dir:
                os.chdir(temp_dir)
                subprocess.run(["git", "init"], cwd=temp_dir)
                pipe_input = create_input(StringIO(""))
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                pipe_input.close()

            mock_coder_instance = MockCoder.return_value
            self.assertFalse(mock_coder_instance.auto_commits)
>>>>>>> UPDATED
```

This should resolve the issue with the test.
2023-05-27 04:38:32 -07:00
Paul Gauthier
ca752f37af aider: Import unittest module in tests/test_main.py.
# Context:
USER: I ran this command:

 pytest tests/test_main.py

And got this output:

============================= test session starts ==============================
platform darwin -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/gauthier/Projects/aider
collected 4 items

tests/test_main.py F...                                                  [100%]

=================================== FAILURES ===================================
______________________ TestMain.test_main_no_auto_commits ______________________

self = <tests.test_main.TestMain testMethod=test_main_no_auto_commits>

    def test_main_no_auto_commits(self):
>       with unittest.mock.patch("aider.main.Coder") as MockCoder:
E       NameError: name 'unittest' is not defined

tests/test_main.py:37: NameError
=========================== short test summary info ============================
FAILED tests/test_main.py::TestMain::test_main_no_auto_commits - NameError: n...
========================= 1 failed, 3 passed in 1.29s ==========================

ASSISTANT: It looks like the `unittest` module is not imported in the `tests/test_main.py` file. To fix this issue, you need to add the import statement for the `unittest` module at the beginning of the file.

Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
import os
import tempfile
from unittest import TestCase
from aider.main import main
=======
import os
import tempfile
import unittest
from unittest import TestCase
from aider.main import main
>>>>>>> UPDATED
```

This will import the `unittest` module and resolve the `NameError`.
2023-05-27 04:36:54 -07:00
Paul Gauthier
22482deedf aider: Added a new test to check if the --no-auto-commits flag is passed correctly to the Coder class.
# Context:
USER: add a test which runs main with --no-auto-commits and mocks Coder to see that it gets passed in correctly
ASSISTANT: To add the test, we'll modify the `tests/test_main.py` file. We'll create a new test function called `test_main_no_auto_commits` and use the `unittest.mock.patch` to mock the `Coder` class. Inside the test function, we'll run the `main` function with the `--no-auto-commits` flag and check if the `auto_commits` attribute of the mocked `Coder` instance is set to `False`.

Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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)
            pipe_input = create_input(StringIO(""))
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
            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)
            pipe_input = create_input(StringIO(""))
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
            pipe_input.close()
            self.assertTrue(os.path.exists("foo.txt"))

    def test_main_no_auto_commits(self):
        with unittest.mock.patch("aider.main.Coder") as MockCoder:
            with tempfile.TemporaryDirectory() as temp_dir:
                os.chdir(temp_dir)
                subprocess.run(["git", "init"], cwd=temp_dir)
                pipe_input = create_input(StringIO(""))
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                pipe_input.close()

            mock_coder_instance = MockCoder.return_value
            self.assertFalse(mock_coder_instance.auto_commits)
>>>>>>> UPDATED
```

This will add the new test to check if the `--no-auto-commits` flag is passed correctly to the `Coder` class.
2023-05-27 04:36:14 -07:00
Paul Gauthier
c33169756a tweaking cmd line args, readme usage 2023-05-26 16:40:04 -07:00
Paul Gauthier
905a81d78e auto-commitS 2023-05-26 16:32:10 -07:00
Paul Gauthier
1761325eef aider: Added support for --no-auto-commit argument.
# Context:
USER: do the same for --no-auto-commit
ASSISTANT: To support `auto_commit: true` in the configuration file, you can add a `--auto-commit` argument with the `store_true` action. Here's how to modify the `aider/main.py` file:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--no-auto-commits",
        action="store_false",
        dest="auto_commits",
        env_var=f"{env_prefix}AUTO_COMMITS",
        help="Disable auto commit of changes",
        default=True,
    )
=======
    parser.add_argument(
        "--auto-commit",
        action="store_true",
        default=True,
        help="Enable auto commit of changes (default: True)",
    )

    parser.add_argument(
        "--no-auto-commit",
        action="store_false",
        dest="auto_commit",
        env_var=f"{env_prefix}AUTO_COMMIT",
        help="Disable auto commit of changes",
    )
>>>>>>> UPDATED
```

Now, you can use `auto_commit: true` in your configuration file without any issues.
2023-05-26 16:31:01 -07:00
Paul Gauthier
69f591a194 Added an environment variable for pretty output. 2023-05-26 16:30:14 -07:00
Paul Gauthier
000fd853d2 aider: Added --pretty argument with store_true action to support pretty: true in conf yaml.
# Context:
USER: does configargparse support bool args like `--thing' and `--no-thing`
ASSISTANT: Yes, `configargparse` supports boolean arguments. You can create a boolean argument with `--thing` and `--no-thing` by using the `action` parameter with values `"store_true"` and `"store_false"` respectively. Here's an example:

```python
parser.add_argument(
    "--thing",
    action="store_true",
    default=False,
    help="Enable thing (default: False)",
)

parser.add_argument(
    "--no-thing",
    action="store_false",
    dest="thing",
    help="Disable thing (default: True)",
)
```

In this example, the `--thing` argument will set the `thing` variable to `True`, and the `--no-thing` argument will set the `thing` variable to `False`.
USER: i have --no-pretty, but if I add `pretty: true` to the conf yaml, it throws error: aider: error: unrecognized arguments: --pretty=False
ASSISTANT: The error occurs because the `--pretty` argument is not explicitly defined in the parser. To fix this, you can add a `--pretty` argument with the `store_true` action. Here's how to modify the `aider/main.py` file:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--no-pretty",
        action="store_false",
        dest="pretty",
        env_var=f"{env_prefix}PRETTY",
        help="Disable pretty, colorized output",
        default=True,
    )
=======
    parser.add_argument(
        "--pretty",
        action="store_true",
        default=True,
        help="Enable pretty, colorized output (default: True)",
    )

    parser.add_argument(
        "--no-pretty",
        action="store_false",
        dest="pretty",
        env_var=f"{env_prefix}PRETTY",
        help="Disable pretty, colorized output",
    )
>>>>>>> UPDATED
```

Now, you can use `pretty: true` in your configuration file without any issues.
2023-05-26 16:29:29 -07:00
Paul Gauthier
c87fc2e30f added pyyaml 2023-05-26 16:26:30 -07:00
Paul Gauthier
b706c36565 aider: Added --config/-c option to specify the config file and updated the configargparse.ArgumentParser to use the YAMLConfigFileParser.
# Context:
USER: add --config/-c to specify the config file
ASSISTANT: To add the `--config/-c` option to specify the config file, you need to modify the `aider/main.py` file. Update the `configargparse.ArgumentParser` arguments and add a new argument for the `--config/-c` option.

Here's the change:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser = configargparse.ArgumentParser(
        description="aider - chat with GPT about your code",
        add_config_file_help=True,
        default_config_files=default_config_files,
    )
=======
    parser = configargparse.ArgumentParser(
        description="aider - chat with GPT about your code",
        add_config_file_help=True,
        default_config_files=default_config_files,
        config_file_parser_class=configargparse.YAMLConfigFileParser,
    )

    parser.add_argument(
        "-c",
        "--config",
        is_config_file=True,
        metavar="CONFIG_FILE",
        help="Specify the config file (default: search for .aider.conf.yml in git root or home directory)",
    )
>>>>>>> UPDATED
```

This change adds the `--config/-c` option to specify the config file and updates the `configargparse.ArgumentParser` to use the YAMLConfigFileParser for parsing the config file.
2023-05-26 16:23:47 -07:00
Paul Gauthier
ddc97dacf8 aider: Set default values for input and chat history files to be in the git root directory if there is one, otherwise, use the current directory.
# Context:
USER: have the input and chat history files default to be located in the git root, if there is one
ASSISTANT: To change the default location of the input and chat history files to the git root, you need to modify the `aider/main.py` file. Update the default values for `--input-history-file` and `--chat-history-file` arguments.

Here's the change:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--input-history-file",
        metavar="INPUT_HISTORY_FILE",
        env_var=f"{env_prefix}INPUT_HISTORY_FILE",
        default=".aider.input.history",
        help="Specify the chat input history file (default: .aider.input.history)",
    )
    parser.add_argument(
        "--chat-history-file",
        metavar="CHAT_HISTORY_FILE",
        env_var=f"{env_prefix}CHAT_HISTORY_FILE",
        default=".aider.chat.history.md",
        help="Specify the chat history file (default: .aider.chat.history.md)",
    )
=======
    default_input_history_file = os.path.join(git_root, ".aider.input.history") if git_root else ".aider.input.history"
    default_chat_history_file = os.path.join(git_root, ".aider.chat.history.md") if git_root else ".aider.chat.history.md"

    parser.add_argument(
        "--input-history-file",
        metavar="INPUT_HISTORY_FILE",
        env_var=f"{env_prefix}INPUT_HISTORY_FILE",
        default=default_input_history_file,
        help=f"Specify the chat input history file (default: {default_input_history_file})",
    )
    parser.add_argument(
        "--chat-history-file",
        metavar="CHAT_HISTORY_FILE",
        env_var=f"{env_prefix}CHAT_HISTORY_FILE",
        default=default_chat_history_file,
        help=f"Specify the chat history file (default: {default_chat_history_file})",
    )
>>>>>>> UPDATED
```

This change sets the default values for the input and chat history files to be in the git root directory if there is one, otherwise, it will use the current directory.
2023-05-26 16:22:49 -07:00
Paul Gauthier
e4116007b8 updated usage in readme 2023-05-26 16:20:47 -07:00
Paul Gauthier
21b3a538dd advertise /exit 2023-05-26 16:09:40 -07:00
Paul Gauthier
ec933f4417 updated ctags help 2023-05-26 16:07:05 -07:00
Paul Gauthier
81f3c7ac7e aider: Changed default value of --ctags to None and allowed it to be set to true or false via the command line or config.
# Context:
USER: --ctags should default to None now. it should be possible to set it to true or false via cmd line or config
ASSISTANT: To make the `--ctags` option default to `None` and allow it to be set to `true` or `false` via the command line or config, you need to modify the `aider/main.py` file. Here's the change:

```python
aider/main.py
<<<<<<< ORIGINAL
    parser.add_argument(
        "--ctags",
        action="store_true",
        env_var=f"{env_prefix}CTAGS",
        help="Add ctags to the chat to help GPT understand the codebase (default: False)",
        default=False,
    )
=======
    parser.add_argument(
        "--ctags",
        type=lambda x: (str(x).lower() == 'true'),
        nargs='?',
        const=True,
        default=None,
        env_var=f"{env_prefix}CTAGS",
        help="Add ctags to the chat to help GPT understand the codebase (default: None)",
    )
>>>>>>> UPDATED
```

This change will make the `--ctags` option default to `None` and allow it to be set to `true` or `false` via the command line or config.
2023-05-26 16:04:39 -07:00
Paul Gauthier
43413654bd enable ctags if present 2023-05-26 16:03:24 -07:00
Paul Gauthier
ae087287c7 aider: Added check_for_ctags method to RepoMap class and updated __init__ method to set use_ctags to False if check_for_ctags returns False.
# Context:
USER: add a check_for_ctags method that: makes a tempdir with hello.py that defines a hello func. call get_tags on that and catch any error and return false. otherwise true.
ASSISTANT: I will add a `check_for_ctags` method to the `RepoMap` class in the `aider/repomap.py` file. This method will create a temporary directory with a `hello.py` file that defines a `hello` function. It will then call the `get_tags` method on that file, catch any errors, and return `False` if there's an error or `True` otherwise.

Here's the change:

```python
aider/repomap.py
<<<<<<< ORIGINAL
import os
import json
import sys
import subprocess
import tiktoken
=======
import os
import json
import sys
import subprocess
import tiktoken
import tempfile
>>>>>>> UPDATED
```

```python
aider/repomap.py
<<<<<<< ORIGINAL
class RepoMap:
    ctags_cmd = ["ctags", "--fields=+S", "--extras=-F", "--output-format=json"]

    def __init__(self, use_ctags=True, root=None, main_model="gpt-4"):
=======
class RepoMap:
    ctags_cmd = ["ctags", "--fields=+S", "--extras=-F", "--output-format=json"]

    def __init__(self, use_ctags=True, root=None, main_model="gpt-4"):
        if not self.check_for_ctags():
            use_ctags = False
>>>>>>> UPDATED
```

```python
aider/repomap.py
<<<<<<< ORIGINAL
        return tags

if __name__ == "__main__":
    rm = RepoMap()
    res = rm.get_tags_map(sys.argv[1:])
    print(res)
=======
    def check_for_ctags(self):
        try:
            with tempfile.TemporaryDirectory() as tempdir:
                hello_py = os.path.join(tempdir, "hello.py")
                with open(hello_py, "w") as f:
                    f.write("def hello():\n    print('Hello, world!')\n")
                self.get_tags(hello_py)
        except Exception:
            return False
        return True

if __name__ == "__main__":
    rm = RepoMap()
    res = rm.get_tags_map(sys.argv[1:])
    print(res)
>>>>>>> UPDATED
```

This adds the `check_for_ctags` method to the `RepoMap` class and updates the `__init__` method to set `use_ctags` to `False` if `check_for_ctags` returns `False`.
2023-05-26 15:57:31 -07:00
Paul Gauthier
711212ef3c refac 2023-05-26 15:52:35 -07:00
Paul Gauthier
32289bc40f fixed bug caught in tests 2023-05-26 15:50:35 -07:00
Paul Gauthier
7fe7f1e279 fix 2023-05-26 15:48:26 -07:00