Skip to content

Commit c0c9175

Browse files
committed
New Unittest and modified behavior for FileUtils. Note that the name of ResolveAbsolutePath has been changed to ResolveAPath as it does not always return a cannonical absolute path but hopefully a valid normalized path which may or may not exist.
1 parent 51b0472 commit c0c9175

File tree

2 files changed

+117
-7
lines changed

2 files changed

+117
-7
lines changed

projects/biogears/libBiogears/src/cdm/utils/FileUtils.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,17 @@ std::string ResolvePath(const std::string& path)
135135
{
136136
filesystem::path given_path{ path };
137137
filesystem::path cwd{ GetCurrentWorkingDirectory() };
138-
139-
return ((given_path.is_absolute()) ? given_path
140-
: (filesystem::path{ cwd }.is_absolute()) ? filesystem::normalize(cwd / given_path)
141-
: filesystem::normalize(given_path))
142-
.string();
138+
if (path.empty()) {
139+
return "";
140+
} else if (given_path.is_absolute()) {
141+
return given_path.string();
142+
} else if (filesystem::path{ cwd }.is_absolute()) {
143+
return filesystem::normalize(cwd / given_path).string();
144+
} else {
145+
return filesystem::normalize(filesystem::path::getcwd() / cwd / given_path).string();
146+
}
147+
//TODO -- IS this right? If the user wants a relative library CWD is that realtive to the actual CWD
148+
//TODO -- Document this behavior for sure.
143149
}
144150
//!
145151
//! \param const char* path Path to be resolved
@@ -214,9 +220,8 @@ bool TestFirstDirName(std::string path, std::string dirname)
214220
if (!p.is_absolute()) {
215221
if (p.begin() != p.end()) {
216222
auto itr = p.begin();
217-
return *itr == dirname;
223+
return *itr == dirname;
218224
} else {
219-
220225
}
221226
}
222227
return false;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//-------------------------------------------------------------------------------------------
2+
//- Copyright 2017 Applied Research Associates, Inc.
3+
//- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4+
//- this file except in compliance with the License. You may obtain a copy of the License
5+
//- at:
6+
//- http://www.apache.org/licenses/LICENSE-2.0
7+
//- Unless required by applicable law or agreed to in writing, software distributed under
8+
//- the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9+
//- CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
//- specific language governing permissions and limitations under the License.
11+
//-------------------------------------------------------------------------------------------
12+
13+
//!
14+
//! @author David Lee
15+
//! @date 2017 Aug 3rd
16+
//!
17+
//! Unit Test for NGSS Config
18+
//!
19+
#include <thread>
20+
21+
#include <gtest/gtest.h>
22+
23+
#include <biogears/cdm/utils/FileUtils.h>
24+
#include <biogears/filesystem/path.h>
25+
26+
#ifdef DISABLE_BIOGEARS_FileUtils_TEST
27+
#define TEST_FIXTURE_NAME DISABLED_FileUtilsFixture
28+
#else
29+
#define TEST_FIXTURE_NAME FileUtilsFixture
30+
#endif
31+
32+
// The fixture for testing class Foo.
33+
class TEST_FIXTURE_NAME : public ::testing::Test {
34+
protected:
35+
// You can do set-up work for each test here.
36+
TEST_FIXTURE_NAME() = default;
37+
38+
// You can do clean-up work that doesn't throw exceptions here.
39+
virtual ~TEST_FIXTURE_NAME() = default;
40+
41+
// If the constructor and destructor are not enough for setting up
42+
// and cleaning up each test, you can define the following methods:
43+
44+
// Code here will be called immediately after the constructor (right
45+
// before each test).
46+
virtual void SetUp();
47+
48+
// Code here will be called immediately after each test (right
49+
// before the destructor).
50+
virtual void TearDown();
51+
};
52+
53+
void TEST_FIXTURE_NAME::SetUp()
54+
{
55+
}
56+
57+
void TEST_FIXTURE_NAME::TearDown()
58+
{
59+
biogears::SetCurrentWorkingDirectory("");
60+
}
61+
62+
TEST_F(TEST_FIXTURE_NAME, FileUtils_CWD_Change)
63+
{
64+
using namespace biogears;
65+
EXPECT_EQ("", biogears::GetCurrentWorkingDirectory());
66+
#ifdef _WIN32
67+
biogears::SetCurrentWorkingDirectory("C:/");
68+
EXPECT_EQ("C:/", biogears::GetCurrentWorkingDirectory());
69+
#else
70+
SetCurrentWorkingDirectory("/opt/biogears");
71+
EXPECT_EQ("/opt/biogears", biogears::GetCurrentWorkingDirectory());
72+
#endif
73+
}
74+
75+
TEST_F(TEST_FIXTURE_NAME, FileUtils_ResolvePath)
76+
{
77+
using namespace biogears;
78+
using namespace biogears::filesystem;
79+
biogears::SetCurrentWorkingDirectory("");
80+
EXPECT_EQ(path::getcwd() / "test_file.txt", ResolvePath("test_file.txt"));
81+
EXPECT_EQ(path::getcwd() / "test_file.txt", ResolvePath("./test_file.txt"));
82+
EXPECT_EQ(path("C:/biogears/test_file.txt").make_normal(), ResolvePath("C:/biogears/test_file.txt"));
83+
84+
#ifdef _WIN32
85+
biogears::SetCurrentWorkingDirectory("C:/");
86+
EXPECT_EQ(path("C:/test_file.txt").make_normal(), ResolvePath("test_file.txt"));
87+
EXPECT_EQ(path("C:/test_file.txt").make_normal(), ResolvePath("./test_file.txt"));
88+
EXPECT_EQ(path("C:/biogears/test_file.txt").make_normal(), ResolvePath("C:/biogears/test_file.txt"));
89+
#else
90+
SetCurrentWorkingDirectory("/opt/biogears");
91+
EXPECT_EQ(path("/opt/biogears/test_file.txt").make_normal(), ResolvePath("test_file.txt"));
92+
EXPECT_EQ(path("/opt/biogears/test_file.txt").make_normal(), ResolvePath("./test_file.txt"));
93+
EXPECT_EQ(path("/opt/biogears/biogears/test_file.txt").make_normal(), ResolvePath("/opt/biogears/biogears/test_file.txt"));
94+
#endif
95+
96+
auto cwd = path::getcwd().parent_path();
97+
biogears::SetCurrentWorkingDirectory("..");
98+
EXPECT_EQ((cwd/"test_file.txt").make_normal(), ResolvePath("test_file.txt"));
99+
EXPECT_EQ((cwd/"test_file.txt").make_normal(), ResolvePath("./test_file.txt"));
100+
#ifdef _WIN32
101+
EXPECT_EQ(path("C:/biogears/test_file.txt").make_normal(), ResolvePath("C:\\biogears\\test_file.txt"));
102+
#else
103+
EXPECT_EQ(path("/opt/biogears/test_file.txt").make_normal(), ResolvePath("/opt/biogears/test_file.txt"));
104+
#endif
105+
}

0 commit comments

Comments
 (0)