Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemporaryWorkingDirectory helper #11385

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions test/TemporaryDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ TemporaryDirectory::~TemporaryDirectory()
{
// A few paranoid sanity checks just to be extra sure we're not deleting someone's homework.
assert(m_path.string().find(fs::temp_directory_path().string()) == 0);
assert(m_path != fs::temp_directory_path());
assert(m_path != m_path.root_path());
assert(!fs::equivalent(m_path, fs::temp_directory_path()));
assert(!fs::equivalent(m_path, m_path.root_path()));
assert(!m_path.empty());

boost::system::error_code errorCode;
Expand All @@ -56,3 +56,14 @@ TemporaryDirectory::~TemporaryDirectory()
cerr << "Reason: " << errorCode.message() << endl;
}
}

TemporaryWorkingDirectory::TemporaryWorkingDirectory(fs::path const& _newDirectory):
m_originalWorkingDirectory(fs::current_path())
{
fs::current_path(_newDirectory);
}

TemporaryWorkingDirectory::~TemporaryWorkingDirectory()
{
fs::current_path(m_originalWorkingDirectory);
}
18 changes: 17 additions & 1 deletion test/TemporaryDirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Utility for creating temporary directories for use in tests.
* Utilities for creating temporary directories and temporarily changing the working directory
* for use in tests.
*/

#pragma once
Expand Down Expand Up @@ -48,4 +49,19 @@ class TemporaryDirectory
boost::filesystem::path m_path;
};

/**
* An object that changes current working directory and restores it upon destruction.
*/
class TemporaryWorkingDirectory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put the class in TemporaryDirectory.h which already contains a similar helper but maybe I should create a separate TemporaryWorkingDirectory.h instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have such a granular separation in the test directory, then yes, otherwise I think it is fine.

{
public:
TemporaryWorkingDirectory(boost::filesystem::path const& _newDirectory);
~TemporaryWorkingDirectory();

boost::filesystem::path const& originalWorkingDirectory() const { return m_originalWorkingDirectory; }

private:
boost::filesystem::path m_originalWorkingDirectory;
};

}
25 changes: 25 additions & 0 deletions test/TemporaryDirectoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_
BOOST_TEST(!fs::exists(dirPath / "test-file.txt"));
}

BOOST_AUTO_TEST_CASE(TemporaryWorkingDirectory_should_change_and_restore_working_directory)
{
fs::path originalWorkingDirectory = fs::current_path();

try
{
{
TemporaryDirectory tempDir("temporary-directory-test-");
assert(fs::equivalent(fs::current_path(), originalWorkingDirectory));
assert(!fs::equivalent(tempDir.path(), originalWorkingDirectory));

TemporaryWorkingDirectory tempWorkDir(tempDir.path());

BOOST_TEST(fs::equivalent(fs::current_path(), tempDir.path()));
}
BOOST_TEST(fs::equivalent(fs::current_path(), originalWorkingDirectory));

fs::current_path(originalWorkingDirectory);
}
catch (...)
{
fs::current_path(originalWorkingDirectory);
}
}

BOOST_AUTO_TEST_SUITE_END()

}