Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/test_start_script_option_audit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
import tempfile
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools"))

from start_script_option_audit import build


class StartScriptOptionAuditTest(unittest.TestCase):
def test_detects_undocumented_option(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
(root / "start_mxexporter.sh").write_text(
" --port|-p=<port> Specific port, default: non-standard\n"
" --help|-h Display help-message\n"
" --help|-h)\n"
" --port=*|-p=*)\n"
" --pid=*)\n",
encoding="utf-8",
)

report = build(root)

self.assertEqual(report["undocumented_options"], ["--pid"])
Comment on lines +12 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

建议在测试用例中加入包含连字符的描述文本(例如 non-standardhelp-message),以验证并确保审计工具不会将描述中的连字符单词误识别为命令行选项。

Suggested change
def test_detects_undocumented_option(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
(root / "start_mxexporter.sh").write_text(
" --port|-p=<port>\n"
" --help|-h\n"
" --port=*|-p=*)\n"
" --pid=*)\n",
encoding="utf-8",
)
report = build(root)
self.assertEqual(report["undocumented_options"], ["--pid"])
def test_detects_undocumented_option(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
(root / "start_mxexporter.sh").write_text(
" --port|-p=<port> Specific port, default: non-standard\n"
" --help|-h Display help-message\n"
" --port=*|-p=*)\n"
" --pid=*)\n",
encoding="utf-8",
)
report = build(root)
self.assertEqual(report["undocumented_options"], ["--pid"])

self.assertEqual(report["stale_documentation"], [])


if __name__ == "__main__":
unittest.main()
63 changes: 63 additions & 0 deletions tools/start_script_option_audit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""Compare documented and implemented start_mxexporter.sh options."""

from __future__ import annotations

import argparse
import json
import re
Comment on lines +6 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

根据 PEP 8 编码规范,标准库导入应当归为一组,且组内不应有空行。建议将 import re 与其他标准库导入合并到同一个组中,并按字母顺序排序。

Suggested change
import argparse
import json
from pathlib import Path
import re
import argparse
import json
from pathlib import Path
import re
References
  1. PEP 8: Imports should be grouped and standard library imports should be in a single group without extra blank lines. (link)

from pathlib import Path


TOKEN_RE = re.compile(r"--[A-Za-z0-9-]+|-[A-Za-z0-9]+")


def parse_documented(text: str) -> set[str]:
items = set()
for raw_line in text.splitlines():
line = raw_line.strip()
if not line.startswith("--") or line.endswith(")") or ":)" in line:
continue
definition = line.split(maxsplit=1)[0]
items.update(TOKEN_RE.findall(definition))
return items
Comment on lines +15 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

parse_documented 中,直接对整行文本 line 进行 TOKEN_RE.findall(line) 匹配是非常脆弱的。如果选项的描述文本中包含带连字符的单词(例如 cmd-toolk8s-domainnon-root 等),正则表达式会把其中的 -tool-domain-root 误识别为命令行选项,从而导致审计结果出现误报。

建议只对每行开头的第一个空格分隔的标记(即选项定义部分)进行正则匹配,以完全避免描述文本的干扰。

Suggested change
def parse_documented(text: str) -> set[str]:
items = set()
for raw_line in text.splitlines():
line = raw_line.strip()
if not line.startswith("--") or line.endswith(")") or ":)" in line:
continue
items.update(TOKEN_RE.findall(line))
return items
def parse_documented(text: str) -> set[str]:
items = set()
for raw_line in text.splitlines():
line = raw_line.strip()
if not line.startswith("--") or line.endswith(")") or ":)" in line:
continue
first_token = line.split()[0]
items.update(TOKEN_RE.findall(first_token))
return items



def parse_implemented(text: str) -> set[str]:
items = set()
for raw_line in text.splitlines():
line = raw_line.strip()
if not line.endswith(")") or not line.startswith("-"):
continue
items.update(TOKEN_RE.findall(line))
return items


def build(repo_root: Path) -> dict[str, object]:
script_text = (repo_root / "start_mxexporter.sh").read_text(encoding="utf-8")
documented = parse_documented(script_text)
implemented = parse_implemented(script_text)
return {
"documented_count": len(documented),
"implemented_count": len(implemented),
"undocumented_options": sorted(implemented - documented),
"stale_documentation": sorted(documented - implemented),
}


def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repo-root", type=Path, default=Path("."))
parser.add_argument("--output", type=Path)
args = parser.parse_args()

text = json.dumps(build(args.repo_root), indent=2, ensure_ascii=False)
if args.output:
args.output.write_text(text + "\n", encoding="utf-8")
else:
print(text)
return 0


if __name__ == "__main__":
raise SystemExit(main())