示例#1
0
def test_create_params():
    name = 'name 1'
    target_dir = 'some/where'
    entrypoint = 'some/input/file.js'
    b = Bundle(name, target_dir, [entrypoint])
    assert b.entrypoints[0].name == name
    with pytest.raises(TypeError):
        Bundle(name)
示例#2
0
def test_resolve_paths_with_dependencies():
    b = Bundle(
        'p1',
        'some/where',
        ['some/input/file1.js'],
        dependencies=['some/input/file2.js', 'some/input/file3.js'],
    )
    b.resolve_paths('/static/directory')
    for p in b.dependencies:
        assert os.path.isabs(p)
示例#3
0
def test_bundle_argv():
    b = Bundle(
        'p1',
        'some/where',
        ['some/input/file1.js'],
        dependencies=['some/input/file2.js', 'some/input/file3.js'],
    )
    b.resolve_paths('/static/directory')
    rv = b.argv()
    assert len(rv) == 2 + len(b.entrypoints)
    assert rv[0] == '-d'
    assert rv[1] == b.target_dir
示例#4
0
def test_calc_state(mocker):
    mocker.patch('flask_rollup.os.stat',
                 mocker.Mock(return_value=mocker.Mock(st_mtime_ns=100)))
    b = Bundle(
        'p1',
        'some/where',
        ['some/input/file1.js'],
        dependencies=['some/input/file2.js', 'some/input/file3.js'],
    )
    b.resolve_paths('/static/directory')
    rv = b.calc_state()
    assert rv == hashlib.sha256('\n'.join(['100'] *
                                          3).encode('utf-8')).hexdigest()
示例#5
0
def test_run_command(app, mocker):
    b = Bundle('p1', 'some/where', ['some/input/file.js'])
    rollup = Rollup(app)
    rollup.register(b)
    fake_run = mocker.Mock()
    mocker.patch.object(rollup, 'run_rollup', fake_run)
    runner = app.test_cli_runner()
    rv = runner.invoke(rollup_run_cmd)
    assert rv.exit_code == 0
    assert 'All done' in rv.output
    fake_run.assert_called_once()
示例#6
0
def test_run(environment, app, mocker):
    name = 'p1'
    mocker.patch.dict('os.environ', {'FLASK_ENV': environment})
    b = Bundle(name, 'some/where', ['some/input/file.js'])
    rollup = Rollup(app)
    fake_run = mocker.Mock()
    mocker.patch('flask_rollup.subprocess.run', fake_run)
    mocker.patch('flask_rollup.os.stat',
                 mocker.Mock(return_value=mocker.Mock(st_mtime_ns=100)))
    rollup.register(b)
    rollup.run_rollup(name)
    rollup.run_rollup(name)
    fake_run.assert_called_once()
示例#7
0
def test_resolve_output_fail(mocker):
    tgt_paths = [
        '/static/directory/some/where/file1.js',
        '/static/directory/some/where/file2.js'
    ]
    mocker.patch('flask_rollup.glob.glob', mocker.Mock(return_value=tgt_paths))
    b = Bundle('p1', 'some/where', ['some/input/file1.js'])
    b.resolve_paths('/static/directory')
    url_path = '/static'
    b.resolve_output('/static/directory', url_path)
    assert b.output is None
示例#8
0
def test_resolve_output_ok(mocker):
    tgt_path = '/static/directory/some/where/file1.js'
    mocker.patch('flask_rollup.glob.glob',
                 mocker.Mock(return_value=[tgt_path]))
    b = Bundle('p1', 'some/where', ['some/input/file1.js'])
    b.resolve_paths('/static/directory')
    url_path = '/static'
    b.resolve_output('/static/directory', url_path)
    assert b.output.file_path == tgt_path
    assert b.output.static_path == tgt_path.replace('/static/directory/', '')
    assert b.output.url == os.path.join(url_path, b.output.static_path)
