From cbc090914563fbe7422f5505e67a8cbfc4cb06db Mon Sep 17 00:00:00 2001 From: Serj Date: Mon, 17 Apr 2023 22:07:05 +0300 Subject: [PATCH 1/5] added pytest to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 3923778..823e6b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,3 +20,4 @@ termcolor==2.2.0 tqdm==4.65.0 urllib3==1.26.15 yarl==1.8.2 +pytest==7.3.1 From c50b51e9494e4e746dd359c6fbf26ffead5caa63 Mon Sep 17 00:00:00 2001 From: Serj Date: Mon, 17 Apr 2023 22:11:29 +0300 Subject: [PATCH 2/5] Create dev requirements instead --- requirements-dev.txt | 2 ++ requirements.txt | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 requirements-dev.txt diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..b4500ed --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +-r requirements.txt +pytest==7.3.1 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 823e6b1..3923778 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,4 +20,3 @@ termcolor==2.2.0 tqdm==4.65.0 urllib3==1.26.15 yarl==1.8.2 -pytest==7.3.1 From 4c8e7f7964f7925253848dfb82533140aeacc301 Mon Sep 17 00:00:00 2001 From: Serj Date: Mon, 17 Apr 2023 22:13:48 +0300 Subject: [PATCH 3/5] Added tests for apply changes method --- tests/test_wolverine.py | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/test_wolverine.py diff --git a/tests/test_wolverine.py b/tests/test_wolverine.py new file mode 100644 index 0000000..0b44c69 --- /dev/null +++ b/tests/test_wolverine.py @@ -0,0 +1,54 @@ +import os +import pytest +import tempfile +from wolverine import apply_changes + + +@pytest.fixture(scope='function') +def temp_file(): + # Create a temporary file + with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: + f.write("first line\nsecond line\nthird line") + file_path = f.name + yield file_path + # Clean up the temporary file + os.remove(file_path) + + +def test_apply_changes_replace(temp_file): + # Make a "replace" change to the second line + changes = [ + {"operation": "Replace", "line": 2, "content": "new second line"} + ] + apply_changes(temp_file, changes) + + # Check that the file was updated correctly + with open(temp_file) as f: + content = f.read() + assert content == "first line\nnew second line\nthird line" + + +def test_apply_changes_delete(temp_file): + # Make a "delete" change to the third line + changes = [ + {"operation": "Delete", "line": 3}, + ] + apply_changes(temp_file, changes) + + # Check that the file was updated correctly + with open(temp_file) as f: + content = f.read() + assert content == "first line\nsecond line" + + +def test_apply_changes_insert(temp_file): + # Make an "insert" change after the second line + changes = [ + {"operation": "InsertAfter", "line": 2, "content": "inserted line"}, + ] + apply_changes(temp_file, changes) + + # Check that the file was updated correctly + with open(temp_file) as f: + content = f.read() + assert content == "first line\nsecond line\ninserted line" From 507272c4d6c6619ff6fa52b4c77d5074ad250795 Mon Sep 17 00:00:00 2001 From: Serj Date: Mon, 17 Apr 2023 22:23:46 +0300 Subject: [PATCH 4/5] Fix up test logic --- tests/test_wolverine.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_wolverine.py b/tests/test_wolverine.py index 0b44c69..1f82d76 100644 --- a/tests/test_wolverine.py +++ b/tests/test_wolverine.py @@ -1,7 +1,8 @@ import os +import json import pytest import tempfile -from wolverine import apply_changes +from wolverine import apply_changes, json_validated_response @pytest.fixture(scope='function') @@ -31,14 +32,14 @@ def test_apply_changes_replace(temp_file): def test_apply_changes_delete(temp_file): # Make a "delete" change to the third line changes = [ - {"operation": "Delete", "line": 3}, + {"operation": "Delete", "line": 3, "content": ""}, ] apply_changes(temp_file, changes) # Check that the file was updated correctly with open(temp_file) as f: content = f.read() - assert content == "first line\nsecond line" + assert content == "first line\nsecond line\n" def test_apply_changes_insert(temp_file): @@ -51,4 +52,5 @@ def test_apply_changes_insert(temp_file): # Check that the file was updated correctly with open(temp_file) as f: content = f.read() - assert content == "first line\nsecond line\ninserted line" + assert content == 'first line\nsecond line\ninserted line\nthird line' + From b8a7e04e61720ae7654241e6b2091491592ff258 Mon Sep 17 00:00:00 2001 From: BioBootloader Date: Mon, 24 Apr 2023 09:21:14 -0700 Subject: [PATCH 5/5] improve project structure --- README.md | 6 +++--- buggy_script.js => examples/buggy_script.js | 0 tests/__init__.py | 0 wolverine/__init__.py | 1 + wolverine/__main__.py | 6 ++++++ wolverine.py => wolverine/wolverine.py | 7 +------ 6 files changed, 11 insertions(+), 9 deletions(-) rename buggy_script.js => examples/buggy_script.js (100%) create mode 100644 tests/__init__.py create mode 100644 wolverine/__init__.py create mode 100644 wolverine/__main__.py rename wolverine.py => wolverine/wolverine.py (99%) diff --git a/README.md b/README.md index 2b81dd1..66c9f0b 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ _warning!_ By default wolverine uses GPT-4 and may make many repeated calls to t To run with gpt-4 (the default, tested option): - python wolverine.py examples/buggy_script.py "subtract" 20 3 + python -m wolverine examples/buggy_script.py "subtract" 20 3 You can also run with other models, but be warned they may not adhere to the edit format as well: - python wolverine.py --model=gpt-3.5-turbo buggy_script.py "subtract" 20 3 + python -m wolverine --model=gpt-3.5-turbo examples/buggy_script.py "subtract" 20 3 If you want to use GPT-3.5 by default instead of GPT-4 uncomment the default model line in `.env`: @@ -35,7 +35,7 @@ If you want to use GPT-3.5 by default instead of GPT-4 uncomment the default mod You can also use flag `--confirm=True` which will ask you `yes or no` before making changes to the file. If flag is not used then it will apply the changes to the file - python wolverine.py buggy_script.py "subtract" 20 3 --confirm=True + python -m wolverine examples/buggy_script.py "subtract" 20 3 --confirm=True ## Future Plans diff --git a/buggy_script.js b/examples/buggy_script.js similarity index 100% rename from buggy_script.js rename to examples/buggy_script.js diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wolverine/__init__.py b/wolverine/__init__.py new file mode 100644 index 0000000..9d157c1 --- /dev/null +++ b/wolverine/__init__.py @@ -0,0 +1 @@ +from .wolverine import apply_changes, json_validated_response diff --git a/wolverine/__main__.py b/wolverine/__main__.py new file mode 100644 index 0000000..d800804 --- /dev/null +++ b/wolverine/__main__.py @@ -0,0 +1,6 @@ +import fire + +from .wolverine import main + +if __name__ == "__main__": + fire.Fire(main) diff --git a/wolverine.py b/wolverine/wolverine.py similarity index 99% rename from wolverine.py rename to wolverine/wolverine.py index 26ba088..527f0f2 100644 --- a/wolverine.py +++ b/wolverine/wolverine.py @@ -1,5 +1,4 @@ import difflib -import fire import json import os import shutil @@ -20,6 +19,7 @@ DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gpt-4") with open("prompt.txt") as f: SYSTEM_PROMPT = f.read() + def run_script(script_name, script_args): script_args = [str(arg) for arg in script_args] """ @@ -224,8 +224,3 @@ def main(script_name, *script_args, revert=False, model=DEFAULT_MODEL, confirm=F apply_changes(script_name, json_response, confirm=confirm) cprint("Changes applied. Rerunning...", "blue") - - -if __name__ == "__main__": - fire.Fire(main) -