2021-07-06 18:06:54 +02:00
|
|
|
from unittest.mock import patch, call, mock_open
|
|
|
|
|
|
|
|
from spoc import autostart
|
|
|
|
from spoc import config
|
|
|
|
|
|
|
|
@patch('builtins.open', new_callable=mock_open, read_data='someapp\nanotherapp\n')
|
|
|
|
def test_get_apps(file_open):
|
|
|
|
apps = autostart.get_apps()
|
|
|
|
|
2021-12-20 22:21:58 +01:00
|
|
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, encoding='utf-8')
|
2021-07-06 18:06:54 +02:00
|
|
|
assert apps == {'someapp', 'anotherapp'}
|
|
|
|
|
|
|
|
@patch('builtins.open', side_effect=FileNotFoundError('someapp.env'))
|
|
|
|
def test_get_apps_filenotfounderror(file_open):
|
|
|
|
apps = autostart.get_apps()
|
|
|
|
|
2021-12-20 22:21:58 +01:00
|
|
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, encoding='utf-8')
|
2021-07-06 18:06:54 +02:00
|
|
|
assert apps == set()
|
|
|
|
|
|
|
|
@patch('os.makedirs')
|
|
|
|
@patch('spoc.autostart.get_apps', return_value={'someapp'})
|
|
|
|
@patch('builtins.open', new_callable=mock_open)
|
|
|
|
def test_set_app_enable(file_open, get_apps, makedirs):
|
|
|
|
autostart.set_app('anotherapp', True)
|
|
|
|
|
|
|
|
get_apps.assert_called_once()
|
|
|
|
makedirs.assert_called_once_with(config.DATA_DIR, exist_ok=True)
|
2021-12-20 22:21:58 +01:00
|
|
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, 'w', encoding='utf-8')
|
2021-07-06 18:06:54 +02:00
|
|
|
file_open().write.assert_has_calls([
|
|
|
|
call('someapp\n'),
|
|
|
|
call('anotherapp\n'),
|
|
|
|
], any_order=True)
|
|
|
|
|
|
|
|
@patch('os.makedirs')
|
|
|
|
@patch('spoc.autostart.get_apps', return_value={'someapp', 'anotherapp'})
|
|
|
|
@patch('builtins.open', new_callable=mock_open)
|
|
|
|
def test_set_app_disable(file_open, get_apps, makedirs):
|
|
|
|
autostart.set_app('anotherapp', False)
|
|
|
|
|
|
|
|
get_apps.assert_called_once()
|
|
|
|
makedirs.assert_called_once_with(config.DATA_DIR, exist_ok=True)
|
2021-12-20 22:21:58 +01:00
|
|
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, 'w', encoding='utf-8')
|
2021-07-06 18:06:54 +02:00
|
|
|
file_open().write.assert_has_calls([
|
|
|
|
call('someapp\n'),
|
|
|
|
])
|
|
|
|
|
|
|
|
@patch('os.makedirs')
|
|
|
|
@patch('spoc.autostart.get_apps', return_value={'someapp'})
|
|
|
|
@patch('builtins.open', new_callable=mock_open)
|
|
|
|
def test_set_app_nonexistent(file_open, get_apps, makedirs):
|
|
|
|
autostart.set_app('anotherapp', False)
|
|
|
|
|
|
|
|
get_apps.assert_called_once()
|
2021-07-11 00:58:28 +02:00
|
|
|
makedirs.assert_not_called()
|
|
|
|
file_open.assert_not_called()
|
|
|
|
file_open().write.assert_not_called()
|