Skip to content

Commit 3cd4b8f

Browse files
committed
parser: 100% test coverage of types.py
1 parent 0763e77 commit 3cd4b8f

File tree

9 files changed

+74
-24
lines changed

9 files changed

+74
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ parser/snooty.py
66
*.dist/
77
node_modules/
88
.coverage
9+
htmlcov/

parser/snooty/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ def handle_directive(self, node: docutils.nodes.Node, doc: Dict[str, Serializabl
155155
return
156156

157157
try:
158-
static_asset = self.add_static_asset(PurePath(argument_text))
158+
static_asset = self.add_static_asset(Path(argument_text))
159159
options['checksum'] = static_asset.checksum
160160
except OSError as err:
161161
print(util.get_line(node))
162162
msg = '"figure" could not open "{}": {}'.format(
163163
argument_text, os.strerror(err.errno))
164164
self.diagnostics.append(Diagnostic.error(msg, util.get_line(node)))
165165

166-
def add_static_asset(self, path: PurePath) -> StaticAsset:
166+
def add_static_asset(self, path: Path) -> StaticAsset:
167167
fileid, path = util.reroot_path(path, self.docpath, self.project_root)
168168
static_asset = StaticAsset.load(fileid.as_posix(), path)
169169
self.static_assets.add(static_asset)

parser/snooty/test_source_constants.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

parser/snooty/test_types.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from pathlib import Path, PurePath
2+
from .types import Diagnostic, ProjectConfig, StaticAsset, Page
3+
4+
5+
def test_project() -> None:
6+
path = Path('test_data/bad_project')
7+
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
8+
assert len(project_diagnostics) == 1
9+
assert project_config.constants == {
10+
'version': '3.4',
11+
'package_title': '3.4.tar.gz',
12+
'invalid': ''
13+
}
14+
15+
path = Path('test_data/empty_project')
16+
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
17+
assert project_config.constants == {}
18+
19+
# Test missing project behavior
20+
root_path, project_config, project_diagnostics = ProjectConfig.open(Path('.').resolve())
21+
assert project_config.name == 'untitled'
22+
assert len(project_diagnostics) == 0
23+
24+
25+
def test_diagnostics() -> None:
26+
diag = Diagnostic.warning('foo', (0, 0), 10)
27+
assert diag.severity_string == 'Warning'
28+
assert diag.start == (0, 0)
29+
assert diag.end[0] == 10 and diag.end[1] > 100
30+
31+
diag = Diagnostic.warning('foo', (0, 0), (10, 0))
32+
assert diag.end == (10, 0)
33+
34+
35+
def test_static_asset() -> None:
36+
path = Path('test_data/compass-explain-plan-with-index-raw-json.png')
37+
asset = StaticAsset.load('foo', path)
38+
assert asset.checksum == 'e8d907020488a0b0ba070ae3eeb86aae2713a61cc5bb28346c023cb505cced3c'
39+
asset2 = StaticAsset.load('foo', path)
40+
asset3 = StaticAsset.load('bar', path)
41+
42+
assert asset == asset2 != asset3
43+
44+
# Make sure that assets are hashed correctly
45+
collection = set((asset, asset2))
46+
assert len(collection) == 1
47+
collection = set((asset, asset3))
48+
assert len(collection) == 2
49+
50+
51+
def test_page() -> None:
52+
page = Page(Path('foo.rst'), '', {})
53+
assert page.get_id() == PurePath('foo.rst')
54+
55+
page = Page(Path('steps-foo.yaml'), '', {})
56+
page.category = 'steps'
57+
assert page.get_id() == PurePath('steps/foo.yaml')

parser/snooty/test_util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def test_reroot_path() -> None:
1616

1717

1818
def test_get_files() -> None:
19-
assert list(util.get_files(PurePath('test_data'), ('.toml',))) == [
19+
assert set(util.get_files(PurePath('test_data'), ('.toml',))) == {
2020
Path('test_data/snooty.toml'),
21-
Path('test_data/bad_project/snooty.toml')]
21+
Path('test_data/bad_project/snooty.toml'),
22+
Path('test_data/empty_project/snooty.toml')}

parser/snooty/types.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import enum
22
import hashlib
33
import re
4-
from pathlib import Path
4+
from pathlib import Path, PurePath
55
from dataclasses import dataclass, field
6-
from pathlib import PurePath
76
import toml
87
from .flutter import checked, check_type
98
from typing import Any, Callable, Dict, Set, List, Tuple, Optional, Union, Match
@@ -88,10 +87,14 @@ class StaticAsset:
8887
def __hash__(self) -> int:
8988
return hash(self.checksum)
9089

90+
def __eq__(self, other: object) -> bool:
91+
return isinstance(other, StaticAsset) and \
92+
self.checksum == other.checksum and \
93+
self.fileid == other.fileid
94+
9195
@classmethod
92-
def load(cls, fileid: str, path: PurePath) -> 'StaticAsset':
93-
with open(path, 'rb') as f:
94-
data = f.read()
96+
def load(cls, fileid: str, path: Path) -> 'StaticAsset':
97+
data = path.read_bytes()
9598
asset_hash = hashlib.blake2b(data, digest_size=32).hexdigest()
9699
return cls(fileid, asset_hash, data)
97100

@@ -129,7 +132,7 @@ def open(cls, root: Path) -> Tuple[Path, 'ProjectConfig', List[Diagnostic]]:
129132
path = root
130133
while path.parent != path:
131134
try:
132-
with open(path.joinpath('snooty.toml'), 'r') as f:
135+
with path.joinpath('snooty.toml').open() as f:
133136
data = toml.load(f)
134137
data['root'] = root
135138
result, diagnostics = check_type(ProjectConfig, data).render_constants()

parser/snooty/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def reroot_path(filename: PurePath,
88
docpath: PurePath,
9-
project_root: Path) -> Tuple[PurePath, PurePath]:
9+
project_root: Path) -> Tuple[PurePath, Path]:
1010
"""Files within a project may refer to other files. Return a canonical path
1111
relative to the project root."""
1212
if filename.is_absolute():
115 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name = "test_data"

0 commit comments

Comments
 (0)