]> git.localhorst.tv Git - blank.git/blob - tst/io/FilesystemTest.cpp
somewhat self-cleaning temp dir
[blank.git] / tst / io / FilesystemTest.cpp
1 #include "FilesystemTest.hpp"
2
3 #include "io/filesystem.hpp"
4
5 #include <algorithm>
6
7 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::FilesystemTest);
8
9 using namespace std;
10
11
12 namespace blank {
13 namespace test {
14
15 void FilesystemTest::setUp() {
16         test_dir.reset(new TempDir());
17 }
18
19 void FilesystemTest::tearDown() {
20         test_dir.reset();
21 }
22
23
24 void FilesystemTest::testFile() {
25 #ifdef _WIN32
26         const string test_file = test_dir->Path() + "\\test-file.txt";
27 #else
28         const string test_file = test_dir->Path() + "/test-file";
29 #endif
30
31         CPPUNIT_ASSERT_MESSAGE(
32                 "inexistant file is file",
33                 !is_file(test_file));
34         CPPUNIT_ASSERT_EQUAL_MESSAGE(
35                 "mtime of inexistant file should be zero",
36                 time_t(0), file_mtime(test_file));
37         CPPUNIT_ASSERT_MESSAGE(
38                 "inexistant file is a directory",
39                 !is_dir(test_file));
40
41         { // create file
42                 ofstream file(test_file);
43                 file << "hello" << endl;
44         }
45         time_t now = time(nullptr);
46         CPPUNIT_ASSERT_MESSAGE(
47                 "existing file not a file",
48                 is_file(test_file));
49         CPPUNIT_ASSERT_MESSAGE(
50                 "mtime of existing file should be somewhere around now",
51                 // let's assume that creating the file takes less than five seconds
52                 abs(now - file_mtime(test_file) < 5));
53         CPPUNIT_ASSERT_MESSAGE(
54                 "regular file is a directory",
55                 !is_dir(test_file));
56
57         CPPUNIT_ASSERT_MESSAGE(
58                 "failed to remove test file",
59                 remove_file(test_file));
60
61         CPPUNIT_ASSERT_MESSAGE(
62                 "removed file is still a file",
63                 !is_file(test_file));
64         CPPUNIT_ASSERT_EQUAL_MESSAGE(
65                 "mtime of removed file should be zero",
66                 time_t(0), file_mtime(test_file));
67         CPPUNIT_ASSERT_MESSAGE(
68                 "removed file became a directory",
69                 !is_dir(test_file));
70 }
71
72 void FilesystemTest::testDirectory() {
73 #ifdef _WIN32
74         const string test_subdir = test_dir->Path() + "\\a";
75         const string test_subsubdir = test_subdir + "\\b";
76         const string test_file = test_subsubdir + "\\c.txt";
77 #else
78         const string test_subdir = test_dir->Path() + "/a";
79         const string test_subsubdir = test_subdir + "/b";
80         const string test_file = test_subsubdir + "/c";
81 #endif
82
83         CPPUNIT_ASSERT_MESSAGE(
84                 "inexistant directory is a file",
85                 !is_file(test_subdir));
86         CPPUNIT_ASSERT_MESSAGE(
87                 "inexistant directory is a directory",
88                 !is_dir(test_subdir));
89
90         CPPUNIT_ASSERT_MESSAGE(
91                 "failed to create test subdir",
92                 make_dir(test_subdir));
93         CPPUNIT_ASSERT_MESSAGE(
94                 "created directory is a file",
95                 !is_file(test_subdir));
96         CPPUNIT_ASSERT_MESSAGE(
97                 "created directory is not a directory",
98                 is_dir(test_subdir));
99
100         CPPUNIT_ASSERT_MESSAGE(
101                 "failed to remove test subdir",
102                 remove_dir(test_subdir));
103         CPPUNIT_ASSERT_MESSAGE(
104                 "removed directory became a file",
105                 !is_file(test_subdir));
106         CPPUNIT_ASSERT_MESSAGE(
107                 "removed directory is still a directory",
108                 !is_dir(test_subdir));
109
110         CPPUNIT_ASSERT_MESSAGE(
111                 "failed to create test subdirs",
112                 make_dirs(test_subsubdir));
113         CPPUNIT_ASSERT_MESSAGE(
114                 "created directory is a file",
115                 !is_file(test_subdir));
116         CPPUNIT_ASSERT_MESSAGE(
117                 "created directory is not a directory",
118                 is_dir(test_subdir));
119         CPPUNIT_ASSERT_MESSAGE(
120                 "created directory is a file",
121                 !is_file(test_subsubdir));
122         CPPUNIT_ASSERT_MESSAGE(
123                 "created directory is not a directory",
124                 is_dir(test_subsubdir));
125
126         { // create file
127                 ofstream file(test_file);
128                 file << "hello" << endl;
129         }
130         CPPUNIT_ASSERT_MESSAGE(
131                 "failed to create test file",
132                 is_file(test_file));
133
134         CPPUNIT_ASSERT_MESSAGE(
135                 "failed to remove test subdir",
136                 remove_dir(test_subdir));
137         CPPUNIT_ASSERT_MESSAGE(
138                 "removed directory became a file",
139                 !is_file(test_subdir));
140         CPPUNIT_ASSERT_MESSAGE(
141                 "removed directory is still a directory",
142                 !is_dir(test_subdir));
143         CPPUNIT_ASSERT_MESSAGE(
144                 "removed directory became a file",
145                 !is_file(test_subsubdir));
146         CPPUNIT_ASSERT_MESSAGE(
147                 "removed directory is still a directory",
148                 !is_dir(test_subsubdir));
149         CPPUNIT_ASSERT_MESSAGE(
150                 "removed file became a directory",
151                 !is_dir(test_file));
152         CPPUNIT_ASSERT_MESSAGE(
153                 "removed file is still a file",
154                 !is_file(test_file));
155 }
156
157 }
158 }