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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
# -*- coding: utf-8 -*-
"""
Tests for the BaseMode class.
"""
import os
import mu
import pytest
from mu.modes.base import BaseMode, MicroPythonMode
from unittest import mock
def test_base_mode():
"""
Sanity check for the parent class of all modes.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.name == 'UNNAMED MODE'
assert bm.description == 'DESCRIPTION NOT AVAILABLE.'
assert bm.icon == 'help'
assert bm.is_debugger is False
assert bm.editor == editor
assert bm.view == view
assert bm.actions() == NotImplemented
assert bm.workspace_dir()
assert bm.api() == NotImplemented
assert bm.builtins is None
def test_base_mode_workspace_dir():
"""
Return settings file workspace value.
"""
# read from our demo settings.json
with mock.patch('mu.modes.base.get_settings_path',
return_value='tests/settings.json'), \
mock.patch('os.path.isdir', return_value=True):
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.workspace_dir() == '/home/foo/mycode'
def test_base_mode_workspace_not_present():
"""
No workspace key in settings file, return default folder.
"""
default_workspace = os.path.join(mu.logic.HOME_DIRECTORY,
mu.logic.WORKSPACE_NAME)
with mock.patch('mu.modes.base.get_settings_path',
return_value='tests/settingswithoutworkspace.json'):
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.workspace_dir() == default_workspace
def test_base_mode_workspace_invalid_value():
"""
Invalid workspace key in settings file, return default folder.
"""
default_workspace = os.path.join(mu.logic.HOME_DIRECTORY,
mu.logic.WORKSPACE_NAME)
# read from our demo settings.json
with mock.patch('mu.modes.base.get_settings_path',
return_value='tests/settings.json'), \
mock.patch('os.path.isdir', return_value=False), \
mock.patch('mu.modes.base.logger', return_value=None) as logger:
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.workspace_dir() == default_workspace
assert logger.error.call_count == 1
def test_base_mode_workspace_invalid_json():
"""
Invalid workspace key in settings file, return default folder.
"""
default_workspace = os.path.join(mu.logic.HOME_DIRECTORY,
mu.logic.WORKSPACE_NAME)
mock_open = mock.mock_open(read_data='{"workspace": invalid}')
with mock.patch('mu.modes.base.get_settings_path',
return_value='a.json'), \
mock.patch('builtins.open', mock_open), \
mock.patch('mu.modes.base.logger', return_value=None) as logger:
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.workspace_dir() == default_workspace
assert logger.error.call_count == 1
def test_base_mode_workspace_no_settings_file():
"""
Invalid settings file, return default folder.
"""
default_workspace = os.path.join(mu.logic.HOME_DIRECTORY,
mu.logic.WORKSPACE_NAME)
mock_open = mock.MagicMock(side_effect=FileNotFoundError())
with mock.patch('mu.modes.base.get_settings_path',
return_value='tests/settings.json'), \
mock.patch('builtins.open', mock_open), \
mock.patch('mu.modes.base.logger', return_value=None) as logger:
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.workspace_dir() == default_workspace
assert logger.error.call_count == 1
def test_base_mode_set_buttons():
"""
Ensure only buttons for existing actions have their "Enabled" states
updates.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.button_bar.slots = {
'foo': mock.MagicMock(),
'bar': mock.MagicMock()
}
bm = BaseMode(editor, view)
bm.set_buttons(foo=True, bar=False, baz=True)
view.button_bar.slots['foo'].setEnabled.assert_called_once_with(True)
view.button_bar.slots['bar'].setEnabled.assert_called_once_with(False)
assert 'baz' not in view.button_bar.slots
def test_base_mode_add_plotter():
"""
Ensure the child classes need to implement this.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.add_plotter() == NotImplemented
def test_base_mode_remove_plotter():
"""
Ensure the plotter is removed and data is saved as a CSV file in the
expected directory.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.plotter_pane.raw_data = [1, 2, 3]
bm = BaseMode(editor, view)
bm.plotter = mock.MagicMock()
mock_mkdir = mock.MagicMock()
mock_open = mock.mock_open()
mock_csv_writer = mock.MagicMock()
mock_csv = mock.MagicMock()
mock_csv.writer.return_value = mock_csv_writer
with mock.patch('mu.modes.base.os.path.exists', return_value=False), \
mock.patch('mu.modes.base.os.makedirs', mock_mkdir), \
mock.patch('builtins.open', mock_open), \
mock.patch('mu.modes.base.csv', mock_csv):
bm.remove_plotter()
assert bm.plotter is None
view.remove_plotter.assert_called_once_with()
dd = os.path.join(bm.workspace_dir(), 'data_capture')
mock_mkdir.assert_called_once_with(dd)
mock_csv_writer.writerows.\
assert_called_once_with(view.plotter_pane.raw_data)
def test_base_on_data_flood():
"""
Ensure the plotter is removed and a helpful message is displayed to the
user.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
bm.on_data_flood()
view.remove_plotter.assert_called_once_with()
assert view.show_message.call_count == 1
def test_base_mode_open_file():
"""
Ensure the the base class returns None to indicate it can't open the file.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
bm = BaseMode(editor, view)
assert bm.open_file('unused/path') is None
def test_micropython_mode_find_device():
"""
Ensure it's possible to detect a device and return the expected port.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mock_port = mock.MagicMock()
for vid, pid in mm.valid_boards:
mock_port.vid = vid
mock_port.productIdentifier = mock.MagicMock()
mock_port.productIdentifier.return_value = pid
mock_port.vendorIdentifier = mock.MagicMock()
mock_port.vendorIdentifier.return_value = vid
mock_port.portName = mock.MagicMock(return_value='COM0')
mock_port.serialNumber = mock.MagicMock(return_value='12345')
mock_os = mock.MagicMock()
mock_os.name = 'nt'
with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts',
return_value=[mock_port, ]), \
mock.patch('mu.modes.base.os', mock_os):
assert mm.find_device() == ('COM0', '12345')
def test_micropython_mode_find_device_no_ports():
"""
There are no connected devices so return None.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts',
return_value=[]):
assert mm.find_device() == (None, None)
def test_micropython_mode_find_device_but_no_device():
"""
None of the connected devices is a valid board so return None.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mock_port = mock.MagicMock()
mock_port.productIdentifier = mock.MagicMock(return_value=666)
mock_port.vendorIdentifier = mock.MagicMock(return_value=999)
mock_port.serialNumber = mock.MagicMock(return_value='123456')
with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts',
return_value=[mock_port, ]):
assert mm.find_device() == (None, None)
def test_micropython_mode_port_path_posix():
"""
Ensure the correct path for a port_name is returned if the platform is
posix.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
with mock.patch('os.name', 'posix'):
assert mm.port_path('tty1') == "/dev/tty1"
def test_micropython_mode_port_path_nt():
"""
Ensure the correct path for a port_name is returned if the platform is
nt.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
with mock.patch('os.name', 'nt'):
assert mm.port_path('COM0') == "COM0"
def test_micropython_mode_port_path_unknown():
"""
If the platform is unknown, raise NotImplementedError.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
with mock.patch('os.name', 'foo'):
with pytest.raises(NotImplementedError):
mm.port_path('bar')
def test_micropython_mode_add_repl_no_port():
"""
If it's not possible to find a connected micro:bit then ensure a helpful
message is enacted.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=(None, None))
mm.add_repl()
assert view.show_message.call_count == 1
message = 'Could not find an attached device.'
assert view.show_message.call_args[0][0] == message
def test_micropython_mode_add_repl_ioerror():
"""
Sometimes when attempting to connect to the device there is an IOError
because it's still booting up or connecting to the host computer. In this
case, ensure a useful message is displayed.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
ex = IOError('BOOM')
view.add_micropython_repl = mock.MagicMock(side_effect=ex)
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=('COM0', '12345'))
mm.add_repl()
assert view.show_message.call_count == 1
assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_add_repl_exception():
"""
Ensure that any non-IOError based exceptions are logged.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
ex = Exception('BOOM')
view.add_micropython_repl = mock.MagicMock(side_effect=ex)
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=('COM0', '12345'))
with mock.patch('mu.modes.base.logger', return_value=None) as logger:
mm.add_repl()
logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_repl():
"""
Nothing goes wrong so check the _view.add_micropython_repl gets the
expected argument.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
view.add_micropython_repl = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=('COM0', '12345'))
with mock.patch('os.name', 'nt'):
mm.add_repl()
assert view.show_message.call_count == 0
assert view.add_micropython_repl.call_args[0][0] == 'COM0'
def test_micropython_mode_add_repl_no_force_interrupt():
"""
Nothing goes wrong so check the _view.add_micropython_repl gets the
expected arguments (including the flag so no keyboard interrupt is called).
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
view.add_micropython_repl = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.force_interrupt = False
mm.find_device = mock.MagicMock(return_value=('COM0', '12345'))
with mock.patch('os.name', 'nt'):
mm.add_repl()
assert view.show_message.call_count == 0
assert view.add_micropython_repl.call_args[0][0] == 'COM0'
assert view.add_micropython_repl.call_args[0][2] is False
def test_micropython_mode_remove_repl():
"""
If there is a repl, make sure it's removed as expected and the state is
updated.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.remove_repl = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.repl = True
mm.remove_repl()
assert view.remove_repl.call_count == 1
assert mm.repl is False
def test_micropython_mode_toggle_repl_on():
"""
There is no repl, so toggle on.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.add_repl = mock.MagicMock()
mm.repl = None
mm.toggle_repl(None)
assert mm.add_repl.call_count == 1
def test_micropython_mode_toggle_repl_off():
"""
There is a repl, so toggle off.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.remove_repl = mock.MagicMock()
mm.repl = True
mm.toggle_repl(None)
assert mm.remove_repl.call_count == 1
def test_micropython_mode_toggle_plotter_on():
"""
There is no plotter, so toggle on.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.add_plotter = mock.MagicMock()
mm.plotter = None
mm.toggle_plotter(None)
assert mm.add_plotter.call_count == 1
def test_micropython_mode_toggle_plotter_off():
"""
There is a plotter, so toggle off.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.remove_plotter = mock.MagicMock()
mm.plotter = True
mm.toggle_plotter(None)
assert mm.remove_plotter.call_count == 1
def test_micropython_mode_add_plotter_no_port():
"""
If it's not possible to find a connected micro:bit then ensure a helpful
message is enacted.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=(None, None))
mm.add_plotter()
assert view.show_message.call_count == 1
message = 'Could not find an attached device.'
assert view.show_message.call_args[0][0] == message
def test_micropython_mode_add_plotter_ioerror():
"""
Sometimes when attempting to connect to the device there is an IOError
because it's still booting up or connecting to the host computer. In this
case, ensure a useful message is displayed.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
ex = IOError('BOOM')
view.add_micropython_plotter = mock.MagicMock(side_effect=ex)
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=('COM0', '123456'))
mm.add_plotter()
assert view.show_message.call_count == 1
assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_add_plotter_exception():
"""
Ensure that any non-IOError based exceptions are logged.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
ex = Exception('BOOM')
view.add_micropython_plotter = mock.MagicMock(side_effect=ex)
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=('COM0', '12345'))
with mock.patch('mu.modes.base.logger', return_value=None) as logger:
mm.add_plotter()
logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_plotter():
"""
Nothing goes wrong so check the _view.add_micropython_plotter gets the
expected argument.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
view.show_message = mock.MagicMock()
view.add_micropython_plotter = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.find_device = mock.MagicMock(return_value=('COM0', '12345'))
with mock.patch('os.name', 'nt'):
mm.add_plotter()
assert view.show_message.call_count == 0
assert view.add_micropython_plotter.call_args[0][0] == 'COM0'
def test_micropython_on_data_flood():
"""
Ensure that the REPL is removed before calling the base on_data_flood
method.
"""
editor = mock.MagicMock()
view = mock.MagicMock()
mm = MicroPythonMode(editor, view)
mm.remove_repl = mock.MagicMock()
with mock.patch('builtins.super') as mock_super:
mm.on_data_flood()
mm.remove_repl.assert_called_once_with()
mock_super().on_data_flood.assert_called_once_with()
|