Fix bug in setting directories for tests.
authorscroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 6 Jun 2013 14:59:56 +0000 (14:59 +0000)
committerscroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 6 Jun 2013 14:59:56 +0000 (14:59 +0000)
make_canonical_dir_path only worked if the provided directory
did not end with a slash. Remove this function, and call
SkPathJoin instead. Update the documentation to acknowledge
that this is an acceptable use of SkPathJoin, and update its
test.

R=epoger@google.com

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

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

include/core/SkOSFile.h
tests/OSPathTest.cpp
tests/StreamTest.cpp
tests/Test.h
tests/skia_test.cpp

index f8ce06bcaf02a6af3b3669c5cceee1fc859958c2..b75fe6cf7c962152018cc10683e0f26703634316 100644 (file)
@@ -138,7 +138,9 @@ class SkOSPath {
 public:
     /**
      * Assembles rootPath and relativePath into a single path, like this:
-     * rootPath/relativePath
+     * rootPath/relativePath.
+     * It is okay to call with a NULL rootPath and/or relativePath. A path
+     * separator will still be inserted.
      *
      * Uses SkPATH_SEPARATOR, to work on all platforms.
      */
index 96ff8a712d173242e04ca2efb0470e161be80187..5ae32ebc0d352f854dd3d39999cfc5273d0e55bc 100644 (file)
@@ -59,6 +59,12 @@ static void test_os_path_utils_tests(skiatest::Reporter* reporter) {
     dir.appendUnichar(SkPATH_SEPARATOR);
     test_dir_with_file(reporter, dir, filename);
 
+    // Test using no filename.
+    test_dir_with_file(reporter, dir, SkString());
+
+    // Testing using no directory.
+    test_dir_with_file(reporter, SkString(), filename);
+
     // Test with a sub directory.
     dir.append("subDir");
     test_dir_with_file(reporter, dir, filename);
@@ -71,6 +77,11 @@ static void test_os_path_utils_tests(skiatest::Reporter* reporter) {
     // Basename of NULL is an empty string.
     SkString empty = SkOSPath::SkBasename(NULL);
     REPORTER_ASSERT(reporter, empty.size() == 0);
+
+    // Test that NULL can be used for the directory and filename.
+    SkString emptyPath = SkOSPath::SkPathJoin(NULL, NULL);
+    REPORTER_ASSERT(reporter, emptyPath.size() == 1);
+    REPORTER_ASSERT(reporter, emptyPath.contains(SkPATH_SEPARATOR));
 }
 
 #include "TestClassDef.h"
index 8f38f46bea9ac4af4017936f3c416f1b2d9fa01d..00c079abc0436251fd81c864b07b122748055d97 100644 (file)
@@ -35,8 +35,7 @@ static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream,
 }
 
 static void test_filestreams(skiatest::Reporter* reporter, const char* tmpDir) {
-    SkString path;
-    path.printf("%s%s", tmpDir, "wstream_test");
+    SkString path = SkOSPath::SkPathJoin(tmpDir, "wstream_test");
 
     const char s[] = "abcdefghijklmnopqrstuvwxyz";
 
@@ -98,8 +97,9 @@ static void TestWStream(skiatest::Reporter* reporter) {
     }
     delete[] dst;
 
-    if (!skiatest::Test::GetTmpDir().isEmpty()) {
-        test_filestreams(reporter, skiatest::Test::GetTmpDir().c_str());
+    SkString tmpDir = skiatest::Test::GetTmpDir();
+    if (!tmpDir.isEmpty()) {
+        test_filestreams(reporter, tmpDir.c_str());
     }
 }
 
index dfa12a6dfcd57f4f34169fbe1959f4ac5cd4d01b..8cb23c1b48b2728a83cb582621f54845f6705700 100644 (file)
@@ -75,9 +75,9 @@ namespace skiatest {
         bool passed() const { return fPassed; }
         SkMSec elapsedMs() const { return fElapsed; }
 
-        static const SkString& GetTmpDir();
+        static SkString GetTmpDir();
 
-        static const SkString& GetResourcePath();
+        static SkString GetResourcePath();
 
         virtual bool isThreadsafe() const { return true; }
 
index 238252d7377629e19f193b1f7de93d945fa75b0b..8cae6560893fcd6224f8bb5541a25d1db708983f 100644 (file)
@@ -112,34 +112,6 @@ private:
     bool fAllowThreaded;
 };
 
-static const char* make_canonical_dir_path(const char* path, SkString* storage) {
-    if (path) {
-        // clean it up so it always has a trailing searator
-        size_t len = strlen(path);
-        if (0 == len) {
-            path = NULL;
-        } else if (SkPATH_SEPARATOR != path[len - 1]) {
-            // resize to len + 1, to make room for searator
-            storage->set(path, len + 1);
-            storage->writable_str()[len] = SkPATH_SEPARATOR;
-            path = storage->c_str();
-        }
-    }
-    return path;
-}
-
-static SkString gTmpDir;
-
-const SkString& Test::GetTmpDir() {
-    return gTmpDir;
-}
-
-static SkString gResourcePath;
-
-const SkString& Test::GetResourcePath() {
-    return gResourcePath;
-}
-
 DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \
                                "Multiple matches may be separated by spaces.\n" \
                                "~ causes a matching test to always be skipped\n" \
@@ -156,6 +128,16 @@ DEFINE_bool2(verbose, v, false, "enable verbose output.");
 DEFINE_int32(threads, SkThreadPool::kThreadPerCore,
              "Run threadsafe tests on a threadpool with this many threads.");
 
+SkString Test::GetTmpDir() {
+    const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0];
+    return SkString(tmpDir);
+}
+
+SkString Test::GetResourcePath() {
+    const char* resourcePath = FLAGS_resourcePath.isEmpty() ? NULL : FLAGS_resourcePath[0];
+    return SkString(resourcePath);
+}
+
 // Deletes self when run.
 class SkTestRunnable : public SkRunnable {
 public:
@@ -218,13 +200,6 @@ int tool_main(int argc, char** argv) {
     SkCommandLineFlags::SetUsage("");
     SkCommandLineFlags::Parse(argc, argv);
 
-    if (!FLAGS_tmpDir.isEmpty()) {
-        make_canonical_dir_path(FLAGS_tmpDir[0], &gTmpDir);
-    }
-    if (!FLAGS_resourcePath.isEmpty()) {
-        make_canonical_dir_path(FLAGS_resourcePath[0], &gResourcePath);
-    }
-
 #if SK_ENABLE_INST_COUNT
     gPrintInstCount = true;
 #endif
@@ -239,11 +214,13 @@ int tool_main(int argc, char** argv) {
                 header.appendf(" %s", FLAGS_match[index]);
             }
         }
-        if (!gTmpDir.isEmpty()) {
-            header.appendf(" --tmpDir %s", gTmpDir.c_str());
+        SkString tmpDir = Test::GetTmpDir();
+        if (!tmpDir.isEmpty()) {
+            header.appendf(" --tmpDir %s", tmpDir.c_str());
         }
-        if (!gResourcePath.isEmpty()) {
-            header.appendf(" --resourcePath %s", gResourcePath.c_str());
+        SkString resourcePath = Test::GetResourcePath();
+        if (!resourcePath.isEmpty()) {
+            header.appendf(" --resourcePath %s", resourcePath.c_str());
         }
 #ifdef SK_DEBUG
         header.append(" SK_DEBUG");