OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/test/testsupport/fileutils.h" | 11 #include "webrtc/test/testsupport/fileutils.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include <fstream> | |
16 #include <iostream> | |
15 #include <list> | 17 #include <list> |
16 #include <string> | 18 #include <string> |
17 | 19 |
20 #include "webrtc/base/checks.h" | |
21 #include "webrtc/base/pathutils.h" | |
18 #include "webrtc/test/gtest.h" | 22 #include "webrtc/test/gtest.h" |
19 | 23 |
20 #ifdef WIN32 | 24 #ifdef WIN32 |
21 #define chdir _chdir | 25 #define chdir _chdir |
22 static const char* kPathDelimiter = "\\"; | 26 static const char* kPathDelimiter = "\\"; |
23 #else | 27 #else |
24 static const char* kPathDelimiter = "/"; | 28 static const char* kPathDelimiter = "/"; |
25 #endif | 29 #endif |
26 | 30 |
27 static const std::string kResourcesDir = "resources"; | 31 static const char kTestName[] = "fileutils_unittest"; |
28 static const std::string kTestName = "fileutils_unittest"; | 32 static const char kExtension[] = "tmp"; |
29 static const std::string kExtension = "tmp"; | |
30 | 33 |
31 namespace webrtc { | 34 namespace webrtc { |
35 namespace test { | |
36 | |
37 namespace { | |
38 | |
39 // Remove files and directories in a directory non-recursively and writes the | |
40 // number of deleted items in |num_deleted_entries|. | |
41 void CleanDir(const std::string& dir, size_t* num_deleted_entries) { | |
kwiberg-webrtc
2017/05/29 12:11:36
Return the number of deleted entries instead?
AleBzk
2017/05/29 13:04:48
Nope. I can't use FAIL() otherwise, right?
kwiberg-webrtc
2017/05/30 08:28:12
Yes, FAIL() and ASSERT_*() can only be used in fun
AleBzk
2017/05/30 11:09:16
Done.
| |
42 RTC_DCHECK(num_deleted_entries); | |
43 *num_deleted_entries = 0; | |
44 std::vector<std::string> dir_content; | |
45 EXPECT_TRUE(ReadDirectory(dir, &dir_content)); | |
46 for (const auto& entry : dir_content) { | |
47 if (DirExists(entry)) { | |
48 EXPECT_TRUE(RemoveDir(entry)); | |
49 (*num_deleted_entries)++; | |
50 } else if (FileExists(entry)) { | |
51 EXPECT_TRUE(RemoveFile(entry)); | |
52 (*num_deleted_entries)++; | |
53 } else { | |
54 FAIL(); | |
55 } | |
56 } | |
57 } | |
58 | |
59 void WriteStringInFile(const std::string& what, const std::string& file_path) { | |
60 std::ofstream out(file_path); | |
61 out << what; | |
62 out.close(); | |
63 } | |
64 | |
65 } // namespace | |
32 | 66 |
33 // Test fixture to restore the working directory between each test, since some | 67 // Test fixture to restore the working directory between each test, since some |
34 // of them change it with chdir during execution (not restored by the | 68 // of them change it with chdir during execution (not restored by the |
35 // gtest framework). | 69 // gtest framework). |
36 class FileUtilsTest : public testing::Test { | 70 class FileUtilsTest : public testing::Test { |
37 protected: | 71 protected: |
38 FileUtilsTest() { | 72 FileUtilsTest() { |
39 } | 73 } |
40 ~FileUtilsTest() override {} | 74 ~FileUtilsTest() override {} |
41 // Runs before the first test | 75 // Runs before the first test |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 // Check that an existing file is not recognized as an existing directory. | 197 // Check that an existing file is not recognized as an existing directory. |
164 std::string temp_filename = webrtc::test::TempFilename( | 198 std::string temp_filename = webrtc::test::TempFilename( |
165 webrtc::test::OutputPath(), "TempFilenameTest"); | 199 webrtc::test::OutputPath(), "TempFilenameTest"); |
166 ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) | 200 ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) |
167 << "Couldn't find file: " << temp_filename; | 201 << "Couldn't find file: " << temp_filename; |
168 ASSERT_FALSE(webrtc::test::DirExists(temp_filename)) | 202 ASSERT_FALSE(webrtc::test::DirExists(temp_filename)) |
169 << "Existing file recognized as existing directory"; | 203 << "Existing file recognized as existing directory"; |
170 remove(temp_filename.c_str()); | 204 remove(temp_filename.c_str()); |
171 } | 205 } |
172 | 206 |
207 TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) { | |
208 size_t num_deleted_entries; | |
209 | |
210 // Create an empty temporary directory for this test. | |
211 const std::string temp_directory = | |
212 OutputPath() + "TempFileUtilsTestReadDirectory" + kPathDelimiter; | |
213 CreateDir(temp_directory); | |
214 CleanDir(temp_directory, &num_deleted_entries); | |
215 EXPECT_TRUE(DirExists(temp_directory)); | |
216 | |
217 // Add a file. | |
218 const std::string temp_filename = temp_directory + "TempFilenameTest"; | |
219 WriteStringInFile("test\n", temp_filename); | |
220 EXPECT_TRUE(FileExists(temp_filename)); | |
221 | |
222 // Add an empty directory. | |
223 const std::string temp_subdir = temp_directory + "subdir" + kPathDelimiter; | |
224 EXPECT_TRUE(CreateDir(temp_subdir)); | |
225 EXPECT_TRUE(DirExists(temp_subdir)); | |
226 | |
227 // Checks. | |
228 std::vector<std::string> output; | |
229 EXPECT_TRUE(ReadDirectory(temp_directory, &output)); | |
230 EXPECT_EQ(2u, output.size()); | |
231 CleanDir(temp_directory, &num_deleted_entries); | |
232 EXPECT_EQ(2u, num_deleted_entries); | |
233 EXPECT_TRUE(RemoveDir(temp_directory)); | |
234 EXPECT_FALSE(DirExists(temp_directory)); | |
235 } | |
236 | |
237 } // namespace test | |
173 } // namespace webrtc | 238 } // namespace webrtc |
OLD | NEW |