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>
This commit is contained in:
Hal Canary 2017-12-11 14:42:58 -05:00 committed by Skia Commit-Bot
parent c896eddc1c
commit 925e31e749
4 changed files with 64 additions and 15 deletions

View File

@ -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()));

View File

@ -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) {

View File

@ -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()) {

View File

@ -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()) {}