def test_parts(tmp_path: Path): site = Site('https://example.org/') url_factory = lambda location: site / location out = tmp_path page = GenPath(Path('something/page.html'), out, url_factory) index = GenPath(Path('index.html'), out, url_factory) assert page.parts == ('something', 'page.html') assert index.parts == ('index.html', )
def test_exists(tmp_path: Path): site = Site('https://example.org/') url_factory = lambda location: site / location out = tmp_path (tmp_path / 'yes.html').write_text('yea!') yes = GenPath(Path('yes.html'), out, url_factory) no = GenPath(Path('resources/test.html'), out, url_factory) assert yes.exists() is True assert no.exists() is False
def test_url(tmp_path: Path): site = Site('https://example.org/') url_factory = lambda location: site / location out = tmp_path page = GenPath(Path('page.html'), out, url_factory) index = GenPath(Path('index.html'), out, url_factory) style = GenPath(Path('style.css'), out, url_factory) assert style.url == 'https://example.org/style.css' assert page.url == 'https://example.org/page' assert index.url == 'https://example.org/'
def test_include_glob(tmp_path: Path): test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add('resources/glob/**/*.html') site.generate(test_out) result = test_out / 'resources' / 'glob' assert (result / 'a.html').exists() assert (result / 'b.html').exists() assert (result / 'dir' / '1.html').exists()
def assert_site_render(src_location, content, tmp_path): with Path(src_location).open() as f: src_content = f.read() test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(content) site.generate(test_out) assert (test_out / src_location).exists() assert (test_out / src_location).read_text() == src_content
def test_render_markdown_link(tmp_path: Path): src_location = 'resources/md/link.md' link_target_location = 'resources/md/plain.md' out_location = 'md/file.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add( 'plain.html', markdown(link_target_location, template('templates/md/plain.html'))) site.add(out_location, markdown(src_location, template('templates/md/plain.html'))) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/md/md-link.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_slash(tmp_path: Path): site = Site('https://example.org/') url_factory = lambda location: site / location out = tmp_path parent = GenPath(Path('parent'), out, url_factory) child1 = GenPath(Path('child1'), out, url_factory) child2 = Path('child2') child3 = 'child3' assert str(parent / child1) == 'parent/child1' assert str(parent / child2) == 'parent/child2' assert str(parent / child3) == 'parent/child3' bad_behaviour = 0 with pytest.raises(ValueError): parent / bad_behaviour
def test_render_jinja_file(tmp_path: Path): src_location = 'resources/jinja/file.html' out_location = 'jinja/file.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(out_location, jinja(src_location)) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/jinja/file.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_render_toc(tmp_path: Path): src_location = 'resources/md/collection/post-1.md' out_location = 'md/toc.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(out_location, markdown(src_location, template('templates/md/toc.html'))) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/md/toc.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_render_jinja(tmp_path: Path): src_location = 'resources/jinja/title.html' out_location = 'title.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(out_location, jinja(src_location, title='99 reasons lightweight rules')) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/jinja/params.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_include_file(tmp_path: Path): src_location = 'resources/test.html' src_path = Path(src_location) with src_path.open() as f: src_content = f.read() test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(src_location) site.generate(test_out) assert (test_out / src_location).exists() assert (test_out / src_location).read_text() == src_content
def test_include_dir_under_different_name(tmp_path: Path): src_location = 'resources/test_nested/test2/test3/test.html' src_path = Path(src_location) with src_path.open() as f: src_content = f.read() test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add('successful_test', 'resources/test_nested') site.generate(test_out) assert (test_out / 'successful_test/test2/test3/test.html').exists() assert (test_out / 'successful_test/test2/test3/test.html').read_text() == src_content
def test_lazy_params(tmp_path: Path): src_location = 'resources/jinja/lazy.html' out_location = 'lazy.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add( out_location, jinja(src_location, lazy=from_ctx(lambda ctx: f'Hello there! {ctx.tasks[0].path}'))) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/jinja/lazy.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def quickstart(location: str, title: Optional[str]): path = Path(location) path.mkdir(parents=True, exist_ok=True) abs_out = os.path.abspath(path) if not title: title = Path(abs_out).name title_slug = slugify_title(title) template_location = Path(__file__).parent / 'project-template' with directory(template_location), custom_jinja_tags(): site = Site(url="https://example.com/", title=title) [site.add(str(p), jinja(p)) for p in paths('_templates_/**/*.html')] [site.add(str(p), jinja(p)) for p in paths('*.html')] site.add('website.py', jinja('website.py.j2', title_slug=title_slug)) site.add('requirements.txt', jinja('requirements.txt.j2', version=lw_version())) site.add('posts') [ site.add(str(p), jinja(p)) for p in paths('styles/**/*css') if p.name != 'attributes.scss' ] site.add('styles/attributes.scss', jinja('styles/attributes.scss', accent=Color.bright())) site.add('js') site.add('img') site.generate(abs_out) website_file = os.path.join(abs_out, 'website.py') os.chmod(website_file, stat.S_IRWXU | stat.S_IRGRP | stat.S_IROTH) # -rwxr--r-- logger.info(f' Project initialized in: {abs_out}')
def test_url_check(): with pytest.raises(ValueError): Site(url='lightweight.site/') with pytest.raises(ValueError): Site(url='https://lightweight.site')
def test_repr(): site = Site(url='https://drach.uk/') assert repr(site).startswith('<Site title=None url=https://drach.uk/ at')
def build_func_2_args(a, b): return Site(url='http://test-cli.py/')
def build_jinja_file(url): site = Site(url=url) site.add('index', jinja('index')) return site
def test_include_not_found_under_different_name(tmp_path: Path): site = Site(url='https://example.org/') with pytest.raises(FileNotFoundError): site.add('t.html', str(uuid4()))
def test_absolute_includes_not_allowed(): site = Site('https://example.org/') with pytest.raises(AbsolutePathIncluded): site.add('/etc')
def test_site_location(tmp_path: Path): site = Site(url='https://example.org/') assert site / 'test.html' == 'https://example.org/test.html' assert site / '/test.html' == 'https://example.org/test.html' assert site / '/foo/bar' == 'https://example.org/foo/bar'
def build_func(url): return Site(url=url)
def test_site_include_duplicate(): site = Site(url='https://example.org/') site.add('page', 'resources/test.html') with pytest.raises(IncludedDuplicate): site.add('page', 'site/index.html')
def build_func_no_arg(): return Site(url='http://test-cli.py/')
def test_str(): site = Site(url='https://drach.uk/') assert str(site) == 'https://drach.uk/'
def build_func_with_default(a, b='test'): return Site(url='http://test-cli.py/')
def test_include_not_found(tmp_path: Path): site = Site(url='https://example.org/') with pytest.raises(FileNotFoundError): site.add(str(uuid4()))