Skip to content

Commit

Permalink
try fix test for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapmex committed Oct 18, 2024
1 parent 7f63726 commit f9b8e95
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/colorer/parsers/ParserFactoryImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void ParserFactory::Impl::loadCatalog(const UnicodeString* catalog_path)
if (!catalog_path || catalog_path->isEmpty()) {
COLORER_LOG_DEBUG("loadCatalog for empty path");

auto env = colorer::Environment::getOSVariable("COLORER_CATALOG");
auto env = colorer::Environment::getOSEnv("COLORER_CATALOG");
if (!env || env->isEmpty()) {
throw ParserFactoryException("Can't find suitable catalog.xml for parse.");
}
Expand Down Expand Up @@ -163,7 +163,7 @@ void ParserFactory::Impl::fillMapper(const UnicodeString& classID, const Unicode
const UnicodeString* name_id;
const UnicodeString name_default(HrdNameDefault);
if (nameID == nullptr) {
auto hrd = colorer::Environment::getOSVariable("COLORER_HRD");
auto hrd = colorer::Environment::getOSEnv("COLORER_HRD");
if (hrd) {
name_id = hrd.get();
}
Expand Down
14 changes: 12 additions & 2 deletions src/colorer/utils/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifdef WIN32
#include <windows.h>
#include <cwchar>
#include <cstdlib>
#endif

namespace colorer {
Expand All @@ -16,7 +17,7 @@ fs::path Environment::to_filepath(const UnicodeString* str)
return result;
}

uUnicodeString Environment::getOSVariable(const UnicodeString& name)
uUnicodeString Environment::getOSEnv(const UnicodeString& name)
{
#ifdef _WINDOWS
COLORER_LOG_DEBUG("get system environment '%'", name);
Expand Down Expand Up @@ -50,6 +51,15 @@ uUnicodeString Environment::getOSVariable(const UnicodeString& name)
#endif
}

void Environment::setOSEnv(const UnicodeString& name, const UnicodeString& value)
{
#ifdef _WINDOWS
_putenv_s(UStr::to_stdstr(&name).c_str(), UStr::to_stdstr(&value).c_str());
#else
setenv( UStr::to_stdstr(&name).c_str(), UStr::to_stdstr(&value).c_str(), 1);
#endif
}

uUnicodeString Environment::normalizePath(const UnicodeString* path)
{
return std::make_unique<UnicodeString>(normalizeFsPath(path).c_str());
Expand Down Expand Up @@ -186,7 +196,7 @@ std::string Environment::expandEnvByRegexp(const std::string& path, const std::r
auto text = path;
while (std::regex_search(text, matcher, regex)) {
result += matcher.prefix().str();
auto env_value = getOSVariable(matcher[1].str().c_str());
auto env_value = getOSEnv(matcher[1].str().c_str());
if (env_value) {
// add expanded value
result += UStr::to_stdstr(env_value);
Expand Down
3 changes: 2 additions & 1 deletion src/colorer/utils/Environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace colorer {
class Environment
{
public:
static uUnicodeString getOSVariable(const UnicodeString& name);
static uUnicodeString getOSEnv(const UnicodeString& name);
static void setOSEnv(const UnicodeString& name, const UnicodeString& value);
static uUnicodeString normalizePath(const UnicodeString* path);
static fs::path normalizeFsPath(const UnicodeString* path);
static fs::path getClearFilePath(const UnicodeString* basePath, const UnicodeString* relPath);
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_xmlinputsource.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <catch2/catch.hpp>
#include <filesystem>
#include "colorer/Common.h"
#include "colorer/utils/Environment.h"
#include "colorer/xml/XmlInputSource.h"

namespace fs = std::filesystem;
Expand Down Expand Up @@ -105,7 +106,7 @@ TEST_CASE("Check expand paths to files from jar-URI")
SECTION("jar-path with env")
{
const char env[]="/home/user2/base/hrc";
setenv("CUR_DIR", env, 1);
colorer::Environment::setOSEnv("CUR_DIR", env);
UnicodeString path_to_file(u"jar:$CUR_DIR/common.zip!base/c.hrc");
UnicodeString full_path(u"jar:/home/user2/base/hrc/common.zip!base/c.hrc");

Expand All @@ -132,7 +133,7 @@ TEST_CASE("Check expand paths to files from jar-URI")
SECTION("jar-path with env relative normal path")
{
constexpr char env[]="/home/user2/base/hrc";
setenv("CUR_DIR", env, 1);
colorer::Environment::setOSEnv("CUR_DIR", env);
UnicodeString path_to_file(u"jar:$CUR_DIR/common.zip!base/c.hrc");
UnicodeString base_path(u"/home/user/base/hrc/proto.hrc");
UnicodeString full_path(u"jar:/home/user2/base/hrc/common.zip!base/c.hrc");
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_xmlreader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <catch2/catch.hpp>
#include <filesystem>
#include "colorer/utils/Environment.h"
#include "colorer/xml/XmlReader.h"
#include "test_common.h"

Expand Down Expand Up @@ -37,7 +38,7 @@ TEST_CASE("Test read entity with env")

UnicodeString path1(u"data/catalog-env.xml");
auto work_dir = fs::current_path();
setenv("CUR_DIR", work_dir.c_str(), 1);
colorer::Environment::setOSEnv("CUR_DIR", work_dir.c_str());
XmlInputSource is(path1);
std::unique_ptr<XmlReader> test_reader;
REQUIRE_NOTHROW(test_reader = std::make_unique<XmlReader>(is));
Expand Down Expand Up @@ -66,7 +67,7 @@ TEST_CASE("Test read jar entity with env")

UnicodeString path1(u"data/catalog-allpacked-env.xml");
auto work_dir = fs::current_path();
setenv("CUR_DIR", work_dir.c_str(), 1);
colorer::Environment::setOSEnv("CUR_DIR", work_dir.c_str());
XmlInputSource is(path1);
std::unique_ptr<XmlReader> test_reader;
REQUIRE_NOTHROW(test_reader = std::make_unique<XmlReader>(is));
Expand Down

0 comments on commit f9b8e95

Please sign in to comment.