Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#973 Add an html validator #1020

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pipeline:
- python manage.py excel
- python manage.py price
- python manage.py collectstatic --noinput
- python manage.py test --parallel --tag fast -k -v 3
- python manage.py test --tag fast -k -v 3

slow-test:
<<: *test
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ python-telegram-bot==11.1.0
sentry-sdk==0.7.2
https://github.com/selwin/django-user_agents/archive/master.zip
https://github.com/fidals/refarm-site/archive/0.7.2.zip
html5validate
html5lib
3 changes: 2 additions & 1 deletion shopelectro/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@
ROOT_URLCONF = 'shopelectro.urls'

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATE_ASSETS_DIR = os.path.join(BASE_DIR, 'shopelectro/tests/assets')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'DIRS': [TEMPLATE_DIR, TEMPLATE_ASSETS_DIR],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

11

'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down
3 changes: 3 additions & 0 deletions shopelectro/tests/assets/invalid_markup_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<body>
<funny>joke</funny>
12 changes: 12 additions & 0 deletions shopelectro/tests/assets/valid_markup_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<!-- page content -->
</body>
</html>
36 changes: 36 additions & 0 deletions shopelectro/tests/tests_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import unittest

from django.test import TestCase, tag
from django.conf import settings
from django.template.loader import render_to_string

from html5validate import validate


@tag('fast')
class TemplateTests(TestCase):

@unittest.skip('should fix html templates')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need PDD subtask there to fix every html file in templates

def test_templates(self):
for dir, _, filenames in os.walk(settings.TEMPLATE_DIR):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settings.TEMPLATE_DIR is redundant option, because we'll not redefine it for different envs and django extensions. You can move it to the module level const or just hardcode there.
We try to keep django settings files as small as possible to reduce problems with tuning prod/local envs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean TEMPLATE_ASSETS_DIR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olegush , y, i meant it, sorry

for filename in filenames:
filepath = os.path.join(dir, filename)
filename, file_ext = os.path.splitext(filepath)
if file_ext == '.html':
validate(render_to_string(filepath))

def test_valid_example(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also should test not valid html

filepath = os.path.join(
settings.TEMPLATE_ASSETS_DIR,
'valid_markup_example.html'
)
validate(render_to_string(filepath))

@unittest.expectedFailure
Copy link
Contributor

@duker33 duker33 Dec 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assertRaises instead of expectedFailure. The last one should be used only for tests that should be fixed in the future.

It should look like this:

        with self.assertRaises(TypeError):
            s.split(2)

def test_invalid_example(self):
filepath = os.path.join(
settings.TEMPLATE_ASSETS_DIR,
'invalid_markup_example.html'
)
validate(render_to_string(filepath))