|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +import typing as t |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from anchovy.core import BuildSettings, Context, Matcher, PathCalc, Rule, Step |
| 10 | +from anchovy.paths import DirPathCalc, OutputDirPathCalc, REMatcher, WebIndexPathCalc, WorkingDirPathCalc |
| 11 | + |
| 12 | + |
| 13 | +INPUT_PATH = Path('input') |
| 14 | +WORKING_PATH = Path('working') |
| 15 | +OUTPUT_PATH = Path('output') |
| 16 | +EXTERNAL_PATH = Path('external') |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def dummy_context(): |
| 21 | + return Context( |
| 22 | + BuildSettings( |
| 23 | + input_dir=INPUT_PATH, |
| 24 | + output_dir=OUTPUT_PATH, |
| 25 | + working_dir=WORKING_PATH, |
| 26 | + custody_cache=EXTERNAL_PATH / 'custody.json', |
| 27 | + purge_dirs=False |
| 28 | + ), |
| 29 | + [] |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize('config,input,expected', [ |
| 34 | + (('output_dir',), INPUT_PATH / 'foo.txt', OUTPUT_PATH / 'foo.txt'), |
| 35 | + ((OUTPUT_PATH,), INPUT_PATH / 'foo.txt', OUTPUT_PATH / 'foo.txt'), |
| 36 | + ((EXTERNAL_PATH,), WORKING_PATH / 'foo.txt', EXTERNAL_PATH / 'foo.txt'), |
| 37 | + (('working_dir',), INPUT_PATH / 'foo.txt', WORKING_PATH / 'foo.txt'), |
| 38 | + (('working_dir',), WORKING_PATH / 'foo.txt', WORKING_PATH / 'foo.txt'), |
| 39 | + (('working_dir', '.html'), WORKING_PATH / 'foo.txt', WORKING_PATH / 'foo.html'), |
| 40 | + (('working_dir', '.html'), WORKING_PATH / 'foo.j.txt', WORKING_PATH / 'foo.j.html'), |
| 41 | +]) |
| 42 | +def test_dir_path_calc(config: tuple, input: Path, expected: Path, dummy_context: Context): |
| 43 | + calc = DirPathCalc(*config) |
| 44 | + assert calc(dummy_context, input, None) == expected |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize('config,input,regex,expected', [ |
| 48 | + (('output_dir',), INPUT_PATH / 'foo.j.html', r'.*(?P<ext>\.j\.html)', OUTPUT_PATH / 'foo.j.html'), |
| 49 | + (('output_dir', '.zip'), INPUT_PATH / 'foo.j.html', r'.*(?P<ext>\.j\.html)', OUTPUT_PATH / 'foo.zip'), |
| 50 | +]) |
| 51 | +def test_dir_path_calc_regex(config: tuple, input: Path, regex: str, expected: Path, dummy_context: Context): |
| 52 | + calc = DirPathCalc(*config) |
| 53 | + match = re.match(regex, input.as_posix()) |
| 54 | + assert calc(dummy_context, input, match) == expected |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize('config,input,regex,expected', [ |
| 58 | + (('output_dir', None, lambda p: p), INPUT_PATH / 'foo.j.html', r'.*(?P<ext>\.j\.html)', OUTPUT_PATH / 'foo.j.html'), |
| 59 | + (('output_dir', '.zip', lambda p: (p.with_suffix('') / 'index').with_suffix(p.suffix)), INPUT_PATH / 'foo.j.html', r'.*(?P<ext>\.j\.html)', OUTPUT_PATH / 'foo' / 'index.zip'), |
| 60 | +]) |
| 61 | +def test_dir_path_calc_transform(config: tuple, input: Path, regex: str, expected: Path, dummy_context: Context): |
| 62 | + calc = DirPathCalc(*config) |
| 63 | + match = re.match(regex, input.as_posix()) |
| 64 | + assert calc(dummy_context, input, match) == expected |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize('config,input,expected', [ |
| 68 | + ((), INPUT_PATH / 'foo.txt', OUTPUT_PATH / 'foo.txt'), |
| 69 | + ((), WORKING_PATH / 'foo.txt', OUTPUT_PATH / 'foo.txt'), |
| 70 | + (('.html',), WORKING_PATH / 'foo.txt', OUTPUT_PATH / 'foo.html'), |
| 71 | + (('.html',), WORKING_PATH / 'foo.j.txt', OUTPUT_PATH / 'foo.j.html'), |
| 72 | +]) |
| 73 | +def test_output_dir_path_calc(config: tuple, input: Path, expected: Path, dummy_context: Context): |
| 74 | + calc = OutputDirPathCalc(*config) |
| 75 | + assert calc(dummy_context, input, None) == expected |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize('config,input,expected', [ |
| 79 | + (('output_dir', None), INPUT_PATH / 'foo.html', OUTPUT_PATH / 'foo' / 'index.html'), |
| 80 | + (('output_dir', '.zip', lambda p: p.with_stem(p.stem * 2)), INPUT_PATH / 'foo.html', OUTPUT_PATH / 'foofoo' / 'index.zip'), |
| 81 | +]) |
| 82 | +def test_web_index_path_calc(config: tuple, input: Path, expected: Path, dummy_context: Context): |
| 83 | + calc = WebIndexPathCalc(*config) |
| 84 | + assert calc(dummy_context, input, None) == expected |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize('config,input,expected', [ |
| 88 | + ((), (INPUT_PATH / 'foo.txt', None), WORKING_PATH / 'foo.txt'), |
| 89 | + ((), (WORKING_PATH / 'foo.txt', None), WORKING_PATH / 'foo.txt'), |
| 90 | + (('.html',), (WORKING_PATH / 'foo.txt', None), WORKING_PATH / 'foo.html'), |
| 91 | + (('.html',), (WORKING_PATH / 'foo.j.txt', None), WORKING_PATH / 'foo.j.html'), |
| 92 | +]) |
| 93 | +def test_working_dir_path_calc(config: tuple, input: tuple[Path, t.Any], expected: Path, dummy_context: Context): |
| 94 | + calc = WorkingDirPathCalc(*config) |
| 95 | + assert calc(dummy_context, *input) == expected |
0 commit comments