diff options
author | Hal Canary <halcanary@google.com> | 2017-12-11 14:42:58 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-12-12 18:22:39 +0000 |
commit | 925e31e749fc369ebd2ebd7994693b767e674e97 (patch) | |
tree | 1c667218a5d94e0b2a8d3d35c209a8def119ebec | |
parent | c896eddc1cf77ffaaa0d0cb68caac7e251cdb727 (diff) |
dm: require tmpDir, reasonable defaults
Change-Id: I9d84ce1ebbe417160a29ca2221b1df04901238e3
Reviewed-on: https://skia-review.googlesource.com/83541
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
-rw-r--r-- | tests/PDFDocumentTest.cpp | 18 | ||||
-rw-r--r-- | tests/StreamBufferTest.cpp | 18 | ||||
-rw-r--r-- | tests/StreamTest.cpp | 21 | ||||
-rw-r--r-- | tests/Test.cpp | 22 |
4 files changed, 64 insertions, 15 deletions
diff --git a/tests/PDFDocumentTest.cpp b/tests/PDFDocumentTest.cpp index 9a79ec8c01..82dafa3f15 100644 --- a/tests/PDFDocumentTest.cpp +++ b/tests/PDFDocumentTest.cpp @@ -44,12 +44,15 @@ static void test_abortWithFile(skiatest::Reporter* reporter) { SkString tmpDir = skiatest::GetTmpDir(); if (tmpDir.isEmpty()) { - return; // TODO(edisonn): unfortunatelly this pattern is used in other - // tests, but if GetTmpDir() starts returning and empty dir - // allways, then all these tests will be disabled. + ERRORF(reporter, "missing tmpDir."); + return; } SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf"); + if (!SkFILEWStream(path.c_str()).isValid()) { + ERRORF(reporter, "unable to write to: %s", path.c_str()); + return; + } // Make sure doc's destructor is called to flush. { @@ -72,12 +75,15 @@ static void test_abortWithFile(skiatest::Reporter* reporter) { static void test_file(skiatest::Reporter* reporter) { SkString tmpDir = skiatest::GetTmpDir(); if (tmpDir.isEmpty()) { - return; // TODO(edisonn): unfortunatelly this pattern is used in other - // tests, but if GetTmpDir() starts returning and empty dir - // allways, then all these tests will be disabled. + ERRORF(reporter, "missing tmpDir."); + return; } SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf"); + if (!SkFILEWStream(path.c_str()).isValid()) { + ERRORF(reporter, "unable to write to: %s", path.c_str()); + return; + } sk_sp<SkDocument> doc(SkDocument::MakePDF(path.c_str())); diff --git a/tests/StreamBufferTest.cpp b/tests/StreamBufferTest.cpp index 35bebad274..5435bf9365 100644 --- a/tests/StreamBufferTest.cpp +++ b/tests/StreamBufferTest.cpp @@ -28,6 +28,9 @@ static void test_get_data_at_position(skiatest::Reporter* r, SkStreamBuffer* buf // Test buffering from the beginning, by different amounts. static void test_buffer_from_beginning(skiatest::Reporter* r, std::unique_ptr<SkStream> stream, size_t length) { + if (!stream) { + return; + } SkStreamBuffer buffer(std::move(stream)); // Buffer an arbitrary amount: @@ -46,6 +49,9 @@ static void test_buffer_from_beginning(skiatest::Reporter* r, std::unique_ptr<Sk // Test flushing the stream as we read. static void test_flushing(skiatest::Reporter* r, std::unique_ptr<SkStream> stream, size_t length, bool getDataAtPosition) { + if (!stream) { + return; + } SkStreamBuffer buffer(std::move(stream)); const size_t step = 5; for (size_t position = 0; position + step <= length; position += step) { @@ -78,6 +84,10 @@ DEF_TEST(StreamBuffer, r) { if (!tmpDir.isEmpty()) { path = SkOSPath::Join(tmpDir.c_str(), subdir); SkFILEWStream writer(path.c_str()); + if (!writer.isValid()) { + ERRORF(r, "unable to write to '%s'\n", path.c_str()); + return; + } writer.write(gText, size); } @@ -85,9 +95,11 @@ DEF_TEST(StreamBuffer, r) { std::function<std::unique_ptr<SkStream>()> createStream; bool skipIfNoTmpDir; } factories[] = { - { [&data]() { return skstd::make_unique<SkMemoryStream>(data); }, false }, - { [&data]() { return skstd::make_unique<NotAssetMemStream>(data); }, false }, - { [&path]() { return skstd::make_unique<SkFILEStream>(path.c_str()); }, true }, + { [&data]() { return skstd::make_unique<SkMemoryStream>(data); }, false }, + { [&data]() { return skstd::make_unique<NotAssetMemStream>(data); }, false }, + { [&path]() { return path.isEmpty() + ? nullptr + : skstd::make_unique<SkFILEStream>(path.c_str()); }, true }, }; for (auto f : factories) { diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp index 24e74c64b7..01967b70b0 100644 --- a/tests/StreamTest.cpp +++ b/tests/StreamTest.cpp @@ -287,12 +287,25 @@ DEF_TEST(StreamPeek, reporter) { test_fully_peekable_stream(reporter, &memStream, memStream.getLength()); // Test an arbitrary file stream. file streams do not support peeking. - constexpr char filename[] = "images/baby_tux.webp"; - SkString path = GetResourcePath(filename); - if (!sk_exists(path.c_str())) { - ERRORF(reporter, "file missing: %s\n", filename); + auto tmpdir = skiatest::GetTmpDir(); + if (tmpdir.isEmpty()) { + ERRORF(reporter, "no tmp dir!"); return; } + auto path = SkOSPath::Join(tmpdir.c_str(), "file"); + { + SkFILEWStream wStream(path.c_str()); + constexpr char filename[] = "images/baby_tux.webp"; + auto data = GetResourceAsData(filename); + if (!data || data->size() == 0) { + ERRORF(reporter, "resource missing: %s\n", filename); + return; + } + if (!wStream.isValid() || !wStream.write(data->data(), data->size())) { + ERRORF(reporter, "error wrtiting to file %s", path.c_str()); + return; + } + } SkFILEStream fileStream(path.c_str()); REPORTER_ASSERT(reporter, fileStream.isValid()); if (!fileStream.isValid()) { diff --git a/tests/Test.cpp b/tests/Test.cpp index 37515ddf42..96bbbad9b9 100644 --- a/tests/Test.cpp +++ b/tests/Test.cpp @@ -7,6 +7,8 @@ #include "Test.h" +#include <stdlib.h> + #include "SkCommandLineFlags.h" #include "SkString.h" #include "SkTime.h" @@ -32,8 +34,24 @@ SkString skiatest::Failure::toString() const { } SkString skiatest::GetTmpDir() { - const char* tmpDir = FLAGS_tmpDir.isEmpty() ? nullptr : FLAGS_tmpDir[0]; - return SkString(tmpDir); + if (!FLAGS_tmpDir.isEmpty()) { + return SkString(FLAGS_tmpDir[0]); + } +#ifdef SK_BUILD_FOR_ANDROID + const char* environmentVariable = "TMPDIR"; + const char* defaultValue = "/data/local/tmp"; +#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) + const char* environmentVariable = "TMPDIR"; + const char* defaultValue = "/tmp"; +#elif defined(SK_BUILD_FOR_WIN32) + const char* environmentVariable = "TEMP"; + const char* defaultValue = nullptr; +#else + const char* environmentVariable = nullptr; + const char* defaultValue = nullptr; +#endif + const char* tmpdir = environmentVariable ? getenv(environmentVariable) : nullptr; + return SkString(tmpdir ? tmpdir : defaultValue); } skiatest::Timer::Timer() : fStartNanos(SkTime::GetNSecs()) {} |