示例#9
0
def test_template_global_fail_on_prod(app, mocker):
    def handler():
        return render_template_string('{{ jsbundle(request.endpoint) }}')

    mocker.patch.dict('os.environ', {'FLASK_ENV': 'production'})
    app.config['SERVER_NAME'] = '127.0.0.1'
    name = 'p1'
    b = Bundle(name, 'some/where', ['some/input/file.js'])
    rollup = Rollup(app)
    mocker.patch('flask_rollup.glob.glob', mocker.Mock(return_value=[]))
    rollup.register(b)
    app.add_url_rule('/something', endpoint=name, view_func=handler)
    with app.test_client() as client:
        with app.app_context():
            url = url_for(name)
        rv = client.get(url)
        assert rv.status_code == 500
示例#10
0
def test_autobuild_not_running_in_production(app, mocker):
    def handler():
        return 'something'

    app.config['SERVER_NAME'] = '127.0.0.1'
    name = 'p1'
    mocker.patch.dict('os.environ', {'FLASK_ENV': 'production'})
    rollup = Rollup(app)
    b = Bundle(name, 'some/where', ['some/input/file.js'])
    rollup.register(b)
    app.add_url_rule('/something', endpoint=name, view_func=handler)
    fake_run = mocker.Mock()
    mocker.patch.object(rollup, 'run_rollup', fake_run)
    with app.test_client() as client:
        with app.app_context():
            url = url_for(name)
        client.get(url)
    fake_run.assert_not_called()
示例#11
0
def test_autobuild_running_in_development(app, mocker):
    def handler():
        return 'something'

    app.config['SERVER_NAME'] = '127.0.0.1'
    name = 'p1'
    mocker.patch.dict('os.environ', {'FLASK_ENV': 'development'})
    rollup = Rollup(app)
    b = Bundle(name, 'some/where', ['some/input/file.js'])
    mocker.patch('flask_rollup.os.stat',
                 mocker.Mock(return_value=mocker.Mock(st_mtime_ns=200)))
    mocker.patch('flask_rollup.subprocess.run')
    rollup.register(b)
    app.add_url_rule('/something', endpoint=name, view_func=handler)
    fake_run = mocker.Mock()
    mocker.patch.object(rollup, 'run_rollup', fake_run)
    with app.test_client() as client:
        with app.app_context():
            url = url_for(name)
        client.get(url)
    fake_run.assert_called_once_with(name)
示例#12
0
def test_template_global(app, mocker):
    def handler():
        return render_template_string('{{ jsbundle(request.endpoint) }}')

    app.config['SERVER_NAME'] = '127.0.0.1'
    name = 'p1'
    b = Bundle(name, 'some/where', ['some/input/file.js'])
    mocker.patch.object(b, 'calc_state', mocker.Mock(return_value='state'))
    rollup = Rollup(app)
    mocker.patch('flask_rollup.subprocess.run')
    tgt_path = '/static/directory/some/where/file1.js'
    mocker.patch('flask_rollup.glob.glob',
                 mocker.Mock(return_value=[tgt_path]))
    rollup.register(b)
    app.add_url_rule('/something', endpoint=name, view_func=handler)
    mocker.patch('flask_rollup.os.remove')
    rollup.run_rollup(name)
    with app.test_client() as client:
        with app.app_context():
            url = url_for(name)
        rv = client.get(url)
        assert b.output.url.encode('utf-8') in rv.data
示例#13
0
def test_simple_entrypoints_limited(entrypoints):
    name = 'name 1'
    target_dir = 'some/where'
    with pytest.raises(BundleDefinitionError):
        Bundle(name, target_dir, entrypoints)
示例#14
0
def test_resolve_paths_simple():
    b = Bundle('p1', 'some/where', ['some/input/file.js'])
    b.resolve_paths('/static/directory')
    assert os.path.isabs(b.target_dir)
    for e in b.entrypoints:
        assert os.path.isabs(e.path)
示例#15
0
def test_multiple_entrypoints(entrypoints):
    name = 'name 1'
    target_dir = 'some/where'
    b = Bundle(name, target_dir, entrypoints)
    assert len(b.entrypoints) == len(entrypoints)
示例#16
0
def test_register(app):
    b = Bundle('p1', 'some/where', ['some/input/file.js'])
    rollup = Rollup(app)
    rollup.register(b)
    assert len(rollup.bundles) == 1
    assert os.path.isabs(b.target_dir)