| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import unittest |
| 14 |
import sys |
| 15 |
|
| 16 |
import fixtures |
| 17 |
|
| 18 |
import typing |
| 19 |
|
| 20 |
from autopage import command |
| 21 |
|
| 22 |
|
| 23 |
class MoreTest(unittest.TestCase): |
| 24 |
def setUp(self) -> None: |
| 25 |
self.cmd = command.More() |
| 26 |
|
| 27 |
def test_cmd(self) -> None: |
| 28 |
self.assertEqual(['more'], self.cmd.command()) |
| 29 |
|
| 30 |
def test_less_env_defaults(self) -> None: |
| 31 |
config = command.PagerConfig(color=True, |
| 32 |
line_buffering_requested=False, |
| 33 |
reset_terminal=False) |
| 34 |
self.assertIsNone(self.cmd.environment_variables(config)) |
| 35 |
|
| 36 |
|
| 37 |
class LessTest(unittest.TestCase): |
| 38 |
def setUp(self) -> None: |
| 39 |
self.cmd: command.PagerCommand = command.Less() |
| 40 |
|
| 41 |
def _env(self, config: command.PagerConfig) -> typing.Dict: |
| 42 |
return self.cmd.environment_variables(config) or {} |
| 43 |
|
| 44 |
def test_cmd(self) -> None: |
| 45 |
self.assertEqual(['less'], self.cmd.command()) |
| 46 |
|
| 47 |
def test_less_env_defaults(self) -> None: |
| 48 |
config = command.PagerConfig(color=True, |
| 49 |
line_buffering_requested=False, |
| 50 |
reset_terminal=False) |
| 51 |
less_env = self._env(config)['LESS'] |
| 52 |
self.assertEqual('RFX', less_env) |
| 53 |
|
| 54 |
def test_less_env_nocolor(self) -> None: |
| 55 |
config = command.PagerConfig(color=False, |
| 56 |
line_buffering_requested=False, |
| 57 |
reset_terminal=False) |
| 58 |
less_env = self._env(config)['LESS'] |
| 59 |
self.assertEqual('FX', less_env) |
| 60 |
|
| 61 |
def test_less_env_reset(self) -> None: |
| 62 |
config = command.PagerConfig(color=True, |
| 63 |
line_buffering_requested=False, |
| 64 |
reset_terminal=True) |
| 65 |
less_env = self._env(config)['LESS'] |
| 66 |
self.assertEqual('R', less_env) |
| 67 |
|
| 68 |
def test_less_env_nocolor_reset(self) -> None: |
| 69 |
config = command.PagerConfig(color=False, |
| 70 |
line_buffering_requested=False, |
| 71 |
reset_terminal=True) |
| 72 |
self.assertNotIn('LESS', self._env(config)) |
| 73 |
|
| 74 |
def test_less_env_linebuffered(self) -> None: |
| 75 |
config = command.PagerConfig(color=True, |
| 76 |
line_buffering_requested=True, |
| 77 |
reset_terminal=False) |
| 78 |
less_env = self._env(config)['LESS'] |
| 79 |
self.assertEqual('RX', less_env) |
| 80 |
|
| 81 |
def test_less_env_linebuffered_reset(self) -> None: |
| 82 |
config = command.PagerConfig(color=True, |
| 83 |
line_buffering_requested=True, |
| 84 |
reset_terminal=True) |
| 85 |
less_env = self._env(config)['LESS'] |
| 86 |
self.assertEqual('R', less_env) |
| 87 |
|
| 88 |
def test_less_env_linebuffered_nocolor(self) -> None: |
| 89 |
config = command.PagerConfig(color=False, |
| 90 |
line_buffering_requested=True, |
| 91 |
reset_terminal=False) |
| 92 |
less_env = self._env(config)['LESS'] |
| 93 |
self.assertEqual('X', less_env) |
| 94 |
|
| 95 |
def test_less_env_linebuffered_nocolor_reset(self) -> None: |
| 96 |
config = command.PagerConfig(color=False, |
| 97 |
line_buffering_requested=True, |
| 98 |
reset_terminal=True) |
| 99 |
self.assertNotIn('LESS', self._env(config)) |
| 100 |
|
| 101 |
def test_less_env_override(self) -> None: |
| 102 |
config = command.PagerConfig(color=True, |
| 103 |
line_buffering_requested=False, |
| 104 |
reset_terminal=False) |
| 105 |
with fixtures.EnvironmentVariable('LESS', 'abc'): |
| 106 |
env = self._env(config) |
| 107 |
self.assertNotIn('LESS', env) |
| 108 |
|
| 109 |
|
| 110 |
class LVTest(unittest.TestCase): |
| 111 |
def setUp(self) -> None: |
| 112 |
self.cmd: command.PagerCommand = command.LV() |
| 113 |
|
| 114 |
def _env(self, config: command.PagerConfig) -> typing.Dict: |
| 115 |
return self.cmd.environment_variables(config) or {} |
| 116 |
|
| 117 |
def test_cmd(self) -> None: |
| 118 |
self.assertEqual(['lv'], self.cmd.command()) |
| 119 |
|
| 120 |
def test_lv_env_defaults(self) -> None: |
| 121 |
config = command.PagerConfig(color=True, |
| 122 |
line_buffering_requested=False, |
| 123 |
reset_terminal=False) |
| 124 |
lv_env = self._env(config)['LV'] |
| 125 |
self.assertEqual('-c', lv_env) |
| 126 |
|
| 127 |
def test_lv_env_nocolor(self) -> None: |
| 128 |
config = command.PagerConfig(color=False, |
| 129 |
line_buffering_requested=False, |
| 130 |
reset_terminal=False) |
| 131 |
self.assertNotIn('LV', self._env(config)) |
| 132 |
|
| 133 |
def test_lv_env_reset(self) -> None: |
| 134 |
config = command.PagerConfig(color=True, |
| 135 |
line_buffering_requested=False, |
| 136 |
reset_terminal=True) |
| 137 |
lv_env = self._env(config)['LV'] |
| 138 |
self.assertEqual('-c', lv_env) |
| 139 |
|
| 140 |
def test_lv_env_nocolor_reset(self) -> None: |
| 141 |
config = command.PagerConfig(color=False, |
| 142 |
line_buffering_requested=False, |
| 143 |
reset_terminal=True) |
| 144 |
self.assertNotIn('LV', self._env(config)) |
| 145 |
|
| 146 |
def test_lv_env_linebuffered(self) -> None: |
| 147 |
config = command.PagerConfig(color=True, |
| 148 |
line_buffering_requested=True, |
| 149 |
reset_terminal=False) |
| 150 |
lv_env = self._env(config)['LV'] |
| 151 |
self.assertEqual('-c', lv_env) |
| 152 |
|
| 153 |
def test_lv_env_linebuffered_reset(self) -> None: |
| 154 |
config = command.PagerConfig(color=True, |
| 155 |
line_buffering_requested=True, |
| 156 |
reset_terminal=True) |
| 157 |
lv_env = self._env(config)['LV'] |
| 158 |
self.assertEqual('-c', lv_env) |
| 159 |
|
| 160 |
def test_lv_env_linebuffered_nocolor(self) -> None: |
| 161 |
config = command.PagerConfig(color=False, |
| 162 |
line_buffering_requested=True, |
| 163 |
reset_terminal=False) |
| 164 |
self.assertNotIn('LV', self._env(config)) |
| 165 |
|
| 166 |
def test_lv_env_linebuffered_nocolor_reset(self) -> None: |
| 167 |
config = command.PagerConfig(color=False, |
| 168 |
line_buffering_requested=True, |
| 169 |
reset_terminal=True) |
| 170 |
self.assertNotIn('LV', self._env(config)) |
| 171 |
|
| 172 |
def test_lv_env_override(self) -> None: |
| 173 |
config = command.PagerConfig(color=True, |
| 174 |
line_buffering_requested=False, |
| 175 |
reset_terminal=False) |
| 176 |
with fixtures.EnvironmentVariable('LV', 'abc'): |
| 177 |
env = self._env(config) |
| 178 |
self.assertNotIn('LV', env) |
| 179 |
|
| 180 |
|
| 181 |
class DefaultTest(LessTest, LVTest): |
| 182 |
def setUp(self) -> None: |
| 183 |
with fixtures.EnvironmentVariable('PAGER', 'less "-r" +F'): |
| 184 |
self.cmd = command.DefaultPager() |
| 185 |
|
| 186 |
def test_cmd(self) -> None: |
| 187 |
self.assertEqual(['less', '-r', '+F'], self.cmd.command()) |
| 188 |
|
| 189 |
def test_default_cmd(self) -> None: |
| 190 |
with fixtures.EnvironmentVariable('PAGER'): |
| 191 |
cmd = command.DefaultPager() |
| 192 |
self.assertEqual(command.PlatformPager().command(), |
| 193 |
cmd.command()) |
| 194 |
|
| 195 |
|
| 196 |
class UserSpecifiedTest(DefaultTest): |
| 197 |
def setUp(self) -> None: |
| 198 |
with fixtures.EnvironmentVariable('FOO_PAGER', 'less "-r" +F'): |
| 199 |
self.cmd = command.UserSpecifiedPager('FOO_PAGER') |
| 200 |
|
| 201 |
def test_env_var_priority_cmd(self) -> None: |
| 202 |
with fixtures.EnvironmentVariable('FOO', 'foo'): |
| 203 |
cmd = command.UserSpecifiedPager('FOO', 'BAR') |
| 204 |
self.assertEqual(['foo'], cmd.command()) |
| 205 |
|
| 206 |
def test_env_var_fallthrough_cmd(self) -> None: |
| 207 |
with fixtures.EnvironmentVariable('BAR', 'bar'): |
| 208 |
cmd = command.UserSpecifiedPager('FOO', 'BAR') |
| 209 |
self.assertEqual(['bar'], cmd.command()) |
| 210 |
|
| 211 |
def test_default_cmd(self) -> None: |
| 212 |
with fixtures.EnvironmentVariable('FOO'): |
| 213 |
with fixtures.EnvironmentVariable('BAR'): |
| 214 |
cmd = command.UserSpecifiedPager('FOO', 'BAR') |
| 215 |
self.assertEqual(command.PlatformPager().command(), |
| 216 |
cmd.command()) |
| 217 |
|
| 218 |
|
| 219 |
class PlatformFixture(fixtures.Fixture): |
| 220 |
def __init__(self, platform: str): |
| 221 |
self.platform = platform |
| 222 |
|
| 223 |
def _setUp(self) -> None: |
| 224 |
self.addCleanup(setattr, sys, 'platform', sys.platform) |
| 225 |
sys.platform = self.platform |
| 226 |
|
| 227 |
|
| 228 |
class PlatformTest(unittest.TestCase): |
| 229 |
def test_aix_cmd(self) -> None: |
| 230 |
with PlatformFixture('aix'): |
| 231 |
cmd = command.PlatformPager() |
| 232 |
self.assertEqual(['more'], cmd.command()) |
| 233 |
|
| 234 |
|
| 235 |
with PlatformFixture('aix7'): |
| 236 |
cmd = command.PlatformPager() |
| 237 |
self.assertEqual(['more'], cmd.command()) |
| 238 |
|
| 239 |
def test_linux_cmd(self) -> None: |
| 240 |
with PlatformFixture('linux'): |
| 241 |
cmd = command.PlatformPager() |
| 242 |
self.assertEqual(['less'], cmd.command()) |
| 243 |
|
| 244 |
def test_win32_cmd(self) -> None: |
| 245 |
with PlatformFixture('win32'): |
| 246 |
cmd = command.PlatformPager() |
| 247 |
self.assertEqual(['more.com'], cmd.command()) |
| 248 |
|
| 249 |
def test_cygwin_cmd(self) -> None: |
| 250 |
with PlatformFixture('cygwin'): |
| 251 |
cmd = command.PlatformPager() |
| 252 |
self.assertEqual(['less'], cmd.command()) |
| 253 |
|
| 254 |
def test_macos_cmd(self) -> None: |
| 255 |
with PlatformFixture('darwin'): |
| 256 |
cmd = command.PlatformPager() |
| 257 |
self.assertEqual(['less'], cmd.command()) |
| 258 |
|
| 259 |
def test_sunos_cmd(self) -> None: |
| 260 |
with PlatformFixture('sunos5'): |
| 261 |
cmd = command.PlatformPager() |
| 262 |
self.assertEqual(['less'], cmd.command()) |
| 263 |
|
| 264 |
def test_freebsd_cmd(self) -> None: |
| 265 |
with PlatformFixture('freebsd8'): |
| 266 |
cmd = command.PlatformPager() |
| 267 |
self.assertEqual(['less'], cmd.command()) |
| 268 |
|
| 269 |
|
| 270 |
class GetPagerCommandTest(unittest.TestCase): |
| 271 |
def test_instance(self) -> None: |
| 272 |
cmd = command.PlatformPager() |
| 273 |
self.assertIs(cmd, command.get_pager_command(cmd)) |
| 274 |
|
| 275 |
def test_subclass(self) -> None: |
| 276 |
cls = command.Less |
| 277 |
self.assertIsInstance(command.get_pager_command(cls), cls) |
| 278 |
|
| 279 |
def test_func(self) -> None: |
| 280 |
func = command.PlatformPager |
| 281 |
self.assertIsInstance(command.get_pager_command(func), type(func())) |
| 282 |
|
| 283 |
def test_string(self) -> None: |
| 284 |
cmd = command.get_pager_command('foo bar') |
| 285 |
self.assertIsInstance(cmd, command.CustomPager) |
| 286 |
self.assertEqual(['foo', 'bar'], cmd.command()) |
| 287 |
|
| 288 |
def test_list(self) -> None: |
| 289 |
with fixtures.EnvironmentVariable('FOO', 'foo'): |
| 290 |
cmd = command.get_pager_command(['FOO', 'BAR']) |
| 291 |
self.assertIsInstance(cmd, command.CustomPager) |
| 292 |
self.assertEqual(['foo'], cmd.command()) |
| 293 |
|
| 294 |
def test_int(self) -> None: |
| 295 |
self.assertRaises(TypeError, command.get_pager_command, 42) |
| 296 |
|
| 297 |
def test_list_int(self) -> None: |
| 298 |
self.assertRaises(TypeError, command.get_pager_command, ['FOO', 42]) |