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

LGA-3267: Add Ruff Format to the pipeline #25

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 8 additions & 3 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ jobs:
name: Ruff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
- name: Checkout
uses: actions/checkout@v4
- name: Lint
uses: chartboost/ruff-action@v1
with:
args: check --output-format=github
src: './src'
- name: Format
uses: chartboost/ruff-action@v1
with:
args: format --check

sonarcloud:
name: Sonar cloud
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
files: requirements-(base|production).in
args: [requirements/source/requirements-production.in, "--output-file", requirements/generated/requirements-production.txt]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.4
rev: v0.6.2
hooks:
- id: ruff
args: [ --fix ]
Expand Down
4 changes: 1 addition & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def create_app(config_class=Config):
)
# Concat all headscripts seperately so they can be loaded into the DOM head.
headscripts = Bundle(
"src/js/headscripts/*.js",
filters="jsmin",
output="dist/js/headscripts.min.js"
"src/js/headscripts/*.js", filters="jsmin", output="dist/js/headscripts.min.js"
)
if "css" not in assets:
assets.register("css", css)
Expand Down
2 changes: 1 addition & 1 deletion app/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
bp = Blueprint("main", __name__, template_folder="../templates/main")

from app.main import routes # noqa: E402,F401
from app.main import filters
from app.main import filters # noqa: E402,F401
4 changes: 2 additions & 2 deletions app/main/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@bp.app_template_filter("dict")
def str_to_dict(s):
""" Uses the JSON module to parse a string into a dict.
"""Uses the JSON module to parse a string into a dict.
This can be invoked in Jinja templates using:
{{ '{"spam": "eggs"}' | dict }}

Expand All @@ -16,7 +16,7 @@ def str_to_dict(s):

@bp.app_template_filter("get_item")
def get_item_from_dict(d, s):
""" Gets an item from a dict safely returning None if it is not present
"""Gets an item from a dict safely returning None if it is not present
This can be invoked in Jinja templates using:
{{ '{"spam": "eggs"}' | dict | get_item("spam") }}

Expand Down
4 changes: 3 additions & 1 deletion app/main/gtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ def detect_gtm_anon_id():
def remember_gtm_anon_id(response):
session["gtm_anon_id"] = str(uuid.uuid4())
expiration_date = datetime.now(UTC) + timedelta(days=30)
response.set_cookie("gtm_anon_id", session.get("gtm_anon_id"), expires=expiration_date)
response.set_cookie(
"gtm_anon_id", session.get("gtm_anon_id"), expires=expiration_date
)
return response
21 changes: 21 additions & 0 deletions app/templates/components/gtm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{#
This component enables Google Tag Manager.
It should be included in the base template if the user has enabled tracking cookies.
#}

{% set gtm_measurement_id = 'G-S8M7DQZ6N0' %}

<script nonce="{{ csp_nonce() }}" async src="https://www.googletagmanager.com/gtag/js?id={{ gtm_measurement_id }}"></script>
<script nonce="{{ csp_nonce() }}">
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());

gtag('config', '{{ gtm_measurement_id }}');

{% if gtm_anon_id %}
window.dataLayer.push({ user_id: "{{ gtm_anon_id }}" });
{% endif %}
</script>
2 changes: 1 addition & 1 deletion requirements/generated/requirements-development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ requests==2.32.3
# pytest-base-url
rich==13.7.1
# via flask-limiter
ruff==0.5.4
ruff==0.6.2
# via -r requirements/source/requirements-development.in
sentry-sdk[flask]==2.10.0
# via -r requirements/source/requirements-base.in
Expand Down
4 changes: 3 additions & 1 deletion tests/functional_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from app import create_app
from flask import url_for


class TestConfig(Config):
TESTING = True
DEBUG = True
SERVER_NAME = "localhost"
RATELIMIT_ENABLED = False
SECRET_KEY = "TEST_KEY"


@pytest.fixture(scope="session")
def app():
app = create_app(TestConfig)
Expand All @@ -28,4 +30,4 @@ def runner(app):

@pytest.fixture(scope="function", autouse=True)
def startup(app, page):
page.goto(url_for('main.index', _external=True))
page.goto(url_for("main.index", _external=True))
18 changes: 10 additions & 8 deletions tests/functional_tests/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

ACCESSIBILITY_STANDARDS = ["wcag2a", "wcag2aa"]


def check_accessibility(page: Page):
'''
"""
Inserts axe core python into a page at the yield step
to run accessibility based testing. Axe will run on
the page defined in the function.
'''
"""
if page.title() != "localhost":
directory = "tests/functional_tests/accessibility_output"
if not os.path.exists(directory):
Expand All @@ -24,16 +25,16 @@ def check_accessibility(page: Page):

wcag_violations = []

for violation in results['violations']:
for violation in results["violations"]:
if set(violation["tags"]) & set(ACCESSIBILITY_STANDARDS):
wcag_violations.append(violation)

if len(wcag_violations) == 0:
assert "No WCAG accessibility issues found"
else:
# Cleans the URL to remove any invalid characters and replace with _
invalid_filename_chars = r'[\/:*?"<>|]'
sanitized_title = re.sub(invalid_filename_chars, '_', page.title())
sanitized_title = re.sub(invalid_filename_chars, "_", page.title())

max_title_len = 30
file_name = f"axe_results_{sanitized_title[:max_title_len]}.json"
Expand All @@ -44,17 +45,18 @@ def check_accessibility(page: Page):

@pytest.mark.usefixtures("live_server")
def test_all_page_accessibility(app, page: Page):
ignored_routes = ['static', '/']
ignored_routes = ["static", "/"]
routes = app.view_functions
for route in routes:
if route not in ignored_routes:
full_url = url_for(route, _external=True)
page.goto(full_url)
check_accessibility(page)


def test_accessibility_folder():
path = 'tests/functional_tests/accessibility_output'
path = "tests/functional_tests/accessibility_output"
if not any(os.scandir(path)):
assert True
else:
assert not 'WCAG accessibility issues found'
assert not "WCAG accessibility issues found"
3 changes: 2 additions & 1 deletion tests/functional_tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from playwright.sync_api import Page
import pytest


@pytest.mark.usefixtures("live_server")
def test_base_accessibility(page: Page):
pass
pass
2 changes: 1 addition & 1 deletion tests/unit_tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_coverage():
# Test Pytest coverage
assert 1 + 1 == 2
pass
pass
Loading