1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for env_setup.environment.
15
16This tests the error-checking, context manager, and written environment scripts
17of the Environment class.
18
19Tests that end in "_ctx" modify the environment and validate it in-process.
20
21Tests that end in "_written" write the environment to a file intended to be
22evaluated by the shell, then launches the shell and then saves the environment.
23This environment is then validated in the test process.
24"""
25
26import json
27import unittest
28
29import six
30
31from pw_env_setup import environment, json_visitor
32
33
34# pylint: disable=super-with-arguments
35class JSONVisitorTest(unittest.TestCase):
36    """Tests for env_setup.json_visitor."""
37    def setUp(self):
38        self.env = environment.Environment()
39
40    def _write_and_parse_json(self):
41        buf = six.StringIO()
42        json_visitor.JSONVisitor(self.env, buf)
43        return json.loads(buf.getvalue())
44
45    def _assert_json(self, value):
46        self.assertEqual(self._write_and_parse_json(), value)
47
48    def test_set(self):
49        self.env.clear('VAR')
50        self.env.set('VAR', '1')
51        self._assert_json({'set': {'VAR': '1'}})
52
53    def test_clear(self):
54        self.env.set('VAR', '1')
55        self.env.clear('VAR')
56        self._assert_json({'set': {'VAR': None}})
57
58    def test_append(self):
59        self.env.append('VAR', 'path1')
60        self.env.append('VAR', 'path2')
61        self.env.append('VAR', 'path3')
62        self._assert_json(
63            {'modify': {
64                'VAR': {
65                    'append': 'path1 path2 path3'.split()
66                }
67            }})
68
69    def test_prepend(self):
70        self.env.prepend('VAR', 'path1')
71        self.env.prepend('VAR', 'path2')
72        self.env.prepend('VAR', 'path3')
73        self._assert_json(
74            {'modify': {
75                'VAR': {
76                    'prepend': 'path3 path2 path1'.split()
77                }
78            }})
79
80    def test_remove(self):
81        self.env.remove('VAR', 'path1')
82        self._assert_json({'modify': {'VAR': {'remove': ['path1']}}})
83
84    def test_echo(self):
85        self.env.echo('echo')
86        self._assert_json({})
87
88    def test_comment(self):
89        self.env.comment('comment')
90        self._assert_json({})
91
92    def test_command(self):
93        self.env.command('command')
94        self._assert_json({})
95
96    def test_doctor(self):
97        self.env.doctor()
98        self._assert_json({})
99
100    def test_function(self):
101        self.env.function('name', 'body')
102        self._assert_json({})
103