Beispiel #1
0
def test_install_hook(monkeypatch, tmp_nbs):
    # inject cells
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--inject'])
    cli.cmd_router()

    # init repo
    git_init()

    # install hook
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--install-hook'])
    cli.cmd_router()

    # commit (should remove injected cells before committing and re-add them
    # after committing)
    git_commit()

    injected_tag = '# + tags=["injected-parameters"]'
    assert injected_tag in Path('load.py').read_text()

    # check last commit files
    subprocess.check_call(['git', 'stash'])

    assert injected_tag not in Path('load.py').read_text()

    assert ('ploomber nb --entry-point pipeline.yaml --remove'
            in Path('.git', 'hooks', 'pre-commit').read_text())
    assert ('ploomber nb --entry-point pipeline.yaml --inject'
            in Path('.git', 'hooks', 'post-commit').read_text())
Beispiel #2
0
def test_pair_sync(monkeypatch, tmp_nbs):
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--pair', 'nbs'])

    cli.cmd_router()

    def get_formats(nb):
        return nb.metadata.jupytext.formats

    expected_fmt = 'nbs//ipynb,py:light'
    assert get_formats(jupytext.read('load.py')) == expected_fmt
    assert get_formats(jupytext.read('clean.py')) == expected_fmt
    assert get_formats(jupytext.read('plot.py')) == expected_fmt
    assert get_formats(jupytext.read(Path('nbs',
                                          'load.ipynb'))) == expected_fmt
    assert get_formats(jupytext.read(Path('nbs',
                                          'clean.ipynb'))) == expected_fmt
    assert get_formats(jupytext.read(Path('nbs',
                                          'plot.ipynb'))) == expected_fmt

    # modify one and sync
    nb = jupytext.read('load.py')
    current = nbformat.versions[nbformat.current_nbformat]
    cell = current.new_code_cell(source='# this is a new cell')
    nb.cells.append(cell)
    jupytext.write(nb, 'load.py')

    assert '# this is a new cell' not in Path('nbs', 'load.ipynb').read_text()

    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--sync'])
    cli.cmd_router()

    assert '# this is a new cell' in Path('nbs', 'load.ipynb').read_text()
Beispiel #3
0
def test_no_options(monkeypatch):
    # when running "ploomber"
    monkeypatch.setattr(sys, 'argv', ['ploomber'])

    with pytest.raises(SystemExit) as excinfo:
        cmd_router()

    assert excinfo.value.code == 0
Beispiel #4
0
def test_install_hook_error_if_missing_git(monkeypatch, tmp_nbs, capsys):
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--install-hook'])

    with pytest.raises(SystemExit):
        cli.cmd_router()

    captured = capsys.readouterr()
    assert 'Error: Expected a .git/ directory' in captured.err
Beispiel #5
0
def test_help_shows_env_keys(cmd, monkeypatch_session, tmp_nbs, capsys):
    monkeypatch_session.setattr(sys, 'argv', ['ploomber', cmd, '--help'])

    with pytest.raises(SystemExit) as excinfo:
        cmd_router()

    captured = capsys.readouterr()
    assert '--env--sample' in captured.out
    assert not excinfo.value.code
Beispiel #6
0
def test_suggestions(monkeypatch, capsys, cmd, suggestion):
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'execute'])

    with pytest.raises(SystemExit) as excinfo:
        cmd_router()

    captured = capsys.readouterr()
    assert f"Did you mean '{suggestion}'?" in captured.err
    assert excinfo.value.code == 2
Beispiel #7
0
def test_build_suggestion(monkeypatch, capsys):
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'build', 'task'])

    with pytest.raises(SystemExit) as excinfo:
        cmd_router()

    captured = capsys.readouterr()
    suggestion = "To build a single task, try: 'ploomber task {task-name}'"
    assert suggestion in captured.err
    assert excinfo.value.code == 2
Beispiel #8
0
def test_format_with_extension_change(monkeypatch, tmp_nbs):
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--format', 'ipynb'])
    cli.cmd_router()

    assert not Path('load.py').exists()
    assert not Path('clean.py').exists()
    assert not Path('plot.py').exists()
    assert jupytext.read('load.ipynb')
    assert jupytext.read('clean.ipynb')
    assert jupytext.read('plot.ipynb')
Beispiel #9
0
def test_single_click_disable_updates_existing(mock_nb_single_click_disable,
                                               tmp_directory, existing,
                                               expected):
    parent = mock_nb_single_click_disable.parent
    parent.mkdir(parents=True)
    mock_nb_single_click_disable.write_text(json.dumps(existing))

    cli.cmd_router()

    current = json.loads(mock_nb_single_click_disable.read_text())
    assert current == expected
