From 966b7892b28e484e518592488da7cc7768a7b4a4 Mon Sep 17 00:00:00 2001 From: Kan Tang Date: Sun, 11 Oct 2020 23:49:30 -0700 Subject: [PATCH] Avoid test case throw meaningless exception when sys env is not set. (#723) * Avoid test case throw meaningless exception when sys env is not set. * Update sdk/storage/azure-storage-common/test/test_base.cpp Co-authored-by: JinmingHu * Update sdk/storage/azure-storage-common/test/test_base.cpp Co-authored-by: JinmingHu * Update sdk/storage/azure-storage-common/test/test_base.cpp Co-authored-by: JinmingHu * Apply clang format Co-authored-by: JinmingHu --- .../azure-storage-common/test/test_base.cpp | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/sdk/storage/azure-storage-common/test/test_base.cpp b/sdk/storage/azure-storage-common/test/test_base.cpp index f41834ad3..53777d96d 100644 --- a/sdk/storage/azure-storage-common/test/test_base.cpp +++ b/sdk/storage/azure-storage-common/test/test_base.cpp @@ -31,6 +31,17 @@ namespace Azure { namespace Storage { namespace Test { constexpr static const char* c_AadClientId = ""; constexpr static const char* c_AadClientSecret = ""; + std::string GetEnv(const std::string& name) + { + const char* ret = std::getenv(name.data()); + if (!ret) + { + throw std::runtime_error( + name + " is required to run the tests but not set as an environment variable."); + } + return std::string(ret); + } + const std::string& StandardStorageConnectionString() { const static std::string connectionString = []() -> std::string { @@ -38,7 +49,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_StandardStorageConnectionString; } - return std::getenv("STANDARD_STORAGE_CONNECTION_STRING"); + return GetEnv("STANDARD_STORAGE_CONNECTION_STRING"); }(); return connectionString; } @@ -50,7 +61,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_PremiumStorageConnectionString; } - return std::getenv("PREMIUM_STORAGE_CONNECTION_STRING"); + return GetEnv("PREMIUM_STORAGE_CONNECTION_STRING"); }(); return connectionString; } @@ -62,7 +73,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_BlobStorageConnectionString; } - return std::getenv("BLOB_STORAGE_CONNECTION_STRING"); + return GetEnv("BLOB_STORAGE_CONNECTION_STRING"); }(); return connectionString; } @@ -74,7 +85,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_PremiumFileConnectionString; } - return std::getenv("PREMIUM_FILE_CONNECTION_STRING"); + return GetEnv("PREMIUM_FILE_CONNECTION_STRING"); }(); return connectionString; } @@ -86,7 +97,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_AdlsGen2ConnectionString; } - return std::getenv("ADLS_GEN2_CONNECTION_STRING"); + return GetEnv("ADLS_GEN2_CONNECTION_STRING"); }(); return connectionString; } @@ -98,7 +109,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_AadTenantId; } - return std::getenv("AAD_TENANT_ID"); + return GetEnv("AAD_TENANT_ID"); }(); return connectionString; } @@ -110,7 +121,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_AadClientId; } - return std::getenv("AAD_CLIENT_ID"); + return GetEnv("AAD_CLIENT_ID"); }(); return connectionString; } @@ -122,7 +133,7 @@ namespace Azure { namespace Storage { namespace Test { { return c_AadClientSecret; } - return std::getenv("AAD_CLIENT_SECRET"); + return GetEnv("AAD_CLIENT_SECRET"); }(); return connectionString; } @@ -296,5 +307,4 @@ namespace Azure { namespace Storage { namespace Test { return secondaryUri.GetAbsoluteUrl(); } - }}} // namespace Azure::Storage::Test