Skip to content

Commit

Permalink
Switch to utils version of env functions (#854)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <michael@openrobotics.org>
Co-authored-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
mjcarroll and scpeters authored Feb 21, 2022
1 parent bce7061 commit c2ef096
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 195 deletions.
11 changes: 6 additions & 5 deletions src/Console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "sdf/Types.hh"
#include "sdf/sdf_config.h"

#include <ignition/utils/Environment.hh>

using namespace sdf;

/// Static pointer to the console.
Expand All @@ -49,13 +51,12 @@ Console::Console()
#ifndef SDFORMAT_DISABLE_CONSOLE_LOGFILE
// Set up the file that we'll log to.
#ifndef _WIN32
const char *home = std::getenv("HOME");
std::string homeVarName = "HOME";
#else
char *home;
size_t sz = 0;
_dupenv_s(&home, &sz, "HOMEPATH");
std::string homeVarName = "HOMEPATH";
#endif
if (!home)
std::string home;
if (!ignition::utils::env(homeVarName, home))
{
std::cerr << "No HOME defined in the environment. Will not log."
<< std::endl;
Expand Down
17 changes: 7 additions & 10 deletions src/Console_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <gtest/gtest.h>

#include <ignition/utils/Environment.hh>

#ifndef _WIN32
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -30,14 +32,9 @@
bool create_new_temp_dir(std::string &_new_temp_path)
{
std::string tmppath;
const char *tmp = getenv("TMPDIR");
if (tmp)
{
tmppath = std::string(tmp);
}
else
if(!ignition::utils::env("TMPDIR", tmppath))
{
tmppath = std::string("/tmp");
tmppath = "/tmp";
}

tmppath += "/XXXXXX";
Expand All @@ -57,7 +54,7 @@ bool create_new_temp_dir(std::string &_new_temp_path)
TEST(Console, nohome)
{
sdf::Console::Clear();
unsetenv("HOME");
ignition::utils::unsetenv("HOME");

sdferr << "Error.\n";
}
Expand All @@ -69,7 +66,7 @@ TEST(Console, logdir_is_file)

std::string temp_dir;
ASSERT_TRUE(create_new_temp_dir(temp_dir));
ASSERT_EQ(setenv("HOME", temp_dir.c_str(), 1), 0);
ASSERT_TRUE(ignition::utils::setenv("HOME", temp_dir));

std::string sdf_file = temp_dir + "/.sdformat";

Expand All @@ -89,7 +86,7 @@ TEST(Console, logdir_doesnt_exist)
std::string temp_dir;
ASSERT_TRUE(create_new_temp_dir(temp_dir));
temp_dir += "/foobarbaz";
ASSERT_EQ(setenv("HOME", temp_dir.c_str(), 1), 0);
ASSERT_TRUE(ignition::utils::setenv("HOME", temp_dir));

sdferr << "Error.\n";
}
Expand Down
11 changes: 4 additions & 7 deletions src/Filesystem_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <gtest/gtest.h>

#include <ignition/utils/Environment.hh>

#ifndef _WIN32
#include <fcntl.h>
#include <limits.h>
Expand All @@ -34,14 +36,9 @@
bool create_and_switch_to_temp_dir(std::string &_new_temp_path)
{
std::string tmppath;
const char *tmp = getenv("TMPDIR");
if (tmp)
{
tmppath = std::string(tmp);
}
else
if(!ignition::utils::env("TMPDIR", tmppath))
{
tmppath = std::string("/tmp");
tmppath = "/tmp";
}

tmppath += "/XXXXXX";
Expand Down
16 changes: 5 additions & 11 deletions src/SDF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "sdf/sdf_config.h"
#include "EmbeddedSdf.hh"

#include <ignition/utils/Environment.hh>

namespace sdf
{
inline namespace SDF_VERSION_NAMESPACE
Expand Down Expand Up @@ -118,18 +120,10 @@ std::string findFile(const std::string &_filename, bool _searchLocalPath,
return path;
}

// Next check SDF_PATH environment variable
#ifndef _WIN32
const char *pathCStr = std::getenv("SDF_PATH");
#else
char *pathCStr;
size_t sz = 0;
_dupenv_s(&pathCStr, &sz, "SDF_PATH");
#endif

if (pathCStr)
std::string sdfPathEnv;
if(ignition::utils::env("SDF_PATH", sdfPathEnv))
{
std::vector<std::string> paths = sdf::split(pathCStr, ":");
std::vector<std::string> paths = sdf::split(sdfPathEnv, ":");
for (std::vector<std::string>::iterator iter = paths.begin();
iter != paths.end(); ++iter)
{
Expand Down
10 changes: 3 additions & 7 deletions src/SDF_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <gtest/gtest.h>
#include <any>
#include <ignition/math.hh>
#include <ignition/utils/Environment.hh>

#include "sdf/sdf.hh"

Expand Down Expand Up @@ -643,14 +644,9 @@ TEST(SDF, ToStringAndParse)
bool create_new_temp_dir(std::string &_new_temp_path)
{
std::string tmppath;
const char *tmp = getenv("TMPDIR");
if (tmp)
if(!ignition::utils::env("TMPDIR", tmppath))
{
tmppath = std::string(tmp);
}
else
{
tmppath = std::string("/tmp");
tmppath = "/tmp";
}

tmppath += "/XXXXXX";
Expand Down
14 changes: 7 additions & 7 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ TEST(check, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
// Check an SDF world file with an invalid usage of __root__
{
// Set SDF_PATH so that included models can be found
sdf::testing::setenv(
ignition::utils::setenv(
"SDF_PATH", sdf::testing::SourceFile("test", "integration", "model"));
std::string path = pathBase + "/world_invalid_root_reference.sdf";

Expand Down Expand Up @@ -1160,7 +1160,7 @@ TEST(print_rotations_in_quaternions, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
TEST(print_includes_rotations_in_degrees, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
{
// Set SDF_PATH so that included models can be found
sdf::testing::setenv(
ignition::utils::setenv(
"SDF_PATH", sdf::testing::SourceFile("test", "integration", "model"));
const std::string path =
sdf::testing::TestFile("sdf", "includes_rotations_in_degrees.sdf");
Expand Down Expand Up @@ -1230,7 +1230,7 @@ TEST(print_includes_rotations_in_degrees, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
TEST(print_includes_rotations_in_radians, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
{
// Set SDF_PATH so that included models can be found
sdf::testing::setenv(
ignition::utils::setenv(
"SDF_PATH", sdf::testing::SourceFile("test", "integration", "model"));
const std::string path =
sdf::testing::TestFile("sdf", "includes_rotations_in_radians.sdf");
Expand Down Expand Up @@ -1301,7 +1301,7 @@ TEST(print_includes_rotations_in_quaternions,
IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
{
// Set SDF_PATH so that included models can be found
sdf::testing::setenv(
ignition::utils::setenv(
"SDF_PATH", sdf::testing::SourceFile("test", "integration", "model"));
const auto path = sdf::testing::TestFile(
"sdf", "includes_rotations_in_quaternions.sdf");
Expand Down Expand Up @@ -1801,7 +1801,7 @@ int main(int argc, char **argv)
{
// Set IGN_CONFIG_PATH to the directory where the .yaml configuration file
// is located.
sdf::testing::setenv("IGN_CONFIG_PATH", IGN_CONFIG_PATH);
ignition::utils::setenv("IGN_CONFIG_PATH", IGN_CONFIG_PATH);

// Make sure that we load the library recently built and not the one installed
// in your system. This is done by placing the the current build directory
Expand All @@ -1813,12 +1813,12 @@ int main(int argc, char **argv)
std::string testLibraryPath = IGN_TEST_LIBRARY_PATH;

std::string currentLibraryPath;
if (sdf::testing::env("LD_LIBRARY_PATH", currentLibraryPath))
if (ignition::utils::env("LD_LIBRARY_PATH", currentLibraryPath))
{
testLibraryPath = testLibraryPath + ":" + currentLibraryPath;
}

sdf::testing::setenv("LD_LIBRARY_PATH", testLibraryPath);
ignition::utils::setenv("LD_LIBRARY_PATH", testLibraryPath);
#endif

::testing::InitGoogleTest(&argc, argv);
Expand Down
11 changes: 7 additions & 4 deletions src/parser_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "sdf/Filesystem.hh"
#include "test_config.h"

#include <ignition/utils/Environment.hh>

/////////////////////////////////////////////////
TEST(Parser, initStringTrim)
{
Expand Down Expand Up @@ -75,13 +77,14 @@ TEST(Parser, CustomUnknownElements)
EXPECT_TRUE(sdf::readFile(path, sdf));

#ifndef _WIN32
char *homeDir = getenv("HOME");
std::string homeVarName = "HOME";
#else
char *homeDir;
size_t sz = 0;
_dupenv_s(&homeDir, &sz, "HOMEPATH");
std::string homeVarName = "HOMEPATH";
#endif

std::string homeDir;
ASSERT_TRUE(ignition::utils::env(homeVarName, homeDir));

std::string pathLog =
sdf::filesystem::append(homeDir, ".sdformat", "sdformat.log");

Expand Down
139 changes: 0 additions & 139 deletions test/env.hh

This file was deleted.

Loading

0 comments on commit c2ef096

Please sign in to comment.