| |
| """ |
| Verification script for File Filtering Task: Find Files with Common Substring |
| """ |
|
|
| import sys |
| from pathlib import Path |
| import os |
|
|
| def get_test_directory() -> Path: |
| """Get the test directory from FILESYSTEM_TEST_DIR env var.""" |
| test_root = os.environ.get("FILESYSTEM_TEST_DIR") |
| if not test_root: |
| raise ValueError("FILESYSTEM_TEST_DIR environment variable is required") |
| return Path(test_root) |
|
|
| def verify_answer_file_exists(test_dir: Path) -> bool: |
| """Verify that the answer.txt file exists.""" |
| answer_file = test_dir / "answer.txt" |
| |
| if not answer_file.exists(): |
| print("β File 'answer.txt' not found") |
| return False |
| |
| print("β
Answer file found") |
| return True |
|
|
| def verify_answer_format(test_dir: Path) -> bool: |
| """Verify that the answer file has the correct format.""" |
| answer_file = test_dir / "answer.txt" |
| |
| try: |
| content = answer_file.read_text().strip() |
| |
| |
| if not content: |
| print("β
Answer file is empty (no matches found)") |
| return True |
| |
| lines = content.split('\n') |
| |
| for i, line in enumerate(lines, 1): |
| line = line.strip() |
| if not line: |
| continue |
| |
| |
| if not line.endswith('.txt') or not line.startswith('file_'): |
| print(f"β Line {i} has incorrect format: {line}") |
| print(" Expected format: filename.txt") |
| return False |
| |
| print("β
Answer format is correct") |
| return True |
| |
| except Exception as e: |
| print(f"β Error reading answer file: {e}") |
| return False |
|
|
| def find_30_plus_char_matches(test_dir: Path) -> set: |
| """Find all files that have 30+ character substring matches with large_file.txt.""" |
| large_file = test_dir / "large_file.txt" |
| if not large_file.exists(): |
| print("β large_file.txt not found") |
| return set() |
| |
| large_content = large_file.read_text() |
| matching_files = set() |
| |
| |
| for i in range(1, 21): |
| filename = f"file_{i:02d}.txt" |
| file_path = test_dir / filename |
| |
| if not file_path.exists(): |
| continue |
| |
| file_content = file_path.read_text() |
| |
| |
| has_match = False |
| for start_pos in range(len(file_content)): |
| for end_pos in range(start_pos + 30, len(file_content) + 1): |
| substring = file_content[start_pos:end_pos] |
| if substring in large_content: |
| has_match = True |
| break |
| if has_match: |
| break |
| |
| if has_match: |
| matching_files.add(filename) |
| |
| return matching_files |
|
|
| def verify_matches_are_correct(test_dir: Path) -> bool: |
| """Verify that the files listed in answer.txt actually have 30+ character matches.""" |
| answer_file = test_dir / "answer.txt" |
| |
| try: |
| content = answer_file.read_text().strip() |
| |
| |
| if not content: |
| expected_matches = find_30_plus_char_matches(test_dir) |
| if expected_matches: |
| print("β Answer file is empty but matches should exist") |
| for filename in expected_matches: |
| print(f" Expected: {filename}") |
| return False |
| else: |
| print("β
No matches found (correct)") |
| return True |
| |
| |
| answer_files = set() |
| lines = content.split('\n') |
| for line in lines: |
| line = line.strip() |
| if not line: |
| continue |
| answer_files.add(line) |
| |
| |
| expected_matches = find_30_plus_char_matches(test_dir) |
| |
| |
| for filename in answer_files: |
| if filename not in expected_matches: |
| print(f"β File {filename} listed in answer but has no valid 30+ character match") |
| return False |
| |
| |
| for filename in expected_matches: |
| if filename not in answer_files: |
| print(f"β Missing match for {filename} in answer file") |
| return False |
| |
| print("β
All matches are correct") |
| return True |
| |
| except Exception as e: |
| print(f"β Error verifying matches: {e}") |
| return False |
|
|
| def verify_files_exist(test_dir: Path) -> bool: |
| """Verify that all files mentioned in answer.txt actually exist.""" |
| answer_file = test_dir / "answer.txt" |
| |
| try: |
| content = answer_file.read_text().strip() |
| |
| if not content: |
| return True |
| |
| lines = content.split('\n') |
| for line in lines: |
| line = line.strip() |
| if not line: |
| continue |
| |
| file_path = test_dir / line |
| |
| if not file_path.exists(): |
| print(f"β File mentioned in answer does not exist: {line}") |
| return False |
| |
| print("β
All files mentioned in answer exist") |
| return True |
| |
| except Exception as e: |
| print(f"β Error verifying file existence: {e}") |
| return False |
|
|
| def main(): |
| """Main verification function.""" |
| test_dir = get_test_directory() |
| print("π Verifying File Filtering Task: Find Files with Common Substring...") |
| |
| |
| verification_steps = [ |
| ("Answer File Exists", verify_answer_file_exists), |
| ("Answer Format", verify_answer_format), |
| ("Files Exist", verify_files_exist), |
| ("Matches are Correct", verify_matches_are_correct), |
| ] |
| |
| |
| all_passed = True |
| for step_name, verify_func in verification_steps: |
| print(f"\n--- {step_name} ---") |
| if not verify_func(test_dir): |
| all_passed = False |
| |
| |
| print("\n" + "="*50) |
| if all_passed: |
| print("β
File filtering task completed correctly!") |
| print("π Task verification: PASS") |
| sys.exit(0) |
| else: |
| print("β Task verification: FAIL") |
| sys.exit(1) |
|
|
| if __name__ == "__main__": |
| main() |