feat: replace hash// plugin with comment plugin that writes tags in each language's comment style

This commit is contained in:
randogoth 2026-10-24 21:32:28 +03:00
parent 90047c22b5
commit 633012d1ef
6 changed files with 198 additions and 67 deletions

91
tests/test_comment.py Normal file
View file

@ -0,0 +1,91 @@
import os
import shutil
import subprocess
import sys
import pytest
import orvanitag.orvani as orvani
from orvanitag.orvani import OrvaniTag
@pytest.fixture
def cwd(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return tmp_path
def write(name, content, *tags):
with open(name, 'w', newline='') as file:
file.write(content)
OrvaniTag().add(name, *tags)
return OrvaniTag().write_metadata(name, 'comment')
def read_back(cwd, name):
(cwd / orvani.CONTEXT_FILE).unlink()
OrvaniTag().read_metadata(name)
return (cwd / orvani.CONTEXT_FILE).read_text()
def test_python_keeps_shebang_coding_and_docstring(cwd):
source = '#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n"""Real docstring."""\nprint(__doc__)\n'
(cwd / 's.py').write_text(source)
os.chmod(cwd / 's.py', 0o755)
assert write('s.py', source, '+tools', '#demo', 'a note')[0] == 0
assert (cwd / 's.py').read_text() == (
'#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# otag: +tools #demo a note\n\n'
'"""Real docstring."""\nprint(__doc__)\n'
)
assert os.stat(cwd / 's.py').st_mode & 0o111
run = subprocess.run([sys.executable, 's.py'], capture_output=True, text=True)
assert run.stdout == 'Real docstring.\n'
assert read_back(cwd, 's.py') == 's.py --- +tools #demo a note\n'
@pytest.mark.skipif(not shutil.which('perl'), reason='perl not installed')
def test_perl_still_runs(cwd):
assert write('s.pl', 'print "perl ok\\n";\n', '#demo')[0] == 0
run = subprocess.run(['perl', 's.pl'], capture_output=True, text=True)
assert run.stdout == 'perl ok\n'
def test_php_tag_goes_inside_the_php_block(cwd):
assert write('p.php', '<?php\necho "ok";\n', '#demo')[0] == 0
assert (cwd / 'p.php').read_text() == '<?php\n// otag: #demo\n\necho "ok";\n'
def test_php_without_own_opening_line_is_refused(cwd):
with pytest.raises(ValueError):
write('p.php', '<?php echo "ok"; ?>\n', '#demo')
assert (cwd / 'p.php').read_text() == '<?php echo "ok"; ?>\n'
def test_go_tag_is_not_a_package_doc_comment(cwd):
assert write('m.go', 'package main\n', '#demo')[0] == 0
assert (cwd / 'm.go').read_text() == '// otag: #demo\n\npackage main\n'
def test_html_keeps_doctype_first(cwd):
assert write('i.html', '<!DOCTYPE html>\n<html></html>\n', '#demo', 'home page')[0] == 0
assert (cwd / 'i.html').read_text() == '<!DOCTYPE html>\n<!-- otag: #demo home page -->\n\n<html></html>\n'
assert read_back(cwd, 'i.html') == 'i.html --- #demo home page\n'
def test_existing_tag_line_is_replaced_and_crlf_kept(cwd):
source = '// otag: #old\r\n\r\nint main() {}\r\n'
assert write('m.c', source, '#new')[0] == 0
assert (cwd / 'm.c').read_bytes() == b'// otag: #new\r\n\r\nint main() {}\r\n'
def test_reads_block_comment_and_toml(cwd):
(cwd / 'a.js').write_text('/* otag: +site #ui */\nlet a = 1\n')
(cwd / 'pyproject.toml').write_text('# otag: +orvanitag @orga\n[project]\n')
OrvaniTag().scan('.', False)
assert (cwd / orvani.CONTEXT_FILE).read_text() == 'a.js --- +site #ui\npyproject.toml --- +orvanitag @orga\n'
def test_marker_inside_code_is_ignored(cwd):
(cwd / 'a.py').write_text('x = "otag: #nope"\n')
OrvaniTag().scan('.', False)
assert not (cwd / orvani.CONTEXT_FILE).exists()