7204: Completed fixing bugs in tests for `arv get`.
authorColin Nolan <colin.nolan@sanger.ac.uk>
Tue, 22 Sep 2015 13:52:27 +0000 (14:52 +0100)
committerColin Nolan <colin.nolan@sanger.ac.uk>
Tue, 22 Sep 2015 13:52:27 +0000 (14:52 +0100)
sdk/cli/test/test_arv-get.rb

index d0b569c60633f0d4730f2c04e9266510186a1870..fc5e226ccde346cf0d9ff79032fa7afa28e5acc0 100644 (file)
@@ -16,56 +16,52 @@ class TestArvGet < Minitest::Test
   # Tests that a valid Arvados object can be retrieved in a supported format
   # using: `arv get [uuid]`. Given all other `arv foo` commands return JSON
   # when no format is specified, JSON should be expected in this case.
-  def test_get_valid_object_no_format()
-    stored_value = __method__
+  def test_get_valid_object_no_format_specified()
+    stored_value = __method__.to_s
     uuid = create_arv_object_with_value(stored_value)
     out, err = capture_subprocess_io do
-      arv_get(uuid)
+      assert(arv_get_default(uuid))
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
     arv_object = parse_json_arv_object(out)
     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
   end
 
   # Tests that a valid Arvados object can be retrieved in JSON format using:
   # `arv get [uuid] --format json`.
-  def test_get_valid_object_json_format()
-    stored_value = __method__
+  def test_get_valid_object_json_format_specified()
+    stored_value = __method__.to_s
     uuid = create_arv_object_with_value(stored_value)
     out, err = capture_subprocess_io do
-      arv_get(uuid, '--format', 'json')
+      assert(arv_get_json(uuid))
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
     arv_object = parse_json_arv_object(out)
     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
   end
 
   # Tests that a valid Arvados object can be retrieved in YAML format using:
   # `arv get [uuid] --format yaml`.
-  def test_get_valid_object_yaml_format()
-    stored_value = __method__
+  def test_get_valid_object_yaml_format_specified()
+    stored_value = __method__.to_s
     uuid = create_arv_object_with_value(stored_value)
     out, err = capture_subprocess_io do
-      arv_get(uuid, '--format', 'yaml')
+      assert(arv_get_yaml(uuid))
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
     arv_object = parse_yaml_arv_object(out)
     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
   end
 
   # Tests that a subset of all fields of a valid Arvados object can be retrieved
   # using: `arv get [uuid] [fields...]`.
-  def test_get_valid_object_with_specific_valid_fields()
-    stored_value = __method__
+  def test_get_valid_object_with_valid_fields()
+    stored_value = __method__.to_s
     uuid = create_arv_object_with_value(stored_value)
     out, err = capture_subprocess_io do
-      arv_get(uuid, STORED_VALUE_FIELD_NAME, UUID_FIELD_NAME, "--format", "json")
+      assert(arv_get_json(uuid, STORED_VALUE_FIELD_NAME, UUID_FIELD_NAME))
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
     arv_object = parse_json_arv_object(out)
     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
     assert(has_field_with_value(arv_object, UUID_FIELD_NAME, uuid))
@@ -74,14 +70,13 @@ class TestArvGet < Minitest::Test
   # Tests that the valid field is retrieved when both a valid and invalid field
   # are requested from a valid Arvados object, using:
   # `arv get [uuid] [fields...]`.
-  def test_get_valid_object_with_both_specific_valid_and_invalid_fields()
-    stored_value = __method__
+  def test_get_valid_object_with_both_valid_and_invalid_fields()
+    stored_value = __method__.to_s
     uuid = create_arv_object_with_value(stored_value)
     out, err = capture_subprocess_io do
-      arv_get(uuid, STORED_VALUE_FIELD_NAME, INVALID_FIELD_NAME, "--format", "json")
+      assert(arv_get_json(uuid, STORED_VALUE_FIELD_NAME, INVALID_FIELD_NAME))
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
     arv_object = parse_json_arv_object(out)
     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
     refute(has_field_with_value(arv_object, INVALID_FIELD_NAME, stored_value))
@@ -89,65 +84,69 @@ class TestArvGet < Minitest::Test
 
   # Tests that no fields are retreived when no valid fields are requested from
   # a valid Arvados object, using: `arv get [uuid] [fields...]`.
