summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/environment.py
blob: 6c8f5ea43c3a2ec349d0bd48d37f6d0ffc42abb3 (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
import unittest
import os
from spack.environment import EnvironmentModifications


class EnvironmentTest(unittest.TestCase):
    def setUp(self):
        os.environ.clear()
        os.environ['UNSET_ME'] = 'foo'
        os.environ['EMPTY_PATH_LIST'] = ''
        os.environ['PATH_LIST'] = '/path/second:/path/third'
        os.environ['REMOVE_PATH_LIST'] = '/a/b:/duplicate:/a/c:/remove/this:/a/d:/duplicate/:/f/g'

    def test_set(self):
        env = EnvironmentModifications()
        env.set('A', 'dummy value')
        env.set('B', 3)
        env.apply_modifications()
        self.assertEqual('dummy value', os.environ['A'])
        self.assertEqual(str(3), os.environ['B'])

    def test_unset(self):
        env = EnvironmentModifications()
        self.assertEqual('foo', os.environ['UNSET_ME'])
        env.unset('UNSET_ME')
        env.apply_modifications()
        self.assertRaises(KeyError, os.environ.__getitem__, 'UNSET_ME')

    def test_set_path(self):
        env = EnvironmentModifications()
        env.set_path('A', ['foo', 'bar', 'baz'])
        env.apply_modifications()
        self.assertEqual('foo:bar:baz', os.environ['A'])

    def test_path_manipulation(self):
        env = EnvironmentModifications()

        env.append_path('PATH_LIST', '/path/last')
        env.prepend_path('PATH_LIST', '/path/first')

        env.append_path('EMPTY_PATH_LIST', '/path/middle')
        env.append_path('EMPTY_PATH_LIST', '/path/last')
        env.prepend_path('EMPTY_PATH_LIST', '/path/first')

        env.append_path('NEWLY_CREATED_PATH_LIST', '/path/middle')
        env.append_path('NEWLY_CREATED_PATH_LIST', '/path/last')
        env.prepend_path('NEWLY_CREATED_PATH_LIST', '/path/first')

        env.remove_path('REMOVE_PATH_LIST', '/remove/this')
        env.remove_path('REMOVE_PATH_LIST', '/duplicate/')

        env.apply_modifications()
        self.assertEqual('/path/first:/path/second:/path/third:/path/last', os.environ['PATH_LIST'])
        self.assertEqual('/path/first:/path/middle:/path/last', os.environ['EMPTY_PATH_LIST'])
        self.assertEqual('/path/first:/path/middle:/path/last', os.environ['NEWLY_CREATED_PATH_LIST'])
        self.assertEqual('/a/b:/a/c:/a/d:/f/g', os.environ['REMOVE_PATH_LIST'])

    def test_extra_arguments(self):
        env = EnvironmentModifications()
        env.set('A', 'dummy value', who='Pkg1')
        for x in env:
            assert 'who' in x.args
        env.apply_modifications()
        self.assertEqual('dummy value', os.environ['A'])

    def test_extend(self):
        env = EnvironmentModifications()
        env.set('A', 'dummy value')
        env.set('B', 3)
        copy_construct = EnvironmentModifications(env)
        self.assertEqual(len(copy_construct), 2)
        for x, y in zip(env, copy_construct):
            assert x is y