]>
Commit | Line | Data |
---|---|---|
f095b1fc VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/filename/filename.cpp | |
3 | // Purpose: wxFileName unit test | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2004-07-25 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
f095b1fc VZ |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
f7c69b90 | 21 | #include "wx/utils.h" |
f095b1fc VZ |
22 | #endif // WX_PRECOMP |
23 | ||
24 | #include "wx/filename.h" | |
c08dd08b | 25 | #include "wx/filefn.h" |
1fe1aecb | 26 | #include "wx/stdpaths.h" |
41fec01f | 27 | #include "wx/scopeguard.h" |
f095b1fc | 28 | |
bb5a9514 | 29 | #ifdef __WINDOWS__ |
525711d7 | 30 | #include "wx/msw/registry.h" |
bb5a9514 | 31 | #endif // __WINDOWS__ |
525711d7 | 32 | |
7c9b6c91 VZ |
33 | #ifdef __UNIX__ |
34 | #include <unistd.h> | |
35 | #endif // __UNIX__ | |
36 | ||
40152925 | 37 | #include "testfile.h" |
2264775b | 38 | |
f095b1fc VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // test data | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
5fed01a9 | 43 | static struct TestFileNameInfo |
f095b1fc | 44 | { |
9630954d VZ |
45 | const char *fullname; |
46 | const char *volume; | |
47 | const char *path; | |
48 | const char *name; | |
49 | const char *ext; | |
f095b1fc VZ |
50 | bool isAbsolute; |
51 | wxPathFormat format; | |
52 | } filenames[] = | |
53 | { | |
69858116 | 54 | // the empty string |
9630954d VZ |
55 | { "", "", "", "", "", false, wxPATH_UNIX }, |
56 | { "", "", "", "", "", false, wxPATH_DOS }, | |
57 | { "", "", "", "", "", false, wxPATH_VMS }, | |
69858116 | 58 | |
f095b1fc | 59 | // Unix file names |
9630954d VZ |
60 | { "/usr/bin/ls", "", "/usr/bin", "ls", "", true, wxPATH_UNIX }, |
61 | { "/usr/bin/", "", "/usr/bin", "", "", true, wxPATH_UNIX }, | |
62 | { "~/.zshrc", "", "~", ".zshrc", "", true, wxPATH_UNIX }, | |
63 | { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX }, | |
64 | { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX }, | |
65 | { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX }, | |
be5be16a VZ |
66 | { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX }, |
67 | { "~user/", "", "~user", "", "", true, wxPATH_UNIX }, | |
9630954d VZ |
68 | { "/foo", "", "/", "foo", "", true, wxPATH_UNIX }, |
69 | { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX }, | |
70 | { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX }, | |
f095b1fc VZ |
71 | |
72 | // Windows file names | |
9630954d VZ |
73 | { "foo.bar", "", "", "foo", "bar", false, wxPATH_DOS }, |
74 | { "\\foo.bar", "", "\\", "foo", "bar", false, wxPATH_DOS }, | |
75 | { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS }, | |
76 | { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS }, | |
77 | { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS }, | |
e01a788e VZ |
78 | { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", |
79 | "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\", "", "", true, wxPATH_DOS }, | |
80 | { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\Program Files\\setup.exe", | |
81 | "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\Program Files", "setup", "exe", true, wxPATH_DOS }, | |
bf7f7793 | 82 | |
ea6319cb | 83 | #if 0 |
c7099635 VZ |
84 | // NB: when using the wxFileName::GetLongPath() function on these two |
85 | // strings, the program will hang for several seconds blocking inside | |
86 | // Win32 GetLongPathName() function | |
9630954d VZ |
87 | { "\\\\server\\foo.bar", "server", "\\", "foo", "bar", true, wxPATH_DOS }, |
88 | { "\\\\server\\dir\\foo.bar", "server", "\\dir", "foo", "bar", true, wxPATH_DOS }, | |
ea6319cb | 89 | #endif |
f095b1fc | 90 | |
34841b21 VZ |
91 | // consecutive [back]slashes should be treated as single occurrences of |
92 | // them and not interpreted as share names if there is a volume name | |
9630954d VZ |
93 | { "c:\\aaa\\bbb\\ccc", "c", "\\aaa\\bbb", "ccc", "", true, wxPATH_DOS }, |
94 | { "c:\\\\aaa\\bbb\\ccc", "c", "\\\\aaa\\bbb", "ccc", "", true, wxPATH_DOS }, | |
bf7f7793 | 95 | |
f095b1fc VZ |
96 | // wxFileName support for Mac file names is broken currently |
97 | #if 0 | |
98 | // Mac file names | |
9630954d VZ |
99 | { "Volume:Dir:File", "Volume", "Dir", "File", "", true, wxPATH_MAC }, |
100 | { "Volume:Dir:Subdir:File", "Volume", "Dir:Subdir", "File", "", true, wxPATH_MAC }, | |
101 | { "Volume:", "Volume", "", "", "", true, wxPATH_MAC }, | |
102 | { ":Dir:File", "", "Dir", "File", "", false, wxPATH_MAC }, | |
103 | { ":File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC }, | |
104 | { "File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC }, | |
f095b1fc VZ |
105 | #endif // 0 |
106 | ||
ea6319cb | 107 | #if 0 |
f095b1fc | 108 | // VMS file names |
bf7f7793 RR |
109 | // NB: on Windows they have the same effect of the \\server\\ strings |
110 | // (see the note above) | |
9630954d | 111 | { "device:[dir1.dir2.dir3]file.txt", "device", "dir1.dir2.dir3", "file", "txt", true, wxPATH_VMS }, |
ea6319cb | 112 | #endif |
9630954d | 113 | { "file.txt", "", "", "file", "txt", false, wxPATH_VMS }, |
f095b1fc VZ |
114 | }; |
115 | ||
116 | // ---------------------------------------------------------------------------- | |
117 | // test class | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | class FileNameTestCase : public CppUnit::TestCase | |
121 | { | |
122 | public: | |
123 | FileNameTestCase() { } | |
124 | ||
125 | private: | |
126 | CPPUNIT_TEST_SUITE( FileNameTestCase ); | |
127 | CPPUNIT_TEST( TestConstruction ); | |
33366127 | 128 | CPPUNIT_TEST( TestComparison ); |
f095b1fc | 129 | CPPUNIT_TEST( TestSplit ); |
4524a24b | 130 | CPPUNIT_TEST( TestSetPath ); |
c08dd08b | 131 | CPPUNIT_TEST( TestStrip ); |
bf7f7793 | 132 | CPPUNIT_TEST( TestNormalize ); |
395f3aa8 | 133 | CPPUNIT_TEST( TestReplace ); |
b2edb8f3 | 134 | CPPUNIT_TEST( TestGetHumanReadable ); |
60c0dfe5 VZ |
135 | #ifdef __WINDOWS__ |
136 | CPPUNIT_TEST( TestShortLongPath ); | |
137 | #endif // __WINDOWS__ | |
7b611a3a | 138 | CPPUNIT_TEST( TestUNC ); |
e01a788e | 139 | CPPUNIT_TEST( TestVolumeUniqueName ); |
1fe1aecb FM |
140 | CPPUNIT_TEST( TestCreateTempFileName ); |
141 | CPPUNIT_TEST( TestGetTimes ); | |
901504c3 | 142 | CPPUNIT_TEST( TestExists ); |
7c9b6c91 | 143 | CPPUNIT_TEST( TestIsSame ); |
c063adeb VZ |
144 | #if defined(__UNIX__) |
145 | CPPUNIT_TEST( TestSymlinks ); | |
146 | #endif // __UNIX__ | |
f095b1fc VZ |
147 | CPPUNIT_TEST_SUITE_END(); |
148 | ||
149 | void TestConstruction(); | |
33366127 | 150 | void TestComparison(); |
f095b1fc VZ |
151 | void TestSplit(); |
152 | void TestSetPath(); | |
c08dd08b | 153 | void TestStrip(); |
bf7f7793 | 154 | void TestNormalize(); |
395f3aa8 | 155 | void TestReplace(); |
b2edb8f3 | 156 | void TestGetHumanReadable(); |
60c0dfe5 VZ |
157 | #ifdef __WINDOWS__ |
158 | void TestShortLongPath(); | |
159 | #endif // __WINDOWS__ | |
7b611a3a | 160 | void TestUNC(); |
e01a788e | 161 | void TestVolumeUniqueName(); |
1fe1aecb FM |
162 | void TestCreateTempFileName(); |
163 | void TestGetTimes(); | |
901504c3 | 164 | void TestExists(); |
7c9b6c91 | 165 | void TestIsSame(); |
c063adeb VZ |
166 | #if defined(__UNIX__) |
167 | void TestSymlinks(); | |
168 | #endif // __UNIX__ | |
f095b1fc VZ |
169 | |
170 | DECLARE_NO_COPY_CLASS(FileNameTestCase) | |
171 | }; | |
172 | ||
173 | // register in the unnamed registry so that these tests are run by default | |
174 | CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase ); | |
175 | ||
e3778b4d | 176 | // also include in its own registry so that these tests can be run alone |
f095b1fc VZ |
177 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" ); |
178 | ||
179 | void FileNameTestCase::TestConstruction() | |
180 | { | |
181 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
182 | { | |
5fed01a9 | 183 | const TestFileNameInfo& fni = filenames[n]; |
f095b1fc VZ |
184 | |
185 | wxFileName fn(fni.fullname, fni.format); | |
186 | ||
9b9596de VZ |
187 | // the original full name could contain consecutive [back]slashes, |
188 | // squeeze them except for the double backslash in the beginning in | |
189 | // Windows filenames where it has special meaning | |
190 | wxString fullnameOrig; | |
191 | if ( fni.format == wxPATH_DOS ) | |
192 | { | |
193 | // copy the backslashes at beginning unchanged | |
9630954d VZ |
194 | const char *p = fni.fullname; |
195 | while ( *p == '\\' ) | |
9b9596de VZ |
196 | fullnameOrig += *p++; |
197 | ||
198 | // replace consecutive slashes with single ones in the rest | |
9630954d | 199 | for ( char chPrev = '\0'; *p; p++ ) |
9b9596de | 200 | { |
9630954d | 201 | if ( *p == '\\' && chPrev == '\\' ) |
9b9596de VZ |
202 | continue; |
203 | ||
204 | chPrev = *p; | |
205 | fullnameOrig += chPrev; | |
206 | } | |
207 | } | |
208 | else // !wxPATH_DOS | |
209 | { | |
210 | fullnameOrig = fni.fullname; | |
211 | } | |
212 | ||
9630954d | 213 | fullnameOrig.Replace("//", "/"); |
9b9596de VZ |
214 | |
215 | ||
f095b1fc | 216 | wxString fullname = fn.GetFullPath(fni.format); |
9b9596de | 217 | CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname ); |
f095b1fc | 218 | |
ab8576b4 VZ |
219 | // notice that we use a dummy working directory to ensure that paths |
220 | // with "../.." in them could be normalized, otherwise this would fail | |
221 | // if the test is run from root directory or its direct subdirectory | |
2264775b VZ |
222 | CPPUNIT_ASSERT_MESSAGE |
223 | ( | |
9630954d VZ |
224 | (const char *)wxString::Format("Normalize(%s) failed", fni.fullname).mb_str(), |
225 | fn.Normalize(wxPATH_NORM_ALL, "/foo/bar/baz", fni.format) | |
2264775b | 226 | ); |
4c2deb19 VZ |
227 | |
228 | if ( *fni.volume && *fni.path ) | |
229 | { | |
230 | // check that specifying the volume separately or as part of the | |
231 | // path doesn't make any difference | |
232 | wxString pathWithVolume = fni.volume; | |
233 | pathWithVolume += wxFileName::GetVolumeSeparator(fni.format); | |
234 | pathWithVolume += fni.path; | |
235 | ||
2264775b | 236 | CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume, |
4c2deb19 VZ |
237 | fni.name, |
238 | fni.ext, | |
2264775b | 239 | fni.format), fn ); |
4c2deb19 | 240 | } |
f095b1fc | 241 | } |
69858116 VZ |
242 | |
243 | wxFileName fn; | |
244 | ||
245 | // empty strings | |
246 | fn.AssignDir(wxEmptyString); | |
247 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
248 | ||
249 | fn.Assign(wxEmptyString); | |
250 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
251 | ||
252 | fn.Assign(wxEmptyString, wxEmptyString); | |
253 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
254 | ||
255 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString); | |
256 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
257 | ||
258 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString); | |
259 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
f095b1fc VZ |
260 | } |
261 | ||
33366127 VZ |
262 | void FileNameTestCase::TestComparison() |
263 | { | |
523cd68a VZ |
264 | wxFileName fn1(wxT("/tmp/file1")); |
265 | wxFileName fn2(wxT("/tmp/dir2/../file2")); | |
266 | fn1.Normalize(); | |
267 | fn2.Normalize(); | |
2264775b | 268 | CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath()); |
33366127 VZ |
269 | } |
270 | ||
f095b1fc VZ |
271 | void FileNameTestCase::TestSplit() |
272 | { | |
273 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
274 | { | |
5fed01a9 | 275 | const TestFileNameInfo& fni = filenames[n]; |
f095b1fc VZ |
276 | wxString volume, path, name, ext; |
277 | wxFileName::SplitPath(fni.fullname, | |
278 | &volume, &path, &name, &ext, fni.format); | |
279 | ||
2264775b VZ |
280 | CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume ); |
281 | CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path ); | |
282 | CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name ); | |
283 | CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext ); | |
f095b1fc | 284 | } |
dfecbee5 VZ |
285 | |
286 | // special case of empty extension | |
9630954d VZ |
287 | wxFileName fn("foo."); |
288 | CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn.GetFullPath() ); | |
f095b1fc VZ |
289 | } |
290 | ||
4524a24b VZ |
291 | void FileNameTestCase::TestSetPath() |
292 | { | |
9630954d VZ |
293 | wxFileName fn("d:\\test\\foo.bar", wxPATH_DOS); |
294 | fn.SetPath("c:\\temp", wxPATH_DOS); | |
295 | CPPUNIT_ASSERT( fn.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS)) ); | |
4524a24b | 296 | |
9630954d VZ |
297 | fn = wxFileName("/usr/bin/ls", wxPATH_UNIX); |
298 | fn.SetPath("/usr/local/bin", wxPATH_UNIX); | |
299 | CPPUNIT_ASSERT( fn.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX)) ); | |
4524a24b VZ |
300 | } |
301 | ||
bf7f7793 RR |
302 | void FileNameTestCase::TestNormalize() |
303 | { | |
304 | // prepare some data to be used later | |
305 | wxString sep = wxFileName::GetPathSeparator(); | |
306 | wxString cwd = wxGetCwd(); | |
307 | wxString home = wxGetUserHome(); | |
308 | ||
309 | cwd.Replace(sep, wxT("/")); | |
310 | if (cwd.Last() != wxT('/')) | |
311 | cwd += wxT('/'); | |
312 | home.Replace(sep, wxT("/")); | |
313 | if (home.Last() != wxT('/')) | |
314 | home += wxT('/'); | |
315 | ||
316 | // since we will always be testing paths using the wxPATH_UNIX | |
317 | // format, we need to remove the volume, if present | |
318 | if (home.Contains(wxT(':'))) | |
319 | home = home.AfterFirst(wxT(':')); | |
320 | if (cwd.Contains(wxT(':'))) | |
321 | cwd = cwd.AfterFirst(wxT(':')); | |
322 | ||
8e083702 | 323 | static const struct FileNameTest |
bf7f7793 | 324 | { |
527587d3 | 325 | const char *original; |
bf7f7793 | 326 | int flags; |
527587d3 | 327 | const char *expected; |
c7099635 | 328 | wxPathFormat fmt; |
bf7f7793 RR |
329 | } tests[] = |
330 | { | |
331 | // test wxPATH_NORM_ENV_VARS | |
bb5a9514 | 332 | #ifdef __WINDOWS__ |
c7099635 | 333 | { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX }, |
bf7f7793 | 334 | #else |
c7099635 | 335 | { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX }, |
bf7f7793 RR |
336 | #endif |
337 | ||
338 | // test wxPATH_NORM_DOTS | |
c7099635 | 339 | { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, |
f822acce VZ |
340 | { "", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, |
341 | { "./foo", wxPATH_NORM_DOTS, "foo", wxPATH_UNIX }, | |
342 | { "b/../bar", wxPATH_NORM_DOTS, "bar", wxPATH_UNIX }, | |
343 | { "c/../../quux", wxPATH_NORM_DOTS, "../quux", wxPATH_UNIX }, | |
344 | { "/c/../../quux", wxPATH_NORM_DOTS, "/quux", wxPATH_UNIX }, | |
bf7f7793 | 345 | |
be5be16a VZ |
346 | // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially |
347 | // when it is the first character in the file name | |
c7099635 | 348 | { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX }, |
be5be16a | 349 | { "/~/a/b", wxPATH_NORM_TILDE, "/~/a/b", wxPATH_UNIX }, |
527587d3 | 350 | { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, |
c7099635 VZ |
351 | |
352 | // test wxPATH_NORM_CASE | |
353 | { "Foo", wxPATH_NORM_CASE, "Foo", wxPATH_UNIX }, | |
354 | { "Foo", wxPATH_NORM_CASE, "foo", wxPATH_DOS }, | |
355 | { "C:\\Program Files\\wx", wxPATH_NORM_CASE, | |
356 | "c:\\program files\\wx", wxPATH_DOS }, | |
357 | { "C:/Program Files/wx", wxPATH_NORM_ALL | wxPATH_NORM_CASE, | |
358 | "c:\\program files\\wx", wxPATH_DOS }, | |
9a0c5c01 VZ |
359 | { "C:\\Users\\zeitlin", wxPATH_NORM_ALL | wxPATH_NORM_CASE, |
360 | "c:\\users\\zeitlin", wxPATH_DOS }, | |
bf7f7793 RR |
361 | |
362 | // test wxPATH_NORM_ABSOLUTE | |
527587d3 VZ |
363 | { "a/b/", wxPATH_NORM_ABSOLUTE, "CWD/a/b/", wxPATH_UNIX }, |
364 | { "a/b/c.ext", wxPATH_NORM_ABSOLUTE, "CWD/a/b/c.ext", wxPATH_UNIX }, | |
c7099635 | 365 | { "/a", wxPATH_NORM_ABSOLUTE, "/a", wxPATH_UNIX }, |
bf7f7793 RR |
366 | |
367 | // test giving no flags at all to Normalize() | |
c7099635 VZ |
368 | { "a/b/", 0, "a/b/", wxPATH_UNIX }, |
369 | { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX }, | |
ea6319cb VZ |
370 | { "/a", 0, "/a", wxPATH_UNIX }, |
371 | ||
372 | // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE | |
373 | // for both existing and non-existent files (this is important under | |
374 | // MSW where GetLongPathName() works only for the former) | |
375 | { "./foo", wxPATH_NORM_LONG, "./foo", wxPATH_UNIX }, | |
376 | { "../foo", wxPATH_NORM_LONG, "../foo", wxPATH_UNIX }, | |
377 | { ".\\test.bkl", wxPATH_NORM_LONG, ".\\test.bkl", wxPATH_DOS }, | |
378 | { ".\\foo", wxPATH_NORM_LONG, ".\\foo", wxPATH_DOS }, | |
379 | { "..\\Makefile.in", wxPATH_NORM_LONG, "..\\Makefile.in", wxPATH_DOS }, | |
380 | { "..\\foo", wxPATH_NORM_LONG, "..\\foo", wxPATH_DOS }, | |
bf7f7793 RR |
381 | }; |
382 | ||
383 | // set the env var ABCDEF | |
9630954d | 384 | wxSetEnv("ABCDEF", "abcdef"); |
bf7f7793 | 385 | |
2264775b | 386 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) |
bf7f7793 | 387 | { |
c7099635 VZ |
388 | const FileNameTest& fnt = tests[i]; |
389 | wxFileName fn(fnt.original, fnt.fmt); | |
bf7f7793 RR |
390 | |
391 | // be sure this normalization does not fail | |
a779d809 | 392 | WX_ASSERT_MESSAGE |
2264775b | 393 | ( |
a779d809 | 394 | ("#%d: Normalize(%s) failed", (int)i, fnt.original), |
c7099635 | 395 | fn.Normalize(fnt.flags, cwd, fnt.fmt) |
2264775b | 396 | ); |
bf7f7793 RR |
397 | |
398 | // compare result with expected string | |
527587d3 | 399 | wxString expected(tests[i].expected); |
9630954d VZ |
400 | expected.Replace("HOME/", home); |
401 | expected.Replace("CWD/", cwd); | |
a779d809 VZ |
402 | WX_ASSERT_EQUAL_MESSAGE |
403 | ( | |
404 | ("array element #%d", (int)i), | |
405 | expected, fn.GetFullPath(fnt.fmt) | |
406 | ); | |
bf7f7793 | 407 | } |
525711d7 VZ |
408 | |
409 | // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short | |
410 | // names generation is not disabled for this system as otherwise the file | |
411 | // MKINST~1 doesn't exist at all and normalizing it fails (it's possible | |
412 | // that we're on a FAT partition in which case the test would still succeed | |
413 | // and also that the registry key was changed recently and didn't take | |
414 | // effect yet but these are marginal cases which we consciously choose to | |
415 | // ignore for now) | |
bb5a9514 | 416 | #ifdef __WINDOWS__ |
525711d7 VZ |
417 | long shortNamesDisabled; |
418 | if ( wxRegKey | |
419 | ( | |
420 | wxRegKey::HKLM, | |
421 | "SYSTEM\\CurrentControlSet\\Control\\FileSystem" | |
422 | ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled) && | |
423 | !shortNamesDisabled ) | |
424 | { | |
425 | wxFileName fn("..\\MKINST~1"); | |
426 | CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_LONG, cwd) ); | |
427 | CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn.GetFullPath() ); | |
428 | } | |
429 | //else: when in doubt, don't run the test | |
bb5a9514 | 430 | #endif // __WINDOWS__ |
bf7f7793 RR |
431 | } |
432 | ||
395f3aa8 FM |
433 | void FileNameTestCase::TestReplace() |
434 | { | |
435 | static const struct FileNameTest | |
436 | { | |
437 | const char *original; | |
438 | const char *env_contents; | |
439 | const char *replace_fmtstring; | |
440 | const char *expected; | |
441 | wxPathFormat fmt; | |
442 | } tests[] = | |
443 | { | |
444 | { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX }, | |
445 | { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX }, | |
446 | { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX }, | |
447 | { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX }, | |
448 | { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX }, | |
449 | { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX }, | |
450 | { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX }, | |
451 | // note: empty directory components are automatically removed by wxFileName thus | |
452 | // using // in the replace format string has no effect | |
453 | ||
454 | { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX }, | |
455 | { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX }, | |
456 | { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX }, | |
457 | { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX }, | |
458 | ||
459 | { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN }, | |
460 | { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN }, | |
461 | { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN } | |
462 | }; | |
463 | ||
464 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) | |
465 | { | |
466 | const FileNameTest& fnt = tests[i]; | |
467 | wxFileName fn(fnt.original, fnt.fmt); | |
468 | ||
469 | // set the environment variable | |
9630954d | 470 | wxSetEnv("TEST_VAR", fnt.env_contents); |
395f3aa8 FM |
471 | |
472 | // be sure this ReplaceEnvVariable does not fail | |
473 | WX_ASSERT_MESSAGE | |
474 | ( | |
475 | ("#%d: ReplaceEnvVariable(%s) failed", (int)i, fnt.replace_fmtstring), | |
476 | fn.ReplaceEnvVariable("TEST_VAR", fnt.replace_fmtstring, fnt.fmt) | |
477 | ); | |
478 | ||
479 | // compare result with expected string | |
480 | wxString expected(fnt.expected); | |
481 | WX_ASSERT_EQUAL_MESSAGE | |
482 | ( | |
483 | ("array element #%d", (int)i), | |
484 | expected, fn.GetFullPath(fnt.fmt) | |
485 | ); | |
486 | } | |
487 | ||
488 | // now test ReplaceHomeDir | |
489 | ||
490 | wxFileName fn = wxFileName::DirName(wxGetHomeDir()); | |
491 | fn.AppendDir("test1"); | |
492 | fn.AppendDir("test2"); | |
493 | fn.AppendDir("test3"); | |
494 | fn.SetName("some file"); | |
495 | ||
496 | WX_ASSERT_MESSAGE | |
497 | ( | |
498 | ("ReplaceHomeDir(%s) failed", fn.GetFullPath()), | |
499 | fn.ReplaceHomeDir() | |
500 | ); | |
501 | ||
9630954d | 502 | CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"), |
395f3aa8 FM |
503 | fn.GetFullPath(wxPATH_UNIX) ); |
504 | } | |
505 | ||
b2edb8f3 VZ |
506 | void FileNameTestCase::TestGetHumanReadable() |
507 | { | |
508 | static const struct TestData | |
509 | { | |
510 | const char *result; | |
58271f42 | 511 | int size; |
b2edb8f3 VZ |
512 | int prec; |
513 | wxSizeConvention conv; | |
514 | } testData[] = | |
515 | { | |
a0752618 VZ |
516 | { "NA", 0, 1, wxSIZE_CONV_TRADITIONAL }, |
517 | { "2.0 KB", 2000, 1, wxSIZE_CONV_TRADITIONAL }, | |
518 | { "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC }, | |
519 | { "2.000 KB", 2000, 3, wxSIZE_CONV_SI }, | |
520 | { "297 KB", 304351, 0, wxSIZE_CONV_TRADITIONAL }, | |
521 | { "304 KB", 304351, 0, wxSIZE_CONV_SI }, | |
b2edb8f3 VZ |
522 | }; |
523 | ||
701aa4d8 FM |
524 | CLocaleSetter loc; // we want to use "C" locale for LC_NUMERIC |
525 | // so that regardless of the system's locale | |
526 | // the decimal point used by GetHumanReadableSize() | |
527 | // is always '.' | |
b2edb8f3 VZ |
528 | for ( unsigned n = 0; n < WXSIZEOF(testData); n++ ) |
529 | { | |
530 | const TestData& td = testData[n]; | |
531 | ||
1fe1aecb FM |
532 | // take care of using the decimal point for the current locale before |
533 | // the actual comparison | |
b2edb8f3 VZ |
534 | CPPUNIT_ASSERT_EQUAL |
535 | ( | |
701aa4d8 | 536 | td.result, |
b2edb8f3 VZ |
537 | wxFileName::GetHumanReadableSize(td.size, "NA", td.prec, td.conv) |
538 | ); | |
539 | } | |
540 | ||
541 | // also test the default convention value | |
701aa4d8 | 542 | CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") ); |
b2edb8f3 VZ |
543 | } |
544 | ||
c08dd08b RN |
545 | void FileNameTestCase::TestStrip() |
546 | { | |
9630954d VZ |
547 | CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") ); |
548 | CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") ); | |
549 | CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") ); | |
550 | CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") ); | |
551 | CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") ); | |
552 | CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") ); | |
ff3d9a35 | 553 | } |
60c0dfe5 VZ |
554 | |
555 | #ifdef __WINDOWS__ | |
556 | ||
557 | void FileNameTestCase::TestShortLongPath() | |
558 | { | |
9630954d | 559 | wxFileName fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"); |
60c0dfe5 VZ |
560 | |
561 | // incredibly enough, GetLongPath() used to return different results during | |
562 | // the first and subsequent runs, test for this | |
563 | CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() ); | |
564 | CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() ); | |
565 | } | |
566 | ||
567 | #endif // __WINDOWS__ | |
7b611a3a VZ |
568 | |
569 | void FileNameTestCase::TestUNC() | |
570 | { | |
571 | wxFileName fn("//share/path/name.ext", wxPATH_DOS); | |
572 | CPPUNIT_ASSERT_EQUAL( "share", fn.GetVolume() ); | |
acaa8337 | 573 | CPPUNIT_ASSERT_EQUAL( "\\path", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); |
7b611a3a VZ |
574 | |
575 | fn.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS); | |
576 | CPPUNIT_ASSERT_EQUAL( "share2", fn.GetVolume() ); | |
acaa8337 | 577 | CPPUNIT_ASSERT_EQUAL( "\\path2", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); |
7b611a3a VZ |
578 | } |
579 | ||
e01a788e VZ |
580 | void FileNameTestCase::TestVolumeUniqueName() |
581 | { | |
582 | wxFileName fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", | |
583 | wxPATH_DOS); | |
584 | CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", | |
585 | fn.GetVolume() ); | |
586 | CPPUNIT_ASSERT_EQUAL( "\\", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); | |
587 | CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", | |
588 | fn.GetFullPath(wxPATH_DOS) ); | |
589 | ||
590 | fn.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\" | |
591 | "Program Files\\setup.exe", wxPATH_DOS); | |
592 | CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", | |
593 | fn.GetVolume() ); | |
594 | CPPUNIT_ASSERT_EQUAL( "\\Program Files", | |
595 | fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); | |
596 | CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\" | |
597 | "Program Files\\setup.exe", | |
598 | fn.GetFullPath(wxPATH_DOS) ); | |
599 | } | |
1fe1aecb FM |
600 | |
601 | void FileNameTestCase::TestCreateTempFileName() | |
602 | { | |
603 | static const struct TestData | |
604 | { | |
605 | const char *prefix; | |
606 | const char *expectedFolder; | |
d6609db5 | 607 | bool shouldSucceed; |
1fe1aecb FM |
608 | } testData[] = |
609 | { | |
d6609db5 FM |
610 | { "", "$SYSTEM_TEMP", true }, |
611 | { "foo", "$SYSTEM_TEMP", true }, | |
612 | { "..", "$SYSTEM_TEMP", true }, | |
613 | { "../bar", "..", true }, | |
bb5a9514 | 614 | #ifdef __WINDOWS__ |
d6609db5 FM |
615 | { "$USER_DOCS_DIR\\", "$USER_DOCS_DIR", true }, |
616 | { "c:\\a\\directory\\which\\does\\not\\exist", "", false }, | |
995202d0 | 617 | #elif defined( __UNIX__ ) |
d6609db5 FM |
618 | { "$USER_DOCS_DIR/", "$USER_DOCS_DIR", true }, |
619 | { "/tmp/foo", "/tmp", true }, | |
620 | { "/tmp/a/directory/which/does/not/exist", "", false }, | |
1fe1aecb FM |
621 | #endif // __UNIX__ |
622 | }; | |
623 | ||
624 | for ( size_t n = 0; n < WXSIZEOF(testData); n++ ) | |
625 | { | |
d6609db5 FM |
626 | wxString prefix = testData[n].prefix; |
627 | prefix.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); | |
1fe1aecb | 628 | |
d6609db5 FM |
629 | std::string errDesc = wxString::Format("failed on prefix '%s'", prefix).ToStdString(); |
630 | ||
631 | wxString path = wxFileName::CreateTempFileName(prefix); | |
632 | CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, !testData[n].shouldSucceed, path.empty() ); | |
633 | ||
634 | if (testData[n].shouldSucceed) | |
1fe1aecb | 635 | { |
d6609db5 FM |
636 | errDesc += "; path is " + path.ToStdString(); |
637 | ||
1fe1aecb FM |
638 | // test the place where the temp file has been created |
639 | wxString expected = testData[n].expectedFolder; | |
640 | expected.Replace("$SYSTEM_TEMP", wxStandardPaths::Get().GetTempDir()); | |
d6609db5 FM |
641 | expected.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); |
642 | CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, expected, wxFileName(path).GetPath() ); | |
1fe1aecb FM |
643 | |
644 | // the temporary file is created with full permissions for the current process | |
645 | // so we should always be able to remove it: | |
d6609db5 | 646 | CPPUNIT_ASSERT_MESSAGE( errDesc, wxRemoveFile(path) ); |
1fe1aecb FM |
647 | } |
648 | } | |
649 | } | |
650 | ||
651 | void FileNameTestCase::TestGetTimes() | |
652 | { | |
653 | wxFileName fn(wxFileName::CreateTempFileName("filenametest")); | |
654 | CPPUNIT_ASSERT( fn.IsOk() ); | |
41fec01f | 655 | wxON_BLOCK_EXIT1( wxRemoveFile, fn.GetFullPath() ); |
1fe1aecb FM |
656 | |
657 | wxDateTime dtAccess, dtMod, dtCreate; | |
658 | CPPUNIT_ASSERT( fn.GetTimes(&dtAccess, &dtMod, &dtCreate) ); | |
659 | ||
660 | // make sure all retrieved dates are equal to the current date&time | |
661 | // with an accuracy up to 1 minute | |
662 | CPPUNIT_ASSERT(dtCreate.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); | |
663 | CPPUNIT_ASSERT(dtMod.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); | |
664 | CPPUNIT_ASSERT(dtAccess.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); | |
665 | } | |
901504c3 VZ |
666 | |
667 | void FileNameTestCase::TestExists() | |
668 | { | |
669 | wxFileName fn(wxFileName::CreateTempFileName("filenametest")); | |
670 | CPPUNIT_ASSERT( fn.IsOk() ); | |
41fec01f | 671 | wxON_BLOCK_EXIT1( wxRemoveFile, fn.GetFullPath() ); |
901504c3 VZ |
672 | |
673 | CPPUNIT_ASSERT( fn.FileExists() ); | |
674 | CPPUNIT_ASSERT( !wxFileName::DirExists(fn.GetFullPath()) ); | |
c50db847 | 675 | |
9ff99cb5 VZ |
676 | // FIXME-VC6: This compiler crashes with |
677 | // | |
678 | // fatal error C1001: INTERNAL COMPILER ERROR | |
679 | // (compiler file 'msc1.cpp', line 1794) | |
680 | // | |
681 | // when compiling calls to Exists() with parameter for some reason, just | |
682 | // disable these tests there. | |
683 | #ifndef __VISUALC6__ | |
c50db847 VZ |
684 | CPPUNIT_ASSERT( fn.Exists(wxFILE_EXISTS_REGULAR) ); |
685 | CPPUNIT_ASSERT( !fn.Exists(wxFILE_EXISTS_DIR) ); | |
9ff99cb5 | 686 | #endif |
996d3fe3 | 687 | CPPUNIT_ASSERT( fn.Exists() ); |
901504c3 | 688 | |
988f7eec VZ |
689 | const wxString& tempdir = wxFileName::GetTempDir(); |
690 | ||
691 | wxFileName fileInTempDir(tempdir, "bloordyblop"); | |
692 | CPPUNIT_ASSERT( !fileInTempDir.Exists() ); | |
693 | CPPUNIT_ASSERT( fileInTempDir.DirExists() ); | |
694 | ||
695 | wxFileName dirTemp(wxFileName::DirName(tempdir)); | |
901504c3 VZ |
696 | CPPUNIT_ASSERT( !dirTemp.FileExists() ); |
697 | CPPUNIT_ASSERT( dirTemp.DirExists() ); | |
c50db847 | 698 | |
9ff99cb5 | 699 | #ifndef __VISUALC6__ |
c50db847 VZ |
700 | CPPUNIT_ASSERT( dirTemp.Exists(wxFILE_EXISTS_DIR) ); |
701 | CPPUNIT_ASSERT( !dirTemp.Exists(wxFILE_EXISTS_REGULAR) ); | |
9ff99cb5 | 702 | #endif |
996d3fe3 | 703 | CPPUNIT_ASSERT( dirTemp.Exists() ); |
901504c3 VZ |
704 | |
705 | #ifdef __UNIX__ | |
706 | CPPUNIT_ASSERT( !wxFileName::FileExists("/dev/null") ); | |
707 | CPPUNIT_ASSERT( !wxFileName::DirExists("/dev/null") ); | |
996d3fe3 | 708 | CPPUNIT_ASSERT( wxFileName::Exists("/dev/null") ); |
c50db847 VZ |
709 | CPPUNIT_ASSERT( wxFileName::Exists("/dev/null", wxFILE_EXISTS_DEVICE) ); |
710 | #ifdef __LINUX__ | |
711 | // These files are only guaranteed to exist under Linux. | |
58263bb4 VZ |
712 | // No need for wxFILE_EXISTS_NO_FOLLOW here; wxFILE_EXISTS_SYMLINK implies it |
713 | CPPUNIT_ASSERT( wxFileName::Exists("/dev/core", wxFILE_EXISTS_SYMLINK) ); | |
c50db847 VZ |
714 | CPPUNIT_ASSERT( wxFileName::Exists("/dev/log", wxFILE_EXISTS_SOCKET) ); |
715 | #endif // __LINUX__ | |
67b09eac | 716 | #ifndef __VMS |
c50db847 | 717 | wxString fifo = dirTemp.GetPath() + "/fifo"; |
67b09eac | 718 | if (mkfifo(fifo.c_str(), 0600) == 0) |
c50db847 VZ |
719 | { |
720 | wxON_BLOCK_EXIT1(wxRemoveFile, fifo); | |
721 | ||
722 | CPPUNIT_ASSERT( wxFileName::Exists(fifo, wxFILE_EXISTS_FIFO) ); | |
723 | } | |
67b09eac | 724 | #endif |
901504c3 VZ |
725 | #endif // __UNIX__ |
726 | } | |
7c9b6c91 VZ |
727 | |
728 | void FileNameTestCase::TestIsSame() | |
729 | { | |
730 | wxFileName fn1( wxFileName::CreateTempFileName( "filenametest1" ) ); | |
731 | CPPUNIT_ASSERT( fn1.IsOk() ); | |
732 | wxON_BLOCK_EXIT1( wxRemoveFile, fn1.GetFullPath() ); | |
733 | ||
734 | wxFileName fn2( wxFileName::CreateTempFileName( "filenametest2" ) ); | |
735 | CPPUNIT_ASSERT( fn2.IsOk() ); | |
736 | wxON_BLOCK_EXIT1( wxRemoveFile, fn2.GetFullPath() ); | |
737 | ||
738 | CPPUNIT_ASSERT( fn1.SameAs( fn1 ) ); | |
739 | CPPUNIT_ASSERT( !fn1.SameAs( fn2 ) ); | |
740 | ||
741 | #if defined(__UNIX__) | |
742 | // We need to create a temporary directory and a temporary link. | |
743 | // Unfortunately we can't use wxFileName::CreateTempFileName() for neither | |
744 | // as it creates plain files, so use tempnam() explicitly instead. | |
745 | char* tn = tempnam(NULL, "wxfn1"); | |
746 | const wxString tempdir1 = wxString::From8BitData(tn); | |
747 | free(tn); | |
748 | ||
749 | CPPUNIT_ASSERT( wxFileName::Mkdir(tempdir1) ); | |
750 | // Unfortunately the casts are needed to select the overload we need here. | |
751 | wxON_BLOCK_EXIT2( static_cast<bool (*)(const wxString&, int)>(wxFileName::Rmdir), | |
752 | tempdir1, static_cast<int>(wxPATH_RMDIR_RECURSIVE) ); | |
753 | ||
754 | tn = tempnam(NULL, "wxfn2"); | |
755 | const wxString tempdir2 = wxString::From8BitData(tn); | |
756 | free(tn); | |
fd78ef47 | 757 | CPPUNIT_ASSERT_EQUAL( 0, symlink(tempdir1.c_str(), tempdir2.c_str()) ); |
7c9b6c91 VZ |
758 | wxON_BLOCK_EXIT1( wxRemoveFile, tempdir2 ); |
759 | ||
760 | ||
761 | wxFileName fn3(tempdir1, "foo"); | |
762 | wxFileName fn4(tempdir2, "foo"); | |
763 | ||
764 | // These files have different paths, hence are different. | |
765 | CPPUNIT_ASSERT( !fn3.SameAs(fn4) ); | |
766 | ||
767 | // Create and close a file to trigger creating it. | |
768 | wxFile(fn3.GetFullPath(), wxFile::write); | |
769 | ||
770 | // Now that both files do exist we should be able to detect that they are | |
771 | // actually the same file. | |
772 | CPPUNIT_ASSERT( fn3.SameAs(fn4) ); | |
773 | #endif // __UNIX__ | |
774 | } | |
c063adeb VZ |
775 | |
776 | #if defined(__UNIX__) | |
777 | ||
778 | // Tests for functions that are changed by ShouldFollowLink() | |
779 | void FileNameTestCase::TestSymlinks() | |
780 | { | |
781 | const wxString tmpdir(wxStandardPaths::Get().GetTempDir()); | |
782 | ||
783 | wxFileName tmpfn(wxFileName::DirName(tmpdir)); | |
784 | ||
785 | wxDateTime dtAccessTmp, dtModTmp, dtCreateTmp; | |
786 | CPPUNIT_ASSERT(tmpfn.GetTimes(&dtAccessTmp, &dtModTmp, &dtCreateTmp)); | |
787 | ||
788 | // Create a temporary directory | |
67b09eac JJ |
789 | #ifdef __VMS |
790 | wxString name = tmpdir + ".filenametestXXXXXX]"; | |
791 | mkdir( name.char_str() , 0222 ); | |
792 | wxString tempdir = name; | |
793 | #else | |
c063adeb VZ |
794 | wxString name = tmpdir + "/filenametestXXXXXX"; |
795 | wxString tempdir = wxString::From8BitData(mkdtemp(name.char_str())); | |
796 | tempdir << wxFileName::GetPathSeparator(); | |
67b09eac | 797 | #endif |
c063adeb VZ |
798 | wxFileName tempdirfn(wxFileName::DirName(tempdir)); |
799 | CPPUNIT_ASSERT(tempdirfn.DirExists()); | |
800 | ||
801 | // Create a regular file in that dir, to act as a symlink target | |
802 | wxFileName targetfn(wxFileName::CreateTempFileName(tempdir)); | |
803 | CPPUNIT_ASSERT(targetfn.FileExists()); | |
804 | ||
25db2c25 | 805 | // Create a symlink to that file |
c063adeb VZ |
806 | wxFileName linktofile(tempdir, "linktofile"); |
807 | CPPUNIT_ASSERT_EQUAL(0, symlink(targetfn.GetFullPath().c_str(), | |
808 | linktofile.GetFullPath().c_str())); | |
809 | ||
25db2c25 | 810 | // ... and another to the temporary directory |
c063adeb VZ |
811 | const wxString linktodirName(tempdir + "/linktodir"); |
812 | wxFileName linktodir(wxFileName::DirName(linktodirName)); | |
813 | CPPUNIT_ASSERT_EQUAL(0, symlink(tmpfn.GetFullPath().c_str(), | |
814 | linktodirName.c_str())); | |
815 | ||
816 | // And symlinks to both of those symlinks | |
817 | wxFileName linktofilelnk(tempdir, "linktofilelnk"); | |
818 | CPPUNIT_ASSERT_EQUAL(0, symlink(linktofile.GetFullPath().c_str(), | |
819 | linktofilelnk.GetFullPath().c_str())); | |
820 | wxFileName linktodirlnk(tempdir, "linktodirlnk"); | |
821 | CPPUNIT_ASSERT_EQUAL(0, symlink(linktodir.GetFullPath().c_str(), | |
822 | linktodirlnk.GetFullPath().c_str())); | |
823 | ||
824 | // Run the tests twice: once in the default symlink following mode and the | |
825 | // second time without following symlinks. | |
826 | bool deref = true; | |
827 | for ( int n = 0; n < 2; ++n, deref = !deref ) | |
828 | { | |
829 | const std::string msg(deref ? " failed for the link target" | |
830 | : " failed for the path itself"); | |
831 | ||
832 | if ( !deref ) | |
833 | { | |
834 | linktofile.DontFollowLink(); | |
835 | linktodir.DontFollowLink(); | |
836 | linktofilelnk.DontFollowLink(); | |
837 | linktodirlnk.DontFollowLink(); | |
838 | } | |
839 | ||
840 | // Test SameAs() | |
841 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
842 | ( | |
843 | "Comparison with file" + msg, | |
844 | deref, linktofile.SameAs(targetfn) | |
845 | ); | |
846 | ||
847 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
848 | ( | |
849 | "Comparison with directory" + msg, | |
850 | deref, linktodir.SameAs(tmpfn) | |
851 | ); | |
852 | ||
853 | // A link-to-a-link should dereference through to the final target | |
854 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
855 | ( | |
856 | "Comparison with link to a file" + msg, | |
857 | deref, | |
858 | linktofilelnk.SameAs(targetfn) | |
859 | ); | |
860 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
861 | ( | |
862 | "Comparison with link to a directory" + msg, | |
863 | deref, | |
864 | linktodirlnk.SameAs(tmpfn) | |
865 | ); | |
866 | ||
867 | // Test GetTimes() | |
868 | wxDateTime dtAccess, dtMod, dtCreate; | |
869 | CPPUNIT_ASSERT_MESSAGE | |
870 | ( | |
871 | "Getting times of a directory" + msg, | |
872 | linktodir.GetTimes(&dtAccess, &dtMod, &dtCreate) | |
873 | ); | |
874 | ||
875 | // IsEqualTo() should be true only when dereferencing. Don't test each | |
876 | // individually: accessing to create the link will have updated some | |
877 | bool equal = dtCreate.IsEqualTo(dtCreateTmp) && | |
878 | dtMod.IsEqualTo(dtModTmp) && | |
879 | dtAccess.IsEqualTo(dtAccessTmp); | |
880 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
881 | ( | |
882 | "Comparing directory times" + msg, | |
883 | deref, | |
884 | equal | |
885 | ); | |
886 | ||
c50db847 | 887 | // Test (File|Dir)Exists() |
c063adeb VZ |
888 | CPPUNIT_ASSERT_EQUAL_MESSAGE |
889 | ( | |
890 | "Testing file existence" + msg, | |
891 | deref, | |
892 | linktofile.FileExists() | |
893 | ); | |
894 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
895 | ( | |
896 | "Testing directory existence" + msg, | |
897 | deref, | |
898 | linktodir.DirExists() | |
899 | ); | |
c50db847 VZ |
900 | |
901 | // Test wxFileName::Exists | |
902 | // The wxFILE_EXISTS_NO_FOLLOW flag should override DontFollowLink() | |
903 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
904 | ( | |
905 | "Testing file existence" + msg, | |
906 | false, | |
907 | linktofile.Exists(wxFILE_EXISTS_REGULAR | wxFILE_EXISTS_NO_FOLLOW) | |
908 | ); | |
909 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
910 | ( | |
911 | "Testing directory existence" + msg, | |
912 | false, | |
913 | linktodir.Exists(wxFILE_EXISTS_DIR | wxFILE_EXISTS_NO_FOLLOW) | |
914 | ); | |
915 | // and the static versions | |
916 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
917 | ( | |
918 | "Testing file existence" + msg, | |
919 | false, | |
920 | wxFileName::Exists(linktofile.GetFullPath(), wxFILE_EXISTS_REGULAR | wxFILE_EXISTS_NO_FOLLOW) | |
921 | ); | |
922 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
923 | ( | |
924 | "Testing file existence" + msg, | |
925 | true, | |
926 | wxFileName::Exists(linktofile.GetFullPath(), wxFILE_EXISTS_REGULAR) | |
927 | ); | |
928 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
929 | ( | |
930 | "Testing directory existence" + msg, | |
931 | false, | |
932 | wxFileName::Exists(linktodir.GetFullPath(), wxFILE_EXISTS_DIR | wxFILE_EXISTS_NO_FOLLOW) | |
933 | ); | |
934 | CPPUNIT_ASSERT_EQUAL_MESSAGE | |
935 | ( | |
936 | "Testing directory existence" + msg, | |
937 | true, | |
938 | wxFileName::Exists(linktodir.GetFullPath(), wxFILE_EXISTS_DIR) | |
939 | ); | |
c063adeb VZ |
940 | } |
941 | ||
942 | // Finally test Exists() after removing the file. | |
943 | CPPUNIT_ASSERT(wxRemoveFile(targetfn.GetFullPath())); | |
58263bb4 VZ |
944 | // This should succeed, as the symlink still exists and |
945 | // the default wxFILE_EXISTS_ANY implies wxFILE_EXISTS_NO_FOLLOW | |
946 | CPPUNIT_ASSERT(wxFileName(tempdir, "linktofile").Exists()); | |
947 | // So should this one, as wxFILE_EXISTS_SYMLINK does too | |
948 | CPPUNIT_ASSERT(wxFileName(tempdir, "linktofile"). | |
949 | Exists(wxFILE_EXISTS_SYMLINK)); | |
950 | // but not this one, as the now broken symlink is followed | |
951 | CPPUNIT_ASSERT(!wxFileName(tempdir, "linktofile"). | |
952 | Exists(wxFILE_EXISTS_REGULAR)); | |
c063adeb VZ |
953 | CPPUNIT_ASSERT(linktofile.Exists()); |
954 | ||
25db2c25 VZ |
955 | // This is also a convenient place to test Rmdir() as we have things to |
956 | // remove. | |
957 | ||
958 | // First, check that removing a symlink to a directory fails. | |
959 | CPPUNIT_ASSERT( !wxFileName::Rmdir(linktodirName) ); | |
960 | ||
961 | // And recursively removing it only removes the symlink itself, not the | |
962 | // directory. | |
963 | CPPUNIT_ASSERT( wxFileName::Rmdir(linktodirName, wxPATH_RMDIR_RECURSIVE) ); | |
964 | CPPUNIT_ASSERT( tmpfn.Exists() ); | |
965 | ||
966 | // Finally removing the directory itself does remove everything. | |
c063adeb | 967 | CPPUNIT_ASSERT(tempdirfn.Rmdir(wxPATH_RMDIR_RECURSIVE)); |
25db2c25 | 968 | CPPUNIT_ASSERT( !tempdirfn.Exists() ); |
c063adeb VZ |
969 | } |
970 | ||
971 | #endif // __UNIX__ |