]> git.saurik.com Git - wxWidgets.git/blob - tests/filename/filenametest.cpp
interpret ~ specially only when it is the first character of the path (closes #10948...
[wxWidgets.git] / tests / filename / filenametest.cpp
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
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/utils.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/filename.h"
25 #include "wx/filefn.h"
26
27 #ifdef __WXMSW__
28 #include "wx/msw/registry.h"
29 #endif // __WXMSW__
30
31 // ----------------------------------------------------------------------------
32 // local functions
33 // ----------------------------------------------------------------------------
34
35 // define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL()
36 inline std::ostream& operator<<(std::ostream& o, const wxFileName& fn)
37 {
38 return o << fn.GetFullPath();
39 }
40
41 // ----------------------------------------------------------------------------
42 // test data
43 // ----------------------------------------------------------------------------
44
45 static struct TestFileNameInfo
46 {
47 const char *fullname;
48 const char *volume;
49 const char *path;
50 const char *name;
51 const char *ext;
52 bool isAbsolute;
53 wxPathFormat format;
54 } filenames[] =
55 {
56 // the empty string
57 { "", "", "", "", "", false, wxPATH_UNIX },
58 { "", "", "", "", "", false, wxPATH_DOS },
59 { "", "", "", "", "", false, wxPATH_VMS },
60
61 // Unix file names
62 { "/usr/bin/ls", "", "/usr/bin", "ls", "", true, wxPATH_UNIX },
63 { "/usr/bin/", "", "/usr/bin", "", "", true, wxPATH_UNIX },
64 { "~/.zshrc", "", "~", ".zshrc", "", true, wxPATH_UNIX },
65 { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX },
66 { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX },
67 { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX },
68 { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX },
69 { "~user/", "", "~user", "", "", true, wxPATH_UNIX },
70 { "/foo", "", "/", "foo", "", true, wxPATH_UNIX },
71 { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX },
72 { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX },
73
74 // Windows file names
75 { "foo.bar", "", "", "foo", "bar", false, wxPATH_DOS },
76 { "\\foo.bar", "", "\\", "foo", "bar", false, wxPATH_DOS },
77 { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS },
78 { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS },
79 { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS },
80
81 #if 0
82 // NB: when using the wxFileName::GetLongPath() function on these two
83 // strings, the program will hang for several seconds blocking inside
84 // Win32 GetLongPathName() function
85 { "\\\\server\\foo.bar", "server", "\\", "foo", "bar", true, wxPATH_DOS },
86 { "\\\\server\\dir\\foo.bar", "server", "\\dir", "foo", "bar", true, wxPATH_DOS },
87 #endif
88
89 // consecutive [back]slashes should be treated as single occurrences of
90 // them and not interpreted as share names if there is a volume name
91 { "c:\\aaa\\bbb\\ccc", "c", "\\aaa\\bbb", "ccc", "", true, wxPATH_DOS },
92 { "c:\\\\aaa\\bbb\\ccc", "c", "\\\\aaa\\bbb", "ccc", "", true, wxPATH_DOS },
93
94 // wxFileName support for Mac file names is broken currently
95 #if 0
96 // Mac file names
97 { "Volume:Dir:File", "Volume", "Dir", "File", "", true, wxPATH_MAC },
98 { "Volume:Dir:Subdir:File", "Volume", "Dir:Subdir", "File", "", true, wxPATH_MAC },
99 { "Volume:", "Volume", "", "", "", true, wxPATH_MAC },
100 { ":Dir:File", "", "Dir", "File", "", false, wxPATH_MAC },
101 { ":File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC },
102 { "File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC },
103 #endif // 0
104
105 #if 0
106 // VMS file names
107 // NB: on Windows they have the same effect of the \\server\\ strings
108 // (see the note above)
109 { "device:[dir1.dir2.dir3]file.txt", "device", "dir1.dir2.dir3", "file", "txt", true, wxPATH_VMS },
110 #endif
111 { "file.txt", "", "", "file", "txt", false, wxPATH_VMS },
112 };
113
114 // ----------------------------------------------------------------------------
115 // test class
116 // ----------------------------------------------------------------------------
117
118 class FileNameTestCase : public CppUnit::TestCase
119 {
120 public:
121 FileNameTestCase() { }
122
123 private:
124 CPPUNIT_TEST_SUITE( FileNameTestCase );
125 CPPUNIT_TEST( TestConstruction );
126 CPPUNIT_TEST( TestComparison );
127 CPPUNIT_TEST( TestSplit );
128 CPPUNIT_TEST( TestSetPath );
129 CPPUNIT_TEST( TestStrip );
130 CPPUNIT_TEST( TestNormalize );
131 CPPUNIT_TEST( TestReplace );
132 #ifdef __WINDOWS__
133 CPPUNIT_TEST( TestShortLongPath );
134 #endif // __WINDOWS__
135 CPPUNIT_TEST_SUITE_END();
136
137 void TestConstruction();
138 void TestComparison();
139 void TestSplit();
140 void TestSetPath();
141 void TestStrip();
142 void TestNormalize();
143 void TestReplace();
144 #ifdef __WINDOWS__
145 void TestShortLongPath();
146 #endif // __WINDOWS__
147
148 DECLARE_NO_COPY_CLASS(FileNameTestCase)
149 };
150
151 // register in the unnamed registry so that these tests are run by default
152 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
153
154 // also include in it's own registry so that these tests can be run alone
155 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
156
157 void FileNameTestCase::TestConstruction()
158 {
159 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
160 {
161 const TestFileNameInfo& fni = filenames[n];
162
163 wxFileName fn(fni.fullname, fni.format);
164
165 // the original full name could contain consecutive [back]slashes,
166 // squeeze them except for the double backslash in the beginning in
167 // Windows filenames where it has special meaning
168 wxString fullnameOrig;
169 if ( fni.format == wxPATH_DOS )
170 {
171 // copy the backslashes at beginning unchanged
172 const char *p = fni.fullname;
173 while ( *p == '\\' )
174 fullnameOrig += *p++;
175
176 // replace consecutive slashes with single ones in the rest
177 for ( char chPrev = '\0'; *p; p++ )
178 {
179 if ( *p == '\\' && chPrev == '\\' )
180 continue;
181
182 chPrev = *p;
183 fullnameOrig += chPrev;
184 }
185 }
186 else // !wxPATH_DOS
187 {
188 fullnameOrig = fni.fullname;
189 }
190
191 fullnameOrig.Replace("//", "/");
192
193
194 wxString fullname = fn.GetFullPath(fni.format);
195 CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname );
196
197 // notice that we use a dummy working directory to ensure that paths
198 // with "../.." in them could be normalized, otherwise this would fail
199 // if the test is run from root directory or its direct subdirectory
200 CPPUNIT_ASSERT_MESSAGE
201 (
202 (const char *)wxString::Format("Normalize(%s) failed", fni.fullname).mb_str(),
203 fn.Normalize(wxPATH_NORM_ALL, "/foo/bar/baz", fni.format)
204 );
205
206 if ( *fni.volume && *fni.path )
207 {
208 // check that specifying the volume separately or as part of the
209 // path doesn't make any difference
210 wxString pathWithVolume = fni.volume;
211 pathWithVolume += wxFileName::GetVolumeSeparator(fni.format);
212 pathWithVolume += fni.path;
213
214 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume,
215 fni.name,
216 fni.ext,
217 fni.format), fn );
218 }
219 }
220
221 wxFileName fn;
222
223 // empty strings
224 fn.AssignDir(wxEmptyString);
225 CPPUNIT_ASSERT( !fn.IsOk() );
226
227 fn.Assign(wxEmptyString);
228 CPPUNIT_ASSERT( !fn.IsOk() );
229
230 fn.Assign(wxEmptyString, wxEmptyString);
231 CPPUNIT_ASSERT( !fn.IsOk() );
232
233 fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString);
234 CPPUNIT_ASSERT( !fn.IsOk() );
235
236 fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString);
237 CPPUNIT_ASSERT( !fn.IsOk() );
238 }
239
240 void FileNameTestCase::TestComparison()
241 {
242 wxFileName fn1(wxT("/tmp/file1"));
243 wxFileName fn2(wxT("/tmp/dir2/../file2"));
244 fn1.Normalize();
245 fn2.Normalize();
246 CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath());
247 }
248
249 void FileNameTestCase::TestSplit()
250 {
251 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
252 {
253 const TestFileNameInfo& fni = filenames[n];
254 wxString volume, path, name, ext;
255 wxFileName::SplitPath(fni.fullname,
256 &volume, &path, &name, &ext, fni.format);
257
258 CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume );
259 CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path );
260 CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name );
261 CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext );
262 }
263
264 // special case of empty extension
265 wxFileName fn("foo.");
266 CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn.GetFullPath() );
267 }
268
269 void FileNameTestCase::TestSetPath()
270 {
271 wxFileName fn("d:\\test\\foo.bar", wxPATH_DOS);
272 fn.SetPath("c:\\temp", wxPATH_DOS);
273 CPPUNIT_ASSERT( fn.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS)) );
274
275 fn = wxFileName("/usr/bin/ls", wxPATH_UNIX);
276 fn.SetPath("/usr/local/bin", wxPATH_UNIX);
277 CPPUNIT_ASSERT( fn.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX)) );
278 }
279
280 void FileNameTestCase::TestNormalize()
281 {
282 // prepare some data to be used later
283 wxString sep = wxFileName::GetPathSeparator();
284 wxString cwd = wxGetCwd();
285 wxString home = wxGetUserHome();
286
287 cwd.Replace(sep, wxT("/"));
288 if (cwd.Last() != wxT('/'))
289 cwd += wxT('/');
290 home.Replace(sep, wxT("/"));
291 if (home.Last() != wxT('/'))
292 home += wxT('/');
293
294 // since we will always be testing paths using the wxPATH_UNIX
295 // format, we need to remove the volume, if present
296 if (home.Contains(wxT(':')))
297 home = home.AfterFirst(wxT(':'));
298 if (cwd.Contains(wxT(':')))
299 cwd = cwd.AfterFirst(wxT(':'));
300
301 static const struct FileNameTest
302 {
303 const char *original;
304 int flags;
305 const char *expected;
306 wxPathFormat fmt;
307 } tests[] =
308 {
309 // test wxPATH_NORM_ENV_VARS
310 #ifdef __WXMSW__
311 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX },
312 #else
313 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX },
314 #endif
315
316 // test wxPATH_NORM_DOTS
317 { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
318
319 // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially
320 // when it is the first character in the file name
321 { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX },
322 { "/~/a/b", wxPATH_NORM_TILDE, "/~/a/b", wxPATH_UNIX },
323 { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX },
324
325 // test wxPATH_NORM_CASE
326 { "Foo", wxPATH_NORM_CASE, "Foo", wxPATH_UNIX },
327 { "Foo", wxPATH_NORM_CASE, "foo", wxPATH_DOS },
328 { "C:\\Program Files\\wx", wxPATH_NORM_CASE,
329 "c:\\program files\\wx", wxPATH_DOS },
330 { "C:/Program Files/wx", wxPATH_NORM_ALL | wxPATH_NORM_CASE,
331 "c:\\program files\\wx", wxPATH_DOS },
332 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL | wxPATH_NORM_CASE,
333 "c:\\users\\zeitlin", wxPATH_DOS },
334
335 // test wxPATH_NORM_ABSOLUTE
336 { "a/b/", wxPATH_NORM_ABSOLUTE, "CWD/a/b/", wxPATH_UNIX },
337 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE, "CWD/a/b/c.ext", wxPATH_UNIX },
338 { "/a", wxPATH_NORM_ABSOLUTE, "/a", wxPATH_UNIX },
339
340 // test giving no flags at all to Normalize()
341 { "a/b/", 0, "a/b/", wxPATH_UNIX },
342 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX },
343 { "/a", 0, "/a", wxPATH_UNIX },
344
345 // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE
346 // for both existing and non-existent files (this is important under
347 // MSW where GetLongPathName() works only for the former)
348 { "./foo", wxPATH_NORM_LONG, "./foo", wxPATH_UNIX },
349 { "../foo", wxPATH_NORM_LONG, "../foo", wxPATH_UNIX },
350 { ".\\test.bkl", wxPATH_NORM_LONG, ".\\test.bkl", wxPATH_DOS },
351 { ".\\foo", wxPATH_NORM_LONG, ".\\foo", wxPATH_DOS },
352 { "..\\Makefile.in", wxPATH_NORM_LONG, "..\\Makefile.in", wxPATH_DOS },
353 { "..\\foo", wxPATH_NORM_LONG, "..\\foo", wxPATH_DOS },
354 };
355
356 // set the env var ABCDEF
357 wxSetEnv("ABCDEF", "abcdef");
358
359 for ( size_t i = 0; i < WXSIZEOF(tests); i++ )
360 {
361 const FileNameTest& fnt = tests[i];
362 wxFileName fn(fnt.original, fnt.fmt);
363
364 // be sure this normalization does not fail
365 WX_ASSERT_MESSAGE
366 (
367 ("#%d: Normalize(%s) failed", (int)i, fnt.original),
368 fn.Normalize(fnt.flags, cwd, fnt.fmt)
369 );
370
371 // compare result with expected string
372 wxString expected(tests[i].expected);
373 expected.Replace("HOME/", home);
374 expected.Replace("CWD/", cwd);
375 WX_ASSERT_EQUAL_MESSAGE
376 (
377 ("array element #%d", (int)i),
378 expected, fn.GetFullPath(fnt.fmt)
379 );
380 }
381
382 // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short
383 // names generation is not disabled for this system as otherwise the file
384 // MKINST~1 doesn't exist at all and normalizing it fails (it's possible
385 // that we're on a FAT partition in which case the test would still succeed
386 // and also that the registry key was changed recently and didn't take
387 // effect yet but these are marginal cases which we consciously choose to
388 // ignore for now)
389 #ifdef __WXMSW__
390 long shortNamesDisabled;
391 if ( wxRegKey
392 (
393 wxRegKey::HKLM,
394 "SYSTEM\\CurrentControlSet\\Control\\FileSystem"
395 ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled) &&
396 !shortNamesDisabled )
397 {
398 wxFileName fn("..\\MKINST~1");
399 CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_LONG, cwd) );
400 CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn.GetFullPath() );
401 }
402 //else: when in doubt, don't run the test
403 #endif // __WXMSW__
404 }
405
406 void FileNameTestCase::TestReplace()
407 {
408 static const struct FileNameTest
409 {
410 const char *original;
411 const char *env_contents;
412 const char *replace_fmtstring;
413 const char *expected;
414 wxPathFormat fmt;
415 } tests[] =
416 {
417 { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX },
418 { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX },
419 { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX },
420 { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX },
421 { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX },
422 { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX },
423 { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX },
424 // note: empty directory components are automatically removed by wxFileName thus
425 // using // in the replace format string has no effect
426
427 { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX },
428 { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX },
429 { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX },
430 { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX },
431
432 { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN },
433 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN },
434 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN }
435 };
436
437 for ( size_t i = 0; i < WXSIZEOF(tests); i++ )
438 {
439 const FileNameTest& fnt = tests[i];
440 wxFileName fn(fnt.original, fnt.fmt);
441
442 // set the environment variable
443 wxSetEnv("TEST_VAR", fnt.env_contents);
444
445 // be sure this ReplaceEnvVariable does not fail
446 WX_ASSERT_MESSAGE
447 (
448 ("#%d: ReplaceEnvVariable(%s) failed", (int)i, fnt.replace_fmtstring),
449 fn.ReplaceEnvVariable("TEST_VAR", fnt.replace_fmtstring, fnt.fmt)
450 );
451
452 // compare result with expected string
453 wxString expected(fnt.expected);
454 WX_ASSERT_EQUAL_MESSAGE
455 (
456 ("array element #%d", (int)i),
457 expected, fn.GetFullPath(fnt.fmt)
458 );
459 }
460
461 // now test ReplaceHomeDir
462
463 wxFileName fn = wxFileName::DirName(wxGetHomeDir());
464 fn.AppendDir("test1");
465 fn.AppendDir("test2");
466 fn.AppendDir("test3");
467 fn.SetName("some file");
468
469 WX_ASSERT_MESSAGE
470 (
471 ("ReplaceHomeDir(%s) failed", fn.GetFullPath()),
472 fn.ReplaceHomeDir()
473 );
474
475 CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"),
476 fn.GetFullPath(wxPATH_UNIX) );
477 }
478
479 void FileNameTestCase::TestStrip()
480 {
481 CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") );
482 CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") );
483 CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") );
484 CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") );
485 CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") );
486 CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") );
487 }
488
489 #ifdef __WINDOWS__
490
491 void FileNameTestCase::TestShortLongPath()
492 {
493 wxFileName fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
494
495 // incredibly enough, GetLongPath() used to return different results during
496 // the first and subsequent runs, test for this
497 CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() );
498 CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() );
499 }
500
501 #endif // __WINDOWS__