Skip to content

Commit 344312c

Browse files
author
Addisu Z. Taddese
authored
Backport test utilities from sdf10 (#731)
Adds sdf::testing::SourceFile, sdf::testing::TestFile, sdf::testing::env, and sdf::testing::setenv and updates tests to use them. This makes it easier to backport other changes from newer branches. Signed-off-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent 8cbc9b5 commit 344312c

38 files changed

Lines changed: 390 additions & 249 deletions

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ if (BUILD_SDF_TEST)
151151
)
152152
endif()
153153

154+
include_directories(${PROJECT_SOURCE_DIR}/test)
154155
sdf_build_tests(${gtest_sources})
155156

156157
if (NOT WIN32)

src/Converter_TEST.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,13 +1887,11 @@ TEST(Converter, MuchNewerVersion)
18871887

18881888
static std::string ConvertDoc_15_16()
18891889
{
1890-
return sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.6",
1891-
"1_5.convert");
1890+
return sdf::testing::SourceFile("sdf", "1.6", "1_5.convert");
18921891
}
18931892
static std::string ConvertDoc_16_17()
18941893
{
1895-
return sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.7",
1896-
"1_6.convert");
1894+
return sdf::testing::SourceFile("sdf", "1.7", "1_6.convert");
18971895
}
18981896

18991897
/////////////////////////////////////////////////

src/FrameSemantics_TEST.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
TEST(FrameSemantics, buildFrameAttachedToGraph_Model)
3939
{
4040
const std::string testFile =
41-
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
42-
"model_frame_attached_to.sdf");
41+
sdf::testing::TestFile("sdf", "model_frame_attached_to.sdf");
4342

4443
// Load the SDF file
4544
sdf::Root root;
@@ -103,8 +102,7 @@ TEST(FrameSemantics, buildFrameAttachedToGraph_Model)
103102
TEST(FrameSemantics, buildFrameAttachedToGraph_World)
104103
{
105104
const std::string testFile =
106-
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
107-
"world_frame_attached_to.sdf");
105+
sdf::testing::TestFile("sdf", "world_frame_attached_to.sdf");
108106

109107
// Load the SDF file
110108
sdf::Root root;
@@ -198,8 +196,7 @@ TEST(FrameSemantics, buildFrameAttachedToGraph_World)
198196
TEST(FrameSemantics, buildPoseRelativeToGraph)
199197
{
200198
const std::string testFile =
201-
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
202-
"model_frame_relative_to_joint.sdf");
199+
sdf::testing::TestFile("sdf", "model_frame_relative_to_joint.sdf");
203200

204201
// Load the SDF file
205202
sdf::Root root;

