Skip to content

Commit 172510f

Browse files
committed
fix bug in tree tester, add pyproject.toml
1 parent c7790f9 commit 172510f

File tree

3 files changed

+41
-32
lines changed

3 files changed

+41
-32
lines changed

pyproject.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[tool.ruff.lint]
2+
select = [
3+
# pycodestyle
4+
"E",
5+
# Pyflakes
6+
"F",
7+
# pyupgrade
8+
"UP",
9+
# flake8-bugbear
10+
"B",
11+
# flake8-simplify
12+
"SIM",
13+
# isort
14+
"I",
15+
# pylint
16+
"PL",
17+
]
18+
ignore = [
19+
"E741", # single-letter variable
20+
"PLR2004", # magic values
21+
]

tests/book_parser.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
import re
43

5-
64
COMMIT_REF_FINDER = r"ch\d\dl\d\d\d-?\d?"
75

86

9-
class CodeListing(object):
7+
class CodeListing:
108
COMMIT_REF_FINDER = r"^(.+) \((" + COMMIT_REF_FINDER + r")\)$"
119

1210
def __init__(self, filename, contents):
@@ -80,8 +78,7 @@ def type(self):
8078
return "interactive manage.py"
8179
if self.startswith("STAGING_SERVER="):
8280
return "against staging"
83-
else:
84-
return "other command"
81+
return "other command"
8582

8683
def __repr__(self):
8784
return "<Command %s>" % (str.__repr__(self),)
@@ -120,11 +117,7 @@ def parse_output(listing):
120117
return [Output(text)]
121118

122119
outputs = []
123-
output_before = listing.text
124-
if output_before:
125-
output_before = fix_newlines(output_before.strip())
126-
else:
127-
output_before = ""
120+
output_before = fix_newlines(listing.text.strip()) if listing.text else ""
128121

129122
for command in commands:
130123
if "$" in output_before and "\n" in output_before:
@@ -157,7 +150,7 @@ def _strip_callouts(content):
157150
return content
158151

159152

160-
def parse_listing(listing):
153+
def parse_listing(listing): # noqa: PLR0912
161154
classes = listing.get("class").split()
162155
skip = "skipme" in classes
163156
dofirst_classes = [c for c in classes if c.startswith("dofirst")]
@@ -171,7 +164,7 @@ def parse_listing(listing):
171164
filename = listing.cssselect(".title")[0].text_content().strip()
172165
except IndexError:
173166
raise Exception(
174-
"could not find title for listing {}".format(listing.text_content())
167+
f"could not find title for listing {listing.text_content()}"
175168
)
176169
contents = (
177170
listing.cssselect(".content")[0]

tests/book_tester.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
from lxml import html
2-
from getpass import getuser
31
import io
42
import os
5-
import stat
63
import re
4+
import stat
75
import subprocess
8-
import time
96
import tempfile
10-
from textwrap import wrap
7+
import time
118
import unittest
9+
from getpass import getuser
10+
from textwrap import wrap
1211

13-
from write_to_file import write_to_file
1412
from book_parser import (
1513
CodeListing,
1614
Command,
1715
Output,
1816
parse_listing,
1917
)
18+
from lxml import html
2019
from sourcetree import Commit, SourceTree
2120
from update_source_repo import update_sources_for_chapter
21+
from write_to_file import write_to_file
2222

2323
PHANTOMJS_RUNNER = os.path.join(
2424
os.path.abspath(os.path.dirname(__file__)), "my-phantomjs-qunit-runner.js"
@@ -256,9 +256,7 @@ def parse_listings(self):
256256

257257
def check_final_diff(self, ignore=None, diff=None):
258258
if diff is None:
259-
diff = self.run_command(
260-
Command("git diff -w repo/{}".format(self.chapter_name))
261-
)
259+
diff = self.run_command(Command(f"git diff -w repo/{self.chapter_name}"))
262260
try:
263261
print("checking final diff", diff)
264262
except io.BlockingIOError:
@@ -269,11 +267,9 @@ def check_final_diff(self, ignore=None, diff=None):
269267

270268
if ignore is None:
271269
if commit.lines_to_add:
272-
self.fail("Found lines to add in diff:\n{}".format(commit.lines_to_add))
270+
self.fail(f"Found lines to add in diff:\n{commit.lines_to_add}")
273271
if commit.lines_to_remove:
274-
self.fail(
275-
"Found lines to remove in diff:\n{}".format(commit.lines_to_remove)
276-
)
272+
self.fail(f"Found lines to remove in diff:\n{commit.lines_to_remove}")
277273
return
278274

279275
if "moves" in ignore:
@@ -285,7 +281,7 @@ def check_final_diff(self, ignore=None, diff=None):
285281
for line in difference_lines:
286282
if any(ignorable in line for ignorable in ignore):
287283
continue
288-
self.fail("Found divergent line in diff:\n{}".format(line))
284+
self.fail(f"Found divergent line in diff:\n{line}")
289285

290286
def start_with_checkout(self):
291287
update_sources_for_chapter(self.chapter_name, self.previous_chapter)
@@ -305,7 +301,7 @@ def write_to_file(self, codelisting):
305301
def apply_patch(self, codelisting):
306302
tf = tempfile.NamedTemporaryFile(delete=False)
307303
tf.write(codelisting.contents.encode("utf8"))
308-
tf.write("\n".encode("utf8"))
304+
tf.write(b"\n")
309305
tf.close()
310306
print("patch:\n", codelisting.contents)
311307
patch_output = self.run_command(
@@ -445,9 +441,8 @@ def skip_with_check(self, pos, expected_content):
445441
if hasattr(listing, "contents"):
446442
if expected_content not in listing.contents:
447443
raise Exception(error)
448-
else:
449-
if expected_content not in listing:
450-
raise Exception(error)
444+
elif expected_content not in listing:
445+
raise Exception(error)
451446
listing.skip = True
452447

453448
def replace_command_with_check(self, pos, old, new):
@@ -465,7 +460,7 @@ def replace_command_with_check(self, pos, old, new):
465460

466461
def _run_tree(self, target=""):
467462
return self.sourcetree.run_command(
468-
f"tree -v -I __pycache__ --noreport {target}", cwd,
463+
f"tree -v -I __pycache__ --noreport {target}"
469464
)
470465

471466
def assert_directory_tree_correct(self, expected_tree):
@@ -855,7 +850,7 @@ def recognise_listing_and_process_it(self):
855850
listing.was_checked = True
856851
self.pos += 2
857852
elif "tree" in listing and next_listing.type == "tree":
858-
assert listing.startswith('tree')
853+
assert listing.startswith("tree")
859854
_, _, target = listing.partition("tree")
860855
output = self._run_tree(target=target)
861856
self.assert_console_output_correct(output, next_listing)

0 commit comments

Comments
 (0)