mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
Rollup merge of #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 https://github.com/rust-lang/rust/pull/92908#discussion_r785191758 for an example of when this would be useful. r? `@GuillaumeGomez`
This commit is contained in:
commit
be3d25bd78
@ -401,7 +401,7 @@ def get_tree_count(tree, path):
|
|||||||
return len(tree.findall(path))
|
return len(tree.findall(path))
|
||||||
|
|
||||||
|
|
||||||
def check_snapshot(snapshot_name, tree):
|
def check_snapshot(snapshot_name, tree, normalize_to_text):
|
||||||
assert rust_test_path.endswith('.rs')
|
assert rust_test_path.endswith('.rs')
|
||||||
snapshot_path = '{}.{}.{}'.format(rust_test_path[:-3], snapshot_name, 'html')
|
snapshot_path = '{}.{}.{}'.format(rust_test_path[:-3], snapshot_name, 'html')
|
||||||
try:
|
try:
|
||||||
@ -413,7 +413,10 @@ def check_snapshot(snapshot_name, tree):
|
|||||||
else:
|
else:
|
||||||
raise FailedCheck('No saved snapshot value')
|
raise FailedCheck('No saved snapshot value')
|
||||||
|
|
||||||
actual_str = ET.tostring(tree).decode('utf-8')
|
if not normalize_to_text:
|
||||||
|
actual_str = ET.tostring(tree).decode('utf-8')
|
||||||
|
else:
|
||||||
|
actual_str = flatten(tree)
|
||||||
|
|
||||||
if expected_str != actual_str:
|
if expected_str != actual_str:
|
||||||
if bless:
|
if bless:
|
||||||
@ -494,11 +497,16 @@ def check_command(c, cache):
|
|||||||
[snapshot_name, html_path, pattern] = c.args
|
[snapshot_name, html_path, pattern] = c.args
|
||||||
tree = cache.get_tree(html_path)
|
tree = cache.get_tree(html_path)
|
||||||
xpath = normalize_xpath(pattern)
|
xpath = normalize_xpath(pattern)
|
||||||
|
normalize_to_text = False
|
||||||
|
if xpath.endswith('/text()'):
|
||||||
|
xpath = xpath[:-7]
|
||||||
|
normalize_to_text = True
|
||||||
|
|
||||||
subtrees = tree.findall(xpath)
|
subtrees = tree.findall(xpath)
|
||||||
if len(subtrees) == 1:
|
if len(subtrees) == 1:
|
||||||
[subtree] = subtrees
|
[subtree] = subtrees
|
||||||
try:
|
try:
|
||||||
check_snapshot(snapshot_name, subtree)
|
check_snapshot(snapshot_name, subtree, normalize_to_text)
|
||||||
ret = True
|
ret = True
|
||||||
except FailedCheck as err:
|
except FailedCheck as err:
|
||||||
cerr = str(err)
|
cerr = str(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user