Skip to content

Commit

Permalink
fix: log error on waffle name with blank space
Browse files Browse the repository at this point in the history
This should help platform identify faulty waffle names.

Close #389.
  • Loading branch information
regisb committed Feb 25, 2025
1 parent cbaf5a7 commit 581bd61
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Change Log
Unreleased
~~~~~~~~~~

* Log error on waffle object creation when name includes a blank space as a prefix or suffix.

[5.3.0] - 2025-02-14
--------------------

Expand Down
24 changes: 22 additions & 2 deletions edx_toggles/tests/test_waffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
"""

from django.test import TestCase

from edx_toggles.toggles import NonNamespacedWaffleFlag, NonNamespacedWaffleSwitch, WaffleFlag, WaffleSwitch
from unittest.mock import patch

from edx_toggles.toggles import (
NonNamespacedWaffleFlag,
NonNamespacedWaffleSwitch,
WaffleFlag,
WaffleSwitch,
)
from edx_toggles.toggles.internal.waffle.base import BaseWaffle
from edx_toggles.toggles.internal.waffle.base import logger as base_logger


class NaiveWaffle(BaseWaffle):
"""
Simple waffle class that implements a basic instance-tracking mechanism
"""

_class_instances = set()

def is_enabled(self):
Expand All @@ -29,6 +37,18 @@ def test_constructor(self):
self.assertEqual("module1", waffle.module_name)
self.assertEqual(1, len(NaiveWaffle.get_instances()))

def test_no_blank_space_in_name(self):
with patch.object(base_logger, "error") as mock_logger_error:
NaiveWaffle("namespaced.name ", "module")
mock_logger_error.assert_called_with(
"NaiveWaffle instance name should not include a blank space prefix or suffix: 'namespaced.name '"
)
with patch.object(base_logger, "error") as mock_logger_error:
NaiveWaffle(" namespaced.name", "module")
mock_logger_error.assert_called_with(
"NaiveWaffle instance name should not include a blank space prefix or suffix: ' namespaced.name'"
)


class WaffleFlagTests(TestCase):
"""
Expand Down
12 changes: 9 additions & 3 deletions edx_toggles/toggles/internal/waffle/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
Base waffle toggle classes.
"""

import logging

from ..base import BaseToggle

logger = logging.getLogger(__name__)


class BaseWaffle(BaseToggle):
"""
Expand All @@ -30,7 +34,9 @@ def validate_name(cls, name):
"""
if "." not in name:
raise ValueError(
"Cannot create non-namespaced '{}' {} instance".format(
name, cls.__name__
)
f"Cannot create non-namespaced '{name}' {cls.__name__} instance"
)
if name.startswith(" ") or name.endswith(" "):
logger.error(
f"{cls.__name__} instance name should not include a blank space prefix or suffix: '{name}'"
)

0 comments on commit 581bd61

Please sign in to comment.