summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/util/spack_yaml.py
blob: ed7ba789c06cedea34fbcebd8fede9ba89629e3d (plain) (blame)
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
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import re

import spack.config
from spack.main import SpackCommand

config_cmd = SpackCommand('config')


def get_config_line(pattern, lines):
    """Get a configuration line that matches a particular pattern."""
    line = next((l for l in lines if re.search(pattern, l)), None)
    assert line is not None, 'no such line!'
    return line


def check_blame(element, file_name, line=None):
    """Check that `config blame config` gets right file/line for an element.

    This runs `spack config blame config` and scrapes the output for a
    particular YAML key. It thne checks that the requested file/line info
    is also on that line.

    Line is optional; if it is ``None`` we just check for the
    ``file_name``, which may just be a name for a special config scope
    like ``_builtin`` or ``command_line``.
    """
    output = config_cmd('blame', 'config')

    blame_lines = output.rstrip().split('\n')
    element_line = get_config_line(element + ':', blame_lines)

    annotation = file_name
    if line is not None:
        annotation += ':%d' % line

    assert file_name in element_line


def test_config_blame(config):
    """check blame info for elements in mock configuration."""
    config_file = config.get_config_filename('site', 'config')

    check_blame('install_tree', config_file, 2)
    check_blame('source_cache', config_file, 11)
    check_blame('misc_cache', config_file, 12)
    check_blame('verify_ssl', config_file, 13)
    check_blame('checksum', config_file, 14)
    check_blame('dirty', config_file, 15)


def test_config_blame_with_override(config):
    """check blame for an element from an override scope"""
    config_file = config.get_config_filename('site', 'config')

    with spack.config.override('config:install_tree', 'foobar'):
        check_blame('install_tree', 'overrides')

        check_blame('source_cache', config_file, 11)
        check_blame('misc_cache', config_file, 12)
        check_blame('verify_ssl', config_file, 13)
        check_blame('checksum', config_file, 14)
        check_blame('dirty', config_file, 15)