fix: prevent hidden file creation when base_name is empty in MakeFileName

When base_name is empty, MakeFileName would produce filenames
starting with '.' (e.g., '.xml'), creating hidden files on Unix.
Fall back to 'file' as the base name to avoid this.

Fixes #3815
This commit is contained in:
Manikandan K. S. 2026-07-26 17:06:07 +05:30
parent b78aa5ee64
commit 653aec68af
2 changed files with 15 additions and 3 deletions

View File

@ -227,12 +227,12 @@ FilePath FilePath::RemoveFileName() const {
FilePath FilePath::MakeFileName(const FilePath& directory,
const FilePath& base_name, int number,
const char* extension) {
std::string base = base_name.IsEmpty() ? "file" : base_name.string();
std::string file;
if (number == 0) {
file = base_name.string() + "." + extension;
file = base + "." + extension;
} else {
file =
base_name.string() + "_" + StreamableToString(number) + "." + extension;
file = base + "_" + StreamableToString(number) + "." + extension;
}
return ConcatPaths(directory, FilePath(file));
}

View File

@ -274,6 +274,18 @@ TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
EXPECT_EQ("bar_14.xml", actual.string());
}
TEST(MakeFileNameTest, GenerateWhenBaseNameEmpty) {
FilePath actual =
FilePath::MakeFileName(FilePath("foo"), FilePath(""), 0, "xml");
EXPECT_EQ("foo" GTEST_PATH_SEP_ "file.xml", actual.string());
}
TEST(MakeFileNameTest, GenerateWhenBaseNameEmptyNumberNonZero) {
FilePath actual =
FilePath::MakeFileName(FilePath("foo"), FilePath(""), 42, "xml");
EXPECT_EQ("foo" GTEST_PATH_SEP_ "file_42.xml", actual.string());
}
TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath("bar.xml"));
EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());