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

not-in-ldap pushed a commit to branch jennis/new_artifact_subcommands
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 0ca62cf42f03df8f6c46beca8eeadd15cb2aac99
Author: James Ennis <[email protected]>
AuthorDate: Fri Jan 11 15:53:10 2019 +0000

    cli.py: Print to stderr when trying to delete an uncached artifact
    
    If we specify multiple elements/artifacts for deletion, we should not
    throw an exception, and more importantly, stop, when we come across
    an artifact that is uncached.
---
 buildstream/_frontend/cli.py  | 23 +++++++++++++++++++++--
 tests/integration/artifact.py | 27 +++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py
index f2bf45c..828ea0a 100644
--- a/buildstream/_frontend/cli.py
+++ b/buildstream/_frontend/cli.py
@@ -1107,10 +1107,29 @@ def artifact_delete(app, artifacts):
             for element in elements:
                 cache_key = element._get_cache_key()
                 ref = cache.get_artifact_fullname(element, cache_key)
-                cache.remove(ref, defer_prune=True)
+                if cache.contains(element, cache_key):
+                    cache.remove(ref, defer_prune=True)
+                    click.echo("Removed {}.".format(ref))
+                else:
+                    # If the ref is not present when we try to delete it, we 
should
+                    # not fail but just continue to delete. The pruning will 
take care
+                    # of any unreachable objects.
+                    click.echo("WARNING: {}, not found in local cache - no 
delete required"
+                               .format(ref), err=True)
+                    continue
+
         if artifacts:
             for ref in artifacts:
-                cache.remove(ref, defer_prune=True)
+                if cache.contains_ref(ref):
+                    cache.remove(ref, defer_prune=True)
+                    click.echo("Removed {}.".format(ref))
+                else:
+                    # If the ref is not present when we try to delete it, we 
should
+                    # not fail but just continue to delete. The pruning will 
take care
+                    # of any unreachable objects.
+                    click.echo("WARNING: {}, not found in local cache - no 
delete required"
+                               .format(ref), err=True)
+                    continue
 
         # Now we've removed all the refs, prune the unreachable objects
         cache.prune()
diff --git a/tests/integration/artifact.py b/tests/integration/artifact.py
index ccd66db..607919e 100644
--- a/tests/integration/artifact.py
+++ b/tests/integration/artifact.py
@@ -153,3 +153,30 @@ def test_artifact_delete_element_and_artifact(cli, tmpdir, 
datafiles):
 
     # Check that the dependency ELEMENT is no longer cached
     assert cli.get_element_state(project, dep) != 'cached'
+
+
+# Test that we receive the appropriate stderr when we try to delete an artifact
+# that is not present in the cache.
[email protected]
[email protected](DATA_DIR)
+def test_artifact_delete_unbuilt_artifact(cli, tmpdir, datafiles):
+    project = os.path.join(datafiles.dirname, datafiles.basename)
+    element = 'integration.bst'
+
+    # Configure a local cache
+    local_cache = os.path.join(str(tmpdir), 'artifacts')
+    cli.configure({'artifactdir': local_cache})
+
+    # Ensure the element is not cached
+    assert cli.get_element_state(project, element) != 'cached'
+
+    # Obtain the artifact ref
+    cache_key = cli.get_element_key(project, element)
+    artifact = os.path.join('test', os.path.splitext(element)[0], cache_key)
+
+    # Try deleting the uncached artifact
+    result = cli.run(project=project, args=['artifact', 'delete', artifact])
+    result.assert_success()
+
+    expected_err = 'WARNING: {}, not found in local cache - no delete 
required\n'.format(artifact)
+    assert result.stderr == expected_err

Reply via email to