Skip to content

Commit

Permalink
parser: 100% test coverage of types.py
Browse files Browse the repository at this point in the history
  • Loading branch information
i80and committed Jan 29, 2019
1 parent 0763e77 commit 3cd4b8f
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ parser/snooty.py
*.dist/
node_modules/
.coverage
htmlcov/
4 changes: 2 additions & 2 deletions parser/snooty/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ def handle_directive(self, node: docutils.nodes.Node, doc: Dict[str, Serializabl
return

try:
static_asset = self.add_static_asset(PurePath(argument_text))
static_asset = self.add_static_asset(Path(argument_text))
options['checksum'] = static_asset.checksum
except OSError as err:
print(util.get_line(node))
msg = '"figure" could not open "{}": {}'.format(
argument_text, os.strerror(err.errno))
self.diagnostics.append(Diagnostic.error(msg, util.get_line(node)))

def add_static_asset(self, path: PurePath) -> StaticAsset:
def add_static_asset(self, path: Path) -> StaticAsset:
fileid, path = util.reroot_path(path, self.docpath, self.project_root)
static_asset = StaticAsset.load(fileid.as_posix(), path)
self.static_assets.add(static_asset)
Expand Down
13 changes: 0 additions & 13 deletions parser/snooty/test_source_constants.py

This file was deleted.

57 changes: 57 additions & 0 deletions parser/snooty/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from pathlib import Path, PurePath
from .types import Diagnostic, ProjectConfig, StaticAsset, Page


def test_project() -> None:
path = Path('test_data/bad_project')
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
assert len(project_diagnostics) == 1
assert project_config.constants == {
'version': '3.4',
'package_title': '3.4.tar.gz',
'invalid': ''
}

path = Path('test_data/empty_project')
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
assert project_config.constants == {}

# Test missing project behavior
root_path, project_config, project_diagnostics = ProjectConfig.open(Path('.').resolve())
assert project_config.name == 'untitled'
assert len(project_diagnostics) == 0


def test_diagnostics() -> None:
diag = Diagnostic.warning('foo', (0, 0), 10)
assert diag.severity_string == 'Warning'
assert diag.start == (0, 0)
assert diag.end[0] == 10 and diag.end[1] > 100

diag = Diagnostic.warning('foo', (0, 0), (10, 0))
assert diag.end == (10, 0)


def test_static_asset() -> None:
path = Path('test_data/compass-explain-plan-with-index-raw-json.png')
asset = StaticAsset.load('foo', path)
assert asset.checksum == 'e8d907020488a0b0ba070ae3eeb86aae2713a61cc5bb28346c023cb505cced3c'
asset2 = StaticAsset.load('foo', path)
asset3 = StaticAsset.load('bar', path)

assert asset == asset2 != asset3

# Make sure that assets are hashed correctly
collection = set((asset, asset2))
assert len(collection) == 1
collection = set((asset, asset3))
assert len(collection) == 2


def test_page() -> None:
page = Page(Path('foo.rst'), '', {})
assert page.get_id() == PurePath('foo.rst')

page = Page(Path('steps-foo.yaml'), '', {})
page.category = 'steps'
assert page.get_id() == PurePath('steps/foo.yaml')
5 changes: 3 additions & 2 deletions parser/snooty/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_reroot_path() -> None:


def test_get_files() -> None:
assert list(util.get_files(PurePath('test_data'), ('.toml',))) == [
assert set(util.get_files(PurePath('test_data'), ('.toml',))) == {
Path('test_data/snooty.toml'),
Path('test_data/bad_project/snooty.toml')]
Path('test_data/bad_project/snooty.toml'),
Path('test_data/empty_project/snooty.toml')}
15 changes: 9 additions & 6 deletions parser/snooty/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import enum
import hashlib
import re
from pathlib import Path
from pathlib import Path, PurePath
from dataclasses import dataclass, field
from pathlib import PurePath
import toml
from .flutter import checked, check_type
from typing import Any, Callable, Dict, Set, List, Tuple, Optional, Union, Match
Expand Down Expand Up @@ -88,10 +87,14 @@ class StaticAsset:
def __hash__(self) -> int:
return hash(self.checksum)

def __eq__(self, other: object) -> bool:
return isinstance(other, StaticAsset) and \
self.checksum == other.checksum and \
self.fileid == other.fileid

@classmethod
def load(cls, fileid: str, path: PurePath) -> 'StaticAsset':
with open(path, 'rb') as f:
data = f.read()
def load(cls, fileid: str, path: Path) -> 'StaticAsset':
data = path.read_bytes()
asset_hash = hashlib.blake2b(data, digest_size=32).hexdigest()
return cls(fileid, asset_hash, data)

Expand Down Expand Up @@ -129,7 +132,7 @@ def open(cls, root: Path) -> Tuple[Path, 'ProjectConfig', List[Diagnostic]]:
path = root
while path.parent != path:
try:
with open(path.joinpath('snooty.toml'), 'r') as f:
with path.joinpath('snooty.toml').open() as f:
data = toml.load(f)
data['root'] = root
result, diagnostics = check_type(ProjectConfig, data).render_constants()
Expand Down
2 changes: 1 addition & 1 deletion parser/snooty/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def reroot_path(filename: PurePath,
docpath: PurePath,
project_root: Path) -> Tuple[PurePath, PurePath]:
project_root: Path) -> Tuple[PurePath, Path]:
"""Files within a project may refer to other files. Return a canonical path
relative to the project root."""
if filename.is_absolute():
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions parser/test_data/empty_project/snooty.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "test_data"

0 comments on commit 3cd4b8f

Please sign in to comment.