/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#include "Config.h"
#include "Define.h"
#include "gtest/gtest.h"
#include
#include
#include
#include
#if WIN32
void inline setenv(const char* name, const char* value, int overwrite)
{
_putenv_s(name, value);
}
#endif
std::string CreateConfigWithMap(std::map const& map)
{
auto mTempFileRel = boost::filesystem::unique_path("deleteme.ini");
auto mTempFileAbs = boost::filesystem::temp_directory_path() / mTempFileRel;
std::ofstream iniStream;
iniStream.open(mTempFileAbs.c_str());
iniStream << "[test]\n";
for (auto const& itr : map)
iniStream << itr.first << " = " << itr.second << "\n";
iniStream.close();
#if WIN32
auto tmp = mTempFileAbs.native();
return std::string(tmp.begin(), tmp.end());
#else
return mTempFileAbs.native();
#endif
}
class ConfigEnvTest : public testing::Test {
protected:
void SetUp() override {
std::map config;
config["Int.Nested"] = "4242";
config["lower"] = "simpleString";
config["UPPER"] = "simpleString";
config["SomeLong.NestedNameWithNumber.Like1"] = "1";
config["GM.InGMList.Level"] = "50";
confFilePath = CreateConfigWithMap(config);
sConfigMgr->Configure(confFilePath, std::vector());
sConfigMgr->LoadAppConfigs();
}
void TearDown() override {
std::remove(confFilePath.c_str());
}
std::string confFilePath;
};
TEST_F(ConfigEnvTest, NestedInt)
{
EXPECT_EQ(sConfigMgr->GetOption("Int.Nested", 10), 4242);
setenv("AC_INT_NESTED", "8080", 1);
EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
EXPECT_EQ(sConfigMgr->GetOption("Int.Nested", 10), 8080);
}
TEST_F(ConfigEnvTest, SimpleLowerString)
{
EXPECT_EQ(sConfigMgr->GetOption("lower", ""), "simpleString");
setenv("AC_LOWER", "envstring", 1);
EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
EXPECT_EQ(sConfigMgr->GetOption("lower", ""), "envstring");
}
TEST_F(ConfigEnvTest, SimpleUpperString)
{
EXPECT_EQ(sConfigMgr->GetOption("UPPER", ""), "simpleString");
setenv("AC_UPPER", "envupperstring", 1);
EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
EXPECT_EQ(sConfigMgr->GetOption("UPPER", ""), "envupperstring");
}
TEST_F(ConfigEnvTest, LongNestedNameWithNumber)
{
EXPECT_EQ(sConfigMgr->GetOption("SomeLong.NestedNameWithNumber.Like1", 0), 1);
setenv("AC_SOME_LONG_NESTED_NAME_WITH_NUMBER_LIKE_1", "42", 1);
EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
EXPECT_EQ(sConfigMgr->GetOption("SomeLong.NestedNameWithNumber.Like1", 0), 42);
}
TEST_F(ConfigEnvTest, ValueWithSeveralUpperlLaters)
{
EXPECT_EQ(sConfigMgr->GetOption("GM.InGMList.Level", 1), 50);
setenv("AC_GM_IN_GMLIST_LEVEL", "42", 1);
EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
EXPECT_EQ(sConfigMgr->GetOption("GM.InGMList.Level", 0), 42);
}
TEST_F(ConfigEnvTest, StringThatNotExistInConfig)
{
setenv("AC_UNIQUE_STRING", "somevalue", 1);
EXPECT_EQ(sConfigMgr->GetOption("Unique.String", ""), "somevalue");
}
TEST_F(ConfigEnvTest, IntThatNotExistInConfig)
{
setenv("AC_UNIQUE_INT", "100", 1);
EXPECT_EQ(sConfigMgr->GetOption("Unique.Int", 1), 100);
}
TEST_F(ConfigEnvTest, NotExistingString)
{
EXPECT_EQ(sConfigMgr->GetOption("NotFound.String", "none"), "none");
}
TEST_F(ConfigEnvTest, NotExistingInt)
{
EXPECT_EQ(sConfigMgr->GetOption("NotFound.Int", 1), 1);
}