"""Tests for bin/inject-hashed-filenames.py rewrite() (#3). Pinned behaviour: the rewrite is tag-aware — only `' out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 1 assert out == '' def test_rewrites_script_src_single_quoted(): html = "" out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 1 assert "/static/dist/app.abc123.js" in out def test_rewrites_link_href(): html = '' out, n = ihf.rewrite(html, "/static/dist/app.css", "/static/dist/app.abc123.css") assert n == 1 assert '/static/dist/app.abc123.css' in out def test_does_not_rewrite_inside_html_comment(): html = '' out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 0 assert "/static/dist/app.js" in out assert "/static/dist/app.abc123.js" not in out def test_does_not_rewrite_inside_json_literal(): html = '
{ "src": "/static/dist/app.js" }
' out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 0 assert out == html def test_does_not_rewrite_unrelated_attribute(): html = '' out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 0 assert out == html def test_does_not_rewrite_anchor_href(): # Even though is a `href` attribute, it isn't a . html = 'debug link' out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 0 assert out == html def test_rewrites_with_extra_attributes_around_src(): html = '' out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") assert n == 1 assert '/static/dist/app.abc123.js' in out assert 'type="module"' in out and 'defer' in out