File: test_feature_flags.py

package info (click to toggle)
ros2-colcon-core 0.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,156 kB
  • sloc: python: 10,333; makefile: 7
file content (106 lines) | stat: -rw-r--r-- 3,397 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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 check_implemented_flags
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.fixture
def feature_flag_reports(request):
    reported_uses = patch('colcon_core.feature_flags._REPORTED_USES', set())
    request.addfinalizer(reported_uses.stop)
    reported_uses.start()
    return reported_uses


@pytest.mark.parametrize(
    'feature_flags_value',
    _FLAGS_TO_TEST,
    indirect=True)
@pytest.mark.usefixtures('feature_flags_value', 'feature_flag_reports')
def test_flag_is_set():
    with patch('colcon_core.feature_flags.logger.warning') as warn:
        assert is_feature_flag_set('foo')
        assert warn.call_count == 2
        assert is_feature_flag_set('foo')
        assert warn.call_count == 2


@pytest.mark.parametrize(
    'feature_flags_value',
    (None, *_FLAGS_TO_TEST),
    indirect=True)
@pytest.mark.usefixtures('feature_flags_value', 'feature_flag_reports')
def test_flag_not_set():
    with patch('colcon_core.feature_flags.logger.warning') as warn:
        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')
        assert warn.call_count == 0


@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()


@pytest.mark.parametrize('feature_flags_value', (('baz',),), indirect=True)
@pytest.mark.usefixtures('feature_flags_value')
def test_implemented():
    with patch('colcon_core.feature_flags.IMPLEMENTED_FLAGS', {'foo'}):
        with patch('colcon_core.feature_flags.logger.warning') as warn:
            assert not is_feature_flag_set('bar')
            assert warn.call_count == 0
            assert is_feature_flag_set('baz')
            assert warn.call_count == 2
            assert is_feature_flag_set('foo')
            assert warn.call_count == 2
            check_implemented_flags()
            assert warn.call_count == 2

    with patch('colcon_core.feature_flags.IMPLEMENTED_FLAGS', {'baz'}):
        with patch('colcon_core.feature_flags.logger.warning') as warn:
            check_implemented_flags()
            assert warn.call_count == 1