Files
nvaldez 792beca2c5 daedalus 0.0.0: empty template - one CLI, many small scripts, each with tests
Sanitized open-source starter: renamed to daedalus, Norse-free of
internal references, AI attribution stripped, skill merged into
AGENTS.md, CI converted to Gitea Actions. No registered scripts yet.
2026-11-11 18:55:11 +01:00

162 lines
5.6 KiB
Python

"""Tests for ci.checks.
Pass and fail cases for each file check.
"""
import shutil
import pytest
from ci import checks
needs_shellcheck = pytest.mark.skipif(shutil.which("shellcheck") is None, reason="shellcheck not installed")
def test_iter_files_filters_suffix_and_skips_dirs(tmp_path):
"""Only matching suffixes outside SKIP_DIRS are listed."""
(tmp_path / "keep.py").write_text("", encoding="utf-8")
(tmp_path / "other.txt").write_text("", encoding="utf-8")
(tmp_path / ".venv").mkdir()
(tmp_path / ".venv" / "skip.py").write_text("", encoding="utf-8")
assert checks.iter_files(tmp_path, (".py",)) == [tmp_path / "keep.py"]
@pytest.mark.parametrize("source, expected", [("x = 1\n", 0), ("def broken(:\n", 1)])
def test_check_python_syntax(tmp_path, source, expected):
"""Invalid Python fails, valid Python passes."""
path = tmp_path / "script.py"
path.write_text(source, encoding="utf-8")
assert checks.check_python_syntax(path) == expected
def test_check_pycodestyle_counts_violations(tmp_path):
"""PEP 8 violations are counted."""
clean = tmp_path / "clean.py"
clean.write_text("x = 1\n", encoding="utf-8")
dirty = tmp_path / "dirty.py"
dirty.write_text("x=1 \n", encoding="utf-8")
assert checks.check_pycodestyle([clean]) == 0
assert checks.check_pycodestyle([dirty]) > 0
@needs_shellcheck
@pytest.mark.parametrize("source, expected", [
('#!/bin/bash\necho "$1"\n', 0),
("#!/bin/bash\necho $1\n", 1),
])
def test_check_shell(tmp_path, source, expected):
"""shellcheck warnings fail, clean scripts pass."""
path = tmp_path / "script.sh"
path.write_bytes(source.encode("utf-8"))
assert checks.check_shell(path) == expected
def test_check_shell_fails_without_shellcheck(tmp_path, monkeypatch):
"""A missing shellcheck is an error, not a silent pass."""
monkeypatch.setattr(checks.shutil, "which", lambda _name: None)
assert checks.check_shell(tmp_path / "script.sh") == 1
@pytest.mark.parametrize("source, expected", [('{"a": 1}', 0), ('{"a": }', 1)])
def test_check_json(tmp_path, source, expected):
"""Invalid JSON fails, valid JSON passes."""
path = tmp_path / "data.json"
path.write_text(source, encoding="utf-8")
assert checks.check_json(path) == expected
@pytest.mark.parametrize("source, expected", [("a: 1\n---\nb: 2\n", 0), ("a: [1\n", 1)])
def test_check_yaml(tmp_path, source, expected):
"""Invalid YAML fails, multi-document YAML passes."""
path = tmp_path / "data.yaml"
path.write_text(source, encoding="utf-8")
assert checks.check_yaml(path) == expected
@pytest.mark.parametrize("source, fails", [
("# Title\n\n- item\n", False),
("# Title\n# Title again\n", True),
])
def test_check_markdown(tmp_path, source, fails):
"""markdownlint failures are counted."""
path = tmp_path / "doc.md"
path.write_bytes(source.encode("utf-8"))
assert (checks.check_markdown([path]) > 0) == fails
def test_check_markdown_allows_front_matter(tmp_path):
"""YAML front matter before the first heading is not reported."""
path = tmp_path / "SKILL.md"
path.write_bytes(b"---\nname: demo\nversion: 1.0.0\n---\n\n# Title\n\nText.\n")
assert checks.check_markdown([path]) == 0
def test_check_markdown_ignores_line_length(tmp_path):
"""MD013 long lines are not reported."""
path = tmp_path / "doc.md"
path.write_bytes(("# Title\n\n" + "word " * 60).strip().encode("utf-8") + b"\n")
assert checks.check_markdown([path]) == 0
@pytest.mark.parametrize("data, expected", [
(b"a\nb\n", 0),
(b"a\r\nb\r\n", 1),
(b"\x89PNG\r\n\x00\x00", 0),
])
def test_check_line_ending(tmp_path, data, expected):
"""CRLF in text fails; LF and binary files pass."""
path = tmp_path / "file"
path.write_bytes(data)
assert checks.check_line_ending(path) == expected
@pytest.mark.parametrize("data, expected", [
(b"@echo off\r\nexit /b 0\r\n", 0),
(b"@echo off\nexit /b 0\n", 1),
(b"@echo off\r\nexit /b 0\n", 1),
])
def test_check_line_ending_bat_needs_crlf(tmp_path, data, expected):
""".bat files must be CRLF; any bare LF fails."""
path = tmp_path / "run.bat"
path.write_bytes(data)
assert checks.check_line_ending(path) == expected
@pytest.mark.parametrize("name, first_line, expected", [
("run.sh", b"echo hi\n", True),
("launcher", b"#!/usr/bin/env bash\n", True),
("launcher", b"#!/bin/sh\n", True),
("tool", b"#!/usr/bin/env python3\n", False),
("notes", b"plain text\n", False),
("data.txt", b"#!/bin/bash\n", False),
])
def test_is_shell_script(tmp_path, name, first_line, expected):
"""Shell suffixes and suffixless shell shebangs are shell scripts."""
path = tmp_path / name
path.write_bytes(first_line)
assert checks.is_shell_script(path) == expected
@pytest.mark.parametrize("kind", ["RSA ", "OPENSSH ", ""])
def test_check_private_key_detects_keys(tmp_path, kind):
"""PEM and OpenSSH private key headers fail."""
path = tmp_path / "id_key"
path.write_bytes(b"x\n-----BEGIN " + kind.encode() + b"PRIVATE KEY-----\nabc\n")
assert checks.check_private_key(path) == 1
def test_check_private_key_allows_public_key(tmp_path):
"""Public keys and normal files pass."""
path = tmp_path / "key.pub"
path.write_bytes(b"-----BEGIN PUBLIC KEY-----\nabc\n")
assert checks.check_private_key(path) == 0
def test_check_file_size(tmp_path, monkeypatch):
"""Files above MAX_FILE_SIZE fail."""
monkeypatch.setattr(checks, "MAX_FILE_SIZE", 4)
path = tmp_path / "file"
path.write_bytes(b"1234")
assert checks.check_file_size(path) == 0
path.write_bytes(b"12345")
assert checks.check_file_size(path) == 1