-  def test_get_valid_object_with_no_specific_valid_fields()
-    stored_value = __method__
+  def test_get_valid_object_with_no_valid_fields()
+    stored_value = __method__.to_s
     uuid = create_arv_object_with_value(stored_value)
     out, err = capture_subprocess_io do
-      arv_get(uuid, INVALID_FIELD_NAME, "--format", "json")
+      assert(arv_get_json(uuid, INVALID_FIELD_NAME))
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
     arv_object = parse_json_arv_object(out)
-    assert_equal(0, arv_object.fixnum)
-  end
-
-  # Tests that an valid Arvados object is not retrieved when specifying an
-  # invalid format: `arv get [uuid] --format invalid`.
-  def test_get_valid_object_invalid_format()
-    stored_value = __method__
-    uuid = create_arv_object_with_value(stored_value)
-    out, err = capture_subprocess_io do
-      arv_get(uuid, '--format', 'invalid')
-    end
-    refute_empty(err)
-    assert_empty(out)
+    assert_equal(0, arv_object.length)
   end
 
   # Tests that an invalid (non-existant) Arvados object is not retrieved using:
   # using: `arv get [non-existant-uuid]`.
   def test_get_invalid_object()
     out, err = capture_subprocess_io do
-      arv_get(NON_EXISTANT_OBJECT_UUID, "--format", "json")
+      refute(arv_get_json(NON_EXISTANT_OBJECT_UUID))
     end
-    refute_empty(err)
+    refute_empty(err, "Expected error feedback on request for invalid object")
     assert_empty(out)
   end
 
   # Tests that help text exists using: `arv get --help`.
   def test_help_exists()
     out, err = capture_subprocess_io do
-      arv_get("--help")
+      assert(arv_get_default("--help"), "Expected exit code 0: #{$?}")
     end
-    assert_empty(err)
-    refute_empty(out)
+    assert_empty(err, "Error text not expected: '#{err}'")
+    refute_empty(out, "Help text should be given")
   end
 
   protected
-  # Runs 'arv get <varargs>' with given arguments.
-  def arv_get(*args)
-    system(['./bin/arv', 'get'], *args)
+  # Runs 'arv get <varargs>' with given arguments. Returns whether the exit
+  # status was 0 (i.e. success). Use $? to attain more details on failure.
+  def arv_get_default(*args)
+    return system("arv", "get", *args)
+  end
+
+  # Runs 'arv --format json get <varargs>' with given arguments. Returns whether
+  # the exit status was 0 (i.e. success). Use $? to attain more details on
+  # failure.
+  def arv_get_json(*args)
+    return system("arv", "--format", "json", "get", *args)
+  end
+
+  # Runs 'arv --format yaml get <varargs>' with given arguments. Returns whether
+  # the exit status was 0 (i.e. success). Use $? to attain more details on
+  # failure.
+  def arv_get_yaml(*args)
+    return system("arv", "--format", "yaml", "get", *args)
   end
 
   # Creates an Arvados object that stores a given value. Returns the uuid of the
   # created object.
   def create_arv_object_with_value(value)
       out, err = capture_subprocess_io do
-        # Write (without redirect)
-        system(['./bin/arv', "tag add #{value} --object testing"])
+        if !system("arv", "tag", "add", "#{value}", "--object", "testing")
+          raise "Command failure running `arv tag` with arguments #{args}: #{$?}"
+        end
       end
-      if err.length > 0
+      if err.length > 0 || out.length == 0
         raise "Could not create Arvados object with given value"
       end
+      out = out.delete!("\n")
       return out
   end
 
@@ -160,8 +159,8 @@ class TestArvGet < Minitest::Test
       return parsed
     rescue JSON::ParserError => e
       raise "Invalid JSON representation of Arvados object.\n" \
-            "Parse error: #{e}\n" \
-            "JSON: #{arvObjectAsJson}\n"
+            "Parse error: '#{e}'\n" \
+            "JSON: '#{arvObjectAsJson}'\n"
     end
   end
 
@@ -174,7 +173,7 @@ class TestArvGet < Minitest::Test
       return parsed
     rescue
       raise "Invalid YAML representation of Arvados object.\n" \
-            "YAML: #{arvObjectAsYaml}\n"
+            "YAML: '#{arvObjectAsYaml}'\n"
     end
   end