Example #1
0
def test_pages_not_dir(tmpdir):
    mktree(tmpdir, {
        'pages': '*',
    })

    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert 'does not exist' not in str(exc_info.value)
    assert 'is not a directory' in str(exc_info.value)
Example #2
0
def test_extensions_not_dir(tmpdir):
    mktree(
        tmpdir, {
            'pages': {},
            'theme/templates/main.jinja': '{{ content }}',
            'extensions.py': {},
        })

    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert '"extensions" should be a python file, not directory' in str(
        exc_info.value)
Example #3
0
def test_dist_dir_not_dir(tmpdir):
    mktree(
        tmpdir, {
            'dist': 'foobar',
            'pages': {},
            'theme/templates/main.jinja': '{{ content }}',
        })

    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert 'is not a directory' in str(exc_info.value)
    assert 'parent directory does not exist' not in str(exc_info.value)
Example #4
0
def test_dist_dir_no_parent(tmpdir):
    mktree(
        tmpdir, {
            'pages': {},
            'theme/templates/main.jinja': '{{ content }}',
            'harrier.yml': f'dist_dir: {tmpdir.join("foo/bar")}'
        })

    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert 'parent directory does not exist' in str(exc_info.value)
    assert 'is not a directory' not in str(exc_info.value)
Example #5
0
def test_no_theme_dir(tmpdir):
    mktree(
        tmpdir, {
            'pages': {
                'foo.md': '# foo',
                'bar.html': '{{ 4|add_one }} {{ config.dynamic }}',
            },
            'theme': {},
            'harrier.yml': 'default_template: main.jinja'
        })

    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert 'does not exist' in str(exc_info.value)
    assert 'is not a directory' not in str(exc_info.value)
Example #6
0
def test_run_webpack_cli_wrong(tmpdir, caplog):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/index.js': '*',
            },
            'harrier.yml': (f'webpack:\n'
                            f'  cli: /foo/bar')
        })

    with pytest.raises(ValidationError):
        get_config(str(tmpdir))
Example #7
0
def test_resolve_sass_path_dev(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'assets/assets/image.png':
                '*',
                'sass/main.scss':
                'body {content: resolve_path("/assets/image.png")}',
            },
        })

    config = get_config(str(tmpdir))
    config.mode = Mode.development
    assets_grablib(config)
    mtime = tmpdir.join('theme/sass/main.scss').stat().mtime
    assert gettree(tmpdir.join('dist')) == {
        'assets': {
            'image.png': '*',
        },
        'theme': {
            'main.css.map':
            RegexStr('{.*'),
            'main.css': ("body {\n"
                         "  content: '/assets/image.png?t=%0.0f'; }\n"
                         "\n"
                         "/*# sourceMappingURL=main.css.map */") % mtime,
            '.src': {
                'main.scss':
                'body {content: resolve_path("/assets/image.png")}',
            },
        },
    }
Example #8
0
def test_copy_assets_prod(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'assets': {
                    'image.png': '*',
                    'favicon.ico': '*',
                    'move': {
                        'foobar.svg': 'x'
                    }
                }
            },
        })

    config = get_config(str(tmpdir))
    config.mode = Mode.production
    copy_assets(config)
    assert gettree(tmpdir.join('dist')) == {
        'image.3389dae.png': '*',
        'favicon.ico': '*',
        'move': {
            'foobar.9dd4e46.svg': 'x'
        }
    }
Example #9
0
def test_grablib(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md':
            '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'sass/main.scss': ('@import "DL/demo";'
                                   'body {background: $foo}'),
            },
            'harrier.yml':
            (f'download:\n'
             f"  'https://gist.githubusercontent.com/samuelcolvin/ae6d04dadbb4d552d365f440d3ac8015/"
             f"raw/cda04f66c71e4a5f418e78d111d651ae3a2e3784/demo.scss': '_demo.scss'"
             )
        })

    config = get_config(str(tmpdir))
    run_grablib(config)
    assert gettree(tmpdir.join('dist')) == {
        'theme': {
            'main.7cc3e19.css': 'body{background:#BAD}\n',
        },
    }
Example #10
0
def test_ok(tmpdir):
    mktree(tmpdir, {
        'pages': {},
        'theme/templates/main.jinja': '{{ content }}',
    })

    config = get_config(tmpdir)
    assert config.pages_dir == Path(tmpdir.join('pages'))
    assert not config.webpack.run
Example #11
0
def test_ok_file(tmpdir):
    mktree(
        tmpdir, {
            'pages': {},
            'theme/templates/main.jinja': '{{ content }}',
            'harrier.yml': 'foo: bar'
        })

    config = get_config(tmpdir.join('harrier.yml'))
    assert config.pages_dir == Path(tmpdir.join('pages'))
    assert not config.webpack.run
