summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/common/arguments.py
blob: 76e39bcad0c005d7e18237e53bbf2afef14b3246 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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 argparse
import multiprocessing

import pytest


import spack.cmd
import spack.cmd.common.arguments as arguments
import spack.config


@pytest.fixture()
def parser():
    p = argparse.ArgumentParser()
    arguments.add_common_arguments(p, ['jobs'])
    yield p
    # Cleanup the command line scope if it was set during tests
    spack.config.config.clear_caches()
    if 'command_line' in spack.config.config.scopes:
        spack.config.config.scopes['command_line'].clear()


@pytest.fixture(params=[1, 2, 4, 8, 16, 32])
def ncores(monkeypatch, request):
    """Mocks having a machine with n cores for the purpose of
    computing config:build_jobs.
    """
    def _cpu_count():
        return request.param

    # Patch multiprocessing.cpu_count() to return the value we need
    monkeypatch.setattr(multiprocessing, 'cpu_count', _cpu_count)
    # Patch the configuration parts that have been cached already
    monkeypatch.setitem(spack.config.config_defaults['config'],
                        'build_jobs', min(16, request.param))
    monkeypatch.setitem(
        spack.config.config.scopes, '_builtin',
        spack.config.InternalConfigScope(
            '_builtin', spack.config.config_defaults
        ))
    return request.param


@pytest.mark.parametrize('cli_args,requested', [
    (['-j', '24'], 24),
    # Here we report the default if we have enough cores, as the cap
    # on the available number of cores will be taken care of in the test
    ([], 16)
])
def test_setting_parallel_jobs(parser, cli_args, ncores, requested):
    expected = min(requested, ncores)
    namespace = parser.parse_args(cli_args)
    assert namespace.jobs == expected
    assert spack.config.get('config:build_jobs') == expected


def test_negative_integers_not_allowed_for_parallel_jobs(parser):
    with pytest.raises(ValueError) as exc_info:
        parser.parse_args(['-j', '-2'])

    assert 'expected a positive integer' in str(exc_info.value)


@pytest.mark.parametrize('specs,expected_variants,unexpected_variants', [
    (['coreutils', 'cflags=-O3 -g'], [], ['g']),
    (['coreutils', 'cflags=-O3', '-g'], ['g'], []),
])
@pytest.mark.regression('12951')
def test_parse_spec_flags_with_spaces(
        specs, expected_variants, unexpected_variants
):
    spec_list = spack.cmd.parse_specs(specs)
    assert len(spec_list) == 1

    s = spec_list.pop()

    assert all(x not in s.variants for x in unexpected_variants)
    assert all(x in s.variants for x in expected_variants)