Files
rendercv/tests/renderer/test_path_resolver.py
lif abfe1e1733 feat: add --output-folder option to specify output directory (#578)
* feat: add --output-folder option to specify base output directory

This addresses issue #551 by adding support for customizing the output
folder for all generated files.

Changes:
- Add `output_folder` field to RenderCommand model
- Add `--output-folder` / `-o` CLI option
- When output_folder is set, it replaces the default `rendercv_output`
  folder in all output paths
- Support OUTPUT_FOLDER placeholder for advanced path customization
- Add comprehensive test coverage

Usage examples:
  # Simple: output all files to build/en/
  rendercv render cv.yaml --output-folder build/en/

  # Advanced: use OUTPUT_FOLDER placeholder for custom paths
  rendercv render cv.yaml --output-folder dist/ \
    --pdf-path OUTPUT_FOLDER/final/resume.pdf

Closes #551

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address linting errors and Windows compatibility

- Fix PIE810: use tuple in startswith() call
- Fix ruff format issues
- Make tests cross-platform compatible for Windows

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: split compound assertions per PT018 rule

* Improve and update examples and schema

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Sina Atalay <79940989+sinaatalay@users.noreply.github.com>
2026-02-17 00:01:19 +03:00

207 lines
7.5 KiB
Python

import datetime
import pathlib
import pytest
from rendercv.renderer.path_resolver import (
resolve_output_folder_placeholder,
resolve_rendercv_file_path,
)
from rendercv.schema.models.cv.cv import Cv
from rendercv.schema.models.rendercv_model import RenderCVModel
from rendercv.schema.models.settings.render_command import RenderCommand
from rendercv.schema.models.settings.settings import Settings
class TestResolveRendercvFilePath:
@pytest.mark.parametrize(
("file_path_template", "cv_name", "current_date", "expected_filename"),
[
# Name placeholders
("NAME.pdf", "John Doe", None, "John Doe.pdf"),
("NAME_IN_SNAKE_CASE.pdf", "John Doe", None, "John_Doe.pdf"),
("NAME_IN_LOWER_SNAKE_CASE.pdf", "John Doe", None, "john_doe.pdf"),
("NAME_IN_UPPER_SNAKE_CASE.pdf", "John Doe", None, "JOHN_DOE.pdf"),
("NAME_IN_KEBAB_CASE.pdf", "John Doe", None, "John-Doe.pdf"),
("NAME_IN_LOWER_KEBAB_CASE.pdf", "John Doe", None, "john-doe.pdf"),
("NAME_IN_UPPER_KEBAB_CASE.pdf", "John Doe", None, "JOHN-DOE.pdf"),
# Date placeholders
("MONTH_NAME.pdf", "John Doe", datetime.date(2024, 3, 15), "March.pdf"),
(
"MONTH_ABBREVIATION.pdf",
"John Doe",
datetime.date(2024, 3, 15),
"Mar.pdf",
),
("MONTH.pdf", "John Doe", datetime.date(2024, 3, 15), "3.pdf"),
(
"MONTH_IN_TWO_DIGITS.pdf",
"John Doe",
datetime.date(2024, 3, 15),
"03.pdf",
),
("YEAR.pdf", "John Doe", datetime.date(2024, 3, 15), "2024.pdf"),
(
"YEAR_IN_TWO_DIGITS.pdf",
"John Doe",
datetime.date(2024, 3, 15),
"24.pdf",
),
# Different months
("MONTH_NAME.pdf", "John Doe", datetime.date(2024, 1, 1), "January.pdf"),
(
"MONTH_ABBREVIATION.pdf",
"John Doe",
datetime.date(2024, 1, 1),
"Jan.pdf",
),
("MONTH_NAME.pdf", "John Doe", datetime.date(2024, 6, 1), "June.pdf"),
(
"MONTH_ABBREVIATION.pdf",
"John Doe",
datetime.date(2024, 6, 1),
"June.pdf",
),
("MONTH_NAME.pdf", "John Doe", datetime.date(2024, 12, 1), "December.pdf"),
(
"MONTH_ABBREVIATION.pdf",
"John Doe",
datetime.date(2024, 12, 1),
"Dec.pdf",
),
# Day placeholders
("DAY.pdf", "John Doe", datetime.date(2024, 3, 15), "15.pdf"),
("DAY_IN_TWO_DIGITS.pdf", "John Doe", datetime.date(2024, 3, 5), "05.pdf"),
# Multiple placeholders
(
"NAME_IN_SNAKE_CASE_CV_YEAR-MONTH_IN_TWO_DIGITS.pdf",
"John Doe",
datetime.date(2024, 3, 15),
"John_Doe_CV_2024-03.pdf",
),
# No placeholders
("my_cv.pdf", "John Doe", None, "my_cv.pdf"),
],
)
def test_resolve_rendercv_file_path(
self,
tmp_path: pathlib.Path,
file_path_template: str,
cv_name: str,
current_date: datetime.date | None,
expected_filename: str,
):
if current_date is not None:
model = RenderCVModel(
cv=Cv(name=cv_name), settings=Settings(current_date=current_date)
)
else:
model = RenderCVModel(cv=Cv(name=cv_name))
file_path = tmp_path / file_path_template
result = resolve_rendercv_file_path(model, file_path)
assert result.name == expected_filename
assert result.parent == tmp_path
def test_creates_parent_directories(self, tmp_path: pathlib.Path):
model = RenderCVModel(cv=Cv(name="John Doe"))
nested_dir = tmp_path / "output" / "cv" / "final"
file_path = nested_dir / "NAME_IN_SNAKE_CASE_CV.pdf"
result = resolve_rendercv_file_path(model, file_path)
assert result.parent.exists()
assert result == nested_dir / "John_Doe_CV.pdf"
def test_output_folder_placeholder_resolved(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "rendercv_output"
model = RenderCVModel(
cv=Cv(name="John Doe"),
settings=Settings(
render_command=RenderCommand(output_folder=output_folder)
),
)
file_path = tmp_path / "OUTPUT_FOLDER" / "NAME_IN_SNAKE_CASE_CV.pdf"
result = resolve_rendercv_file_path(model, file_path)
assert result == output_folder / "John_Doe_CV.pdf"
assert result.parent.exists()
def test_custom_output_folder(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "build" / "en"
model = RenderCVModel(
cv=Cv(name="John Doe"),
settings=Settings(
render_command=RenderCommand(output_folder=output_folder)
),
)
file_path = tmp_path / "OUTPUT_FOLDER" / "NAME_IN_SNAKE_CASE_CV.pdf"
result = resolve_rendercv_file_path(model, file_path)
assert result == output_folder / "John_Doe_CV.pdf"
def test_no_output_folder_placeholder_in_path(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "build"
model = RenderCVModel(
cv=Cv(name="John Doe"),
settings=Settings(
render_command=RenderCommand(output_folder=output_folder)
),
)
file_path = tmp_path / "custom_dir" / "NAME_IN_SNAKE_CASE_CV.pdf"
result = resolve_rendercv_file_path(model, file_path)
assert result == tmp_path / "custom_dir" / "John_Doe_CV.pdf"
def test_output_folder_with_subdirectory_in_path(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "build"
model = RenderCVModel(
cv=Cv(name="John Doe"),
settings=Settings(
render_command=RenderCommand(output_folder=output_folder)
),
)
file_path = tmp_path / "OUTPUT_FOLDER" / "typst" / "NAME_IN_SNAKE_CASE_CV.typ"
result = resolve_rendercv_file_path(model, file_path)
assert result == output_folder / "typst" / "John_Doe_CV.typ"
class TestResolveOutputFolderPlaceholder:
def test_replaces_output_folder_component(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "my_output"
file_path = tmp_path / "OUTPUT_FOLDER" / "file.pdf"
result = resolve_output_folder_placeholder(file_path, output_folder)
assert result == output_folder / "file.pdf"
def test_no_placeholder_returns_unchanged(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "my_output"
file_path = tmp_path / "some_dir" / "file.pdf"
result = resolve_output_folder_placeholder(file_path, output_folder)
assert result == file_path
def test_nested_output_folder(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "build" / "en"
file_path = tmp_path / "OUTPUT_FOLDER" / "sub" / "file.pdf"
result = resolve_output_folder_placeholder(file_path, output_folder)
assert result == output_folder / "sub" / "file.pdf"
def test_output_folder_only(self, tmp_path: pathlib.Path):
output_folder = tmp_path / "build"
file_path = tmp_path / "OUTPUT_FOLDER"
result = resolve_output_folder_placeholder(file_path, output_folder)
assert result == output_folder