1 #include "FilesystemTest.hpp"
3 #include "io/filesystem.hpp"
7 CPPUNIT_TEST_SUITE_REGISTRATION(gong::io::test::FilesystemTest);
16 void FilesystemTest::setUp() {
17 test_dir.reset(new TempDir());
20 void FilesystemTest::tearDown() {
25 void FilesystemTest::testFile() {
27 const string test_file = test_dir->Path() + "\\test-file.txt";
29 const string test_file = test_dir->Path() + "/test-file";
32 CPPUNIT_ASSERT_MESSAGE(
33 "inexistant file is file",
35 CPPUNIT_ASSERT_EQUAL_MESSAGE(
36 "mtime of inexistant file should be zero",
37 time_t(0), file_mtime(test_file));
38 CPPUNIT_ASSERT_MESSAGE(
39 "inexistant file is a directory",
43 ofstream file(test_file);
44 file << "hello" << endl;
46 time_t now = time(nullptr);
47 CPPUNIT_ASSERT_MESSAGE(
48 "existing file not a file",
50 CPPUNIT_ASSERT_MESSAGE(
51 "mtime of existing file should be somewhere around now",
52 // let's assume that creating the file takes less than five seconds
53 abs(now - file_mtime(test_file) < 5));
54 CPPUNIT_ASSERT_MESSAGE(
55 "regular file is a directory",
58 CPPUNIT_ASSERT_MESSAGE(
59 "failed to remove test file",
60 remove_file(test_file));
62 CPPUNIT_ASSERT_MESSAGE(
63 "removed file is still a file",
65 CPPUNIT_ASSERT_EQUAL_MESSAGE(
66 "mtime of removed file should be zero",
67 time_t(0), file_mtime(test_file));
68 CPPUNIT_ASSERT_MESSAGE(
69 "removed file became a directory",
73 void FilesystemTest::testDirectory() {
75 const string test_subdir = test_dir->Path() + "\\a";
76 const string test_subsubdir = test_subdir + "\\b";
77 const string test_file = test_subsubdir + "\\c.txt";
79 const string test_subdir = test_dir->Path() + "/a";
80 const string test_subsubdir = test_subdir + "/b";
81 const string test_file = test_subsubdir + "/c";
84 CPPUNIT_ASSERT_MESSAGE(
85 "inexistant directory is a file",
86 !is_file(test_subdir));
87 CPPUNIT_ASSERT_MESSAGE(
88 "inexistant directory is a directory",
89 !is_dir(test_subdir));
91 CPPUNIT_ASSERT_MESSAGE(
92 "failed to create test subdir",
93 make_dir(test_subdir));
94 CPPUNIT_ASSERT_MESSAGE(
95 "created directory is a file",
96 !is_file(test_subdir));
97 CPPUNIT_ASSERT_MESSAGE(
98 "created directory is not a directory",
101 CPPUNIT_ASSERT_MESSAGE(
102 "failed to remove test subdir",
103 remove_dir(test_subdir));
104 CPPUNIT_ASSERT_MESSAGE(
105 "removed directory became a file",
106 !is_file(test_subdir));
107 CPPUNIT_ASSERT_MESSAGE(
108 "removed directory is still a directory",
109 !is_dir(test_subdir));
111 CPPUNIT_ASSERT_MESSAGE(
112 "failed to create test subdirs",
113 make_dirs(test_subsubdir));
114 CPPUNIT_ASSERT_MESSAGE(
115 "created directory is a file",
116 !is_file(test_subdir));
117 CPPUNIT_ASSERT_MESSAGE(
118 "created directory is not a directory",
119 is_dir(test_subdir));
120 CPPUNIT_ASSERT_MESSAGE(
121 "created directory is a file",
122 !is_file(test_subsubdir));
123 CPPUNIT_ASSERT_MESSAGE(
124 "created directory is not a directory",
125 is_dir(test_subsubdir));
128 ofstream file(test_file);
129 file << "hello" << endl;
131 CPPUNIT_ASSERT_MESSAGE(
132 "failed to create test file",
135 CPPUNIT_ASSERT_MESSAGE(
136 "failed to remove test subdir",
137 remove_dir(test_subdir));
138 CPPUNIT_ASSERT_MESSAGE(
139 "removed directory became a file",
140 !is_file(test_subdir));
141 CPPUNIT_ASSERT_MESSAGE(
142 "removed directory is still a directory",
143 !is_dir(test_subdir));
144 CPPUNIT_ASSERT_MESSAGE(
145 "removed directory became a file",
146 !is_file(test_subsubdir));
147 CPPUNIT_ASSERT_MESSAGE(
148 "removed directory is still a directory",
149 !is_dir(test_subsubdir));
150 CPPUNIT_ASSERT_MESSAGE(
151 "removed file became a directory",
153 CPPUNIT_ASSERT_MESSAGE(
154 "removed file is still a file",
155 !is_file(test_file));