59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
|
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()
|
||
|
|
||
|
file_open.assert_called_once_with(config.AUTOSTART_FILE)
|
||
|
assert apps == {'someapp', 'anotherapp'}
|
||
|
|
||
|
@patch('builtins.open', side_effect=FileNotFoundError('someapp.env'))
|
||
|
def test_get_apps_filenotfounderror(file_open):
|
||
|
apps = autostart.get_apps()
|
||
|
|
||
|
file_open.assert_called_once_with(config.AUTOSTART_FILE)
|
||
|
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)
|
||
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, 'w')
|
||
|
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)
|
||
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, 'w')
|
||
|
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()
|
||
|
makedirs.assert_called_once_with(config.DATA_DIR, exist_ok=True)
|
||
|
file_open.assert_called_once_with(config.AUTOSTART_FILE, 'w')
|
||
|
file_open().write.assert_has_calls([
|
||
|
call('someapp\n'),
|
||
|
])
|