[Notes] [Git][BuildStream/buildstream][tiagogomes/issue-287] 2 commits: Add validation of configuration variables



Title: GitLab

Tiago Gomes pushed to branch tiagogomes/issue-287 at BuildStream / buildstream

Commits:

6 changed files:

Changes:

  • buildstream/_loader/loadelement.py
    ... ... @@ -74,6 +74,9 @@ class LoadElement():
    74 74
                 'build-depends', 'runtime-depends',
    
    75 75
             ])
    
    76 76
     
    
    77
    +        # Validate configuration variables
    
    78
    +        _yaml.node_get_configuration_variables(self.node)
    
    79
    +
    
    77 80
             # Extract the Dependencies
    
    78 81
             self.deps = _extract_depends_from_node(self.node)
    
    79 82
     
    

  • buildstream/_project.py
    ... ... @@ -555,6 +555,13 @@ class Project():
    555 555
             output.source_overrides = _yaml.node_get(config, Mapping, 'sources', default_value={})
    
    556 556
             config.pop('elements', None)
    
    557 557
             config.pop('sources', None)
    
    558
    +
    
    559
    +        for _, val in _yaml.node_items(output.element_overrides):
    
    560
    +            _yaml.node_get_configuration_variables(val)
    
    561
    +
    
    562
    +        for _, val in _yaml.node_items(output.source_overrides):
    
    563
    +            _yaml.node_get_configuration_variables(val)
    
    564
    +
    
    558 565
             _yaml.node_final_assertions(config)
    
    559 566
     
    
    560 567
             self._load_plugin_factories(config, output)
    
    ... ... @@ -584,7 +591,7 @@ class Project():
    584 591
             output.options.process_node(config)
    
    585 592
     
    
    586 593
             # Load base variables
    
    587
    -        output.base_variables = _yaml.node_get(config, Mapping, 'variables')
    
    594
    +        output.base_variables = _yaml.node_get_configuration_variables(config)
    
    588 595
     
    
    589 596
             # Add the project name as a default variable
    
    590 597
             output.base_variables['project-name'] = self.name
    

  • buildstream/data/projectconfig.yaml
    ... ... @@ -25,12 +25,7 @@ variables:
    25 25
       # Note: this value defaults to the number of cores available
    
    26 26
       max-jobs: 4
    
    27 27
     
    
    28
    -  # Note: These variables are defined later on in element.py and _project.py
    
    29
    -  element-name: ""
    
    30
    -  project-name: ""
    
    31
    -
    
    32 28
       # Path configuration, to be used in build instructions.
    
    33
    -  #
    
    34 29
       prefix: "/usr"
    
    35 30
       exec_prefix: "%{prefix}"
    
    36 31
       bindir: "%{exec_prefix}/bin"
    
    ... ... @@ -89,6 +84,9 @@ variables:
    89 84
         find "%{install-root}" -name '*.pyc' -exec \
    
    90 85
           dd if=/dev/zero of={} bs=1 count=4 seek=4 conv=notrunc ';'
    
    91 86
     
    
    87
    +  # The following variables are set internally
    
    88
    +  # element-name: ""
    
    89
    +  # project-name: ""
    
    92 90
     
    
    93 91
     # Base sandbox environment, can be overridden by plugins
    
    94 92
     environment:
    

  • tests/loader/variables.py
    1
    +import os
    
    2
    +import pytest
    
    3
    +
    
    4
    +from buildstream import _yaml
    
    5
    +from buildstream._exceptions import ErrorDomain, LoadErrorReason
    
    6
    +from tests.testutils import cli
    
    7
    +
    
    8
    +DATA_DIR = os.path.join(
    
    9
    +    os.path.dirname(os.path.realpath(__file__)),
    
    10
    +    'variables',
    
    11
    +)
    
    12
    +
    
    13
    +PROTECTED_VARIABLES = [('project-name'), ('element-name')]
    
    14
    +
    
    15
    +
    
    16
    +@pytest.mark.parametrize('protected_var', PROTECTED_VARIABLES)
    
    17
    +@pytest.mark.datafiles(DATA_DIR)
    
    18
    +def test_use_of_protected_var_in_element(cli, tmpdir, datafiles, protected_var):
    
    19
    +    project = os.path.join(str(datafiles), 'simple')
    
    20
    +
    
    21
    +    element = {
    
    22
    +        'kind': 'import',
    
    23
    +        'sources': [
    
    24
    +            {
    
    25
    +                'kind': 'local',
    
    26
    +                'path': 'foo.txt'
    
    27
    +            }
    
    28
    +        ],
    
    29
    +        'variables': {
    
    30
    +            protected_var: 'some-value'
    
    31
    +        }
    
    32
    +    }
    
    33
    +    _yaml.dump(element, os.path.join(project, 'target.bst'))
    
    34
    +
    
    35
    +    result = cli.run(project=project, args=['build', 'target.bst'])
    
    36
    +    result.assert_main_error(ErrorDomain.LOAD,
    
    37
    +                             LoadErrorReason.PROTECTED_VARIABLE_REDEFINED)
    
    38
    +
    
    39
    +
    
    40
    +@pytest.mark.parametrize('protected_var', PROTECTED_VARIABLES)
    
    41
    +@pytest.mark.datafiles(DATA_DIR)
    
    42
    +def test_use_of_protected_var_project_conf(cli, tmpdir, datafiles, protected_var):
    
    43
    +    project = os.path.join(str(datafiles), 'simple')
    
    44
    +
    
    45
    +    conf = {
    
    46
    +        'name': 'test',
    
    47
    +        'variables': {
    
    48
    +            protected_var: 'some-value'
    
    49
    +        }
    
    50
    +    }
    
    51
    +    _yaml.dump(conf, os.path.join(project, 'project.conf'))
    
    52
    +
    
    53
    +    result = cli.run(project=project, args=['show'])
    
    54
    +    result.assert_main_error(ErrorDomain.LOAD,
    
    55
    +                             LoadErrorReason.PROTECTED_VARIABLE_REDEFINED)
    
    56
    +
    
    57
    +
    
    58
    +@pytest.mark.parametrize('plugin_type', [
    
    59
    +    ('sources'),
    
    60
    +    ('elements')
    
    61
    +])
    
    62
    +@pytest.mark.parametrize('protected_var', PROTECTED_VARIABLES)
    
    63
    +@pytest.mark.datafiles(DATA_DIR)
    
    64
    +def test_use_of_protected_var_element_overrides(cli, tmpdir, datafiles, plugin_type, protected_var):
    
    65
    +    project = os.path.join(str(datafiles), 'simple')
    
    66
    +
    
    67
    +    conf = {
    
    68
    +        'name': 'test',
    
    69
    +        plugin_type: {
    
    70
    +            'some-plugin-name': {
    
    71
    +                'variables': {
    
    72
    +                    protected_var: 'some-value'
    
    73
    +                }
    
    74
    +            }
    
    75
    +        }
    
    76
    +    }
    
    77
    +    _yaml.dump(conf, os.path.join(project, 'project.conf'))
    
    78
    +
    
    79
    +    result = cli.run(project=project, args=['show'])
    
    80
    +    result.assert_main_error(ErrorDomain.LOAD,
    
    81
    +                             LoadErrorReason.PROTECTED_VARIABLE_REDEFINED)

  • tests/loader/variables/simple/foo.txt
    1
    +foo

  • tests/loader/variables/simple/project.conf
    1
    +name: foo



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]