Esempio n. 1
0
def test_normal_flow(monkeypatch):

    def _fake_prepare(args, dest):
        db_conn = sqlite3.connect(':memory:')
        db_conn.row_factory = sqlite3.Row
        db_conn.execute(
            'CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, '
            'type TEXT, path TEXT)'
        )
        return 'data', db_conn

    def _yielder():
        yield 'testmethod', 'testpath', 'cm'

    with tempfile.TemporaryDirectory() as td:
        monkeypatch.chdir(td)
        os.mkdir('foo')
        monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-n', 'bar',
                                          '-a', '-i', 'qux.png'])
        monkeypatch.setattr(main, 'prepare_docset', _fake_prepare)
        dt = MagicMock(detect=lambda _: True)
        dt.name = 'testtype'
        dt.return_value = MagicMock(parse=_yielder)
        monkeypatch.setattr(doc2dash.parsers, 'get_doctype', lambda _: dt)
        with patch('doc2dash.__main__.log.info') as info, \
             patch('os.system') as system, \
             patch('shutil.copy2') as cp:
            main.main()
            # assert mock.call_args_list is None
            out = '\n'.join(call[0][0] for call in info.call_args_list) + '\n'
            assert system.call_args[0] == ('open -a dash "bar.docset"', )
            assert cp.call_args[0] == ('qux.png', 'bar.docset/icon.png')

    assert out == '''\
Esempio n. 2
0
def test_normal_flow(monkeypatch):

    def _fake_prepare(args, dest):
        db_conn = sqlite3.connect(':memory:')
        db_conn.row_factory = sqlite3.Row
        db_conn.execute(
            'CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, '
            'type TEXT, path TEXT)'
        )
        return 'data', db_conn

    def _yielder():
        yield 'testmethod', 'testpath', 'cm'

    with tempfile.TemporaryDirectory() as td:
        monkeypatch.chdir(td)
        os.mkdir('foo')
        monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-n', 'bar',
                                          '-a', '-i', 'qux.png'])
        monkeypatch.setattr(main, 'prepare_docset', _fake_prepare)
        dt = MagicMock(detect=lambda _: True)
        dt.name = 'testtype'
        dt.return_value = MagicMock(parse=_yielder)
        monkeypatch.setattr(doc2dash.parsers, 'get_doctype', lambda _: dt)
        with patch('doc2dash.__main__.log.info') as info, \
                patch('os.system') as system, \
                patch('shutil.copy2') as cp:
            main.main()
            # assert mock.call_args_list is None
            out = '\n'.join(call[0][0] for call in info.call_args_list) + '\n'
            assert system.call_args[0] == ('open -a dash "bar.docset"', )
            assert cp.call_args[0] == ('qux.png', 'bar.docset/icon.png')

    assert out == '''\
Esempio n. 3
0
def test_fails_with_not_exist_index_page(capsys, monkeypatch):
    monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-I', 'bar.html'])
    with pytest.raises(SystemExit):
        main.main()

    out, err = capsys.readouterr()
    assert err == ''
    assert 'Index file bar.html dose not exists.' in out
Esempio n. 4
0
def test_fails_with_unknown_icon(capsys, monkeypatch):
    monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-i', 'bar.bmp'])
    with pytest.raises(SystemExit):
        main.main()

    out, err = capsys.readouterr()
    assert err == ''
    assert 'Please supply a PNG icon.' in out
Esempio n. 5
0
def test_fails_without_source(capsys, monkeypatch):
    monkeypatch.setattr(sys, 'argv', ['doc2dash'])
    with pytest.raises(SystemExit):
        main.main()

    out, err = capsys.readouterr()
    assert out == ''
    assert 'doc2dash: error: too few arguments' in err
Esempio n. 6
0
def test_fails_with_unknown_icon(capsys, monkeypatch):
    monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-i', 'bar.bmp'])
    with pytest.raises(SystemExit):
        main.main()

    out, err = capsys.readouterr()
    assert err == ''
    assert 'Please supply a PNG icon.' in out
Esempio n. 7
0
def test_handles_unknown_doc_types(monkeypatch):
    with tempfile.TemporaryDirectory() as td:
        monkeypatch.chdir(td)
        monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo'])
        os.mkdir('foo')
        with pytest.raises(SystemExit) as e:
            main.main()
        assert e.value.code == errno.EINVAL
