1 #include <QTest>
2 #include <QTemporaryDir>
3 #include <QStandardPaths>
4 #include "TestUtil.h"
5 
6 #include "FileSystem.h"
7 
8 class FileSystemTest : public QObject
9 {
10     Q_OBJECT
11 
12     const QString bothSlash = "/foo/";
13     const QString trailingSlash = "foo/";
14     const QString leadingSlash = "/foo";
15 
16 private
17 slots:
test_pathCombine()18     void test_pathCombine()
19     {
20         QCOMPARE(QString("/foo/foo"), FS::PathCombine(bothSlash, bothSlash));
21         QCOMPARE(QString("foo/foo"), FS::PathCombine(trailingSlash, trailingSlash));
22         QCOMPARE(QString("/foo/foo"), FS::PathCombine(leadingSlash, leadingSlash));
23 
24         QCOMPARE(QString("/foo/foo/foo"), FS::PathCombine(bothSlash, bothSlash, bothSlash));
25         QCOMPARE(QString("foo/foo/foo"), FS::PathCombine(trailingSlash, trailingSlash, trailingSlash));
26         QCOMPARE(QString("/foo/foo/foo"), FS::PathCombine(leadingSlash, leadingSlash, leadingSlash));
27     }
28 
test_PathCombine1_data()29     void test_PathCombine1_data()
30     {
31         QTest::addColumn<QString>("result");
32         QTest::addColumn<QString>("path1");
33         QTest::addColumn<QString>("path2");
34 
35         QTest::newRow("qt 1") << "/abc/def/ghi/jkl" << "/abc/def" << "ghi/jkl";
36         QTest::newRow("qt 2") << "/abc/def/ghi/jkl" << "/abc/def/" << "ghi/jkl";
37 #if defined(Q_OS_WIN)
38         QTest::newRow("win native, from C:") << "C:/abc" << "C:" << "abc";
39         QTest::newRow("win native 1") << "C:/abc/def/ghi/jkl" << "C:\\abc\\def" << "ghi\\jkl";
40         QTest::newRow("win native 2") << "C:/abc/def/ghi/jkl" << "C:\\abc\\def\\" << "ghi\\jkl";
41 #endif
42     }
43 
test_PathCombine1()44     void test_PathCombine1()
45     {
46         QFETCH(QString, result);
47         QFETCH(QString, path1);
48         QFETCH(QString, path2);
49 
50         QCOMPARE(FS::PathCombine(path1, path2), result);
51     }
52 
test_PathCombine2_data()53     void test_PathCombine2_data()
54     {
55         QTest::addColumn<QString>("result");
56         QTest::addColumn<QString>("path1");
57         QTest::addColumn<QString>("path2");
58         QTest::addColumn<QString>("path3");
59 
60         QTest::newRow("qt 1") << "/abc/def/ghi/jkl" << "/abc" << "def" << "ghi/jkl";
61         QTest::newRow("qt 2") << "/abc/def/ghi/jkl" << "/abc/" << "def" << "ghi/jkl";
62         QTest::newRow("qt 3") << "/abc/def/ghi/jkl" << "/abc" << "def/" << "ghi/jkl";
63         QTest::newRow("qt 4") << "/abc/def/ghi/jkl" << "/abc/" << "def/" << "ghi/jkl";
64 #if defined(Q_OS_WIN)
65         QTest::newRow("win 1") << "C:/abc/def/ghi/jkl" << "C:\\abc" << "def" << "ghi\\jkl";
66         QTest::newRow("win 2") << "C:/abc/def/ghi/jkl" << "C:\\abc\\" << "def" << "ghi\\jkl";
67         QTest::newRow("win 3") << "C:/abc/def/ghi/jkl" << "C:\\abc" << "def\\" << "ghi\\jkl";
68         QTest::newRow("win 4") << "C:/abc/def/ghi/jkl" << "C:\\abc\\" << "def" << "ghi\\jkl";
69 #endif
70     }
71 
test_PathCombine2()72     void test_PathCombine2()
73     {
74         QFETCH(QString, result);
75         QFETCH(QString, path1);
76         QFETCH(QString, path2);
77         QFETCH(QString, path3);
78 
79         QCOMPARE(FS::PathCombine(path1, path2, path3), result);
80     }
81 
test_copy()82     void test_copy()
83     {
84         QString folder = QFINDTESTDATA("data/test_folder");
85         auto f = [&folder]()
86         {
87             QTemporaryDir tempDir;
88             tempDir.setAutoRemove(true);
89             qDebug() << "From:" << folder << "To:" << tempDir.path();
90 
91             QDir target_dir(FS::PathCombine(tempDir.path(), "test_folder"));
92             qDebug() << tempDir.path();
93             qDebug() << target_dir.path();
94             FS::copy c(folder, target_dir.path());
95             c();
96 
97             for(auto entry: target_dir.entryList())
98             {
99                 qDebug() << entry;
100             }
101             QVERIFY(target_dir.entryList().contains("pack.mcmeta"));
102             QVERIFY(target_dir.entryList().contains("assets"));
103         };
104 
105         // first try variant without trailing /
106         QVERIFY(!folder.endsWith('/'));
107         f();
108 
109         // then variant with trailing /
110         folder.append('/');
111         QVERIFY(folder.endsWith('/'));
112         f();
113     }
114 
test_getDesktop()115     void test_getDesktop()
116     {
117         QCOMPARE(FS::getDesktopDir(), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
118     }
119 
120 // this is only valid on linux
121 // FIXME: implement on windows, OSX, then test.
122 #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
test_createShortcut_data()123     void test_createShortcut_data()
124     {
125         QTest::addColumn<QString>("location");
126         QTest::addColumn<QString>("dest");
127         QTest::addColumn<QStringList>("args");
128         QTest::addColumn<QString>("name");
129         QTest::addColumn<QString>("iconLocation");
130         QTest::addColumn<QByteArray>("result");
131 
132         QTest::newRow("unix") << QDir::currentPath()
133                               << "asdfDest"
134                               << (QStringList() << "arg1" << "arg2")
135                               << "asdf"
136                               << QString()
137                          #if defined(Q_OS_LINUX)
138                               << MULTIMC_GET_TEST_FILE("data/FileSystem-test_createShortcut-unix")
139                          #elif defined(Q_OS_WIN)
140                               << QByteArray()
141                          #endif
142                                  ;
143     }
144 
test_createShortcut()145     void test_createShortcut()
146     {
147         QFETCH(QString, location);
148         QFETCH(QString, dest);
149         QFETCH(QStringList, args);
150         QFETCH(QString, name);
151         QFETCH(QString, iconLocation);
152         QFETCH(QByteArray, result);
153 
154         QVERIFY(FS::createShortCut(location, dest, args, name, iconLocation));
155         QCOMPARE(QString::fromLocal8Bit(TestsInternal::readFile(location + QDir::separator() + name + ".desktop")), QString::fromLocal8Bit(result));
156 
157         //QDir().remove(location);
158     }
159 #endif
160 };
161 
162 QTEST_GUILESS_MAIN(FileSystemTest)
163 
164 #include "FileSystem_test.moc"
165