Exemplo n.º 1
0
def test_pipe_lines_string_mocked(capsys, mock_popen, quiet, encoding='ascii'):
    proc = mock_popen.return_value
    proc.configure_mock(args=_common.INVALID_CMD,
                        returncode=0,
                        stdin=io.StringIO(),
                        stdout=io.StringIO('stdout'),
                        stderr=io.StringIO('stderr'))
    proc.communicate.side_effect = lambda: (proc.stdout.read(),
                                            proc.stderr.read())

    assert graphviz.pipe_lines_string('dot',
                                      'png',
                                      iter(['nongraph\n']),
                                      encoding=encoding,
                                      quiet=quiet) == 'stdout'

    assert proc.stdin.getvalue() == 'nongraph\n'

    mock_popen.assert_called_once_with(
        [_common.EXPECTED_DOT_BINARY, '-Kdot', '-Tpng'],
        encoding=encoding,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        startupinfo=_common.StartupinfoMatcher())
    assert capsys.readouterr() == ('', '' if quiet else 'stderr')
def test_version_mocked(mock_run, stdout, expected):
    mock_run.return_value = subprocess.CompletedProcess(_common.INVALID_CMD,
                                                        returncode=0,
                                                        stdout=stdout,
                                                        stderr=None)

    assert graphviz.version() == expected

    mock_run.assert_called_once_with([_common.EXPECTED_DOT_BINARY, '-V'],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     startupinfo=_common.StartupinfoMatcher(),
                                     encoding='ascii')
Exemplo n.º 3
0
def test_pipe_mocked(capsys, mock_run, quiet):
    mock_run.return_value = subprocess.CompletedProcess(_common.INVALID_CMD,
                                                        returncode=0,
                                                        stdout=b'stdout',
                                                        stderr=b'stderr')

    assert graphviz.pipe('dot', 'png', b'nongraph', quiet=quiet) == b'stdout'

    mock_run.assert_called_once_with(
        [_common.EXPECTED_DOT_BINARY, '-Kdot', '-Tpng'],
        input=b'nongraph',
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        startupinfo=_common.StartupinfoMatcher())
    assert capsys.readouterr() == ('', '' if quiet else 'stderr')
def test_version_parsefail_mocked(mock_run):
    mock_run.return_value = subprocess.CompletedProcess(
        _common.INVALID_CMD,
        returncode=0,
        stdout='nonversioninfo',
        stderr=None)

    with pytest.raises(RuntimeError, match=r'nonversioninfo'):
        graphviz.version()

    mock_run.assert_called_once_with([_common.EXPECTED_DOT_BINARY, '-V'],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     startupinfo=_common.StartupinfoMatcher(),
                                     encoding='ascii')
Exemplo n.º 5
0
def test_pipe_pipe_invalid_data_mocked(mocker, sentinel, mock_run, quiet):
    mock_sys_stderr = mocker.patch('sys.stderr',
                                   autospec=True,
                                   flush=mocker.Mock(),
                                   encoding=sentinel.encoding)

    mock_out = mocker.create_autospec(bytes, instance=True, name='mock_out')
    mock_err = mocker.create_autospec(bytes,
                                      instance=True,
                                      name='mock_err',
                                      **{'__len__.return_value': 1})

    mock_run.return_value = subprocess.CompletedProcess(_common.INVALID_CMD,
                                                        returncode=5,
                                                        stdout=mock_out,
                                                        stderr=mock_err)

    with pytest.raises(subprocess.CalledProcessError) as e:
        graphviz.pipe('dot', 'png', b'nongraph', quiet=quiet)

    assert e.value.returncode == 5
    assert e.value.cmd == _common.INVALID_CMD
    assert e.value.stdout is mock_out
    assert e.value.stderr is mock_err
    e.value.stdout = sentinel.new_stdout
    assert e.value.stdout is sentinel.new_stdout
    mock_run.assert_called_once_with(
        [_common.EXPECTED_DOT_BINARY, '-Kdot', '-Tpng'],
        input=b'nongraph',
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        startupinfo=_common.StartupinfoMatcher())
    if not quiet:
        mock_out.decode.assert_not_called()
        mock_err.decode.assert_called_once_with(sentinel.encoding)
        mock_sys_stderr.write.assert_called_once_with(
            mock_err.decode.return_value)
        mock_sys_stderr.flush.assert_called_once_with()
Exemplo n.º 6
0
def test_run_check_input_lines_mocked(mocker, sentinel, mock_popen,
                                      line=b'sp\xc3\xa4m'):  # noqa: N803
    mock_sys_stderr = mocker.patch('sys.stderr', autospec=True,
                                   flush=mocker.Mock(),
                                   encoding=sentinel.encoding)

    mock_out = mocker.create_autospec(bytes, instance=True, name='mock_out')
    mock_err = mocker.create_autospec(bytes, instance=True, name='mock_err',
                                      **{'__len__.return_value': 1})

    proc = mock_popen.return_value
    proc.configure_mock(args=_common.INVALID_CMD,
                        returncode=0,
                        stdin=mocker.create_autospec(io.BytesIO, instance=True))
    proc.communicate.return_value = (mock_out, mock_err)

    result = execute.run_check(proc.args, input_lines=iter([line]),
                               capture_output=True)

    # subprocess.CompletedProcess.__eq__() is not implemented
    assert isinstance(result, subprocess.CompletedProcess)
    assert result.args is proc.args
    assert result.returncode == proc.returncode
    assert result.stdout is mock_out
    assert result.stderr is mock_err

    mock_popen.assert_called_once_with(_common.INVALID_CMD,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       startupinfo=_common.StartupinfoMatcher())
    proc.communicate.assert_called_once_with()
    mock_out.decode.assert_not_called()
    mock_err.decode.assert_called_once_with(sentinel.encoding)
    mock_sys_stderr.write.assert_called_once_with(mock_err.decode.return_value)
    mock_sys_stderr.flush.assert_called_once_with()
Exemplo n.º 7
0
def test_render_mocked(capsys,
                       mock_run,
                       quiet,
                       directory,
                       filepath='nonfilepath'):
    mock_run.return_value = subprocess.CompletedProcess(_common.INVALID_CMD,
                                                        returncode=0,
                                                        stdout='stdout',
                                                        stderr='stderr')

    if directory is not None:
        filepath = os.path.join(directory, filepath)

    result = graphviz.render('dot', 'pdf', filepath, quiet=quiet)

    assert result == f'{filepath}.pdf'

    mock_run.assert_called_once_with(
        [_common.EXPECTED_DOT_BINARY, '-Kdot', '-Tpdf', '-O', 'nonfilepath'],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        cwd=directory,
        startupinfo=_common.StartupinfoMatcher())
    assert capsys.readouterr() == ('', '' if quiet else 'stderr')