Esempio n. 8
0
def test_handles_unknown_doc_types(monkeypatch):
    with tempfile.TemporaryDirectory() as td:
        monkeypatch.chdir(td)
        monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo'])
        os.mkdir('foo')
        with pytest.raises(SystemExit) as e:
            main.main()
        assert e.value.code == errno.EINVAL
Esempio n. 9
0
def test_fails_without_source(capsys, monkeypatch):
    monkeypatch.setattr(sys, 'argv', ['doc2dash'])
    with pytest.raises(SystemExit):
        main.main()

    out, err = capsys.readouterr()
    assert out == ''
    assert ('doc2dash: error: too few arguments' in err
            or 'error: the following arguments are required: source' in err)
Esempio n. 10
0
 def test_handles_unknown_doc_types(self, monkeypatch, tmpdir):
     """
     If docs are passed but are unknown, exit with EINVAL.
     """
     monkeypatch.chdir(tmpdir)
     os.mkdir('foo')
     with pytest.raises(SystemExit) as e:
         main.main(['foo'])
     assert e.value.code == errno.EINVAL
Esempio n. 11
0
 def test_handles_unknown_doc_types(self, monkeypatch, tmpdir):
     """
     If docs are passed but are unknown, exit with EINVAL.
     """
     monkeypatch.chdir(tmpdir)
     os.mkdir('foo')
     with pytest.raises(SystemExit) as e:
         main.main(['foo'])
     assert e.value.code == errno.EINVAL
Esempio n. 12
0
    def test_fails_with_unknown_icon(self, capsys):
        """
        Fail if icon is not PNG.
        """
        with pytest.raises(SystemExit):
            main.main(['foo', '-i', 'bar.bmp'])

        out, err = capsys.readouterr()
        assert err == ''
        assert 'Please supply a PNG icon.' in out
Esempio n. 13
0
    def test_fails_with_unknown_icon(self, capsys):
        """
        Fail if icon is not PNG.
        """
        with pytest.raises(SystemExit):
            main.main(['foo', '-i', 'bar.bmp'])

        out, err = capsys.readouterr()
        assert err == ''
        assert 'Please supply a PNG icon.' in out
Esempio n. 14
0
def test_logging(monkeypatch):
    with pytest.raises(ValueError):
        _check_logging(True, True, logging.INFO)
    _check_logging(False, False, logging.INFO)
    _check_logging(True, False, logging.DEBUG)
    _check_logging(False, True, logging.ERROR)

    monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-q', '-v'])
    with pytest.raises(SystemExit):
        main.main()
Esempio n. 15
0
    def test_fails_with_inexistent_index_page(self, capsys):
        """
        Fail if an index is supplied but doesn't exit.
        """
        with pytest.raises(SystemExit):
            main.main(['foo', '-I', 'bar.html'])

        out, err = capsys.readouterr()
        assert err == ''
        assert 'Index file bar.html does not exists.' in out
Esempio n. 16
0
def test_logging(monkeypatch):
    with pytest.raises(ValueError):
        _check_logging(True, True, logging.INFO)
    _check_logging(False, False, logging.INFO)
    _check_logging(True, False, logging.DEBUG)
    _check_logging(False, True, logging.ERROR)

    monkeypatch.setattr(sys, 'argv', ['doc2dash', 'foo', '-q', '-v'])
    with pytest.raises(SystemExit):
        main.main()
Esempio n. 17
0
    def test_fails_without_source(self, capsys):
        """
        Fail If no source is passed.
        """
        with pytest.raises(SystemExit):
            main.main([])

        out, err = capsys.readouterr()
        assert out == ''
        assert ('error: too few arguments' in err or
                'error: the following arguments are required: source' in err)
Esempio n. 18
0
    def test_fails_without_source(self, capsys):
        """
        Fail If no source is passed.
        """
        with pytest.raises(SystemExit):
            main.main([])

        out, err = capsys.readouterr()
        assert out == ''
        assert (
            'error: too few arguments' in err
            or 'error: the following arguments are required: source' in err
        )
Esempio n. 19
0
 def test_quiet_and_verbose_integration(self):
     """
     Ensure main() exists on -q + -v
     """
     with pytest.raises(SystemExit):
         main.main(['foo', '-q', '-v'])
Esempio n. 20
0
 def test_quiet_and_verbose_integration(self):
     """
     Ensure main() exists on -q + -v
     """
     with pytest.raises(SystemExit):
         main.main(['foo', '-q', '-v'])