File size: 1,610 Bytes
66ee87e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Every release gets its own asset URLs, so a browser can never run a stale app.js or app.css from an older release."""
import unittest
from pathlib import Path

from app.assets import stamp_assets

INDEX = Path(__file__).resolve().parent.parent / "app" / "static" / "index.html"


class StampAssetsTest(unittest.TestCase):
    def test_script_and_stylesheet_get_the_version(self):
        html = '<link rel="stylesheet" href="/static/app.css"><script src="/static/app.js"></script>'
        self.assertEqual(stamp_assets(html, "1.9.2"),
                         '<link rel="stylesheet" href="/static/app.css?v=1.9.2"><script src="/static/app.js?v=1.9.2"></script>')

    def test_images_and_other_urls_get_it_too(self):
        self.assertEqual(stamp_assets('<img src="/static/logo.png">', "2"), '<img src="/static/logo.png?v=2">')

    def test_links_to_other_sites_are_untouched(self):
        html = '<a href="https://huggingface.co/x">x</a><a href="#setup">s</a>'
        self.assertEqual(stamp_assets(html, "2"), html)

    def test_version_is_url_safe(self):
        self.assertEqual(stamp_assets('<script src="/static/app.js"></script>', "1.9.2 beta/1"),
                         '<script src="/static/app.js?v=1.9.2-beta-1"></script>')

    def test_the_real_page_references_only_stamped_assets(self):
        stamped = stamp_assets(INDEX.read_text(encoding="utf-8"), "9.9.9")
        self.assertIn('/static/app.js?v=9.9.9"', stamped)
        self.assertIn('/static/app.css?v=9.9.9"', stamped)
        self.assertNotRegex(stamped, r'/static/[^"?]+"')


if __name__ == "__main__":
    unittest.main()