File size: 4,503 Bytes
14daa4c |
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 |
#!python3
import unittest
from pathlib import Path
import tempfile
import os
class TestSkeletonMapper(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.patterns = create_language_patterns()
def create_test_file(self, content: str, extension: str) -> str:
path = Path(self.temp_dir) / f"test{extension}"
path.write_text(content)
return str(path)
def test_kotlin_edge_cases(self):
kotlin_code = '''
@DslMarker
annotation class NioProxyDsl
interface EnhancedNioProxy<T : Any> {
val original: T
fun verifyIdentity(): Boolean = enhanced.equals(original)
}
class ProxyContext {
private val _events = MutableSharedFlow<ProxyEvent>()
}
'''
file_path = self.create_test_file(kotlin_code, ".kt")
results = extract_skeleton(file_path, self.patterns)
# BUG 1: Missing generic type parameters in class/interface detection
self.assertIn("interface EnhancedNioProxy<T : Any>", results['interface'])
# BUG 2: Property detection fails with initialization
self.assertIn("val original: T", results['property'])
# BUG 3: Annotation detection drops parameters
self.assertIn("@DslMarker", results['annotation'])
def fix_kotlin_patterns():
return {
'class': r'^\s*(?:data\s+)?class\s+(\w+)(?:<[^>]+>)?',
'function': r'^\s*fun\s+(\w+)(?:<[^>]+>)?',
'property': r'^\s*(?:var|val)\s+(\w+)(?:\s*:\s*[^=]+)?(?:\s*=.+)?',
'interface': r'^\s*interface\s+(\w+)(?:<[^>]+>)?',
'annotation': r'^\s*@(\w+)(?:\s*[\w\s.()]+)?',
'suspend': r'^\s*suspend\s+fun\s+\w+',
}
# Critical fixes for main implementation
def patch_implementation():
"""
Critical patches for identified issues
"""
# 1. Fix subprocess handling for large files
def safe_grep(cmd: str, timeout: int = 30) -> str:
try:
return subprocess.run(
cmd,
shell=True,
text=True,
capture_output=True,
timeout=timeout
).stdout
except subprocess.TimeoutExpired:
return ""
# 2. Fix pattern escaping in grep command
def escape_grep_pattern(pattern: str) -> str:
return pattern.replace('(', '\\(').replace(')', '\\)')
# 3. Add file encoding handling
def read_file_safe(file_path: str) -> str:
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
except UnicodeDecodeError:
try:
with open(file_path, 'r', encoding='latin-1') as f:
return f.read()
except Exception:
return ""
return safe_grep, escape_grep_pattern, read_file_safe
# Shell script fixes
def generate_fixed_shell_script():
return '''
#!/bin/bash
# Fixed file handling
while IFS= read -r -d '' file; do
if [[ ! -f "$file" ]]; then
continue
fi
# Handle filenames with spaces and special chars
file_ext="${file##*.}"
file_name=$(printf '%q' "$file")
# Prevent grep pattern injection
safe_grep() {
local pattern=$1
local file=$2
grep -E "^[[:space:]]*${pattern}" "$file" 2>/dev/null || true
}
case "$file_ext" in
kt|kts)
safe_grep "(@\\w+|class\\s+\\w+|interface\\s+\\w+|fun\\s+\\w+)" "$file_name"
;;
# ... other extensions
esac
done < <(find . -type f -print0)
'''
# Runtime monitoring hooks
def add_monitoring():
import time
import psutil
def monitor_execution(func):
def wrapper(*args, **kwargs):
start = time.time()
process = psutil.Process()
mem_before = process.memory_info().rss
result = func(*args, **kwargs)
elapsed = time.time() - start
mem_after = process.memory_info().rss
mem_delta = mem_after - mem_before
if elapsed > 5.0 or mem_delta > 100*1024*1024: # 100MB
print(f"Warning: High resource usage in {func.__name__}")
return result
return wrapper
return monitor_execution
|