Example #12
0
def test_webpack_missing_config(tmpdir):
    mktree(
        tmpdir, {
            'pages': {},
            'theme': {
                'templates': {
                    'main.jinja': '{{ content }}'
                },
                'js/index.js': '*'
            },
            'mock_webpack':
            '*',
            'harrier.yml': (f'webpack:\n'
                            f'  cli: mock_webpack\n'
                            f'  config: missing\n')
        })

    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert 'webpack config set but does not exist' in str(exc_info.value)
Example #13
0
def test_webpack_no_entry(tmpdir, caplog):
    mktree(
        tmpdir, {
            'pages': {},
            'theme/templates/main.jinja': '{{ content }}',
            'mock_webpack': '*',
            'harrier.yml': (f'webpack:\n'
                            f'  cli: mock_webpack')
        })

    config = get_config(tmpdir)
    assert not config.webpack.run
    assert 'webpack entry point' in caplog.text
Example #14
0
def test_sass_wrong(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'sass/main.scss':
                'body {content: resolve_path("assets/image.png")}',
            },
        })

    config = get_config(str(tmpdir))
    with pytest.raises(HarrierProblem):
        assets_grablib(config)
Example #15
0
def test_run_webpack_error(tmpdir):
    webpack_path = tmpdir.join('mock_webpack')
    mktree(
        tmpdir, {
            'pages/foobar.md':
            '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/error.js': '*',
            },
            'webpack_config.js':
            '*',
            'mock_webpack':
            MOCK_WEBPACK,
            'harrier.yml': (f'mode: development\n'
                            f'webpack:\n'
                            f'  cli: {webpack_path}\n'
                            f'  entry: js/error.js\n'
                            f'  config: webpack_config.js\n')
        })
    webpack_path.chmod(0o777)

    config = get_config(str(tmpdir))
    with pytest.raises(HarrierProblem):
        run_webpack(config)
    args = json.loads(tmpdir.join('webpack_args.json').read_text('utf8'))
    assert [
        f'{tmpdir}/mock_webpack',
        '--context',
        f'{tmpdir}',
        '--entry',
        './theme/js/error.js',
        '--output-path',
        f'{tmpdir}/dist/theme',
        '--output-filename',
        'main.js',
        '--devtool',
        'source-map',
        '--mode',
        'development',
        '--config',
        './webpack_config.js',
        '--json',
    ] == args
    webpack_env = json.loads(tmpdir.join('webpack_env.json').read_text('utf8'))
    assert webpack_env['NODE_ENV'] == 'development'
Example #16
0
def test_run_webpack_default(tmpdir, caplog):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/index.js': '*',
            },
            'node_modules/.bin/webpack-cli': MOCK_WEBPACK,
        })
    tmpdir.join('node_modules/.bin/webpack-cli').chmod(0o777)

    caplog.set_level(logging.DEBUG)
    config = get_config(str(tmpdir))
    assert config.webpack.run is True
Example #17
0
def test_webpack_ok(tmpdir, caplog):
    mktree(
        tmpdir, {
            'pages': {},
            'theme': {
                'templates': {
                    'main.jinja': '{{ content }}'
                },
                'js/index.js': '*'
            },
            'mock_webpack': '*',
            'harrier.yml': (f'webpack:\n'
                            f'  cli: mock_webpack')
        })

    config = get_config(tmpdir)
    assert config.webpack.run
    assert 'webpack entry point' not in caplog.text
Example #18
0
async def test_start_webpack_watch(tmpdir, loop):
    webpack_path = tmpdir.join('mock_webpack')
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/index.js': '*',
            },
            'mock_webpack': MOCK_WEBPACK,
            'harrier.yml': (f'webpack:\n'
                            f'  cli: {webpack_path}')
        })
    webpack_path.chmod(0o777)

    config = get_config(str(tmpdir))
    config.mode = Mode.development

    asyncio.set_event_loop(loop)
    p = await start_webpack_watch(config)
    await p.wait()

    assert [
        f'{tmpdir}/mock_webpack',
        '--context',
        f'{tmpdir}',
        '--entry',
        './theme/js/index.js',
        '--output-path',
        f'{tmpdir}/dist/theme',
        '--output-filename',
        'main.js',
        '--devtool',
        'source-map',
        '--mode',
        'development',
        '--watch',
    ] == json.loads(tmpdir.join('webpack_args.json').read_text('utf8'))
    webpack_env = json.loads(tmpdir.join('webpack_env.json').read_text('utf8'))
    assert webpack_env['NODE_ENV'] == 'development'
