#!/usr/bin/env python3 """Script to remove patch contexts from test file.""" import re # Read the file with open('tests/api/test_nfo_endpoints.py', 'r') as f: lines = f.readlines() new_lines = [] i = 0 while i < len(lines): line = lines[i] # Check if this line starts a patch context if re.match(r'\s+with patch\(', line) or re.match(r'\s+with patch', line): # Found start of patch context, skip it and find the end indent = len(line) - len(line.lstrip()) # Skip this line and all continuation lines while i < len(lines): current = lines[i] # If it's a continuation (ends with comma or backslash) or contains patch/return_value if (current.rstrip().endswith(',') or current.rstrip().endswith('\\') or 'patch(' in current or 'return_value=' in current): i += 1 continue # If it's the closing '):' if current.strip() == '):': i += 1 break # Otherwise we're past the patch context break # Now dedent the code that was inside the context # Continue until we find a line at the same or less indent level context_indent = indent + 4 # Code inside 'with' is indented 4 more while i < len(lines): current = lines[i] current_indent = len(current) - len(current.lstrip()) # If it's a blank line, keep it if not current.strip(): new_lines.append(current) i += 1 continue # If we're back to original indent or less, we're done with this context if current_indent <= indent and current.strip(): break # Dedent by 4 spaces if it's indented more than original if current_indent > indent: new_lines.append(' ' * (current_indent - 4) + current.lstrip()) else: new_lines.append(current) i += 1 else: # Not a patch line, keep it new_lines.append(line) i += 1 # Write back with open('tests/api/test_nfo_endpoints.py', 'w') as f: f.writelines(new_lines) print(f"Processed {len(lines)} lines, output {len(new_lines)} lines")