Skip to content

Commit fb74253

Browse files
authored
Merge pull request #32 from commit-0/aider_reorg
fix aider and commit0 compatibility issues
2 parents 02da7e9 + e23feba commit fb74253

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

commit0/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ def main() -> None:
6565
repo = sys.argv[2]
6666
commit0.harness.get_pytest_ids.main(repo, stdout=True)
6767
elif command == "test" or command == "test-reference":
68-
repo = sys.argv[2]
68+
# this command assume execution in arbitrary working directory
69+
repo_or_repo_path = sys.argv[2]
6970
test_ids = sys.argv[3]
7071
if command == "test-reference":
7172
config.branch = "reference"
7273
commit0.harness.run_pytest_ids.main(
7374
config.dataset_name,
7475
config.dataset_split,
7576
config.base_dir,
76-
repo,
77+
repo_or_repo_path,
7778
config.branch,
7879
test_ids,
7980
config.backend,

commit0/harness/evaluate.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import os
3-
import traceback
43
from collections import Counter
54

65
from concurrent.futures import ThreadPoolExecutor, as_completed
@@ -10,7 +9,8 @@
109

1110
from commit0.harness.run_pytest_ids import main as run_tests
1211
from commit0.harness.get_pytest_ids import main as get_tests
13-
from commit0.harness.constants import RepoInstance, SPLIT
12+
from commit0.harness.constants import RepoInstance, SPLIT, RUN_PYTEST_LOG_DIR
13+
from commit0.harness.utils import get_hash_string
1414

1515
logging.basicConfig(
1616
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
@@ -32,13 +32,16 @@ def main(
3232
dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore
3333
repos = SPLIT[repo_split]
3434
pairs = []
35+
log_dirs = []
3536
for example in dataset:
3637
repo_name = example["repo"].split("/")[-1]
3738
if repo_split != "all" and repo_name not in SPLIT[repo_split]:
3839
continue
3940
pairs.append((repo_name, example["test"]["test_dir"]))
41+
hashed_test_ids = get_hash_string(example["test"]["test_dir"])
42+
log_dir = RUN_PYTEST_LOG_DIR / repo_name / branch / hashed_test_ids
43+
log_dirs.append(str(log_dir))
4044

41-
log_dirs = []
4245
with tqdm(total=len(repos), smoothing=0, desc="Evaluating repos") as pbar:
4346
with ThreadPoolExecutor(max_workers=num_workers) as executor:
4447
# Create a future for running each instance
@@ -61,13 +64,6 @@ def main(
6164
# Wait for each future to complete
6265
for future in as_completed(futures):
6366
pbar.update(1)
64-
try:
65-
# Update progress bar, check if instance ran successfully
66-
result = future.result()
67-
log_dirs.append(result)
68-
except Exception:
69-
traceback.print_exc()
70-
continue
7167

7268
# get numbers
7369
out = []

commit0/harness/run_pytest_ids.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from datasets import load_dataset
1+
import git
2+
import os
3+
import sys
24
import traceback
5+
from datasets import load_dataset
36
from pathlib import Path
47

58
from typing import Iterator
6-
from git import Repo
79
from commit0.harness.constants import (
810
EVAL_BACKENDS,
911
Files,
@@ -31,14 +33,14 @@ def main(
3133
dataset_name: str,
3234
dataset_split: str,
3335
base_dir: str,
34-
repo: str,
36+
repo_or_repo_dir: str,
3537
branch: str,
3638
test_ids: str,
3739
backend: str,
3840
timeout: int,
3941
num_cpus: int,
4042
stdout: bool,
41-
) -> str:
43+
) -> None:
4244
"""Runs the pytests for repos in a dataset.
4345
4446
Tests are run either locally through docker
@@ -47,21 +49,36 @@ def main(
4749
dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore
4850
spec = None
4951
example = None
52+
repo_name = None
5053
for example in dataset:
51-
if example["repo"].endswith(repo):
54+
repo_name = example["repo"].split("/")[-1]
55+
if repo_name in os.path.basename(repo_or_repo_dir):
5256
spec = make_spec(example)
5357
break
5458
assert spec is not None, "No spec available"
5559
assert example is not None, "No example available"
60+
assert repo_name is not None, "No repo available"
5661

5762
hashed_test_ids = get_hash_string(test_ids)
5863
# set up logging
59-
log_dir = RUN_PYTEST_LOG_DIR / repo / branch / hashed_test_ids
64+
log_dir = RUN_PYTEST_LOG_DIR / repo_name / branch / hashed_test_ids
6065
log_dir.mkdir(parents=True, exist_ok=True)
6166
log_file = log_dir / "run_pytest.log"
62-
logger = setup_logger(repo, log_file)
67+
logger = setup_logger(repo_name, log_file)
6368

64-
local_repo = Repo(f"{base_dir}/{repo}")
69+
try:
70+
local_repo = git.Repo(repo_or_repo_dir)
71+
except git.exc.NoSuchPathError: # type: ignore
72+
repo_dir = os.path.join(base_dir, repo_name)
73+
logger.error(f"{repo_or_repo_dir} is not a git dir, trying {repo_dir} again")
74+
try:
75+
local_repo = git.Repo(repo_dir)
76+
except git.exc.NoSuchPathError: # type: ignore
77+
raise Exception(
78+
f"{repo_dir} and {repo_or_repo_dir} are not git directories.\nUsage: commit0 test {{repo_dir}} {test_ids}"
79+
)
80+
except Exception as e:
81+
raise e
6582
if branch == "reference":
6683
commit_id = example["reference_commit"]
6784
else:
@@ -106,21 +123,28 @@ def main(
106123
context.write_test_output(test_output, timed_out)
107124
if stdout:
108125
print(test_output)
126+
pytest_exit_code = extract_test_output(output, "echo ")
127+
try:
128+
pytest_exit_code = int(pytest_exit_code)
129+
except Exception:
130+
raise Exception(
131+
f"Fail to convert pytest_exit_code {pytest_exit_code} into an integer."
132+
)
133+
sys.exit(pytest_exit_code)
109134
except EvaluationError as e:
110135
error_msg = (
111-
f"Error in running pytest for {repo}: {e}\n"
136+
f"Error in running pytest for {repo_name}: {e}\n"
112137
f"{traceback.format_exc()}\n"
113138
f"Check ({log_file}) for more information."
114139
)
115-
raise EvaluationError(repo, error_msg, logger)
140+
raise EvaluationError(repo_name, error_msg, logger)
116141
except Exception as e:
117142
error_msg = (
118143
f"General error: {e}\n"
119144
f"{traceback.format_exc()}\n"
120145
f"Check ({log_file}) for more information."
121146
)
122147
raise RuntimeError(error_msg)
123-
return str(log_dir)
124148

125149

126150
__all__ = []

commit0/harness/spec.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def make_eval_script_list(instance: RepoInstance, repo_directory: str) -> list[s
154154
"git apply --allow-empty -v /patch.diff",
155155
"git status",
156156
f"{instance['test']['test_cmd']} --json-report --json-report-file=report.json {{test_ids}}",
157+
"echo $?",
157158
f"git reset --hard {instance['base_commit']}",
158159
"git status",
159160
]

commit0/harness/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def extract_test_output(ss: str, pattern: str) -> str:
4444
append = True
4545
# the next command started here, so we finished reading test output
4646
elif append and one.startswith("+"):
47-
return "\n".join(out)
47+
# remove the first element "+ {command}"
48+
out = out[1:]
49+
return "\n".join(out).strip()
4850
if append:
4951
out.append(one)
5052
return ""

0 commit comments

Comments
 (0)