filenametest.cpp 37 KB

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