Example #1
0
def test_app(monkeypatch, tmpdir):
    monkeypatch.setenv('GRIPHOME', str(tmpdir))
    zero_path = input_filename('zero.md')
    zero_output = output_file('app', 'zero.html')
    gfm_test_path = input_filename('gfm-test.md')
    gfm_test_output = output_file('app', 'gfm-test.html')
    assets = GitHubAssetManagerMock()

    with GitHubRequestsMock() as responses:
        assert Grip(zero_path, assets=assets).render() == zero_output
        assert Grip(zero_path, assets=assets).render('/') == zero_output
        assert Grip(zero_path, assets=assets).render('/x/../') == zero_output
        with Grip(zero_path, assets=assets).test_client() as client:
            assert client.get('/').data.decode('utf-8') == zero_output
        assert len(responses.calls) == 4

    with GitHubRequestsMock() as responses:
        app = Grip(gfm_test_path, assets=assets)
        assert app.render() == gfm_test_output
        assert app.render('/') == gfm_test_output
        assert len(responses.calls) == 2
Example #2
0
def regenerate_app():
    zero = input_filename('zero.md')
    simple = input_filename('simple.md')
    gfm_test = input_filename('gfm-test.md')

    write(GripMock(zero, AUTH).render(), 'app', 'zero.html')
    write(GripMock(zero, AUTH, GitHubRenderer(True)).render(),
          'app', 'zero-user-context.html')
    write(GripMock(zero, AUTH, GitHubRenderer(True, USER_CONTEXT)).render(),
          'app', 'zero-user-context.html')

    write(GripMock(simple, AUTH).render(), 'app', 'simple.html')
    write(GripMock(simple, AUTH, GitHubRenderer(True)).render(),
          'app', 'simple-user-context.html')
    write(GripMock(simple, AUTH, GitHubRenderer(True, USER_CONTEXT)).render(),
          'app', 'simple-user-context.html')

    write(GripMock(gfm_test, AUTH).render(), 'app', 'gfm-test.html')
    write(GripMock(gfm_test, AUTH, GitHubRenderer(True)).render(),
          'app', 'gfm-test-user-context.html')
    write(GripMock(gfm_test, AUTH, GitHubRenderer(True, USER_CONTEXT))
          .render(), 'app', 'gfm-test-user-context.html')
Example #3
0
def test_directory_reader():
    input_path = 'input'
    markdown_path = posixpath.join(input_path, 'gfm-test.md')
    default_path = posixpath.join(input_path, 'default')
    input_img_path = posixpath.join(input_path, 'img.png')

    input_dir = os.path.join(DIRNAME, 'input')
    markdown_file = os.path.join(input_dir, 'gfm-test.md')
    default_dir = os.path.join(input_dir, 'default')
    default_file = os.path.join(default_dir, DEFAULT_FILENAME)

    DirectoryReader(input_filename('default'))
    DirectoryReader(input_filename(default_file))
    DirectoryReader(input_filename(default_file), silent=True)
    DirectoryReader(input_filename('empty'), silent=True)
    with pytest.raises(ReadmeNotFoundError):
        DirectoryReader(input_filename('empty'))
    with pytest.raises(ReadmeNotFoundError):
        DirectoryReader(input_filename('empty', DEFAULT_FILENAME))

    reader = DirectoryReader(DIRNAME, silent=True)
    assert reader.root_filename == os.path.join(DIRNAME, DEFAULT_FILENAME)
    assert reader.root_directory == DIRNAME

    assert reader.normalize_subpath(None) is None
    assert reader.normalize_subpath('.') == './'
    assert reader.normalize_subpath('./././') == './'
    assert reader.normalize_subpath('non-existent/.././') == './'
    assert reader.normalize_subpath('non-existent/') == 'non-existent'
    assert reader.normalize_subpath('non-existent') == 'non-existent'
    with pytest.raises(NotFound):
        reader.normalize_subpath('../unsafe')
    with pytest.raises(NotFound):
        reader.normalize_subpath('/unsafe')
    assert reader.normalize_subpath(input_path) == input_path + '/'
    assert reader.normalize_subpath(markdown_path) == markdown_path
    assert reader.normalize_subpath(markdown_path + '/') == markdown_path

    assert reader.readme_for(None) == os.path.join(DIRNAME, DEFAULT_FILENAME)
    with pytest.raises(ReadmeNotFoundError):
        reader.readme_for('non-existent')
    with pytest.raises(ReadmeNotFoundError):
        reader.readme_for(input_path)
    assert reader.readme_for(markdown_path) == os.path.abspath(markdown_file)
    assert reader.readme_for(default_path) == os.path.abspath(default_file)

    # TODO: 'README.md' vs 'readme.md'

    assert reader.filename_for(None) == DEFAULT_FILENAME
    assert reader.filename_for(input_path) is None
    assert reader.filename_for(default_path) == os.path.relpath(
        default_file, reader.root_directory)

    assert not reader.is_binary()
    assert not reader.is_binary(input_path)
    assert not reader.is_binary(markdown_path)
    assert reader.is_binary(input_img_path)

    assert reader.last_updated() is None
    assert reader.last_updated(input_path) is None
    assert reader.last_updated(markdown_path) is not None
    assert reader.last_updated(default_path) is not None
    assert DirectoryReader(default_dir).last_updated is not None

    with pytest.raises(ReadmeNotFoundError):
        assert reader.read(input_path) is not None
    assert reader.read(markdown_path)
    assert reader.read(default_path)
    with pytest.raises(ReadmeNotFoundError):
        assert reader.read()
    assert DirectoryReader(default_dir).read() is not None