]>
git.saurik.com Git - apt.git/blob - test/libapt/file-helpers.cc
1 #include <apt-pkg/fileutl.h>
10 #include <gtest/gtest.h>
12 #include "file-helpers.h"
14 void helperCreateTemporaryDirectory(std::string
const &id
, std::string
&dir
)
16 std::string
const strtempdir
= GetTempDir().append("/apt-tests-").append(id
).append(".XXXXXX");
17 char * tempdir
= strdup(strtempdir
.c_str());
18 ASSERT_STREQ(tempdir
, mkdtemp(tempdir
));
22 void helperRemoveDirectory(std::string
const &dir
)
24 // basic sanity check to avoid removing random directories based on earlier failures
25 if (dir
.find("/apt-tests-") == std::string::npos
|| dir
.find_first_of("*?") != std::string::npos
)
26 FAIL() << "Directory '" << dir
<< "' seems invalid. It is therefore not removed!";
28 ASSERT_EQ(0, system(std::string("rm -rf ").append(dir
).c_str()));
30 void helperCreateFile(std::string
const &dir
, std::string
const &name
)
32 std::string file
= dir
;
35 int const fd
= creat(file
.c_str(), 0600);
39 void helperCreateDirectory(std::string
const &dir
, std::string
const &name
)
41 std::string file
= dir
;
44 ASSERT_TRUE(CreateDirectory(dir
, file
));
46 void helperCreateLink(std::string
const &dir
, std::string
const &targetname
, std::string
const &linkname
)
48 std::string target
= dir
;
50 target
.append(targetname
);
51 std::string link
= dir
;
53 link
.append(linkname
);
54 ASSERT_EQ(0, symlink(target
.c_str(), link
.c_str()));
56 void helperCreateTemporaryFile(std::string
const &id
, FileFd
&fd
, std::string
* const filename
, char const * const content
)
58 std::string
name("apt-test-");
60 size_t const giventmp
= name
.find(".XXXXXX.");
61 if (giventmp
== std::string::npos
)
62 name
.append(".XXXXXX");
63 char * tempfile
= strdup(name
.c_str());
64 ASSERT_STRNE(NULL
, tempfile
);
66 if (giventmp
== std::string::npos
)
67 tempfile_fd
= mkstemp(tempfile
);
69 tempfile_fd
= mkstemps(tempfile
, name
.length() - (giventmp
+ 7));
70 ASSERT_NE(-1, tempfile_fd
);
77 EXPECT_TRUE(fd
.OpenDescriptor(tempfile_fd
, FileFd::ReadWrite
));
80 ASSERT_TRUE(fd
.Write(content
, strlen(content
)));