Debian Codegraph @

autopage/tests/test_pager_settings.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import unittest
14
15 import fixtures # type: ignore
16
17 from typing import Any, Optional, Dict, List
18
19 import autopage
20 from autopage import command
21
22
23 _PagerConfig = command.PagerConfig
24
25
26 class ConfigTest(unittest.TestCase):
27 def setUp(self) -> None:
28 class TestCommand(command.PagerCommand):
29 def __init__(self) -> None:
30 self.config: Optional[_PagerConfig] = None
31
32 def command(self) -> List[str]:
33 return []
34
35 def environment_variables(
36 self,
37 config: _PagerConfig) -> Optional[Dict[str, str]]:
38 self.config = config
39 return None
40
41 self.test_command = TestCommand()
42
43 def _get_ap_config(self, **args: Any) -> command.PagerConfig:
44 ap = autopage.AutoPager(pager_command=self.test_command, **args)
45 ap._pager_env()
46 config = self.test_command.config
47 assert config is not None
48 return config
49
50 def test_defaults(self) -> None:
51 config = self._get_ap_config()
52 self.assertTrue(config.color)
53 self.assertFalse(config.line_buffering_requested)
54 self.assertFalse(config.reset_terminal)
55
56 def test_nocolor(self) -> None:
57 config = self._get_ap_config(allow_color=False)
58 self.assertFalse(config.color)
59 self.assertFalse(config.line_buffering_requested)
60 self.assertFalse(config.reset_terminal)
61
62 def test_reset(self) -> None:
63 config = self._get_ap_config(reset_on_exit=True)
64 self.assertTrue(config.color)
65 self.assertFalse(config.line_buffering_requested)
66 self.assertTrue(config.reset_terminal)
67
68 def test_linebuffered(self) -> None:
69 config = self._get_ap_config(line_buffering=True)
70 self.assertTrue(config.color)
71 self.assertTrue(config.line_buffering_requested)
72 self.assertFalse(config.reset_terminal)
73
74 def test_not_linebuffered(self) -> None:
75 config = self._get_ap_config(line_buffering=False)
76 self.assertTrue(config.color)
77 self.assertFalse(config.line_buffering_requested)
78 self.assertFalse(config.reset_terminal)
79
80
81 class EnvironmentBuildTest(unittest.TestCase):
82 class TestCommand(command.PagerCommand):
83 def __init__(self, env: Optional[Dict[str, str]]):
84 self._env = env
85
86 def command(self) -> List[str]:
87 return ['foo']
88
89 def environment_variables(self,
90 config: _PagerConfig) -> Optional[Dict[str,
91 str]]:
92 return self._env
93
94 def test_env(self) -> None:
95 cmd = self.TestCommand({"FOO": "bar"})
96 ap = autopage.AutoPager(pager_command=cmd)
97 with fixtures.EnvironmentVariable('BAZ', 'quux'):
98 env = ap._pager_env()
99 self.assertIsNotNone(env)
100 assert env is not None
101 self.assertEqual('quux', env['BAZ'])
102 self.assertEqual('bar', env['FOO'])
103
104 def test_env_empty(self) -> None:
105 cmd = self.TestCommand({})
106 ap = autopage.AutoPager(pager_command=cmd)
107 with fixtures.EnvironmentVariable('BAZ', 'quux'):
108 env = ap._pager_env()
109 self.assertIsNone(env)
110
111 def test_env_none(self) -> None:
112 cmd = self.TestCommand(None)
113 ap = autopage.AutoPager(pager_command=cmd)
114 with fixtures.EnvironmentVariable('BAZ', 'quux'):
115 env = ap._pager_env()
116 self.assertIsNone(env)