dawn-cmake/src/tests/unittests/SystemUtilsTests.cpp
Austin Eng 09c308cfcf Improve ScopedEnvironmentVar to distinguish between null and empty
Found while investigating Swiftshader support. Some Vulkan loaders
fail to create an instance if VK_ICD_FILENAMES is empty string
rather than entirely absent. It was set to empty string because
Dawn did not distinguish between nonexistent environment variables
and the empty string. This CL adds distinguishing between the two,
including tests for the behavior.

Bug: chromium:1266550
Change-Id: I1680a281f62e6b340009e01da65db9d485e2975e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/69520
Auto-Submit: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Austin Eng <enga@chromium.org>
2021-11-16 23:37:15 +00:00

116 lines
4.8 KiB
C++

// Copyright 2019 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "common/Assert.h"
#include "common/SystemUtils.h"
using ::testing::_;
using ::testing::Pair;
// Tests for GetEnvironmentVar
TEST(SystemUtilsTests, GetEnvironmentVar) {
// Test nonexistent environment variable
EXPECT_THAT(GetEnvironmentVar("NonexistentEnvironmentVar"), Pair("", false));
}
// Tests for SetEnvironmentVar
TEST(SystemUtilsTests, SetEnvironmentVar) {
// Test new environment variable
EXPECT_TRUE(SetEnvironmentVar("EnvironmentVarForTest", "NewEnvironmentVarValue"));
EXPECT_THAT(GetEnvironmentVar("EnvironmentVarForTest"), Pair("NewEnvironmentVarValue", true));
// Test override environment variable
EXPECT_TRUE(SetEnvironmentVar("EnvironmentVarForTest", "OverrideEnvironmentVarValue"));
EXPECT_THAT(GetEnvironmentVar("EnvironmentVarForTest"),
Pair("OverrideEnvironmentVarValue", true));
}
// Tests for GetExecutableDirectory
TEST(SystemUtilsTests, GetExecutableDirectory) {
// Test returned value is non-empty string
EXPECT_NE(GetExecutableDirectory(), "");
// Test last charecter in path
EXPECT_EQ(GetExecutableDirectory().back(), *GetPathSeparator());
}
// Tests for ScopedEnvironmentVar
TEST(SystemUtilsTests, ScopedEnvironmentVar) {
SetEnvironmentVar("ScopedEnvironmentVarForTest", "original");
// Test empty environment variable doesn't crash
{ ScopedEnvironmentVar var; }
// Test setting empty environment variable
{
ScopedEnvironmentVar var;
var.Set("ScopedEnvironmentVarForTest", "NewEnvironmentVarValue");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"),
Pair("NewEnvironmentVarValue", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("original", true));
// Test that the environment variable can be set, and it is unset at the end of the scope.
{
ScopedEnvironmentVar var("ScopedEnvironmentVarForTest", "NewEnvironmentVarValue");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"),
Pair("NewEnvironmentVarValue", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("original", true));
// Test nested scopes
{
ScopedEnvironmentVar outer("ScopedEnvironmentVarForTest", "outer");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("outer", true));
{
ScopedEnvironmentVar inner("ScopedEnvironmentVarForTest", "inner");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("inner", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("outer", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("original", true));
// Test redundantly setting scoped variables
{
ScopedEnvironmentVar var1("ScopedEnvironmentVarForTest", "var1");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("var1", true));
ScopedEnvironmentVar var2("ScopedEnvironmentVarForTest", "var2");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("var2", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("original", true));
}
// Test that restoring a scoped environment variable to the empty string.
TEST(SystemUtilsTests, ScopedEnvironmentVarRestoresEmptyString) {
ScopedEnvironmentVar empty("ScopedEnvironmentVarForTest", "");
{
ScopedEnvironmentVar var1("ScopedEnvironmentVarForTest", "var1");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("var1", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("", true));
}
// Test that restoring a scoped environment variable to not set (distuishable from empty string)
// works.
TEST(SystemUtilsTests, ScopedEnvironmentVarRestoresNotSet) {
ScopedEnvironmentVar null("ScopedEnvironmentVarForTest", nullptr);
{
ScopedEnvironmentVar var1("ScopedEnvironmentVarForTest", "var1");
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("var1", true));
}
EXPECT_THAT(GetEnvironmentVar("ScopedEnvironmentVarForTest"), Pair("", false));
}