Ejemplo n.º 1
0
def test_transform_sources(tmp_path):
    """Make build_file_tree to transform certain files."""

    # source
    src_path = tmp_path / 'src/'
    src_path.mkdir()
    foo = src_path / 'foo.txt'
    foo.write_text('foo content')
    baz = src_path / 'baz.css'
    baz.write_text('body { background: red; }')

    # destination
    dst_path = tmp_path / 'dst/'
    dst_path.mkdir()

    # build tree
    def make_uppercase(root_src_path, src_path, root_dst_path, dst_path):
        src_content = src_path.read_text()
        dst_content = src_content.upper()
        dst_path.write_text(dst_content)

    build_file_tree(
        src_path, dst_path, no_cache=True,
        actions={'.txt': make_uppercase}
    )

    dst_foo = dst_path / 'foo.txt'
    dst_baz = dst_path / 'baz.css'

    assert dst_foo.is_file()
    assert dst_baz.is_file()

    assert dst_foo.read_text() == 'FOO CONTENT'
    assert dst_baz.read_text() == 'body { background: red; }'
Ejemplo n.º 2
0
def test_basic_file_tree(tmp_path):
    """Create a basic file tree."""

    # source
    src_path = tmp_path / 'src/'
    src_path.mkdir()
    foo = src_path / 'foo.txt'
    foo.write_text('foo content')
    baz = src_path / 'baz.css'
    baz.write_text('body { background: red; }')
    dir1 = src_path / 'dir1/'
    dir1.mkdir()
    bar = dir1 / 'bar.json'
    bar.write_text('{"spam": "eggs"}')

    # destination
    dst_path = tmp_path / 'dst/'
    dst_path.mkdir()

    # build tree
    build_file_tree(src_path, dst_path, no_cache=True, actions={})

    dst_foo = dst_path / 'foo.txt'
    dst_baz = dst_path / 'baz.css'
    dst_bar = dst_path / 'dir1/bar.json'
    dst_dir1 = dst_path / 'dir1/'

    assert dst_foo.is_file()
    assert dst_baz.is_file()
    assert dst_bar.is_file()
    assert dst_dir1.is_dir()

    assert dst_foo.read_text() == 'foo content'
Ejemplo n.º 3
0
def test_ignore_sources(tmp_path):
    """Make build_file_tree to ignore certain files."""

    # source
    src_path = tmp_path / 'src/'
    src_path.mkdir()
    foo = src_path / 'foo.txt'
    foo.write_text('foo content')
    baz = src_path / 'baz.css'
    baz.write_text('body { background: red; }')
    dir1 = src_path / 'dir1/'
    dir1.mkdir()
    bar = dir1 / 'bar.json'
    bar.write_text('{"spam": "eggs"}')

    # destination
    dst_path = tmp_path / 'dst/'
    dst_path.mkdir()

    # build tree
    def ignore(*args, **kwargs):
        pass
    build_file_tree(src_path, dst_path, no_cache=True, actions={'.css': ignore})

    dst_foo = dst_path / 'foo.txt'
    dst_baz = dst_path / 'baz.css'
    dst_bar = dst_path / 'dir1/bar.json'

    assert dst_foo.is_file()
    assert not dst_baz.is_file()
    assert dst_bar.is_file()

    assert dst_foo.read_text() == 'foo content'
Ejemplo n.º 4
0
def test_transform_suffixes(tmp_path):
    """Check that suffixes are transformed when specified."""

    # source
    src_path = tmp_path / 'src/'
    src_path.mkdir()
    foo = src_path / 'foo.rst'
    foo.write_text('foo content')

    # destination
    dst_path = tmp_path / 'dst/'
    dst_path.mkdir()

    # build tree
    def just_copy(root_src_path, src_path, root_dst_path, dst_path):
        dst_path.write_text(src_path.read_text())

    build_file_tree(
        src_path, dst_path, no_cache=True,
        actions={'.rst': ('.html', just_copy)}
    )

    wrong_dst_foo = dst_path / 'foo.rst'
    dst_foo = dst_path / 'foo.html'

    assert not wrong_dst_foo.is_file()
    assert dst_foo.is_file()
    assert dst_foo.read_text() == 'foo content'
Ejemplo n.º 5
0
def create_site(build_dir, content_dir, template_dir, no_cache, verbose):
    """Create static site from content files using a set of HTML templates."""

    # set logging options
    logging.basicConfig(
        level=logging.DEBUG if verbose else logging.INFO,
        format='%(levelname)s: %(message)s',
    )

    # set directories
    build_path = Path(build_dir)

    content_path = Path(content_dir)
    if not content_path.is_dir():
        click.echo(f"Directory '{content_path}' not found.")
        sys.exit(-1)

    template_path = Path(template_dir)
    if not template_path.is_dir():
        click.echo(f"Directory '{template_path}' not found.")
        sys.exit(-1)

    template_paths = (default_template_path, template_path)

    try:
        # process file tree in template path
        build_file_tree(template_path,
                        build_path,
                        no_cache,
                        actions={
                            '.html': ignore,
                            '.scss': ('.css', compile_scss),
                        })

        # get site object model
        som = SOM(content_path)

        # initialize render action
        render_html = render_html_factory(som, template_paths)

        # process file tree in content path
        build_file_tree(content_path,
                        build_path,
                        no_cache,
                        actions={
                            '.scss': ('.css', compile_scss),
                            '.rst': ('.html', render_html),
                            '.json': ('.html', render_html),
                            '.yaml': ('.html', render_html),
                            '.yml': ('.html', render_html),
                            '.html': ('.html', render_html),
                            '.htm': ('.html', render_html),
                            '.md': ('.html', render_html),
                            '.markdown': ('.html', render_html),
                        })

    except SpekulatioError as err:
        click.echo(str(err))
        sys.exit(-1)
Ejemplo n.º 6
0
def test_basic_file_tree_cache(tmp_path):
    """Check that targets aren't overwritten when src file is unchanged."""

    # source
    src_path = tmp_path / 'src/'
    src_path.mkdir()
    foo = src_path / 'foo.txt'
    foo.write_text('foo content')

    # destination
    dst_path = tmp_path / 'dst/'
    dst_path.mkdir()

    # build tree
    time.sleep(0.1)
    build_file_tree(src_path, dst_path, no_cache=False, actions={})

    dst_foo = dst_path / 'foo.txt'

    # check that dst timestamp is older than that of the original file
    assert dst_foo.stat().st_mtime >= foo.stat().st_mtime

    # check that dst doesn't get overwritten if src is unchanged
    prev_timestamp = dst_foo.stat().st_mtime
    time.sleep(0.1)
    build_file_tree(src_path, dst_path, no_cache=False, actions={})
    assert dst_foo.stat().st_mtime == prev_timestamp

    # modify original file to check that destination changes
    time.sleep(0.1)
    foo.touch()
    build_file_tree(src_path, dst_path, no_cache=False, actions={})
    assert dst_foo.stat().st_mtime >= prev_timestamp