tfexpand.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. program Tst_FExp;
  2. (* Test for possible bugs in Dos.FExpand *)
  3. { $DEFINE DEBUG}
  4. (* Defining DEBUG causes all the source and target strings *)
  5. (* to be written to the console to make debugging easier. *)
  6. uses
  7. Dos;
  8. {$IFDEF LINUX}
  9. {$IFNDEF UNIX}
  10. {$DEFINE UNIX}
  11. {$ENDIF}
  12. {$ENDIF}
  13. const
  14. {$IFNDEF FPC}
  15. FileNameCaseSensitive = false;
  16. {$ENDIF}
  17. {$IFDEF UNIX}
  18. DirSep = '/';
  19. CDrive = '';
  20. {$ELSE}
  21. DirSep = '\';
  22. CDrive = 'C:';
  23. {$ENDIF}
  24. HasErrors: boolean = false;
  25. var
  26. TestDir, OrigDir, OrigTstDir, CurDir, CDir, S: DirStr;
  27. TestDrive: string [2];
  28. I: byte;
  29. IOR: longint;
  30. function Translate (S: PathStr): PathStr;
  31. var
  32. I: byte;
  33. begin
  34. {$IFDEF UNIX}
  35. if (Length (S) > 1) and (S [2] = ':') then Delete (S, 1, 2);
  36. {$ELSE}
  37. for I := 1 to Length (S) do if S [I] = '/' then S [I] := DirSep;
  38. if (Length (S) > 0) and (S [1] in ['a'..'z']) then S [1] := UpCase (S [1]);
  39. {$ENDIF}
  40. if not (FileNameCaseSensitive) then
  41. for I := 1 to Length (S) do S [I] := UpCase (S [I]);
  42. Translate := S;
  43. end;
  44. procedure Check (Src, Rslt: PathStr);
  45. var
  46. Rslt2: PathStr;
  47. begin
  48. {$IFDEF DEBUG}
  49. WriteLn (Src, '=>', Rslt);
  50. {$ENDIF}
  51. Rslt := Translate (Rslt);
  52. Rslt2 := FExpand (Src);
  53. if Rslt <> Rslt2 then
  54. begin
  55. WriteLn ('Error: FExpand (', Src, ') should be "', Rslt, '", not "',
  56. Rslt2, '"');
  57. HasErrors := true;
  58. end;
  59. end;
  60. begin
  61. if ParamCount <> 1 then
  62. begin
  63. WriteLn ('Warning: Parameter missing!');
  64. WriteLn ('Full path to a directory with write access' +
  65. {$IFNDEF UNIX}
  66. #13#10'(preferably not on a C: drive)' +
  67. {$ENDIF}
  68. ' expected.');
  69. WriteLn ('Trying to use the current directory instead (not quite ideal).');
  70. GetDir (0, TestDir);
  71. end else TestDir := ParamStr (1);
  72. if TestDir [Length (TestDir)] <> DirSep then TestDir := TestDir + DirSep;
  73. GetDir (0, OrigDir);
  74. {$IFDEF UNIX}
  75. CDir := CurDir;
  76. TestDrive := '';
  77. {$ELSE}
  78. GetDir (3, CDir);
  79. TestDrive := Copy (TestDir, 1, 2);
  80. GetDir ((Ord (TestDir [1]) and not ($20)) - 64, OrigTstDir);
  81. {$ENDIF}
  82. {$I-}
  83. MkDir (TestDir + 'TESTDIR1');
  84. if IOResult <> 0 then ;
  85. MkDir (TestDir + 'TESTDIR1' + DirSep + 'TESTDIR2');
  86. if IOResult <> 0 then ;
  87. {$I+}
  88. ChDir (TestDir + 'TESTDIR1' + DirSep + 'TESTDIR2');
  89. GetDir (0, CurDir);
  90. Check (' ', CurDir + DirSep + ' ');
  91. Check ('', CurDir + DirSep);
  92. Check ('.', CurDir);
  93. Check ('C:', CDir);
  94. Check ('C:.', CDir);
  95. if CDir [Length (CDir)] = DirSep then Check ('c:anything', CDir + 'anything')
  96. else Check ('c:anything', CDir + DirSep + 'anything');
  97. Check ('C:' + DirSep, CDrive + DirSep);
  98. Check ('C:' + DirSep + '.', CDrive + DirSep);
  99. Check ('C:' + DirSep + '..', CDrive + DirSep);
  100. Check ('C:' + DirSep + 'DOS', CDrive + DirSep + 'DOS');
  101. Check ('C:' + DirSep + '..' + DirSep + 'DOS', CDrive + DirSep + 'DOS');
  102. Check ('C:' + DirSep + 'DOS.', CDrive + DirSep + 'DOS.');
  103. Check ('C:' + DirSep + 'DOS' + DirSep, CDrive + DirSep + 'DOS' + DirSep);
  104. Check ('C:' + DirSep + 'DOS' + DirSep + '.', CDrive + DirSep + 'DOS');
  105. Check ('C:' + DirSep + 'DOS' + DirSep + '..', CDrive + DirSep);
  106. Check ('C:' + DirSep + 'DOS' + DirSep + '..' + DirSep, CDrive + DirSep);
  107. Check ('C:' + DirSep + 'DOS' + DirSep + 'TEST' + DirSep + '..', CDrive +
  108. DirSep + 'DOS');
  109. Check ('C:' + DirSep + 'DOS' + DirSep + 'TEST' + DirSep + '..' + DirSep,
  110. CDrive + DirSep + 'DOS' + DirSep);
  111. Check (DirSep, TestDrive + DirSep);
  112. Check (DirSep + '.', TestDrive + DirSep);
  113. Check (DirSep + '..', TestDrive + DirSep);
  114. Check (DirSep + 'DOS', TestDrive + DirSep + 'DOS');
  115. Check ('d', CurDir + DirSep + 'd');
  116. Check (' d', CurDir + DirSep + ' d');
  117. Check ('dd', CurDir + DirSep + 'dd');
  118. Check ('dd' + DirSep + 'dd', CurDir + DirSep + 'dd' + DirSep + 'dd');
  119. Check ('ddd', CurDir + DirSep + 'ddd');
  120. Check ('dddd' + DirSep + 'eeee.ffff', CurDir + DirSep + 'dddd' + DirSep
  121. + 'eeee.ffff');
  122. Check ('.special', CurDir + DirSep + '.special');
  123. Check ('..special', CurDir + DirSep + '..special');
  124. Check ('special..', CurDir + DirSep + 'special..');
  125. Check ('special.' + DirSep, CurDir + DirSep + 'special.' + DirSep);
  126. Check (DirSep + '.special', TestDrive + DirSep + '.special');
  127. Check ('..', TestDir + 'TESTDIR1');
  128. Check ('.' + DirSep + '..', TestDir + 'TESTDIR1');
  129. Check ('..' + DirSep + '.', TestDir + 'TESTDIR1');
  130. Check ('...', CurDir + DirSep + '...');
  131. {$IFDEF UNIX}
  132. S := GetEnv ('HOME');
  133. Check ('~', S);
  134. if (Length (S) > 0) and (S [Length (S)] <> DirSep) then S := S + DirSep;
  135. Check ('~NobodyWithThisNameShouldEverExist.test/nothing', '~NobodyWithThisNameShouldEverExist.test/nothing');
  136. Check ('~' + DirSep, S);
  137. Check ('~' + DirSep + '.', S + '.');
  138. Check ('~' + DirSep + 'directory' + DirSep + 'another',
  139. S + 'directory' + DirSep + 'another');
  140. {$ELSE UNIX}
  141. Check (TestDrive + '..', TestDir + 'TESTDIR1');
  142. Check (TestDrive + '..' + DirSep, TestDir + 'TESTDIR1' + DirSep);
  143. Check (TestDrive + '.' + DirSep + '.', CurDir);
  144. Check (TestDrive + '.' + DirSep + '..', TestDir + 'TESTDIR1');
  145. {$I-}
  146. I := 1;
  147. repeat
  148. S := '';
  149. GetDir (I, S);
  150. IOR := IOResult;
  151. if IOR = 0 then Inc (I);
  152. until (I > 26) or (IOR <> 0);
  153. if I <= 26 then
  154. begin
  155. S := Chr (I + 64) + ':ddd';
  156. Check (S, Chr (I + 64) + ':' + DirSep + 'ddd');
  157. end else
  158. WriteLn ('Sorry, cannot test FExpand behaviour for incorrect drives here.');
  159. {$I+}
  160. {$IFDEF FPC}
  161. Check ('d\d/d', CurDir + DirSep + 'd' + DirSep + 'd' + DirSep + 'd');
  162. Check ('\\server\share\directory', '\\server\share\directory');
  163. Check ('\\server\share\directory1\directory2\..',
  164. '\\server\share\directory1');
  165. Check ('\\.', '\\');
  166. Check ('\\.\', '\\');
  167. Check ('\\.\.', '\\');
  168. Check ('\\.\TEST', '\\TEST');
  169. Check ('\\..\', '\\');
  170. Check ('\\..\TEST', '\\TEST');
  171. {$ENDIF FPC}
  172. ChDir (OrigTstDir);
  173. {$ENDIF UNIX}
  174. ChDir (OrigDir);
  175. RmDir (TestDir + 'TESTDIR1' + DirSep + 'TESTDIR2');
  176. RmDir (TestDir + 'TESTDIR1');
  177. if HasErrors then
  178. begin
  179. WriteLn ('FExpand doesn''t work correctly.');
  180. Halt (1);
  181. end;
  182. end.