forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileinfo_test.cpp
112 lines (91 loc) · 4.26 KB
/
fileinfo_test.cpp
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
#include "util/fileinfo.h"
#include <gtest/gtest.h>
#include "test/mixxxtest.h"
#include <QTemporaryDir>
#include <QtDebug>
namespace mixxx {
class FileInfoTest : public testing::Test {
protected:
const QTemporaryDir m_tempDir;
const QString m_relativePath;
const QString m_absolutePath;
const QString m_relativePathMissing;
const QString m_absolutePathMissing;
FileInfoTest()
: m_relativePath(QStringLiteral("relative")),
m_absolutePath(m_tempDir.filePath(m_relativePath)),
m_relativePathMissing(QStringLiteral("missing")),
m_absolutePathMissing(m_tempDir.filePath(m_relativePathMissing)) {
}
void SetUp() override {
ASSERT_TRUE(m_tempDir.isValid());
ASSERT_TRUE(QDir(m_tempDir.path()).mkpath(m_relativePath));
ASSERT_TRUE(FileInfo(m_absolutePath).exists());
ASSERT_FALSE(FileInfo(m_absolutePathMissing).exists());
}
};
TEST_F(FileInfoTest, emptyPathIsRelative) {
EXPECT_FALSE(FileInfo().isAbsolute());
EXPECT_TRUE(FileInfo().isRelative());
}
TEST_F(FileInfoTest, nonEmptyPathIsEitherAbsoluteOrRelative) {
EXPECT_TRUE(FileInfo(m_absolutePath).isAbsolute());
EXPECT_FALSE(FileInfo(m_absolutePath).isRelative());
EXPECT_TRUE(FileInfo(m_absolutePathMissing).isAbsolute());
EXPECT_FALSE(FileInfo(m_absolutePathMissing).isRelative());
EXPECT_FALSE(FileInfo(m_relativePath).isAbsolute());
EXPECT_TRUE(FileInfo(m_relativePath).isRelative());
EXPECT_FALSE(FileInfo(m_relativePathMissing).isAbsolute());
EXPECT_TRUE(FileInfo(m_relativePathMissing).isRelative());
}
TEST_F(FileInfoTest, hasLocation) {
EXPECT_FALSE(FileInfo().hasLocation());
EXPECT_TRUE(FileInfo(m_absolutePath).hasLocation());
EXPECT_TRUE(FileInfo(m_absolutePath).hasLocation());
EXPECT_FALSE(FileInfo(m_relativePath).hasLocation());
EXPECT_TRUE(FileInfo(m_absolutePathMissing).hasLocation());
EXPECT_FALSE(FileInfo(m_relativePathMissing).hasLocation());
}
TEST_F(FileInfoTest, trailingSlash) {
ASSERT_FALSE(m_absolutePath.endsWith(QLatin1Char('/')));
QString absolutePathTrailingSlash = m_absolutePath + "/";
EXPECT_TRUE(FileInfo(absolutePathTrailingSlash).hasLocation());
EXPECT_FALSE(FileInfo(absolutePathTrailingSlash).isFile());
EXPECT_TRUE(FileInfo(absolutePathTrailingSlash).isDir());
EXPECT_QSTRING_EQ(FileInfo(absolutePathTrailingSlash).location(), m_absolutePath);
}
TEST_F(FileInfoTest, freshCanonicalFileInfo) {
FileInfo fileInfo(m_absolutePathMissing);
// This test assumes that caching is enabled resulting
// in expected inconsistencies until refreshed.
ASSERT_TRUE(fileInfo.m_fileInfo.caching());
ASSERT_TRUE(fileInfo.canonicalLocation().isEmpty());
ASSERT_TRUE(fileInfo.resolveCanonicalLocation().isEmpty());
// Restore the missing file
QFile file(m_absolutePathMissing);
ASSERT_FALSE(fileInfo.checkFileExists());
ASSERT_TRUE(file.open(QIODevice::ReadWrite | QIODevice::NewOnly));
ASSERT_TRUE(fileInfo.checkFileExists());
// The cached canonical location should still be invalid
EXPECT_TRUE(fileInfo.canonicalLocation().isEmpty());
// The refreshed canonical location should be valid
EXPECT_FALSE(fileInfo.resolveCanonicalLocation().isEmpty());
// The cached canonical location should have been updated
EXPECT_FALSE(fileInfo.canonicalLocation().isEmpty());
// Remove the file
ASSERT_TRUE(file.remove());
ASSERT_FALSE(fileInfo.checkFileExists());
ASSERT_TRUE(FileInfo(m_absolutePathMissing).canonicalLocation().isEmpty());
// Note: Qt (5.14.x) doesn't seem to invalidate the canonical location
// after it has been set once, even when refreshing the QFileInfo. This
// is what the remaining part of the test validates, although Mixxx does
// NOT rely on this behavior! Just to get notified when this behavior
// changes for some future Qt version and for ensuring that the behavior
// is identical on all platforms.
// The cached canonical location should still be valid
EXPECT_FALSE(fileInfo.canonicalLocation().isEmpty());
// The canonical location should not be refreshed again, i.e. it remains
// valid after the file has disappeared
EXPECT_FALSE(fileInfo.resolveCanonicalLocation().isEmpty());
}
} // namespace mixxx