Treat empty directory properly in skimage.
authorscroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 21 Jun 2013 19:12:47 +0000 (19:12 +0000)
committerscroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 21 Jun 2013 19:12:47 +0000 (19:12 +0000)
Instead of treating an empty directory as a file that failed to
decode, treat it as an empty directory.

Add self tests to check for this.

Builds upon from https://codereview.chromium.org/16866010,
which was reverted.

R=epoger@google.com

Review URL: https://codereview.chromium.org/17101029

git-svn-id: http://skia.googlecode.com/svn/trunk@9730 2bbb7eff-a529-9590-31e7-b0007b416f81

tools/skimage_main.cpp
tools/tests/skimage/output-actual/empty-dir/README [new file with mode: 0644]
tools/tests/skimage/output-actual/nonexistent-dir/README [new file with mode: 0644]
tools/tests/skimage/output-expected/empty-dir/expectations.json [new file with mode: 0644]
tools/tests/skimage/output-expected/nonexistent-dir/expectations.json [new file with mode: 0644]
tools/tests/skimage_self_test.py

index a6a2736..01dbc46 100644 (file)
@@ -501,21 +501,20 @@ int tool_main(int argc, char** argv) {
     }
 
     for (int i = 0; i < FLAGS_readPath.count(); i++) {
-        if (strlen(FLAGS_readPath[i]) < 1) {
+        const char* readPath = FLAGS_readPath[i];
+        if (strlen(readPath) < 1) {
             break;
         }
-        SkOSFile::Iter iter(FLAGS_readPath[i]);
-        SkString filename;
-        if (iter.next(&filename)) {
-            SkString directory(FLAGS_readPath[i]);
-            append_path_separator_if_necessary(&directory);
-            do {
-                SkString fullname(directory);
-                fullname.append(filename);
+        if (sk_isdir(readPath)) {
+            const char* dir = readPath;
+            SkOSFile::Iter iter(dir);
+            SkString filename;
+            while (iter.next(&filename)) {
+                SkString fullname = SkOSPath::SkPathJoin(dir, filename.c_str());
                 decodeFileAndWrite(fullname.c_str(), outDirPtr);
-            } while (iter.next(&filename));
-        } else {
-            decodeFileAndWrite(FLAGS_readPath[i], outDirPtr);
+            }
+        } else if (sk_exists(readPath)) {
+            decodeFileAndWrite(readPath, outDirPtr);
         }
     }
 
diff --git a/tools/tests/skimage/output-actual/empty-dir/README b/tools/tests/skimage/output-actual/empty-dir/README
new file mode 100644 (file)
index 0000000..87331d4
--- /dev/null
@@ -0,0 +1 @@
+Actual output of skimage self-tests reading from an empty dir goes here.
diff --git a/tools/tests/skimage/output-actual/nonexistent-dir/README b/tools/tests/skimage/output-actual/nonexistent-dir/README
new file mode 100644 (file)
index 0000000..006d6ce
--- /dev/null
@@ -0,0 +1 @@
+Actual output of skimage self-tests run on a nonexistent directory goes here.
diff --git a/tools/tests/skimage/output-expected/empty-dir/expectations.json b/tools/tests/skimage/output-expected/empty-dir/expectations.json
new file mode 100644 (file)
index 0000000..1ce8e27
--- /dev/null
@@ -0,0 +1,9 @@
+{
+   "actual-results" : {
+      "failed" : null,
+      "failure-ignored" : null,
+      "no-comparison" : null,
+      "succeeded" : null
+   },
+   "expected-results" : null
+}
diff --git a/tools/tests/skimage/output-expected/nonexistent-dir/expectations.json b/tools/tests/skimage/output-expected/nonexistent-dir/expectations.json
new file mode 100644 (file)
index 0000000..1ce8e27
--- /dev/null
@@ -0,0 +1,9 @@
+{
+   "actual-results" : {
+      "failed" : null,
+      "failure-ignored" : null,
+      "no-comparison" : null,
+      "succeeded" : null
+   },
+   "expected-results" : null
+}
index cb66fe1..24164d4 100755 (executable)
@@ -9,6 +9,7 @@ import filecmp
 import os
 import subprocess
 import sys
+import tempfile
 
 class BinaryNotFoundException(Exception):
     def __str__ (self):
@@ -70,6 +71,26 @@ def main():
     # TODO(scroggo): Add a test that compares expectations and image files that
     # are known to NOT match, and make sure it returns an error.
 
+    # Generate an expectations file from an empty directory.
+    empty_dir = tempfile.mkdtemp()
+    expectations_path = os.path.join(file_dir, "skimage", "output-actual",
+                                     "empty-dir", "expectations.json")
+    subprocess.check_call([skimage_binary, "--readPath", empty_dir,
+                           "--createExpectationsPath", expectations_path])
+    golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
+                                       "empty-dir", "expectations.json")
+    DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
+    os.rmdir(empty_dir)
+
+    # Generate an expectations file from a nonexistent directory.
+    expectations_path = os.path.join(file_dir, "skimage", "output-actual",
+                                     "nonexistent-dir", "expectations.json")
+    subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir",
+                           "--createExpectationsPath", expectations_path])
+    golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
+                                       "nonexistent-dir", "expectations.json")
+    DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
+
     # Done with all tests.
     print "Self tests succeeded!"