src/ign_TEST.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ int main(int argc, char **argv)
721721
{
722722
// Set IGN_CONFIG_PATH to the directory where the .yaml configuration file
723723
// is located.
724-
setenv("IGN_CONFIG_PATH", IGN_CONFIG_PATH, 1);
724+
sdf::testing::setenv("IGN_CONFIG_PATH", IGN_CONFIG_PATH);
725725

726726
// Make sure that we load the library recently built and not the one installed
727727
// in your system. This is done by placing the the current build directory
@@ -732,13 +732,13 @@ int main(int argc, char **argv)
732732
#ifndef _WIN32
733733
std::string testLibraryPath = IGN_TEST_LIBRARY_PATH;
734734

735-
char *currentLibraryPath = std::getenv("LD_LIBRARY_PATH");
736-
if (currentLibraryPath)
735+
std::string currentLibraryPath;
736+
if (sdf::testing::env("LD_LIBRARY_PATH", currentLibraryPath))
737737
{
738738
testLibraryPath = testLibraryPath + ":" + currentLibraryPath;
739739
}
740740

741-
setenv("LD_LIBRARY_PATH", testLibraryPath.c_str(), 1);
741+
sdf::testing::setenv("LD_LIBRARY_PATH", testLibraryPath);
742742
#endif
743743

744744
::testing::InitGoogleTest(&argc, argv);

src/parser_TEST.cc

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ sdf::SDFPtr InitSDF()
6262
/////////////////////////////////////////////////
6363
TEST(Parser, ReusedSDFVersion)
6464
{
65-
std::string pathBase = PROJECT_SOURCE_PATH;
66-
pathBase += "/test/sdf";
67-
const std::string path17 = pathBase +"/model_link_relative_to.sdf";
68-
const std::string path16 = pathBase +"/joint_complete.sdf";
65+
const auto path17 = sdf::testing::TestFile(
66+
"sdf", "model_link_relative_to.sdf");
67+
const auto path16 = sdf::testing::TestFile("sdf", "joint_complete.sdf");
6968

7069
// Call readFile API that always converts
7170
sdf::SDFPtr sdf = InitSDF();
@@ -85,9 +84,7 @@ TEST(Parser, ReusedSDFVersion)
8584
/////////////////////////////////////////////////
8685
TEST(Parser, readFileConversions)
8786
{
88-
std::string pathBase = PROJECT_SOURCE_PATH;
89-
pathBase += "/test/sdf";
90-
const std::string path = pathBase +"/joint_complete.sdf";
87+
const auto path = sdf::testing::TestFile("sdf", "joint_complete.sdf");
9188

9289
// Call readFile API that always converts
9390
{
@@ -346,16 +343,13 @@ TEST(Parser, addNestedModel)
346343
/////////////////////////////////////////////////
347344
TEST(Parser, NameUniqueness)
348345
{
349-
std::string pathBase = PROJECT_SOURCE_PATH;
350-
pathBase += "/test/sdf";
351-
352346
// These tests are copies of the ones in ign_TEST.cc but use direct calls to
353347
// name uniqueness validator functions instead of going through ign.
354348

355349
// Check an SDF file with sibling elements of the same type (world)
356350
// that have duplicate names.
357351
{
358-
std::string path = pathBase +"/world_duplicate.sdf";
352+
const auto path = sdf::testing::TestFile("sdf", "world_duplicate.sdf");
359353
sdf::SDFPtr sdf = InitSDF();
360354
EXPECT_TRUE(sdf::readFile(path, sdf));
361355
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
@@ -368,7 +362,8 @@ TEST(Parser, NameUniqueness)
368362
// Check an SDF file with sibling elements of different types (model, light)
369363
// that have duplicate names.
370364
{
371-
std::string path = pathBase +"/world_sibling_same_names.sdf";
365+
const auto path = sdf::testing::TestFile("sdf",
366+
"world_sibling_same_names.sdf");
372367
sdf::SDFPtr sdf = InitSDF();
373368
EXPECT_TRUE(sdf::readFile(path, sdf));
374369
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
@@ -381,7 +376,8 @@ TEST(Parser, NameUniqueness)
381376
// Check an SDF file with sibling elements of the same type (link)
382377
// that have duplicate names.
383378
{
384-
std::string path = pathBase +"/model_duplicate_links.sdf";
379+
const auto path = sdf::testing::TestFile("sdf",
380+
"model_duplicate_links.sdf");
385381
sdf::SDFPtr sdf = InitSDF();
386382
EXPECT_TRUE(sdf::readFile(path, sdf));
387383
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
@@ -394,7 +390,8 @@ TEST(Parser, NameUniqueness)
394390
// Check an SDF file with sibling elements of the same type (joint)
395391
// that have duplicate names.
396392
{
397-
std::string path = pathBase +"/model_duplicate_joints.sdf";
393+
const auto path = sdf::testing::TestFile("sdf",
394+
"model_duplicate_joints.sdf");
398395
sdf::SDFPtr sdf = InitSDF();
399396
EXPECT_TRUE(sdf::readFile(path, sdf));
400397
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
@@ -407,7 +404,8 @@ TEST(Parser, NameUniqueness)
407404
// Check an SDF file with sibling elements of different types (link, joint)
408405
// that have duplicate names.
409406
{
410-
std::string path = pathBase +"/model_link_joint_same_name.sdf";
407+
const auto path = sdf::testing::TestFile("sdf",
408+
"model_link_joint_same_name.sdf");
411409
sdf::SDFPtr sdf = InitSDF();
412410
EXPECT_TRUE(sdf::readFile(path, sdf));
413411
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
@@ -420,7 +418,8 @@ TEST(Parser, NameUniqueness)
420418
// Check an SDF file with sibling elements of the same type (collision)
421419
// that have duplicate names.
422420
{
423-
std::string path = pathBase +"/link_duplicate_sibling_collisions.sdf";
421+
const auto path = sdf::testing::TestFile("sdf",
422+
"link_duplicate_sibling_collisions.sdf");
424423
sdf::SDFPtr sdf = InitSDF();
425424
EXPECT_TRUE(sdf::readFile(path, sdf));
426425
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
@@ -433,7 +432,8 @@ TEST(Parser, NameUniqueness)
433432
// Check an SDF file with sibling elements of the same type (visual)
434433
// that have duplicate names.
435434
{
436-
std::string path = pathBase +"/link_duplicate_sibling_visuals.sdf";
435+
const auto path = sdf::testing::TestFile("sdf",
436+
"link_duplicate_sibling_visuals.sdf");
437437
sdf::SDFPtr sdf = InitSDF();
438438
EXPECT_TRUE(sdf::readFile(path, sdf));
439439
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
@@ -446,7 +446,8 @@ TEST(Parser, NameUniqueness)
446446
// Check an SDF file with cousin elements of the same type (collision)
447447
// that have duplicate names. This is a valid file.
448448
{
449-
std::string path = pathBase +"/link_duplicate_cousin_collisions.sdf";
449+
const auto path = sdf::testing::TestFile("sdf",
450+
"link_duplicate_cousin_collisions.sdf");
450451
sdf::SDFPtr sdf = InitSDF();
451452
EXPECT_TRUE(sdf::readFile(path, sdf));
452453
EXPECT_TRUE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
@@ -460,7 +461,8 @@ TEST(Parser, NameUniqueness)
460461
// Check an SDF file with cousin elements of the same type (visual)
461462
// that have duplicate names. This is a valid file.
462463
{
463-
std::string path = pathBase +"/link_duplicate_cousin_visuals.sdf";
464+
const auto path = sdf::testing::TestFile("sdf",
465+
"link_duplicate_cousin_visuals.sdf");
464466
sdf::SDFPtr sdf = InitSDF();
465467
EXPECT_TRUE(sdf::readFile(path, sdf));
466468
EXPECT_TRUE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));

test/env.hh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (C) 2021 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
// Note: this is a direct copy from ignition::common,
19+
// and can be removed when sdformat has common as a dependency
20+
21+
#ifndef SDF_TEST_ENV_HH
22+
#define SDF_TEST_ENV_HH
23+
24+
#include <cstdlib>
25+
#include <string>
26+
27+
#ifdef _WIN32
28+
#include <windows.h>
29+
#endif
30+
31+
namespace sdf
32+
{
33+
namespace testing
34+
{
35+
36+
/// \brief Find the environment variable '_name' and return its value.
37+
///
38+
/// Note: the intention is to put this in ign-util, and remove it
39+
/// from sdformat once the depcnency is in place
40+
///
41+
/// \param[in] _name Name of the environment variable.
42+
/// \param[out] _value Value if the variable was found.
43+
/// \param[in] _allowEmpty Allow set-but-empty variables.
44+
/// (Unsupported on Windows)
45+
/// \return True if the variable was found or false otherwise.
46+
bool env(const std::string &_name,
47+
std::string &_value,
48+
bool _allowEmpty)
49+
{
50+
std::string v;
51+
bool valid = false;
52+
#ifdef _WIN32
53+
// Unused on Windows, suppress warning
54+
(void) _allowEmpty;
55+
const DWORD buffSize = 32767;
56+
static char buffer[buffSize];
57+
if (GetEnvironmentVariable(_name.c_str(), buffer, buffSize))
58+
{
59+
v = buffer;
60+
}
61+
62+
if (!v.empty())
63+
{
64+
valid = true;
65+
}
66+
67+
#else
68+
const char *cvar = std::getenv(_name.c_str());
69+
if (cvar != nullptr)
70+
{
71+
v = cvar;
72+
valid = true;
73+
74+
if (v[0] == '\0' && !_allowEmpty)
75+
{
76+
valid = false;
77+
}
78+
}
79+
#endif
80+
if (valid)
81+
{
82+
_value = v;
83+
return true;
84+
}
85+
return false;
86+
}
87+
88+
bool env(const std::string &_name, std::string &_value)
89+
{
90+
return env(_name, _value, false);
91+
}
92+
93+
/// \brief Set the environment variable '_name'.
94+
///
95+
/// Note that on Windows setting an empty string (_value=="")
96+
/// is the equivalent of unsetting the variable.
97+
///
98+
/// \param[in] _name Name of the environment variable.
99+
/// \param[in] _value Value of the variable to be set.
100+
/// \return True if the variable was set or false otherwise.
101+
bool setenv(const std::string &_name,
102+
const std::string &_value)
103+
{
104+
#ifdef _WIN32
105+
if (0 != _putenv_s(_name.c_str(), _value.c_str()))
106+
{
107+
return false;
108+
}
109+
#else
110+
if (0 != ::setenv(_name.c_str(), _value.c_str(), true))
111+
{
112+
return false;
113+
}
114+
#endif
115+
return true;
116+
}
117+
118+
/// \brief Unset the environment variable '_name'.
119+
/// \param[in] _name Name of the environment variable.
120+
/// \return True if the variable was unset or false otherwise.
121+
bool unsetenv(const std::string &_name)
122+
{
123+
#ifdef _WIN32
124+
if (0 != _putenv_s(_name.c_str(), ""))
125+
{
126+
return false;
127+
}
128+
#else
129+
if (0 != ::unsetenv(_name.c_str()))
130+
{
131+
return false;
132+
}
133+
#endif
134+
return true;
135+
}
136+
} // namespace testing
137+
} // namespace sdf
138+
139+
#endif

test/integration/actor_dom.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
TEST(DOMActor, LoadActors)
3232
{
3333
const std::string testFile =
34-
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
35-
"world_complete.sdf");
34+
sdf::testing::TestFile("sdf", "world_complete.sdf");
3635

3736
sdf::Root root;
3837
sdf::Errors errors = root.Load(testFile);
@@ -145,8 +144,7 @@ TEST(DOMActor, CopySdfLoadedProperties)
145144
// Verify that copying an actor also copies the underlying ElementPtr
146145
// Joints and Links
147146
const std::string testFile =
148-
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
149-
"world_complete.sdf");
147+
sdf::testing::TestFile("sdf", "world_complete.sdf");
150148

151149
sdf::Root root;
152150
sdf::Errors errors = root.Load(testFile);

test/integration/audio.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323

2424
#include "test_config.h"
2525

26-
26+
//////////////////////////////////////////////////
2727
TEST(SDFParser, AudioSDF_FullParameters_noThrow)
2828
{
29-
const std::string sdfTestFile = sdf::filesystem::append(
30-
PROJECT_SOURCE_PATH, "test", "integration", "audio.sdf");
29+
const auto sdfTestFile =
30+
sdf::testing::TestFile("integration", "audio.sdf");
31+
3132
sdf::SDFPtr p(new sdf::SDF());
3233
sdf::init(p);
33-
ASSERT_TRUE(sdf::readFile(sdfTestFile , p));
34+
ASSERT_TRUE(sdf::readFile(sdfTestFile, p));
3435
}

0 commit comments

Comments
 (0)