Repository: PrefectHQ/prefect

Issue:
Add CLI test
Add test to catch behavior as in #1123


Discussion:

Resolving patch:
diff --git a/src/prefect/cli/execute.py b/src/prefect/cli/execute.py
--- a/src/prefect/cli/execute.py
+++ b/src/prefect/cli/execute.py
@@ -38,8 +38,8 @@ def cloud_flow():
     """
     flow_run_id = prefect.context.get("flow_run_id")
     if not flow_run_id:
-        click.echo("Not currently executing a flow within a cloud context.")
-        raise Exception("Not currently executing a flow within a cloud context.")
+        click.echo("Not currently executing a flow within a Cloud context.")
+        raise Exception("Not currently executing a flow within a Cloud context.")
 
     query = {
         "query": {
@@ -55,6 +55,7 @@ def cloud_flow():
     flow_run = result.data.flow_run
 
     if not flow_run:
+        click.echo("Flow run {} not found".format(flow_run_id))
         raise ValueError("Flow run {} not found".format(flow_run_id))
 
     try:
@@ -74,3 +75,5 @@ def cloud_flow():
         state = prefect.engine.state.Failed(message=msg)
         version = result.data.flow_run[0].version
         client.set_flow_run_state(flow_run_id=flow_run_id, version=version, state=state)
+        click.echo(str(exc))
+        raise exc


Test patch:
diff --git a/tests/cli/test_execute.py b/tests/cli/test_execute.py
--- a/tests/cli/test_execute.py
+++ b/tests/cli/test_execute.py
@@ -1,4 +1,4 @@
-from unittest.mock import MagicMock
+from unittest.mock import MagicMock, PropertyMock
 
 import click
 from click.testing import CliRunner
@@ -27,7 +27,10 @@ def test_execute_cloud_flow_fails():
     runner = CliRunner()
     result = runner.invoke(execute, "cloud-flow")
     assert result.exit_code == 1
-    assert "Not currently executing a flow within a cloud context." in result.output
+    assert "Not currently executing a flow within a Cloud context." in result.output
+    assert "Not currently executing a flow within a Cloud context." in str(
+        result.exc_info[1]
+    )
 
 
 def test_execute_cloud_flow_not_found(monkeypatch):
@@ -39,12 +42,39 @@ def test_execute_cloud_flow_not_found(monkeypatch):
     monkeypatch.setattr("requests.post", post)
 
     with set_temporary_config(
-        {
-            "cloud.graphql": "http://my-cloud.foo",
-            "cloud.auth_token": "secret_token",
-            "context.flow_run_id": "test",
-        }
+        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
     ):
-        runner = CliRunner()
-        result = runner.invoke(execute, "cloud-flow")
-        assert result.exit_code == 1
+        with prefect.context({"flow_run_id": "test"}):
+            runner = CliRunner()
+            result = runner.invoke(execute, "cloud-flow")
+
+    assert result.exit_code == 1
+    assert "Flow run test not found" in result.output
+    assert result.exc_info[0] == ValueError
+    assert "Flow run test not found" in str(result.exc_info[1])
+
+
+def test_execute_cloud_flow_fails(monkeypatch):
+    flow = MagicMock()
+    type(flow).storage = PropertyMock(side_effect=SyntaxError("oops"))
+    data = MagicMock(data=MagicMock(flow_run=[MagicMock(flow=flow)]))
+    client = MagicMock()
+    client.return_value.graphql.return_value = data
+    monkeypatch.setattr("prefect.cli.execute.Client", client)
+
+    with set_temporary_config(
+        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
+    ):
+        with prefect.context({"flow_run_id": "test"}):
+            runner = CliRunner()
+            result = runner.invoke(execute, "cloud-flow")
+
+    assert result.exit_code == 1
+    assert "oops" in result.output
+    assert result.exc_info[0] == SyntaxError
+    assert client.return_value.set_flow_run_state.call_args[1]["flow_run_id"] == "test"
+    assert client.return_value.set_flow_run_state.call_args[1]["state"].is_failed()
+    assert (
+        "Failed to load"
+        in client.return_value.set_flow_run_state.call_args[1]["state"].message
+    )