Refactor test cases for improved readability and consistency
- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
This commit is contained in:
@@ -2,16 +2,17 @@
|
||||
|
||||
This is intentionally small and non-destructive; it touches only files under docs/ and makes safe changes.
|
||||
"""
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
DOCS = Path(__file__).resolve().parents[1] / "docs"
|
||||
|
||||
CODE_LANG_HINTS = {
|
||||
'powershell': ('powershell',),
|
||||
'bash': ('bash', 'sh'),
|
||||
'sql': ('sql',),
|
||||
'python': ('python',),
|
||||
"powershell": ("powershell",),
|
||||
"bash": ("bash", "sh"),
|
||||
"sql": ("sql",),
|
||||
"python": ("python",),
|
||||
}
|
||||
|
||||
|
||||
@@ -19,48 +20,60 @@ def add_code_fence_language(match):
|
||||
fence = match.group(0)
|
||||
inner = match.group(1)
|
||||
# If language already present, return unchanged
|
||||
if fence.startswith('```') and len(fence.splitlines()[0].strip()) > 3:
|
||||
if fence.startswith("```") and len(fence.splitlines()[0].strip()) > 3:
|
||||
return fence
|
||||
# Try to infer language from the code content
|
||||
code = inner.strip().splitlines()[0] if inner.strip() else ''
|
||||
lang = ''
|
||||
if code.startswith('$') or code.startswith('PS') or code.lower().startswith('powershell'):
|
||||
lang = 'powershell'
|
||||
elif code.startswith('#') or code.startswith('import') or code.startswith('from'):
|
||||
lang = 'python'
|
||||
elif re.match(r'^(select|insert|update|create)\b', code.strip(), re.I):
|
||||
lang = 'sql'
|
||||
elif code.startswith('git') or code.startswith('./') or code.startswith('sudo'):
|
||||
lang = 'bash'
|
||||
code = inner.strip().splitlines()[0] if inner.strip() else ""
|
||||
lang = ""
|
||||
if (
|
||||
code.startswith("$")
|
||||
or code.startswith("PS")
|
||||
or code.lower().startswith("powershell")
|
||||
):
|
||||
lang = "powershell"
|
||||
elif (
|
||||
code.startswith("#")
|
||||
or code.startswith("import")
|
||||
or code.startswith("from")
|
||||
):
|
||||
lang = "python"
|
||||
elif re.match(r"^(select|insert|update|create)\b", code.strip(), re.I):
|
||||
lang = "sql"
|
||||
elif (
|
||||
code.startswith("git")
|
||||
or code.startswith("./")
|
||||
or code.startswith("sudo")
|
||||
):
|
||||
lang = "bash"
|
||||
if lang:
|
||||
return f'```{lang}\n{inner}\n```'
|
||||
return f"```{lang}\n{inner}\n```"
|
||||
return fence
|
||||
|
||||
|
||||
def normalize_file(path: Path):
|
||||
text = path.read_text(encoding='utf-8')
|
||||
text = path.read_text(encoding="utf-8")
|
||||
orig = text
|
||||
# Trim trailing whitespace and ensure single trailing newline
|
||||
text = '\n'.join(line.rstrip() for line in text.splitlines()) + '\n'
|
||||
text = "\n".join(line.rstrip() for line in text.splitlines()) + "\n"
|
||||
# Ensure first non-empty line is H1
|
||||
lines = text.splitlines()
|
||||
for i, ln in enumerate(lines):
|
||||
if ln.strip():
|
||||
if not ln.startswith('#'):
|
||||
lines[i] = '# ' + ln
|
||||
if not ln.startswith("#"):
|
||||
lines[i] = "# " + ln
|
||||
break
|
||||
text = '\n'.join(lines) + '\n'
|
||||
text = "\n".join(lines) + "\n"
|
||||
# Add basic code fence languages where missing (simple heuristic)
|
||||
text = re.sub(r'```\n([\s\S]*?)\n```', add_code_fence_language, text)
|
||||
text = re.sub(r"```\n([\s\S]*?)\n```", add_code_fence_language, text)
|
||||
if text != orig:
|
||||
path.write_text(text, encoding='utf-8')
|
||||
path.write_text(text, encoding="utf-8")
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
changed = []
|
||||
for p in DOCS.rglob('*.md'):
|
||||
for p in DOCS.rglob("*.md"):
|
||||
if p.is_file():
|
||||
try:
|
||||
if normalize_file(p):
|
||||
@@ -68,12 +81,12 @@ def main():
|
||||
except Exception as e:
|
||||
print(f"Failed to format {p}: {e}")
|
||||
if changed:
|
||||
print('Formatted files:')
|
||||
print("Formatted files:")
|
||||
for c in changed:
|
||||
print(' -', c)
|
||||
print(" -", c)
|
||||
else:
|
||||
print('No formatting changes required.')
|
||||
print("No formatting changes required.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user