File: TestFileSystem.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (147 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <string>
#include <cstdio>
#include <sys/stat.h>
#include "System/Log/ILog.h"

#define CATCH_CONFIG_MAIN
#include "lib/catch.hpp"

// needs to be included after catch
#include "System/FileSystem/FileSystem.h"

namespace {
	struct PrepareFileSystem {
		PrepareFileSystem() {
			oldDir = FileSystem::GetCwd();
			testCwd = GetTempDir();
			if (!testCwd.empty()) {
				FileSystem::ChDir(testCwd);

				// create files and dirs to test
				FileSystem::CreateDirectory("testDir");
				WriteFile("testFile.txt", "a");
			}
		}
		~PrepareFileSystem() {
			if (!testCwd.empty()) {
				// delete files and dirs created in the ctor
				FileSystem::DeleteFile("testFile.txt");
				FileSystem::DeleteFile("testDir");
			}

			FileSystem::ChDir(oldDir);
			if (!testCwd.empty()) {
				FileSystem::DeleteFile(testCwd);
			}
		}
		static void WriteFile(const std::string& filePath, const std::string& content) {
			FILE* testFile = fopen(filePath.c_str(), "w");
			if (testFile != NULL) {
				fprintf(testFile, "%s", content.c_str());
				fclose(testFile);
				testFile = NULL;
			} else {
				FAIL("Failed to create test-file " + filePath);
			}
		}
		std::string GetTempDir() {
			if (testCwd.empty()) {
				char* tmpDir = tmpnam(NULL);
				if (tmpDir != NULL) {
					testCwd = oldDir + tmpDir;
					FileSystem::CreateDirectory(testCwd);
					if (!FileSystem::DirIsWritable(testCwd)) {
						FAIL("Failed to create temporary test dir");
					}
				} else {
					FAIL("Failed to get temporary file name");
				}
			}
			return testCwd;
		}

		std::string testCwd;
	private:
		std::string oldDir;
	};
}

PrepareFileSystem pfs;

TEST_CASE("FileExists")
{
	CHECK(FileSystem::FileExists("testFile.txt"));
	CHECK_FALSE(FileSystem::FileExists("testFile99.txt"));
	CHECK_FALSE(FileSystem::FileExists("testDir"));
	CHECK_FALSE(FileSystem::FileExists("testDir99"));
}


TEST_CASE("GetFileSize")
{
	CHECK(FileSystem::GetFileSize("testFile.txt") == 1);
	CHECK(FileSystem::GetFileSize("testFile99.txt") == -1);
	CHECK(FileSystem::GetFileSize("testDir") == -1);
	CHECK(FileSystem::GetFileSize("testDir99") == -1);
}


TEST_CASE("GetFileModificationDate")
{
	CHECK(FileSystem::GetFileModificationDate("testDir") != "");
	CHECK(FileSystem::GetFileModificationDate("testFile.txt") != "");
	CHECK(FileSystem::GetFileModificationDate("not_there") == "");
}


TEST_CASE("CreateDirectory")
{
	// create & exists
	CHECK(FileSystem::DirIsWritable("./"));
	CHECK(FileSystem::DirExists("testDir"));
	CHECK(FileSystem::DirExists("testDir///"));
	CHECK(FileSystem::DirExists("testDir////./"));
	CHECK(FileSystem::ComparePaths("testDir", "testDir////./"));
	CHECK_FALSE(FileSystem::ComparePaths("testDir", "test Dir2"));
	CHECK(FileSystem::CreateDirectory("testDir")); // already exists
	CHECK(FileSystem::CreateDirectory("testDir1")); // should be created
	CHECK(FileSystem::CreateDirectory("test Dir2")); // should be created

	// check if exists & no overwrite
	CHECK(FileSystem::CreateDirectory("test Dir2")); // already exists
	CHECK(FileSystem::DirIsWritable("test Dir2"));
	CHECK_FALSE(FileSystem::CreateDirectory("testFile.txt")); // file with this name already exists

	// delete temporaries
	CHECK(FileSystem::DeleteFile("testDir1"));
	CHECK(FileSystem::DeleteFile("test Dir2"));

	// check if really deleted
	CHECK_FALSE(FileSystem::DirExists("testDir1"));
	CHECK_FALSE(FileSystem::DirExists("test Dir2"));
}


TEST_CASE("GetDirectory")
{
#define CHECK_DIR_EXTRACTION(path, dir) \
		CHECK(FileSystem::GetDirectory(path) == dir)

	CHECK_DIR_EXTRACTION("testFile.txt", "");
	CHECK_DIR_EXTRACTION("./foo/testFile.txt", "./foo/");

#undef CHECK_DIR_EXTRACTION
}


TEST_CASE("GetNormalizedPath")
{
#define CHECK_NORM_PATH(path, normPath) \
		CHECK(FileSystem::GetNormalizedPath(path) == normPath)

	CHECK_NORM_PATH("/home/userX/.spring/foo/bar///./../test.log", "/home/userX/.spring/foo/test.log");
	CHECK_NORM_PATH("./symLinkToHome/foo/bar///./../test.log", "./symLinkToHome/foo/test.log");
	CHECK_NORM_PATH("C:\\foo\\bar\\\\\\.\\..\\test.log", "C:/foo/test.log");

#undef CHECK_NORM_PATH
}