Files
fiona-assistant/tests/test_librarian.py
T
2026-09-29 16:01:16 +02:00

99 lines
3.8 KiB
Python

"""Tests for the Librarian tool."""
import os
import pathlib
import tempfile
from unittest.mock import patch
import pytest
from fiona import logging_config
from fiona.librarian.librarian import main
def test_list_all_files(librarian, temp_dir_with_files):
"""Test that list_all_files() correctly lists all files in a given directory."""
temp_dir, test_files = temp_dir_with_files
# Test the method
files = librarian.list_all_files(temp_dir)
# Check that all files are listed (including the one in subdirectory)
assert len(files) == 3
for file_path in test_files:
assert file_path in files
def test_list_all_md_files_in_dir(librarian, temp_dir_with_mdfiles):
temp_dir, test_files, md_files = temp_dir_with_mdfiles
scanned_files = librarian.list_all_md_files(temp_dir)
# Normalize expected paths with resolve()
resolved_test_files = [str(pathlib.Path(f).resolve()) for f in test_files]
resolved_md_files = [str(pathlib.Path(f).resolve()) for f in md_files]
for file in resolved_test_files:
assert file not in scanned_files, f"should be ignored: {file} in {temp_dir}, {scanned_files}"
for file in resolved_md_files:
assert file in scanned_files, f"should be detected: {file} in {temp_dir}, {scanned_files}"
class TestLibrarianMain:
"""Tests for librarian.main() function."""
def test_main_default_directory(self, capsys, monkeypatch):
"""Test main() with default directory (current directory)."""
with tempfile.TemporaryDirectory() as temp_dir:
# Change to temp directory
original_dir = os.getcwd()
os.chdir(temp_dir)
# Isolate from the packaged fiona.log file logging: this
# test counts the files in the working directory.
monkeypatch.setattr(
logging_config, "DEFAULT_SETTINGS_FILE",
pathlib.Path(temp_dir) / "missing-settings.json",
)
# Create some test files
with open("test1.txt", 'w') as f:
f.write("content1")
with open("test2.txt", 'w') as f:
f.write("content2")
try:
# Mock sys.argv to simulate no arguments
with patch('sys.argv', ['librarian']):
main()
captured = capsys.readouterr()
assert "Found 2 files" in captured.out
# The output uses '.' for current directory
assert "." in captured.out or temp_dir in captured.out
finally:
os.chdir(original_dir)
def test_main_custom_directory(self, capsys):
"""Test main() with a custom directory argument."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create some test files
with open(os.path.join(temp_dir, "test1.txt"), 'w') as f:
f.write("content1")
with open(os.path.join(temp_dir, "test2.txt"), 'w') as f:
f.write("content2")
# Mock sys.argv to simulate directory argument
with patch('sys.argv', ['librarian', temp_dir]):
main()
captured = capsys.readouterr()
assert f"Found 2 files in '{temp_dir}'" in captured.out
assert temp_dir in captured.out
def test_main_invalid_directory(self, capsys):
"""Test main() with an invalid directory."""
with patch('sys.argv', ['librarian', '/nonexistent/directory']):
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 1
captured = capsys.readouterr()
assert "not a valid directory" in captured.err