8Tests for content-level secret scanning (OI-416) in ``oct.tools.source_exporter``.
12- Verify ``_scan_content_for_secrets`` flags likely secret assignments.
13- Verify non-secret assignments are not flagged.
14- Verify ``run_exporter`` emits warnings when ``--warn-secrets`` is on.
15- Verify ``--block-secrets`` excludes files with potential secrets from
27Inputs: tmp_path fixtures
28Outputs: pass/fail assertions
32from pathlib
import Path
36from oct.tools
import source_exporter
44 content =
'password = "hunter2_is_bad"\n'
46 assert len(warnings) == 1
47 assert "password" in warnings[0]
48 assert "foo.py:1" in warnings[0]
52 content =
'api_key = "sk-abc123xyz"\n'
54 assert len(warnings) == 1
55 assert "api_key" in warnings[0]
59 content =
'name = "Alice"\nage = "30"\n'
65 """Very short values (< 4 chars) don't look like real secrets."""
66 content =
'password = "a"\n'
76 'secret_token = "abc1234xyz"\n'
79 assert len(warnings) == 1
80 assert ":4:" in warnings[0]
88 """Create a minimal Option C project layout with one python file."""
89 proj = tmp_path /
"proj"
90 (proj /
"docs").mkdir(parents=
True)
91 (proj /
"tests").mkdir()
92 (proj /
"oc_diagnostics").mkdir()
93 (proj / file_name).write_text(content, encoding=
"utf-8")
100 '#!/usr/bin/env python3\n'
101 '# -*- coding: utf-8 -*-\n'
104 'password = "hunter2_literal"\n'
107 captured = capsys.readouterr()
108 assert "potential secret 'password'" in captured.err
114 'password = "hunter2_literal"\n'
117 captured = capsys.readouterr()
118 assert "potential secret" not in captured.err
123 tmp_path,
"leaky.py",
124 '#!/usr/bin/env python3\n'
125 '# -*- coding: utf-8 -*-\n'
128 'api_key = "sk-SECRET_VALUE_1234"\n'
131 captured = capsys.readouterr()
132 assert "potential secret 'api_key'" in captured.err
133 assert "Skipping" in captured.err
136 output_dirs = list(proj.glob(
"_source_code-*"))
137 assert output_dirs,
"exporter should have produced at least one output dir"
139 for p
in output_dirs[0].rglob(
"*.txt"):
140 text = p.read_text(encoding=
"utf-8", errors=
"replace")
141 if "sk-SECRET_VALUE_1234" in text:
144 assert not found_secret,
"blocked file should not appear in exported content"
list[str] _scan_content_for_secrets(str content, str rel_path)
None run_exporter(Path root, list[str] args)