Exemplo n.º 1
0
def test_invalid_import1(mocker, tmp_work_path, capsys):
    sys_exit = mocker.patch('watchfiles.cli.sys.exit', side_effect=SysError)
    with pytest.raises(SysError):
        cli('foo.bar')
    sys_exit.assert_called_once_with(1)
    out, err = capsys.readouterr()
    assert out == ''
    assert err == "ImportError: No module named 'foo'\n"
Exemplo n.º 2
0
def test_invalid_path(mocker, capsys):
    sys_exit = mocker.patch('watchfiles.cli.sys.exit', side_effect=SysError)
    with pytest.raises(SysError):
        cli('os.getcwd', '/does/not/exist')

    sys_exit.assert_called_once_with(1)
    out, err = capsys.readouterr()
    assert out == ''
    assert err == 'path "/does/not/exist" does not exist\n'
Exemplo n.º 3
0
def test_invalid_import2(mocker, tmp_work_path, capsys):
    sys_exit = mocker.patch('watchfiles.cli.sys.exit', side_effect=SysError)
    with pytest.raises(SysError):
        cli('pprint.foobar')

    sys_exit.assert_called_once_with(1)
    out, err = capsys.readouterr()
    assert out == ''
    assert err == 'ImportError: Module "pprint" does not define a "foobar" attribute\n'
Exemplo n.º 4
0
def test_command(mocker, tmp_work_path):
    mocker.patch('watchfiles.cli.sys.stdin.fileno')
    mocker.patch('os.ttyname', return_value='/path/to/tty')
    mock_run_process = mocker.patch('watchfiles.cli.run_process')
    cli('foo --bar -V 3', '.')
    mock_run_process.assert_called_once_with(
        tmp_work_path,
        target='foo --bar -V 3',
        target_type='command',
        watch_filter=IsInstance(DefaultFilter, only_direct_instance=True),
        debug=False,
        sigint_timeout=5,
        sigkill_timeout=1,
    )
Exemplo n.º 5
0
def test_filter_default(mocker, tmp_path):
    mocker.patch('watchfiles.cli.sys.stdin.fileno')
    mocker.patch('os.ttyname', return_value='/path/to/tty')
    mock_run_process = mocker.patch('watchfiles.cli.run_process')
    cli('--filter', 'default', 'os.getcwd', str(tmp_path))
    mock_run_process.assert_called_once_with(
        tmp_path,
        target='os.getcwd',
        target_type='function',
        watch_filter=IsInstance(DefaultFilter, only_direct_instance=True),
        debug=False,
        sigint_timeout=5,
        sigkill_timeout=1,
    )
Exemplo n.º 6
0
def test_args_command(mocker, tmp_path, caplog):
    caplog.set_level('INFO', 'watchfiles')
    mocker.patch('watchfiles.cli.sys.stdin.fileno')
    mocker.patch('os.ttyname', return_value='/path/to/tty')
    mock_run_process = mocker.patch('watchfiles.cli.run_process')
    cli('--args', '--version ', 'foobar.sh', str(tmp_path))

    mock_run_process.assert_called_once_with(
        tmp_path,
        target='foobar.sh',
        target_type='command',
        watch_filter=IsInstance(DefaultFilter, only_direct_instance=True),
        debug=False,
        sigint_timeout=5,
        sigkill_timeout=1,
    )
    assert 'WARNING: --args is only used when the target is a function\n' in caplog.text
Exemplo n.º 7
0
def test_filter_all(mocker, tmp_path, capsys):
    mocker.patch('watchfiles.cli.sys.stdin.fileno')
    mocker.patch('os.ttyname', return_value='/path/to/tty')
    mock_run_process = mocker.patch('watchfiles.cli.run_process')
    cli('--filter', 'all', '--ignore-paths', 'foo', 'os.getcwd', str(tmp_path))
    mock_run_process.assert_called_once_with(
        tmp_path,
        target='os.getcwd',
        target_type='function',
        watch_filter=None,
        debug=False,
        sigint_timeout=5,
        sigkill_timeout=1,
    )
    out, err = capsys.readouterr()
    assert out == ''
    assert '"--ignore-paths" argument ignored as "all" filter was selected\n' in err
Exemplo n.º 8
0
def test_args(mocker, tmp_path, reset_argv, caplog):
    caplog.set_level('INFO', 'watchfiles')
    mocker.patch('watchfiles.cli.sys.stdin.fileno')
    mocker.patch('os.ttyname', return_value='/path/to/tty')
    mock_run_process = mocker.patch('watchfiles.cli.run_process')
    cli('--args', '--version ', 'os.getcwd', str(tmp_path))

    mock_run_process.assert_called_once_with(
        tmp_path,
        target='os.getcwd',
        target_type='function',
        watch_filter=IsInstance(DefaultFilter, only_direct_instance=True),
        debug=False,
        sigint_timeout=5,
        sigkill_timeout=1,
    )
    assert sys.argv == ['os.getcwd', '--version']
    assert 'WARNING: --args' not in caplog.text
Exemplo n.º 9
0
def test_cli_help(mocker, capsys):
    mocker.patch('watchfiles.cli.argparse.ArgumentParser.exit',
                 side_effect=RuntimeError('custom exit'))
    TerminalSize = namedtuple('TerminalSize', ['columns', 'lines'])
    mocker.patch('shutil.get_terminal_size', return_value=TerminalSize(80, 24))

    with pytest.raises(RuntimeError, match='custom exit'):
        cli('--help')

    out, err = capsys.readouterr()
    assert err == ''

    cli_help_path = ROOT_DIR / 'docs' / 'cli_help.txt'
    try:
        assert out == cli_help_path.read_text(
        ), f'cli help output differs from {cli_help_path}, file updated'
    except AssertionError:
        cli_help_path.write_text(out)
        raise
Exemplo n.º 10
0
def test_ignore_paths(mocker, tmp_work_path):
    mocker.patch('watchfiles.cli.sys.stdin.fileno')
    mocker.patch('os.ttyname', return_value='/path/to/tty')
    mock_run_process = mocker.patch('watchfiles.cli.run_process')
    cli(
        '--ignore-paths',
        '/foo/bar,/apple/banana',
        '--filter',
        'python',
        'os.getcwd',
        '.',
    )
    mock_run_process.assert_called_once_with(
        Path(str(tmp_work_path)),
        target='os.getcwd',
        target_type='function',
        watch_filter=(
            IsInstance(PythonFilter)
            & HasAttributes(extensions=('.py', '.pyx', '.pyd'),
                            _ignore_paths=('/foo/bar', '/apple/banana'))),
        debug=False,
        sigint_timeout=5,
        sigkill_timeout=1,
    )