Skip to content

Commit be3d25b

Browse files
authored
Rollup merge of rust-lang#92914 - camelid:snapshot-text, r=GuillaumeGomez
htmldocck: Add support for `/text()` in `@snapshot` This allows just testing the text, in cases where the HTML tags don't matter. See rust-lang#92908 (comment) for an example of when this would be useful. r? `@GuillaumeGomez`
2 parents 6a5663e + 9c6d8ef commit be3d25b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/etc/htmldocck.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def get_tree_count(tree, path):
401401
return len(tree.findall(path))
402402

403403

404-
def check_snapshot(snapshot_name, tree):
404+
def check_snapshot(snapshot_name, tree, normalize_to_text):
405405
assert rust_test_path.endswith('.rs')
406406
snapshot_path = '{}.{}.{}'.format(rust_test_path[:-3], snapshot_name, 'html')
407407
try:
@@ -413,7 +413,10 @@ def check_snapshot(snapshot_name, tree):
413413
else:
414414
raise FailedCheck('No saved snapshot value')
415415

416-
actual_str = ET.tostring(tree).decode('utf-8')
416+
if not normalize_to_text:
417+
actual_str = ET.tostring(tree).decode('utf-8')
418+
else:
419+
actual_str = flatten(tree)
417420

418421
if expected_str != actual_str:
419422
if bless:
@@ -494,11 +497,16 @@ def check_command(c, cache):
494497
[snapshot_name, html_path, pattern] = c.args
495498
tree = cache.get_tree(html_path)
496499
xpath = normalize_xpath(pattern)
500+
normalize_to_text = False
501+
if xpath.endswith('/text()'):
502+
xpath = xpath[:-7]
503+
normalize_to_text = True
504+
497505
subtrees = tree.findall(xpath)
498506
if len(subtrees) == 1:
499507
[subtree] = subtrees
500508
try:
501-
check_snapshot(snapshot_name, subtree)
509+
check_snapshot(snapshot_name, subtree, normalize_to_text)
502510
ret = True
503511
except FailedCheck as err:
504512
cerr = str(err)

0 commit comments

Comments
 (0)