-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_skip_processed.py
More file actions
192 lines (160 loc) · 6.64 KB
/
test_skip_processed.py
File metadata and controls
192 lines (160 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
Test script to verify the --extract-dep and --skip-processed functionality.
This script:
1. Creates a temporary directory with test repositories
2. Runs extract-dep on them
3. Runs extract-dep again with skip-processed
4. Verifies the correct repositories were skipped
Usage:
python test_skip_processed.py
"""
import os
import sys
import tempfile
import shutil
import subprocess
import json
from pathlib import Path
import logging
import time
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s'
)
logger = logging.getLogger(__name__)
def create_test_repo(base_dir, name, packages):
"""Create a test repository with specified packages."""
repo_dir = base_dir / name
repo_dir.mkdir(parents=True, exist_ok=True)
# Create a requirements.txt file
with open(repo_dir / "requirements.txt", "w") as f:
f.write("\n".join(packages))
# Create a dummy test file
with open(repo_dir / "test_dummy.py", "w") as f:
f.write("""
import unittest
class TestDummy(unittest.TestCase):
def test_pass(self):
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()
""")
return repo_dir
def main():
"""Run the test script."""
logger.info("Starting test of --extract-dep and --skip-processed functionality")
# Create a temporary directory for our test
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
logger.info(f"Created temporary directory: {temp_path}")
# Create test repositories
repos = {
"repo1": ["numpy", "pandas", "matplotlib"],
"repo2": ["requests", "beautifulsoup4", "lxml"],
"repo3": ["flask", "sqlalchemy", "alembic"]
}
repo_dirs = {}
for name, packages in repos.items():
repo_dirs[name] = create_test_repo(temp_path, name, packages)
# Create a list of repositories
repo_list_file = temp_path / "repos.txt"
with open(repo_list_file, "w") as f:
for name, path in repo_dirs.items():
f.write(f"{path}\n")
# Create output directory
output_dir = temp_path / "output"
output_dir.mkdir(parents=True, exist_ok=True)
# Run extract-dep on all repositories
logger.info("Running extract-dep on all repositories...")
cmd = [
sys.executable, "-m", "repo2run",
"--global",
"--local-list", str(repo_list_file),
"--output-dir", str(output_dir),
"--extract-dep",
"--verbose"
]
try:
subprocess.run(cmd, check=True, capture_output=True, text=True)
logger.info("First extract-dep run completed successfully")
except subprocess.CalledProcessError as e:
logger.error(f"First extract-dep run failed: {e.stderr}")
return 1
# Check repo_req.jsonl file
repo_req_jsonl = output_dir / "repo_req.jsonl"
if not repo_req_jsonl.exists():
logger.error(f"repo_req.jsonl not found at {repo_req_jsonl}")
return 1
# Read the repositories in repo_req.jsonl
processed_repos = set()
with open(repo_req_jsonl, "r") as f:
for line in f:
try:
record = json.loads(line.strip())
if "repository" in record:
processed_repos.add(record["repository"])
except json.JSONDecodeError:
continue
logger.info(f"Found {len(processed_repos)} processed repositories in repo_req.jsonl")
# Create a new repository
new_repo_name = "repo4"
new_repo_path = create_test_repo(temp_path, new_repo_name, ["pytest", "pytest-cov", "coverage"])
repo_dirs[new_repo_name] = new_repo_path
# Update the repository list
with open(repo_list_file, "w") as f:
for name, path in repo_dirs.items():
f.write(f"{path}\n")
# Run extract-dep again with skip-processed
logger.info("Running extract-dep again with skip-processed...")
cmd = [
sys.executable, "-m", "repo2run",
"--global",
"--local-list", str(repo_list_file),
"--output-dir", str(output_dir),
"--extract-dep",
"--skip-processed",
"--verbose"
]
process = subprocess.run(cmd, check=True, capture_output=True, text=True)
logger.info("Second extract-dep run completed successfully")
# Check output for skipped repositories
output = process.stdout + process.stderr
logger.info("Checking output for skipped repositories...")
# Read the updated repo_req.jsonl
new_processed_repos = set()
with open(repo_req_jsonl, "r") as f:
for line in f:
try:
record = json.loads(line.strip())
if "repository" in record:
new_processed_repos.add(record["repository"])
except json.JSONDecodeError:
continue
logger.info(f"Found {len(new_processed_repos)} processed repositories after second run")
# Check that the original repositories were skipped and only the new one was processed
skipped_count = 0
for repo_name in ["repo1", "repo2", "repo3"]:
repo_path = str(repo_dirs[repo_name].absolute())
if f"Skipping already processed repository: {repo_path}" in output:
skipped_count += 1
logger.info(f"Repository {repo_name} was correctly skipped")
else:
logger.warning(f"Repository {repo_name} was not skipped as expected")
# Check if the new repository was processed
new_repo_path = str(repo_dirs[new_repo_name].absolute())
new_repo_processed = new_repo_path in new_processed_repos
if new_repo_processed:
logger.info(f"New repository {new_repo_name} was correctly processed")
else:
logger.error(f"New repository {new_repo_name} was not processed")
# Check counts
if skipped_count == 3 and new_repo_processed:
logger.info("TEST PASSED: All repositories were handled correctly")
return 0
else:
logger.error(f"TEST FAILED: {skipped_count}/3 repositories skipped, new repo processed: {new_repo_processed}")
return 1
if __name__ == "__main__":
sys.exit(main())