Skip to content

Commit fd5dfb4

Browse files
authored
Merge pull request #63 from dwhswenson/wizard-quit-save
Wizard: `!quit` command should give option to save
2 parents fe719e3 + 7568496 commit fd5dfb4

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎paths_cli/tests/wizard/test_wizard.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,31 @@ def test_run_wizard(self, toy_engine):
368368
assert len(storage.networks) == len(storage.schemes) == 0
369369
assert len(storage.engines) == 1
370370
assert storage.engines[toy_engine.name] == toy_engine
371+
372+
def test_run_wizard_quit(self):
373+
console = MockConsole()
374+
self.wizard.console = console
375+
self.wizard._patched = True
376+
step = mock.Mock(
377+
func=mock.Mock(side_effect=QuitWizard()),
378+
display_name='Engine',
379+
store_name='engines',
380+
minimum=1,
381+
maximum=1
382+
)
383+
self.wizard.steps = [step]
384+
mock_ask_save = mock.Mock(return_value=False)
385+
with mock.patch.object(Wizard, '_ask_save', mock_ask_save):
386+
self.wizard.run_wizard()
387+
assert "Goodbye!" in self.wizard.console.log_text
388+
389+
@pytest.mark.parametrize('inputs', ['y', 'n'])
390+
def test_ask_save(self, inputs):
391+
expected = {'y': True, 'n': False}[inputs]
392+
console = MockConsole(['foo', inputs])
393+
self.wizard.console = console
394+
result = self.wizard._ask_save()
395+
assert result is expected
396+
assert "Before quitting" in self.wizard.console.log_text
397+
assert "Sorry" in self.wizard.console.log_text
398+

‎paths_cli/wizard/wizard.py‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
FILE_LOADING_ERROR_MSG, RestartObjectException
1010
)
1111
from paths_cli.wizard.joke import name_joke
12-
from paths_cli.wizard.helper import Helper
12+
from paths_cli.wizard.helper import Helper, QuitWizard
1313
from paths_cli.compiling.tools import custom_eval
1414

1515
import shutil
@@ -313,13 +313,33 @@ def run_wizard(self):
313313
# TODO: next line is only temporary
314314
self.say("Today I'll help you set up a 2-state TPS simulation.")
315315
self._patch() # try to hide the slowness of our first import
316-
for step in self.steps:
317-
req = step.store_name, step.minimum, step.maximum
318-
do_another = True
319-
while do_another:
320-
do_another = self._do_one(step, req)
321-
storage = self.get_storage()
322-
self.save_to_file(storage)
316+
try:
317+
for step in self.steps:
318+
req = step.store_name, step.minimum, step.maximum
319+
do_another = True
320+
while do_another:
321+
do_another = self._do_one(step, req)
322+
except QuitWizard:
323+
do_save = self._ask_save()
324+
else:
325+
do_save = True
326+
327+
if do_save:
328+
storage = self.get_storage()
329+
self.save_to_file(storage)
330+
else:
331+
self.say("Goodbye! 👋")
332+
333+
@get_object
334+
def _ask_save(self):
335+
do_save_char = self.ask("Before quitting, would you like to save "
336+
"the objects you've created so far?")
337+
try:
338+
do_save = yes_no(do_save_char)
339+
except Exception:
340+
self.bad_input("Sorry, I didn't understance that.")
341+
return None
342+
return do_save
323343

324344

325345
# FIXED_LENGTH_TPS_WIZARD

0 commit comments

Comments
 (0)