Skip to content

Commit 40cab09

Browse files
committed
parser: Add test coverage for steps and util.get_files()
1 parent 3098840 commit 40cab09

File tree

10 files changed

+159
-6
lines changed

10 files changed

+159
-6
lines changed

parser/snooty/flutter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self, **kwargs: object) -> None: ...
1818

1919

2020
class mapping_dict(Dict[str, Any]):
21+
"""A dictionary that also contains source line information."""
2122
__slots__ = ('_start_line', '_end_line')
2223
_start_line: int
2324
_end_line: int

parser/snooty/gizaparser/nodes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,14 @@ def render_heading(self, parse_rst: EmbeddedRstParser) -> Sequence[SerializableT
247247
def ast_to_testing_string(ast: Any) -> str:
248248
value = ast.get('value', '')
249249
children = ast.get('children', [])
250-
attrs = ', '.join(
250+
attrs = ' '.join(
251251
'{}="{}"'.format(k, v) for k, v in ast.items() if k not in (
252-
'value', 'children', 'type', 'position'))
252+
'argument', 'value', 'children', 'type', 'position') and v)
253253
contents = value if value else (''.join(
254254
ast_to_testing_string(child) for child in children)
255255
if children else '')
256+
if 'argument' in ast:
257+
contents = ''.join(ast_to_testing_string(part) for part in ast['argument']) + contents
256258
return '<{}{}>{}</{}>'.format(
257259
ast['type'],
258260
' ' + attrs if attrs else '',

parser/snooty/gizaparser/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ def parse(ty: Type[_T],
6767
return parsed, text, diagnostics
6868
except LoadError as err:
6969
mapping = err.bad_data if isinstance(err.bad_data, dict) else {}
70-
lineno = mapping.get('_start_line', 0)
70+
lineno = mapping._start_line if isinstance(mapping, mapping_dict) else 0
7171
return [], text, diagnostics + [Diagnostic.error(str(err), lineno)]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from pathlib import Path, PurePath
2+
from typing import Dict, Tuple, List
3+
from .steps import GizaStepsCategory
4+
from .nodes import ast_to_testing_string
5+
from ..types import Diagnostic, Page, EmbeddedRstParser, ProjectConfig
6+
from ..parser import make_embedded_rst_parser
7+
8+
9+
def test_step() -> None:
10+
project_path, project_config, project_diagnostics = ProjectConfig.open(Path('test_data'))
11+
assert project_diagnostics == []
12+
13+
category = GizaStepsCategory(project_config)
14+
path = Path('test_data/steps-test.yaml')
15+
child_path = Path('test_data/steps-test-child.yaml')
16+
17+
def add_main_file() -> List[Diagnostic]:
18+
steps, text, parse_diagnostics = category.parse(path)
19+
category.add(path, text, steps)
20+
assert len(parse_diagnostics) == 0
21+
assert len(steps) == 4
22+
return parse_diagnostics
23+
24+
def add_child_file() -> List[Diagnostic]:
25+
steps, text, parse_diagnostics = category.parse(child_path)
26+
category.add(child_path, text, steps)
27+
assert len(parse_diagnostics) == 0
28+
return parse_diagnostics
29+
30+
all_diagnostics: Dict[PurePath, List[Diagnostic]] = {}
31+
all_diagnostics[path] = add_main_file()
32+
all_diagnostics[child_path] = add_child_file()
33+
34+
file_id, giza_node = next(category.reify_all_files(all_diagnostics))
35+
36+
def create_page() -> Tuple[Page, EmbeddedRstParser]:
37+
page = Page(path, '', {})
38+
return page, make_embedded_rst_parser(project_config, page, all_diagnostics[path])
39+
40+
pages = category.to_pages(create_page, giza_node.data)
41+
assert len(pages) == 1
42+
print(repr(ast_to_testing_string(pages[0].ast)))
43+
assert ast_to_testing_string(pages[0].ast) == ''.join((
44+
'<directive name="steps"><directive name="step"><section><heading><text>Import the public ',
45+
'key used by the package management system.</text></heading><paragraph><text>Issue the ',
46+
'following command to import the\n</text><reference><text>MongoDB public GPG Key</text>',
47+
'</reference><target ids="[\'mongodb-public-gpg-key\']"></target></paragraph></section>',
48+
'</directive>',
49+
50+
'<directive name="step"><section><heading><text>Create a </text><literal><text>',
51+
'/etc/apt/sources.list.d/mongodb-org-3.4.list</text></literal><text> file for MongoDB.',
52+
'</text></heading><paragraph><text>Create the list file using the command appropriate for ',
53+
'your version\nof Debian.</text></paragraph></section></directive>',
54+
55+
'<directive name="step"><section><heading><text>Reload local package database.</text>',
56+
'</heading><paragraph><text>Issue the following command to reload the local package ',
57+
'database:</text></paragraph><code lang="sh" copyable="True">sudo apt-get update\n</code>',
58+
'</section></directive>',
59+
60+
'<directive name="step"><section><heading><text>Install the MongoDB packages.</text>',
61+
'</heading><paragraph><text>You can install either the latest stable version of MongoDB ',
62+
'or a\nspecific version of MongoDB.</text></paragraph>',
63+
'<directive name="code-block">',
64+
'<text>sh</text><literal></literal></directive></section></directive></directive>'
65+
))

parser/snooty/test_source_constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44

55
def test_project() -> None:
6-
root_path, project_config, project_diagnostics = ProjectConfig.open(Path('test_data'))
6+
path = Path('test_data/bad_project')
7+
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
78
assert len(project_diagnostics) == 1
89
assert project_config.constants == {
910
'version': '3.4',

parser/snooty/test_util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ def test_reroot_path() -> None:
1313
PurePath('../bar/baz.rst'),
1414
PurePath('foo/dir/test.txt'),
1515
Path('foo'))[0] == PurePath('foo/bar/baz.rst')
16+
17+
18+
def test_get_files() -> None:
19+
assert list(util.get_files(PurePath('test_data'), ('.toml',))) == [
20+
Path('test_data/snooty.toml'),
21+
Path('test_data/bad_project/snooty.toml')]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = "test_data"
2+
3+
[constants]
4+
version = 3.4
5+
package_title = "{+version+}.tar.gz"
6+
invalid = "{+foo+}"

parser/test_data/snooty.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ name = "test_data"
22

33
[constants]
44
version = 3.4
5-
package_title = "{+version+}.tar.gz"
6-
invalid = "{+foo+}"
5+
package-name-org = "mongodb-org"
6+
pgp-version = "{+version+}"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
title: Import the public key used by the package management system.
2+
stepnum: 1
3+
level: 4
4+
ref: import-key
5+
action:
6+
pre: |
7+
Issue the following command to import the
8+
`MongoDB public GPG Key <https://www.mongodb.org/static/pgp/server-{+pgp-version+}.asc>`_
9+
---
10+
title: Create a list file for MongoDB.
11+
stepnum: 2
12+
level: 4
13+
ref: sources-list
14+
content: |
15+
16+
Create the list file
17+
``/etc/apt/sources.list.d/mongodb-org-{+version+}.list`` for your
18+
version of Ubuntu.
19+
20+
---
21+
title: Reload local package database.
22+
stepnum: 3
23+
level: 4
24+
ref: reload
25+
action:
26+
pre: |
27+
Issue the following command to reload the local package database:
28+
language: sh
29+
copyable: true
30+
code: |
31+
sudo apt-get update
32+
---
33+
title: Install the MongoDB packages.
34+
stepnum: 4
35+
level: 4
36+
ref: install
37+
content: |
38+
39+
You can install either the latest stable version of MongoDB or a
40+
specific version of MongoDB.
41+
42+
.. code-block:: sh
43+
44+
echo "{+package-name-org+} hold" | sudo dpkg --set-selections
45+
...

parser/test_data/steps-test.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
stepnum: 1
2+
level: 4
3+
source:
4+
file: steps-test-child.yaml
5+
ref: import-key
6+
---
7+
title: Create a ``/etc/apt/sources.list.d/mongodb-org-{+version+}.list`` file for MongoDB.
8+
stepnum: 2
9+
level: 4
10+
ref: sources-list
11+
action:
12+
pre: |
13+
Create the list file using the command appropriate for your version
14+
of Debian.
15+
---
16+
stepnum: 3
17+
level: 4
18+
source:
19+
file: steps-test-child.yaml
20+
ref: reload
21+
---
22+
stepnum: 4
23+
level: 4
24+
source:
25+
file: steps-test-child.yaml
26+
ref: install
27+
...

0 commit comments

Comments
 (0)