Beispiel #10
0
def test_help_shows_env_keys_w_entry_point(cmd, tmp_nbs,
                                           add_current_to_sys_path,
                                           monkeypatch_session, capsys):
    monkeypatch_session.setattr(
        sys, 'argv', ['ploomber', cmd, '-e', 'factory.make', '--help'])

    with pytest.raises(SystemExit) as excinfo:
        cmd_router()

    captured = capsys.readouterr()
    assert '--env--sample' in captured.out
    assert not excinfo.value.code
Beispiel #11
0
def test_single_click_updates_existing(mock_nb_single_click_enable,
                                       tmp_directory):
    parent = mock_nb_single_click_enable.parent
    parent.mkdir(parents=True)
    mock_nb_single_click_enable.write_text(json.dumps(dict(key='value')))

    cli.cmd_router()

    expected = json.loads(nb._jupyterlab_default_settings_overrides)
    expected['key'] = 'value'

    current = json.loads(mock_nb_single_click_enable.read_text())
    assert current == expected
Beispiel #12
0
def test_help(cmd, monkeypatch_session, tmp_directory):
    Path('pipeline.yaml').touch()

    elements = ['ploomber']

    if cmd:
        elements.append(cmd)

    monkeypatch_session.setattr(sys, 'argv', elements + ['--help'])

    with pytest.raises(SystemExit) as excinfo:
        cmd_router()

    assert not excinfo.value.code
Beispiel #13
0
def test_uninstall_hook(monkeypatch, tmp_nbs):
    git_init()

    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--install-hook'])
    cli.cmd_router()

    assert Path('.git', 'hooks', 'pre-commit').is_file()
    assert Path('.git', 'hooks', 'post-commit').is_file()

    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--uninstall-hook'])
    cli.cmd_router()

    assert not Path('.git', 'hooks', 'pre-commit').exists()
    assert not Path('.git', 'hooks', 'post-commit').exists()
Beispiel #14
0
def test_format(monkeypatch, tmp_nbs):
    monkeypatch.setattr(sys, 'argv',
                        ['ploomber', 'nb', '--format', 'py:percent'])

    expected = '%% tags=["parameters"]'

    assert expected not in Path('load.py').read_text()
    assert expected not in Path('clean.py').read_text()
    assert expected not in Path('plot.py').read_text()

    cli.cmd_router()

    assert expected in Path('load.py').read_text()
    assert expected in Path('clean.py').read_text()
    assert expected in Path('plot.py').read_text()
Beispiel #15
0
def test_inject_remove(monkeypatch, tmp_nbs):
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--inject'])
    expected = 'tags=["injected-parameters"]'

    assert expected not in Path('load.py').read_text()
    assert expected not in Path('clean.py').read_text()
    assert expected not in Path('plot.py').read_text()

    cli.cmd_router()

    assert expected in Path('load.py').read_text()
    assert expected in Path('clean.py').read_text()
    assert expected in Path('plot.py').read_text()

    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--remove'])

    cli.cmd_router()

    assert expected not in Path('load.py').read_text()
    assert expected not in Path('clean.py').read_text()
    assert expected not in Path('plot.py').read_text()
Beispiel #16
0
def test_install_hook_custom_entry_point(monkeypatch, tmp_nbs):
    shutil.copy('pipeline.yaml', 'pipeline.another.yaml')

    # inject cells
    monkeypatch.setattr(sys, 'argv', ['ploomber', 'nb', '--inject'])
    cli.cmd_router()

    # init repo
    git_init()

    # install hook
    monkeypatch.setattr(sys, 'argv', [
        'ploomber',
        'nb',
        '--install-hook',
        '--entry-point',
        'pipeline.another.yaml',
    ])
    cli.cmd_router()

    assert ('ploomber nb --entry-point pipeline.another.yaml --remove'
            in Path('.git', 'hooks', 'pre-commit').read_text())
    assert ('ploomber nb --entry-point pipeline.another.yaml --inject'
            in Path('.git', 'hooks', 'post-commit').read_text())
Beispiel #17
0
def test_format_skips_non_notebooks(monkeypatch, backup_simple,
                                    no_sys_modules_cache):
    monkeypatch.setattr(sys, 'argv',
                        ['ploomber', 'nb', '--format', 'py:percent'])
    cli.cmd_router()
Beispiel #18
0
def test_single_click_disable(mock_nb_single_click_disable, tmp_directory):
    cli.cmd_router()
Beispiel #19
0
def test_single_click(mock_nb_single_click_enable, tmp_directory):
    cli.cmd_router()

    current = json.loads(mock_nb_single_click_enable.read_text())
    expected = json.loads(nb._jupyterlab_default_settings_overrides)
    assert current == expected