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


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_env(self):
        env = EnvironmentModifications()
        env.set_env('A', 'dummy value')
        env.set_env('B', 3)
        apply_environment_modifications(env)
        self.assertEqual('dummy value', os.environ['A'])
        self.assertEqual(str(3), os.environ['B'])

    def test_unset_env(self):
        env = EnvironmentModifications()
        self.assertEqual('foo', os.environ['UNSET_ME'])
        env.unset_env('UNSET_ME')
        apply_environment_modifications(env)
        self.assertRaises(KeyError, os.environ.__getitem__, 'UNSET_ME')

    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/')

        apply_environment_modifications(env)
        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_env('A', 'dummy value', who='Pkg1')
        apply_environment_modifications(env)
        self.assertEqual('dummy value', os.environ['A'])

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