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

Add central interface for defining feature flags #640

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 50 additions & 0 deletions colcon_core/feature_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import os

from colcon_core.environment_variable import EnvironmentVariable
from colcon_core.logging import colcon_logger

logger = colcon_logger.getChild(__name__)

"""Environment variable to enable feature flags"""
FEATURE_FLAGS_ENVIRONMENT_VARIABLE = EnvironmentVariable(
'COLCON_FEATURE_FLAGS',
'Enable pre-production features and behaviors')

_REPORTED_USES = set()


def get_feature_flags():
"""
Retrieve all enabled feature flags.

:returns: List of enabled flags
:rtype: list
"""
return [
flag for flag in (
os.environ.get(FEATURE_FLAGS_ENVIRONMENT_VARIABLE.name) or ''
).split(os.pathsep) if flag
]


def is_feature_flag_set(flag):
"""
Determine if a specific feature flag is enabled.

Feature flags are case-sensitive and separated by the os-specific path
separator character.

:param str flag: Name of the flag to search for

:returns: True if the flag is set
:rtype: bool
"""
if flag in get_feature_flags():
if flag not in _REPORTED_USES:
logger.warning(f'Enabling feature: {flag}')
nuclearsandwich marked this conversation as resolved.
Show resolved Hide resolved
_REPORTED_USES.add(flag)
return True
return False
4 changes: 4 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
addfinalizer
addopts
apache
argparse
Expand Down Expand Up @@ -35,8 +36,10 @@ docstring
executables
exitstatus
fdopen
ffoo
filterwarnings
foobar
fooo
fromhex
functools
getcategory
Expand Down Expand Up @@ -139,5 +142,6 @@ unittest
unittests
unlinking
unrenamed
usefixtures
wildcards
workaround
71 changes: 71 additions & 0 deletions test/test_feature_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import os
from unittest.mock import patch

from colcon_core.feature_flags import FEATURE_FLAGS_ENVIRONMENT_VARIABLE
from colcon_core.feature_flags import get_feature_flags
from colcon_core.feature_flags import is_feature_flag_set
import pytest


_FLAGS_TO_TEST = (
('foo',),
('foo', 'foo'),
('foo', ''),
('', 'foo'),
('', 'foo', ''),
('foo', 'bar'),
('bar', 'foo'),
('bar', 'foo', 'baz'),
)


@pytest.fixture
def feature_flags_value(request):
env = dict(os.environ)
if request.param is not None:
env[FEATURE_FLAGS_ENVIRONMENT_VARIABLE.name] = os.pathsep.join(
request.param)
else:
env.pop(FEATURE_FLAGS_ENVIRONMENT_VARIABLE.name, None)

mock_env = patch('colcon_core.feature_flags.os.environ', env)
request.addfinalizer(mock_env.stop)
mock_env.start()
return request.param


@pytest.mark.parametrize(
'feature_flags_value',
_FLAGS_TO_TEST,
indirect=True)
@pytest.mark.usefixtures('feature_flags_value')
def test_flag_is_set():
assert is_feature_flag_set('foo')


@pytest.mark.parametrize(
'feature_flags_value',
(None, *_FLAGS_TO_TEST),
indirect=True)
@pytest.mark.usefixtures('feature_flags_value')
def test_flag_not_set():
assert not is_feature_flag_set('')
assert not is_feature_flag_set('fo')
assert not is_feature_flag_set('oo')
assert not is_feature_flag_set('fooo')
assert not is_feature_flag_set('ffoo')
assert not is_feature_flag_set('qux')


@pytest.mark.parametrize(
'feature_flags_value',
(None, *_FLAGS_TO_TEST),
indirect=True)
@pytest.mark.usefixtures('feature_flags_value')
def test_get_flags(feature_flags_value):
assert [
flag for flag in (feature_flags_value or ()) if flag
] == get_feature_flags()
Loading