Example #19
0
def test_grablib_pygments(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'sass': {
                    '_other.scss':
                    'div {colour: red}',
                    'main.scss': ('@import "other";\n'
                                  '@import "pygments/default";\n'),
                },
            }
        })

    config = get_config(str(tmpdir))
    run_grablib(config)
    assert gettree(tmpdir.join('dist')) == {
        'theme': {
            'main.9a9caa6.css': RegexStr('div{colour:red}\.hi \.hll.*'),
        },
    }
Example #20
0
def test_run_webpack(tmpdir):
    webpack_path = tmpdir.join('mock_webpack')
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/index.js': '*',
            },
            'mock_webpack': MOCK_WEBPACK,
            'harrier.yml': (f'webpack:\n'
                            f'  cli: {webpack_path}')
        })
    webpack_path.chmod(0o777)

    config = get_config(str(tmpdir))
    count = run_webpack(config)
    assert count == 3
    args = json.loads(tmpdir.join('webpack_args.json').read_text('utf8'))
    assert [
        f'{tmpdir}/mock_webpack',
        '--context',
        f'{tmpdir}',
        '--entry',
        './theme/js/index.js',
        '--output-path',
        f'{tmpdir}/dist/theme',
        '--output-filename',
        'main.[hash].js',
        '--devtool',
        'source-map',
        '--mode',
        'production',
        '--optimize-minimize',
        '--json',
    ] == args
    webpack_env = json.loads(tmpdir.join('webpack_env.json').read_text('utf8'))
    assert webpack_env['NODE_ENV'] == 'production'
Example #21
0
def test_grablib_error(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md':
            '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'sass/main.scss': 'foobar',
            },
            'harrier.yml':
            (f'download:\n'
             f"  'https://gist.githubusercontent.com/samuelcolvin/ae6d04dadbb4d552d365f440d3ac8015/raw/"
             f"cda04f66c71e4a5f418e78d111d651ae3a2e3784/demo.scss': '_demo.scss'"
             )
        })

    config = get_config(str(tmpdir))
    with pytest.raises(HarrierProblem):
        run_grablib(config)
    assert not tmpdir.join('dist').check()
Example #22
0
def test_run_webpack_no_capture(tmpdir, caplog):
    webpack_path = tmpdir.join('mock_webpack')
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/index.js': '*',
            },
            'mock_webpack': MOCK_WEBPACK,
            'harrier.yml': (f'webpack:\n'
                            f'  cli: {webpack_path}')
        })
    webpack_path.chmod(0o777)

    caplog.set_level(logging.DEBUG)
    config = get_config(str(tmpdir))
    count = run_webpack(config)
    # 1 because capture_output is false so no json is loaded
    assert count == 1
Example #23
0
def test_resolve_sass_path(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'assets/assets/image.png':
                '*',
                'sass/main.scss':
                'body {content: resolve_path("/assets/image.png")}',
            },
        })

    config = get_config(str(tmpdir))
    assets_grablib(config)
    assert gettree(tmpdir.join('dist')) == {
        'theme': {
            'main.d024f29.css': "body{content:'/assets/image.3389dae.png'}\n",
        },
        'assets': {
            'image.3389dae.png': '*',
        },
    }
Example #24
0
def test_run_webpack_json_error(tmpdir):
    webpack_path = tmpdir.join('mock_webpack')
    mktree(
        tmpdir, {
            'pages/foobar.md':
            '# hello',
            'theme': {
                'templates': {
                    'main.jinja': 'main:\n {{ content }}'
                },
                'js/nojson.js': '*',
            },
            'mock_webpack':
            MOCK_WEBPACK,
            'harrier.yml': (f'webpack:\n'
                            f'  entry: js/nojson.js\n'
                            f'  cli: {webpack_path}')
        })
    webpack_path.chmod(0o777)

    config = get_config(str(tmpdir))
    count = run_webpack(config)
    assert count == 1
Example #25
0
def test_smart_sass_url(tmpdir):
    mktree(
        tmpdir, {
            'pages/foobar.md': '# hello',
            'theme': {
                'assets/assets/image.png':
                '*',
                'sass/main.scss':
                'body {background: smart_url("assets/image.png")}',
            },
        })

    config = get_config(str(tmpdir))
    assets_grablib(config)
    assert gettree(tmpdir.join('dist')) == {
        'theme': {
            'main.28d2724.css':
            "body{background:url('/assets/image.3389dae.png')}\n",
        },
        'assets': {
            'image.3389dae.png': '*',
        },
    }
Example #26
0
def test_no_pages(tmpdir):
    with pytest.raises(ValidationError) as exc_info:
        get_config(tmpdir)
    assert 'does not exist' in str(exc_info.value)
    assert 'is not a directory' not in str(exc_info.value)