]> git.saurik.com Git - apt.git/blob - test/libapt/file-helpers.cc
add a testcase for support of various build-dependency types
[apt.git] / test / libapt / file-helpers.cc
1 #include <apt-pkg/fileutl.h>
2
3 #include <string>
4
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9
10 #include <gtest/gtest.h>
11
12 #include "file-helpers.h"
13
14 void helperCreateTemporaryDirectory(std::string const &id, std::string &dir)
15 {
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));
19 dir = tempdir;
20 free(tempdir);
21 }
22 void helperRemoveDirectory(std::string const &dir)
23 {
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!";
27 else
28 ASSERT_EQ(0, system(std::string("rm -rf ").append(dir).c_str()));
29 }
30 void helperCreateFile(std::string const &dir, std::string const &name)
31 {
32 std::string file = dir;
33 file.append("/");
34 file.append(name);
35 int const fd = creat(file.c_str(), 0600);
36 ASSERT_NE(-1, fd);
37 close(fd);
38 }
39 void helperCreateDirectory(std::string const &dir, std::string const &name)
40 {
41 std::string file = dir;
42 file.append("/");
43 file.append(name);
44 ASSERT_TRUE(CreateDirectory(dir, file));
45 }
46 void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname)
47 {
48 std::string target = dir;
49 target.append("/");
50 target.append(targetname);
51 std::string link = dir;
52 link.append("/");
53 link.append(linkname);
54 ASSERT_EQ(0, symlink(target.c_str(), link.c_str()));
55 }
56 void helperCreateTemporaryFile(std::string const &id, FileFd &fd, std::string * const filename, char const * const content)
57 {
58 std::string name("apt-test-");
59 name.append(id);
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);
65 int tempfile_fd;
66 if (giventmp == std::string::npos)
67 tempfile_fd = mkstemp(tempfile);
68 else
69 tempfile_fd = mkstemps(tempfile, name.length() - (giventmp + 7));
70 ASSERT_NE(-1, tempfile_fd);
71 if (filename != NULL)
72 *filename = tempfile;
73 else
74 unlink(tempfile);
75 free(tempfile);
76
77 EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite, true));
78 if (content != NULL)
79 {
80 ASSERT_TRUE(fd.Write(content, strlen(content)));
81 fd.Seek(0);
82 }
83 }