From b5ea589e568d3f56bd868d57c850680f999f83fc Mon Sep 17 00:00:00 2001 From: Colm Donelan Date: Fri, 10 Feb 2023 15:19:46 +0000 Subject: IVGCVSW-7510 Delete temporary files created by DebugTestImpl. * The test cases that use DebugTestImpl were creating temporary files but not cleaning them up after running. * Refactored FileSystem to extract a common RemoveDirectoryAndContents function. Signed-off-by: Colm Donelan Change-Id: I35b8d2eeed286742358a9abccbc078493d033902 --- src/armnnUtils/Filesystem.cpp | 44 ++++++++++++++-------- .../test/layerTests/DebugTestImpl.cpp | 3 ++ 2 files changed, 32 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/armnnUtils/Filesystem.cpp b/src/armnnUtils/Filesystem.cpp index 78f928a874..d09f87ba7f 100644 --- a/src/armnnUtils/Filesystem.cpp +++ b/src/armnnUtils/Filesystem.cpp @@ -49,20 +49,8 @@ std::string CreateDirectory(std::string path) // This line is very unlikely to throw an exception. fs::path tmpDir = fs::temp_directory_path(); std::string full_path = tmpDir.generic_string() + path; - if (fs::exists(full_path)) - { - try - { - // This could throw an exception on a multi-user system. - fs::remove_all(full_path); - } - catch (const std::system_error& e) - { - std::string error = "Directory exists and cannot be removed. Reason: "; - error.append(e.what()); - throw armnn::RuntimeException(error); - } - } + // This could throw a file permission exception. + RemoveDirectoryAndContents(full_path); #if defined(_WIN32) result = _mkdir(full_path.c_str()); // can be used on Windows armnn::ConditionalThrow((result == 0), "Was unable to create temporary directory"); @@ -85,7 +73,33 @@ std::string CreateDirectory(std::string path) return full_path + "/"; } -FileContents ReadFileContentsIntoString(const std::string path) { +/** + * @brief Remove a directory and its contents. + * + * Given a directory path delete it's contents and the directory. If the specified directory doesn't exist this + * does nothing. If any item cannot be removed this will throw a RuntimeException. + * + * @param full_path + */ +void RemoveDirectoryAndContents(const std::string& path) +{ + if (fs::exists(path)) + { + try + { + // This could throw an exception on a multi-user system. + fs::remove_all(path); + } + catch (const std::system_error& e) + { + std::string error = "Directory exists and cannot be removed. Reason: "; + error.append(e.what()); + throw armnn::RuntimeException(error); + } + } +} + +FileContents ReadFileContentsIntoString(const std::string& path) { if (!fs::exists(path)) { throw armnn::RuntimeException("Path does not exist: " + path); diff --git a/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp b/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp index 2ec94bca58..1768f5c503 100644 --- a/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp +++ b/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp @@ -79,6 +79,9 @@ LayerTestResult DebugTestImpl( armnnUtils::Filesystem::FileContents output = armnnUtils::Filesystem::ReadFileContentsIntoString(full_path); CHECK((output == expectedStringOutput)); + + // Clean up afterwards. + armnnUtils::Filesystem::RemoveDirectoryAndContents(tmpDir); } else { -- cgit v1.2.1