This is an automated email from the ASF dual-hosted git repository.

root pushed a commit to branch aevri/safe_noninteractive
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 1bb28761ee1a53189a2509b39f497523c637f325
Author: Angelos Evripiotis <[email protected]>
AuthorDate: Thu Nov 22 10:47:43 2018 +0000

    tests/.../workspace: assert --assume-yes is needed
    
    Add tests to ensure that these commands will fail if we are not
    interactive:
    
    - bst workspace close --remove-dir
    - bst workspace reset
    
    They should be invoked with '--assume-yes' if
    'prompt.really-workspace-*' options are not configured.
    
    Also make these code paths testable by raising an AppError instead of
    sys.exit(-1).
    
    This also tests the 'prompt.really-workspace-*' options, they were not
    testable before the '--assume-yes' breaking change to non-interactive
    behaviour.
    
    Note that interactive behaviour is still not covered and must be tested
    manually.
---
 buildstream/_frontend/cli.py | 20 +++++------
 tests/frontend/workspace.py  | 81 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 10 deletions(-)

diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py
index 8375de6..b330366 100644
--- a/buildstream/_frontend/cli.py
+++ b/buildstream/_frontend/cli.py
@@ -769,12 +769,12 @@ def workspace_close(app, remove_dir, all_, assume_yes, 
elements):
         if remove_dir and not assume_yes and 
app.context.prompt_workspace_close_remove_dir:
             if app.interactive:
                 if not click.confirm('This will remove all your changes, are 
you sure?'):
-                    click.echo('Aborting', err=True)
-                    sys.exit(-1)
+                    raise AppError("User aborted")
             else:
-                click.echo("Aborted destructive non-interactive action.", 
err=True)
-                click.echo("Please use the '--assume-yes' option to 
override.", err=True)
-                sys.exit(-1)
+                raise AppError(
+                    "Aborted destructive non-interactive action.",
+                    detail="Please use the '--assume-yes' option to override.",
+                    reason='aborted-destructive-non-interactive-not-confirmed')
 
         for element_name in elements:
             app.stream.workspace_close(element_name, remove_dir=remove_dir)
@@ -810,12 +810,12 @@ def workspace_reset(app, soft, track_, all_, assume_yes, 
elements):
         if not soft and not assume_yes and 
app.context.prompt_workspace_reset_hard:
             if app.interactive:
                 if not click.confirm('This will remove all your changes, are 
you sure?'):
-                    click.echo('Aborting', err=True)
-                    sys.exit(-1)
+                    raise AppError("User aborted")
             else:
-                click.echo("Aborted destructive non-interactive action.", 
err=True)
-                click.echo("Please use the '--assume-yes' option to 
override.", err=True)
-                sys.exit(-1)
+                raise AppError(
+                    "Aborted destructive non-interactive action.",
+                    detail="Please use the '--assume-yes' option to override.",
+                    reason='aborted-destructive-non-interactive-not-confirmed')
 
         if all_:
             elements = tuple(element_name for element_name, _ in 
app.context.get_workspaces().list())
diff --git a/tests/frontend/workspace.py b/tests/frontend/workspace.py
index 03ae262..bdb2da4 100644
--- a/tests/frontend/workspace.py
+++ b/tests/frontend/workspace.py
@@ -22,6 +22,7 @@
 #           Jonathan Maw <[email protected]>
 #           Richard Maw <[email protected]>
 #           William Salmon <[email protected]>
+#           Angelos Evripiotis <[email protected]>
 #
 
 import os
@@ -520,6 +521,44 @@ def test_close_all(cli, tmpdir, datafiles):
 
 
 @pytest.mark.datafiles(DATA_DIR)
[email protected]("scenario", [
+    {'assume_yes': True, 'no_prompt': True},
+    {'assume_yes': False, 'no_prompt': True},
+    # Covered by test_close: {'assume_yes': True, 'no_prompt': False},
+    {'assume_yes': False, 'no_prompt': False},
+])
+def test_close_remove_dir_prompt(cli, tmpdir, datafiles, scenario):
+
+    assume_yes, no_prompt = scenario['assume_yes'], scenario['no_prompt']
+
+    element_name, project, workspace = open_workspace(
+        cli, tmpdir, datafiles, 'git', track=False)
+
+    workspace_args = [
+        'workspace', 'close', '--remove-dir', element_name
+    ]
+
+    if assume_yes:
+        workspace_args.append('--assume-yes')
+
+    if no_prompt:
+        cli.configure(
+            {'prompt': {'really-workspace-close-remove-dir': 'yes'}}
+        )
+
+    result = cli.run(project=project, args=workspace_args)
+
+    if assume_yes or no_prompt:
+        result.assert_success()
+        assert not os.path.exists(workspace)
+    else:
+        result.assert_main_error(
+            ErrorDomain.APP,
+            'aborted-destructive-non-interactive-not-confirmed')
+        assert os.path.exists(workspace)
+
+
[email protected](DATA_DIR)
 def test_reset(cli, tmpdir, datafiles):
     # Open the workspace
     element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 
'git', False)
@@ -593,6 +632,48 @@ def test_reset_all(cli, tmpdir, datafiles):
 
 
 @pytest.mark.datafiles(DATA_DIR)
[email protected]("scenario", [
+    {'assume_yes': True, 'no_prompt': True},
+    {'assume_yes': False, 'no_prompt': True},
+    # Covered by test_reset: {'assume_yes': True, 'no_prompt': False},
+    {'assume_yes': False, 'no_prompt': False},
+])
+def test_reset_prompt(cli, tmpdir, datafiles, scenario):
+
+    assume_yes, no_prompt = scenario['assume_yes'], scenario['no_prompt']
+
+    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 
'git', False)
+
+    # Make a change to revert.
+    os.makedirs(os.path.join(workspace, 'etc'))
+    with open(os.path.join(workspace, 'etc', 'pony.conf'), 'w') as f:
+        f.write("PONY='pink'")
+
+    workspace_args = [
+        'workspace', 'reset', element_name
+    ]
+
+    if assume_yes:
+        workspace_args.append('--assume-yes')
+
+    if no_prompt:
+        cli.configure(
+            {'prompt': {'really-workspace-reset-hard': 'yes'}}
+        )
+
+    result = cli.run(project=project, args=workspace_args)
+
+    if assume_yes or no_prompt:
+        result.assert_success()
+        assert not os.path.exists(os.path.join(workspace, 'etc', 'pony.conf'))
+    else:
+        result.assert_main_error(
+            ErrorDomain.APP,
+            'aborted-destructive-non-interactive-not-confirmed')
+        assert os.path.exists(os.path.join(workspace, 'etc', 'pony.conf'))
+
+
[email protected](DATA_DIR)
 def test_list(cli, tmpdir, datafiles):
     element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 
'git', False)
 

Reply via email to