wordexp_ros_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
18 #include <stdlib.h>
19 
20 class WordExpRosTestFixture : public ::testing::Test
21 {
22 protected:
23  void SetUp() override {
24  env_home = getenv("HOME");
25  env_ros_home = getenv("ROS_HOME");
26  }
27 
28  void TearDown() override {
29  wordfree(&wordexp_standard);
30  wordfree(&wordexp_ros_expansion);
31  // Restore environment variables
32  if (NULL != env_home) {
33  setenv("HOME", env_home, 1);
34  }
35  if (NULL != env_ros_home) {
36  setenv("ROS_HOME", env_ros_home, 1);
37  }
38  }
40  const char *env_home, *env_ros_home;
41 };
42 
43 /* Test Scenarios
44 
45  HOME | ROS_HOME | result:
46  1. unset | unset | same as wordexp (home directory of pwd entry)
47  2. set | unset | same as wordexp ($HOME)
48  3. set | set | same as wordexp ($HOME)
49  4. unset | set | ROS_HOME
50 **/
51 
52 TEST_F(WordExpRosTestFixture, TestHomeUnsetRosHomeUnset)
53 {
54  // Scenario 1
55  unsetenv("HOME");
56  unsetenv("ROS_HOME");
57  EXPECT_EQ(wordexp_ros("~", &wordexp_ros_expansion, 0), wordexp("~", &wordexp_standard, 0));
58  EXPECT_EQ(wordexp_ros_expansion.we_wordc, wordexp_standard.we_wordc);
59  EXPECT_STREQ(wordexp_ros_expansion.we_wordv[0], wordexp_standard.we_wordv[0]);
60 }
61 
62 TEST_F(WordExpRosTestFixture, TestHomeSetRosHomeUnset)
63 {
64  // Scenario 2
65  setenv("HOME", "/home/test2", true);
66  unsetenv("ROS_HOME");
67  EXPECT_EQ(wordexp_ros("~", &wordexp_ros_expansion, 0), wordexp("~", &wordexp_standard, 0));
68  EXPECT_EQ(wordexp_ros_expansion.we_wordc, wordexp_standard.we_wordc);
69  EXPECT_STREQ(wordexp_ros_expansion.we_wordv[0], wordexp_standard.we_wordv[0]);
70  EXPECT_STREQ(wordexp_ros_expansion.we_wordv[0], "/home/test2");
71 }
72 
73 TEST_F(WordExpRosTestFixture, TestHomeSetRosHomeSet)
74 {
75  // Scenario 3
76  setenv("HOME", "/home/test3", true);
77  setenv("ROS_HOME", "/home/roshome3", true);
78  EXPECT_EQ(wordexp_ros("~", &wordexp_ros_expansion, 0), wordexp("~", &wordexp_standard, 0));
79  EXPECT_EQ(wordexp_ros_expansion.we_wordc, wordexp_standard.we_wordc);
80  EXPECT_STREQ(wordexp_ros_expansion.we_wordv[0], wordexp_standard.we_wordv[0]);
81  EXPECT_STREQ(wordexp_ros_expansion.we_wordv[0], "/home/test3");
82 }
83 
84 TEST_F(WordExpRosTestFixture, TestHomeUnsetRosHomeSet)
85 {
86  // Scenario 4
87  unsetenv("HOME");
88  setenv("ROS_HOME", "/home/roshome4", true);
89  EXPECT_EQ(wordexp_ros("~", &wordexp_ros_expansion, 0), wordexp("~", &wordexp_standard, 0));
90  EXPECT_STRNE(wordexp_ros_expansion.we_wordv[0], wordexp_standard.we_wordv[0]);
91  EXPECT_STREQ(wordexp_ros_expansion.we_wordv[0], "/home/roshome4");
92  // Verify that HOME remains unchanged
93  EXPECT_EQ(getenv("HOME"), nullptr);
94 }
95 
96 int main(int argc, char ** argv)
97 {
98  testing::InitGoogleTest(&argc, argv);
99  return RUN_ALL_TESTS();
100 }
void TearDown() override
TEST_F(WordExpRosTestFixture, TestHomeUnsetRosHomeUnset)
int wordexp_ros(const char *words, wordexp_t *pwordexp, int flags)
Provide wordexp-like behavior, with one difference: in the case that $HOME is not defined - use $ROS_...
Definition: wordexp_ros.cpp:20
int main(int argc, char **argv)


aws_common
Author(s): AWS RoboMaker
autogenerated on Sat Mar 6 2021 03:11:38