EnvTests.cpp (3539B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 4 #include "Env.h" 5 6 #include <catch2/catch_test_macros.hpp> 7 8 #define TEST(...) TEST_CASE("[EnvTests] " __VA_ARGS__) 9 10 using namespace SFS::details::env; 11 12 TEST("Testing GetEnv()") 13 { 14 SECTION("Testing GetEnv() on an existing environment variable") 15 { 16 // Get the value of an existing environment variable per platform 17 #ifdef _WIN32 18 const std::string varName = "COMPUTERNAME"; 19 #else 20 const std::string varName = "LANG"; 21 #endif 22 auto env = GetEnv(varName); 23 REQUIRE(env.has_value()); 24 REQUIRE(env.value().size() > 0); 25 } 26 27 SECTION("Testing GetEnv() on a non-existing variable") 28 { 29 auto env = GetEnv("DUMMYVARIABLESHOULDNOTEXIST"); 30 REQUIRE(!env.has_value()); 31 } 32 } 33 34 TEST("Testing SetEnv()") 35 { 36 SECTION("Testing SetEnv() on a non-existing variable") 37 { 38 auto env = GetEnv("DUMMYVARIABLE"); 39 REQUIRE(!env.has_value()); 40 41 REQUIRE(SetEnv("DUMMYVARIABLE", "dummyValue")); 42 env = GetEnv("DUMMYVARIABLE"); 43 REQUIRE(env.has_value()); 44 REQUIRE(*env == "dummyValue"); 45 46 SECTION("Testing SetEnv() on an existing variable overwrites it") 47 { 48 REQUIRE(SetEnv("DUMMYVARIABLE", "dummyValue2")); 49 50 env = GetEnv("DUMMYVARIABLE"); 51 REQUIRE(env.has_value()); 52 REQUIRE(*env == "dummyValue2"); 53 } 54 55 INFO("Unsetting the environment variable"); 56 REQUIRE(UnsetEnv("DUMMYVARIABLE")); 57 env = GetEnv("DUMMYVARIABLE"); 58 REQUIRE(!env.has_value()); 59 } 60 61 SECTION("Testing SetEnv() with empty strings fails") 62 { 63 REQUIRE_FALSE(SetEnv("DUMMYVARIABLE", "")); 64 REQUIRE_FALSE(SetEnv("DUMMYVARIABLE", std::string())); 65 REQUIRE_FALSE(SetEnv("", "dummy")); 66 REQUIRE_FALSE(SetEnv(std::string(), "dummy")); 67 REQUIRE_FALSE(SetEnv(std::string(), std::string())); 68 } 69 } 70 71 TEST("Testing UnsetEnv()") 72 { 73 SECTION("Testing UnsetEnv() on a non-existing variable still succeeds") 74 { 75 auto env = GetEnv("DUMMYVARIABLE"); 76 REQUIRE(!env.has_value()); 77 78 REQUIRE(UnsetEnv("DUMMYVARIABLE")); 79 } 80 } 81 82 TEST("Testing ScopedEnv") 83 { 84 SECTION("Testing ScopedEnv on a non-existing variable") 85 { 86 auto env = GetEnv("DUMMYVARIABLE"); 87 REQUIRE(!env.has_value()); 88 89 { 90 ScopedEnv scopedEnv("DUMMYVARIABLE", "dummyValue"); 91 92 INFO("Variable exists within scope"); 93 env = GetEnv("DUMMYVARIABLE"); 94 REQUIRE(env.has_value()); 95 REQUIRE(*env == "dummyValue"); 96 97 SECTION("Testing ScopedEnv on an existing variable overwrites it") 98 { 99 { 100 ScopedEnv scopedEnv2("DUMMYVARIABLE", "dummyValue2"); 101 102 INFO("Variable has value overwritten within scope"); 103 env = GetEnv("DUMMYVARIABLE"); 104 REQUIRE(env.has_value()); 105 REQUIRE(*env == "dummyValue2"); 106 } 107 108 INFO("Variable goes back to previous value"); 109 env = GetEnv("DUMMYVARIABLE"); 110 REQUIRE(env.has_value()); 111 REQUIRE(*env == "dummyValue"); 112 } 113 } 114 115 INFO("Variable should be unset after the scope ends"); 116 env = GetEnv("DUMMYVARIABLE"); 117 REQUIRE(!env.has_value()); 118 } 119 }