summaryrefslogtreecommitdiff
path: root/test/libapt/file-helpers.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2020-05-18 11:55:54 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2020-05-18 15:55:36 +0200
commitc470d92366d7c3c239a689f0a10d6d0d9daafbff (patch)
tree955534f65524c3f81f36119012b6cd995a9619d0 /test/libapt/file-helpers.cc
parent37cc8dcda9e97e0b9420d37bb886081fa629847d (diff)
Allow prefix to be a complete filename for GetTempFile
Our testcases had their own implementation of GetTempFile with the feature of a temporary file with a choosen suffix. Merging this into GetTempFile lets us drop this duplicate and hence test more our code rather than testing our helpers for test implementation. And then hashsums_test had another implementationā€¦ and extracttar wasn't even trying to use a real tempfileā€¦ one GetTempFile to rule them all! That also ensures that these tempfiles are created in a temporary directory rather than the current directory which is a nice touch and tries a little harder to clean up those tempfiles.
Diffstat (limited to 'test/libapt/file-helpers.cc')
-rw-r--r--test/libapt/file-helpers.cc46
1 files changed, 23 insertions, 23 deletions
diff --git a/test/libapt/file-helpers.cc b/test/libapt/file-helpers.cc
index aa16a2e30..85fc6838e 100644
--- a/test/libapt/file-helpers.cc
+++ b/test/libapt/file-helpers.cc
@@ -53,31 +53,31 @@ void helperCreateLink(std::string const &dir, std::string const &targetname, std
link.append(linkname);
ASSERT_EQ(0, symlink(target.c_str(), link.c_str()));
}
-void helperCreateTemporaryFile(std::string const &id, FileFd &fd, std::string * const filename, char const * const content)
-{
- std::string name("apt-test-");
- name.append(id);
- size_t const giventmp = name.find(".XXXXXX.");
- if (giventmp == std::string::npos)
- name.append(".XXXXXX");
- char * tempfile = strdup(name.c_str());
- ASSERT_STRNE(NULL, tempfile);
- int tempfile_fd;
- if (giventmp == std::string::npos)
- tempfile_fd = mkstemp(tempfile);
- else
- tempfile_fd = mkstemps(tempfile, name.length() - (giventmp + 7));
- ASSERT_NE(-1, tempfile_fd);
- if (filename != NULL)
- *filename = tempfile;
- else
- unlink(tempfile);
- free(tempfile);
- EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite, true));
- if (content != NULL)
+void openTemporaryFile(std::string const &id, FileFd &fd, char const * const content, bool const ImmediateUnlink)
+{
+ EXPECT_NE(nullptr, GetTempFile("apt-" + id, ImmediateUnlink, &fd));
+ EXPECT_TRUE(ImmediateUnlink || not fd.Name().empty());
+ if (content != nullptr)
{
- ASSERT_TRUE(fd.Write(content, strlen(content)));
+ EXPECT_TRUE(fd.Write(content, strlen(content)));
+ EXPECT_TRUE(fd.Sync());
fd.Seek(0);
}
}
+ScopedFileDeleter::ScopedFileDeleter(std::string const &filename) : _filename{filename} {}
+ScopedFileDeleter::ScopedFileDeleter(ScopedFileDeleter &&sfd) = default;
+ScopedFileDeleter& ScopedFileDeleter::operator=(ScopedFileDeleter &&sfd) = default;
+ScopedFileDeleter::~ScopedFileDeleter() {
+ if (not _filename.empty())
+ RemoveFile("ScopedFileDeleter", _filename.c_str());
+}
+[[nodiscard]] ScopedFileDeleter createTemporaryFile(std::string const &id, char const * const content)
+{
+ FileFd fd;
+ openTemporaryFile(id, fd, content, false);
+ EXPECT_TRUE(fd.IsOpen());
+ EXPECT_TRUE(fd.Close());
+ EXPECT_FALSE(fd.Name().empty());
+ return ScopedFileDeleter{fd.Name()};
+}