Last active
August 29, 2015 14:24
-
-
Save syabro/ab074d39af6bb812d2d9 to your computer and use it in GitHub Desktop.
Test static files links existing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestStaticLinks(UnitTestCase): | |
def test_links(self): | |
command = 'grep -nr "{{ STATIC_URL }}" %s --include \\*.html' % settings.PROJECT_ROOT | |
output = os.popen(command).read() | |
missed_files = [] | |
for line in output.split('\n'): | |
if "EXCLUDE_STATIC_CHECK" in line: | |
continue | |
line = line.strip() | |
if not line: | |
continue | |
filename = ":".join(line.split(':')[:2]) | |
html = ":".join(line.split(':')[2:]).strip() | |
# Split from {{ STATIC_URL }} till " | |
script_path = html.split("{{ STATIC_URL }}")[1] | |
if len(script_path.split('"')): | |
script_path = script_path.split('"')[0] | |
# Sometimes it's CSS | |
# background: transparent url({{ STATIC_URL }}img/strike.png) | |
if len(script_path.split(')')): | |
script_path = script_path.split(')')[0].rstrip(';') | |
script_path = script_path.replace("?{{ REVISION }}", "").strip().lstrip('/') | |
if not script_path: | |
continue | |
result = finders.find(script_path) | |
if result is None: | |
missed_files.append([filename, html, script_path]) | |
if missed_files: | |
text = ["Found unexisting static files"] | |
for missed_file in missed_files: | |
text.append("") | |
text.append("FILENAME:" + missed_file[0]) | |
text.append("LINE: " + missed_file[1]) | |
text.append("PATH: " + missed_file[2]) | |
text = "\n".join(text) | |
raise Exception(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment