Provide unified solution for dir name randomization in tests

This approach actually wraps the unique identifier generation into the
function that provides the output path for a test.
This way we don't need to add `CreateRandomUuid()` everywhere that we
have `test::OutputPath` and instead just rename to
`test::OutputPathRandomDir`

Bug: webrtc:15833
Change-Id: Ic9b69b5b599727f07b2906569a84a40edeecd1a0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/338645
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Jeremy Leconte <jleconte@google.com>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41730}
This commit is contained in:
Dor Hen 2024-02-13 11:51:01 +02:00 committed by WebRTC LUCI CQ
parent 495e23e60f
commit 94c3328b61
4 changed files with 36 additions and 0 deletions

View File

@ -861,6 +861,7 @@ rtc_library("fileutils") {
":fileutils_override_api",
":fileutils_override_impl",
"../rtc_base:checks",
"../rtc_base:ssl",
"../rtc_base:stringutils",
]
absl_deps = [

View File

@ -54,6 +54,7 @@
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h"
#include "rtc_base/helpers.h"
#include "rtc_base/string_utils.h"
#include "rtc_base/strings/string_builder.h"
#include "test/testsupport/file_utils_override.h"
@ -94,6 +95,13 @@ std::string OutputPath() {
return webrtc::test::internal::OutputPath();
}
std::string OutputPathWithRandomDirectory() {
std::string path = webrtc::test::internal::OutputPath();
std::string rand_dir = path + rtc::CreateRandomUuid();
return CreateDir(rand_dir) ? rand_dir + std::string(kPathDelimiter) : path;
}
std::string WorkingDir() {
return webrtc::test::internal::WorkingDir();
}

View File

@ -42,6 +42,11 @@ ABSL_CONST_INIT extern const absl::string_view kPathDelimiter;
// found, the current working directory ("./") is returned as a fallback.
std::string OutputPath();
// Same as the above but appends a randomly named folder at the end of the path
// Primerly used to provide a solution for stress testing environments to
// prevent colission of files and folders.
std::string OutputPathWithRandomDirectory();
// Generates an empty file with a unique name in the specified directory and
// returns the file name and path.
// TODO(titovartem) rename to TempFile and next method to TempFilename

View File

@ -120,6 +120,28 @@ TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
ASSERT_THAT(result, EndsWith(expected_end));
}
TEST_F(FileUtilsTest, RandomOutputPathFromUnchangedWorkingDir) {
rtc::SetRandomTestMode(true);
std::string fixed_first_uuid = "def01482-f829-429a-bfd4-841706e92cdd";
std::string expected_end = ExpectedRootDirByPlatform() + fixed_first_uuid +
std::string(kPathDelimiter);
std::string result = webrtc::test::OutputPathWithRandomDirectory();
ASSERT_THAT(result, EndsWith(expected_end));
}
TEST_F(FileUtilsTest, RandomOutputPathFromRootWorkingDir) {
ASSERT_EQ(0, chdir(kPathDelimiter.data()));
rtc::SetRandomTestMode(true);
std::string fixed_first_uuid = "def01482-f829-429a-bfd4-841706e92cdd";
std::string expected_end = ExpectedRootDirByPlatform() + fixed_first_uuid +
std::string(kPathDelimiter);
std::string result = webrtc::test::OutputPathWithRandomDirectory();
ASSERT_THAT(result, EndsWith(expected_end));
}
TEST_F(FileUtilsTest, TempFilename) {
std::string temp_filename = webrtc::test::TempFilename(
webrtc::test::OutputPath(), "TempFilenameTest");