filenametest.cpp 36 KB

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