Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit e418888

Browse files
sanketsauravsanket-deepsource
authored andcommitted
Add tests, setup CI and CQ
1 parent 5ee2265 commit e418888

File tree

9 files changed

+173
-63
lines changed

9 files changed

+173
-63
lines changed

.circleci/config.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: 2
2+
3+
jobs:
4+
build:
5+
docker:
6+
- image: circleci/python:3.7
7+
8+
working_directory: ~/repo
9+
10+
steps:
11+
- checkout
12+
13+
- restore_cache:
14+
keys:
15+
- cache-{{ checksum "Pipfile.lock" }}
16+
- cache-
17+
- run:
18+
name: install dependencies
19+
command: pipenv sync --dev
20+
21+
- save_cache:
22+
paths:
23+
- ~/.local
24+
- ~/.cache
25+
key: cache-{{ checksum "Pipfile.lock" }}
26+
27+
- run:
28+
name: run tests
29+
command: make test

.coveragerc

-8
This file was deleted.

.deepsource.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version = 1
2+
3+
exclude_patterns = [
4+
'tests/data/*',
5+
]
6+
7+
test_patterns = [
8+
'tests/*'
9+
]
10+
11+
[[analyzers]]
12+
name = "python"
13+
enabled = true
14+
15+
[analyzers.meta]
16+
max_line_length = 90
17+
skip_doc_coverage = ["module", "magic", "init"]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
test:
2-
pipenv run pytest --cov
2+
pipenv run pytest
33

44
.DEFAULT_GOAL := test

Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ verify_ssl = true
55

66
[dev-packages]
77
pytest = "*"
8-
pytest-cov = "*"
8+
lxml = "*"
99

1010
[packages]
1111
pygments = "*"

Pipfile.lock

+33-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

formatters/html_slice.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@ def __init__(self, *args, **kwargs):
2525
# if begin_line is set, set `linenostart` to match it
2626
self.linenostart = self.begin_line
2727

28+
# since we've changed the beginning of this file, we'll need to
29+
# offset the hl_lines, if available, appropriately.
30+
hl_line_offset = self.begin_line - 1
31+
self.hl_lines = [i - hl_line_offset for i in self.hl_lines or []]
32+
else:
33+
self.begin_line = None
34+
2835
end_line = kwargs.get('end_line')
2936
self.end_line = int(end_line) if end_line else None
3037

3138
# validate sanity of begin_line and end_line
32-
if self.end_line < self.begin_line:
39+
if ((self.begin_line and self.end_line) and
40+
(self.end_line < self.begin_line)):
3341
raise ValueError(
3442
"Value for `end_line` must be less than or equal to `begin_line`"
3543
)
3644

37-
# since we've changed the beginning of this file, we'll need to
38-
# offset the hl_lines, if available, appropriately.
39-
hl_line_offset = self.begin_line - 1
40-
self.hl_lines = [i - hl_line_offset for i in self.hl_lines or []]
41-
4245
def _format_lines(self, tokensource, *_, **__):
4346
"""
4447
Override the default method to support extraction of the expected
File renamed without changes.

tests/test_e2e.py

+83-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,87 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from subprocess import run
4+
from lxml import html, etree
35

46

5-
def test_foo():
6-
assert True
7+
SAMPLE_FILE = 'tests/data/sample.py'
8+
9+
10+
def test_html_slice_without_range(tmp_path):
11+
"""
12+
Verify that using the HtmlSliceFormatter without passing
13+
begin_line and end_line works identical to HtmlFormatter.
14+
"""
15+
16+
# run with HtmlFormatter
17+
output_html = tmp_path / 'html'
18+
run([
19+
'pygmentize', '-f', 'html',
20+
'-O', 'linenos=table,hl_lines="28 29 30"',
21+
'-o', output_html, SAMPLE_FILE
22+
])
23+
with open(output_html, 'r') as file_desc:
24+
output_html_content = file_desc.read()
25+
26+
# run with HtmlSliceFormatter
27+
output_html_slice = tmp_path / 'html_slice'
28+
run([
29+
'pygmentize', '-f', 'formatters/html_slice.py:HtmlSliceFormatter', '-x',
30+
'-O', 'linenos=table,hl_lines="28 29 30"',
31+
'-o', output_html_slice, SAMPLE_FILE
32+
])
33+
with open(output_html_slice, 'r') as file_desc:
34+
output_html_slice_content = file_desc.read()
35+
36+
assert output_html_content == output_html_slice_content
37+
38+
39+
def test_html_slice_with_range(tmp_path):
40+
"""
41+
Verify that using the HtmlSliceFormatter when passing
42+
begin_line and end_line works as expected.
43+
"""
44+
# run with HtmlFormatter
45+
output_html = tmp_path / 'html'
46+
run([
47+
'pygmentize', '-f', 'html',
48+
'-O', 'linenos=table,hl_lines="28 29 30"',
49+
'-o', output_html, SAMPLE_FILE
50+
])
51+
with open(output_html, 'r') as file_desc:
52+
output_html_content = file_desc.read()
53+
tree = html.fromstring(output_html_content)
54+
55+
# run with HtmlSliceFormatter
56+
output_html_slice = tmp_path / 'html_slice'
57+
run([
58+
'pygmentize', '-f', 'formatters/html_slice.py:HtmlSliceFormatter', '-x',
59+
'-O', 'linenos=table,hl_lines="28 29 30",begin_line=24,end_line=32',
60+
'-o', output_html_slice, SAMPLE_FILE
61+
])
62+
with open(output_html_slice, 'r') as file_desc:
63+
output_html_slice_content = file_desc.read()
64+
slice_tree = html.fromstring(output_html_slice_content)
65+
66+
# in the output for slice, check that line numbers are sane
67+
line_nos = slice_tree.xpath(
68+
"//div[contains(@class, 'linenodiv')]"
69+
)[0][0].text.split('\n')
70+
assert int(line_nos[0]) == 24
71+
assert int(line_nos[-1]) == 32
72+
73+
# the markup for the highlighted code must be a subset of the original
74+
# highlighted code
75+
original_code = ''.join(
76+
[etree.tostring(child).decode() for
77+
child in tree.xpath(
78+
"//div[contains(@class, 'highlight')]")[0][0].iterdescendants()
79+
])
80+
81+
sliced_code = ''.join(
82+
[etree.tostring(child).decode() for
83+
child in tree.xpath(
84+
"//div[contains(@class, 'highlight')]")[0][0].iterdescendants()
85+
])
86+
87+
assert sliced_code in original_code

0 commit comments

Comments
 (0)