11579: Check if paths exist, raise meaningful exception if not.
authorLucas Di Pentima <lucas@curoverse.com>
Fri, 28 Apr 2017 15:25:01 +0000 (12:25 -0300)
committerLucas Di Pentima <lucas@curoverse.com>
Fri, 28 Apr 2017 15:25:01 +0000 (12:25 -0300)
Added test for this case.

sdk/python/arvados/commands/put.py
sdk/python/tests/test_arv_put.py

index d616f3087ed08af5c616483936dd8a92eb82c0fb..90a85ba7730cea6708d1f6bfbf75dd70f97b115d 100644 (file)
@@ -247,6 +247,10 @@ def parse_arguments(arguments):
     return args
 
 
+class PathDoesNotExistError(Exception):
+    pass
+
+
 class CollectionUpdateError(Exception):
     pass
 
@@ -447,6 +451,8 @@ class ArvPutUploadJob(object):
                     if self.dry_run:
                         raise ArvPutUploadIsPending()
                     self._write_stdin(self.filename or 'stdin')
+                elif not os.path.exists(path):
+                     raise PathDoesNotExistError("file or directory '{}' does not exist.".format(path))
                 elif os.path.isdir(path):
                     # Use absolute paths on cache index so CWD doesn't interfere
                     # with the caching logic.
@@ -1000,6 +1006,10 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
     except ArvPutUploadNotPending:
         # No files pending for upload
         sys.exit(0)
+    except PathDoesNotExistError as error:
+        logger.error("\n".join([
+            "arv-put: %s" % str(error)]))
+        exit(1)
 
     if args.progress:  # Print newline to split stderr from stdout for humans.
         logger.info("\n")
index 547cfc296b5a12bcfcbd8ef65c53a65a0e3f30aa..774e199ee1a8b703a3bb7559309c9f806cb8df9e 100644 (file)
@@ -17,6 +17,7 @@ import yaml
 import threading
 import hashlib
 import random
+import uuid
 
 from cStringIO import StringIO
 
@@ -302,6 +303,12 @@ class ArvPutUploadJobTest(run_test_server.TestCaseWithServers,
         self.assertNotIn('linkeddir2', cwriter.manifest_text())
         cwriter.destroy_cache()
 
+    def test_passing_nonexistant_path_raise_exception(self):
+        uuid_str = str(uuid.uuid4())
+        cwriter = arv_put.ArvPutUploadJob(["/this/path/does/not/exist/{}".format(uuid_str)])
+        with self.assertRaises(arv_put.PathDoesNotExistError):
+            cwriter.start(save_collection=False)
+
     def test_writer_works_without_cache(self):
         cwriter = arv_put.ArvPutUploadJob(['/dev/null'], resume=False)
         cwriter.start(save_collection=False)