Li, Hao | 0e1bef3 | 2019-11-07 12:13:27 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | #include "common/SystemUtils.h" |
| 18 | |
| 19 | // Tests for GetEnvironmentVar |
| 20 | TEST(SystemUtilsTests, GetEnvironmentVar) { |
| 21 | // Test nonexistent environment variable |
| 22 | ASSERT_EQ(GetEnvironmentVar("NonexistentEnvironmentVar"), ""); |
| 23 | } |
| 24 | |
| 25 | // Tests for SetEnvironmentVar |
| 26 | TEST(SystemUtilsTests, SetEnvironmentVar) { |
| 27 | // Test new environment variable |
| 28 | ASSERT_TRUE(SetEnvironmentVar("EnvironmentVarForTest", "NewEnvironmentVarValue")); |
| 29 | ASSERT_EQ(GetEnvironmentVar("EnvironmentVarForTest"), "NewEnvironmentVarValue"); |
| 30 | // Test override environment variable |
| 31 | ASSERT_TRUE(SetEnvironmentVar("EnvironmentVarForTest", "OverrideEnvironmentVarValue")); |
| 32 | ASSERT_EQ(GetEnvironmentVar("EnvironmentVarForTest"), "OverrideEnvironmentVarValue"); |
| 33 | } |
| 34 | |
| 35 | // Tests for GetExecutableDirectory |
| 36 | TEST(SystemUtilsTests, GetExecutableDirectory) { |
| 37 | // Test returned value is non-empty string |
| 38 | ASSERT_NE(GetExecutableDirectory(), ""); |
| 39 | // Test last charecter in path |
| 40 | ASSERT_EQ(GetExecutableDirectory().back(), *GetPathSeparator()); |
| 41 | } |
Austin Eng | 295a417 | 2020-05-28 16:05:04 +0000 | [diff] [blame] | 42 | |
| 43 | // Tests for ScopedEnvironmentVar |
| 44 | TEST(SystemUtilsTests, ScopedEnvironmentVar) { |
| 45 | SetEnvironmentVar("ScopedEnvironmentVarForTest", "original"); |
| 46 | |
| 47 | // Test empty environment variable doesn't crash |
| 48 | { ScopedEnvironmentVar var; } |
| 49 | |
| 50 | // Test setting empty environment variable |
| 51 | { |
| 52 | ScopedEnvironmentVar var; |
| 53 | var.Set("ScopedEnvironmentVarForTest", "NewEnvironmentVarValue"); |
| 54 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "NewEnvironmentVarValue"); |
| 55 | } |
| 56 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "original"); |
| 57 | |
| 58 | // Test that the environment variable can be set, and it is unset at the end of the scope. |
| 59 | { |
| 60 | ScopedEnvironmentVar var("ScopedEnvironmentVarForTest", "NewEnvironmentVarValue"); |
| 61 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "NewEnvironmentVarValue"); |
| 62 | } |
| 63 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "original"); |
| 64 | |
| 65 | // Test nested scopes |
| 66 | { |
| 67 | ScopedEnvironmentVar outer("ScopedEnvironmentVarForTest", "outer"); |
| 68 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "outer"); |
| 69 | { |
| 70 | ScopedEnvironmentVar inner("ScopedEnvironmentVarForTest", "inner"); |
| 71 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "inner"); |
| 72 | } |
| 73 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "outer"); |
| 74 | } |
| 75 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "original"); |
| 76 | |
| 77 | // Test redundantly setting scoped variables |
| 78 | { |
| 79 | ScopedEnvironmentVar var1("ScopedEnvironmentVarForTest", "var1"); |
| 80 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "var1"); |
| 81 | |
| 82 | ScopedEnvironmentVar var2("ScopedEnvironmentVarForTest", "var2"); |
| 83 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "var2"); |
| 84 | } |
| 85 | ASSERT_EQ(GetEnvironmentVar("ScopedEnvironmentVarForTest"), "original"); |
| 86 | } |