testu.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. {$mode objfpc}
  2. {$h+}
  3. unit testu;
  4. Interface
  5. uses
  6. dos;
  7. { ---------------------------------------------------------------------
  8. utility functions, shared by several programs of the test suite
  9. ---------------------------------------------------------------------}
  10. type
  11. TVerboseLevel=(V_Abort,V_Error,V_Warning,V_Normal,V_Debug,V_SQL);
  12. TConfig = record
  13. NeedOptions,
  14. DelOptions,
  15. NeedCPU,
  16. SkipCPU,
  17. SkipEmu,
  18. NeedTarget,
  19. SkipTarget,
  20. MinVersion,
  21. MaxVersion,
  22. KnownRunNote,
  23. KnownCompileNote,
  24. RecompileOpt: string;
  25. ResultCode : longint;
  26. KnownRunError : longint;
  27. KnownCompileError : longint;
  28. NeedRecompile : boolean;
  29. NeedLibrary : boolean;
  30. NeededAfter : boolean;
  31. IsInteractive : boolean;
  32. IsKnownRunError,
  33. IsKnownCompileError : boolean;
  34. NoRun : boolean;
  35. UsesGraph : boolean;
  36. ShouldFail : boolean;
  37. Timeout : longint;
  38. Category : string;
  39. Note : string;
  40. Files : string;
  41. ConfigFileSrc : string;
  42. ConfigFileDst : string;
  43. WpoParas : string;
  44. WpoPasses : longint;
  45. DelFiles : string;
  46. end;
  47. Const
  48. DoVerbose : boolean = false;
  49. DoSQL : boolean = false;
  50. MaxLogSize : LongInt = 50000;
  51. procedure TrimB(var s:string);
  52. procedure TrimE(var s:string);
  53. function upper(const s : string) : string;
  54. procedure Verbose(lvl:TVerboseLevel;const s:string);
  55. function GetConfig(const fn:string;var r:TConfig):boolean;
  56. Function GetFileContents (FN : String) : String;
  57. const
  58. { Constants used in IsAbsolute function }
  59. TargetHasDosStyleDirectories : boolean = false;
  60. TargetAmigaLike : boolean = false;
  61. TargetIsMacOS : boolean = false;
  62. TargetIsUnix : boolean = false;
  63. { File path helper functions }
  64. function SplitPath(const s:string):string;
  65. function SplitBasePath(const s:string): string;
  66. Function SplitFileName(const s:string):string;
  67. Function SplitFileBase(const s:string):string;
  68. Function SplitFileExt(const s:string):string;
  69. Function FileExists (Const F : String) : Boolean;
  70. Function PathExists (Const F : String) : Boolean;
  71. Function IsAbsolute (Const F : String) : boolean;
  72. Implementation
  73. function SplitPath(const s:string):string;
  74. var
  75. i : longint;
  76. begin
  77. i:=Length(s);
  78. while (i>0) and not(s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}]) do
  79. dec(i);
  80. SplitPath:=Copy(s,1,i);
  81. end;
  82. function SplitBasePath(const s:string): string;
  83. var
  84. i : longint;
  85. begin
  86. i:=1;
  87. while (i<length(s)) and not(s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}]) do
  88. inc(i);
  89. if s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}] then
  90. dec(i);
  91. SplitBasePath:=Copy(s,1,i);
  92. end;
  93. Function SplitFileName(const s:string):string;
  94. var
  95. p : dirstr;
  96. n : namestr;
  97. e : extstr;
  98. begin
  99. FSplit(s,p,n,e);
  100. SplitFileName:=n+e;
  101. end;
  102. Function SplitFileBase(const s:string):string;
  103. var
  104. p : dirstr;
  105. n : namestr;
  106. e : extstr;
  107. begin
  108. FSplit(s,p,n,e);
  109. SplitFileBase:=n;
  110. end;
  111. Function SplitFileExt(const s:string):string;
  112. var
  113. p : dirstr;
  114. n : namestr;
  115. e : extstr;
  116. begin
  117. FSplit(s,p,n,e);
  118. SplitFileExt:=e;
  119. end;
  120. Function FileExists (Const F : String) : Boolean;
  121. {
  122. Returns True if the file exists, False if not.
  123. }
  124. Var
  125. info : searchrec;
  126. begin
  127. FindFirst (F,anyfile,Info);
  128. FileExists:=DosError=0;
  129. FindClose (Info);
  130. end;
  131. Function PathExists (Const F : String) : Boolean;
  132. {
  133. Returns True if the file exists, False if not.
  134. }
  135. Var
  136. info : searchrec;
  137. begin
  138. FindFirst (F,anyfile,Info);
  139. PathExists:=(DosError=0) and (Info.Attr and Directory=Directory);
  140. FindClose (Info);
  141. end;
  142. { extracted from rtl/macos/macutils.inc }
  143. function IsMacFullPath (const path: string): Boolean;
  144. begin
  145. if Pos(':', path) = 0 then {its partial}
  146. IsMacFullPath := false
  147. else if path[1] = ':' then
  148. IsMacFullPath := false
  149. else
  150. IsMacFullPath := true
  151. end;
  152. Function IsAbsolute (Const F : String) : boolean;
  153. {
  154. Returns True if the name F is a absolute file name
  155. }
  156. begin
  157. IsAbsolute:=false;
  158. if TargetHasDosStyleDirectories then
  159. begin
  160. if (F[1]='/') or (F[1]='\') then
  161. IsAbsolute:=true;
  162. if (Length(F)>2) and (F[2]=':') and ((F[3]='\') or (F[3]='/')) then
  163. IsAbsolute:=true;
  164. end
  165. else if TargetAmigaLike then
  166. begin
  167. if (length(F)>0) and (Pos(':',F) <> 0) then
  168. IsAbsolute:=true;
  169. end
  170. else if TargetIsMacOS then
  171. begin
  172. IsAbsolute:=IsMacFullPath(F);
  173. end
  174. { generic case }
  175. else if (F[1]='/') then
  176. IsAbsolute:=true;
  177. end;
  178. procedure Verbose(lvl:TVerboseLevel;const s:string);
  179. begin
  180. case lvl of
  181. V_Normal :
  182. writeln(s);
  183. V_Debug :
  184. if DoVerbose then
  185. writeln('Debug: ',s);
  186. V_SQL :
  187. if DoSQL then
  188. writeln('SQL: ',s);
  189. V_Warning :
  190. writeln('Warning: ',s);
  191. V_Error :
  192. begin
  193. writeln('Error: ',s);
  194. halt(1);
  195. end;
  196. V_Abort :
  197. begin
  198. writeln('Abort: ',s);
  199. halt(0);
  200. end;
  201. end;
  202. end;
  203. procedure TrimB(var s:string);
  204. begin
  205. while (s<>'') and (s[1] in [' ',#9]) do
  206. delete(s,1,1);
  207. end;
  208. procedure TrimE(var s:string);
  209. begin
  210. while (s<>'') and (s[length(s)] in [' ',#9]) do
  211. delete(s,length(s),1);
  212. end;
  213. function upper(const s : string) : string;
  214. var
  215. i,l : longint;
  216. begin
  217. L:=Length(S);
  218. SetLength(upper,l);
  219. for i:=1 to l do
  220. if s[i] in ['a'..'z'] then
  221. upper[i]:=char(byte(s[i])-32)
  222. else
  223. upper[i]:=s[i];
  224. end;
  225. function GetConfig(const fn:string;var r:TConfig):boolean;
  226. var
  227. t : text;
  228. part,code : integer;
  229. l : longint;
  230. s,res : string;
  231. function GetEntry(const entry:string):boolean;
  232. var
  233. i : longint;
  234. begin
  235. Getentry:=false;
  236. Res:='';
  237. if Upper(Copy(s,1,length(entry)))=Upper(entry) then
  238. begin
  239. Delete(s,1,length(entry));
  240. TrimB(s);
  241. if (s<>'') then
  242. begin
  243. if (s[1]='=') then
  244. begin
  245. delete(s,1,1);
  246. i:=pos('}',s);
  247. if i=0 then
  248. i:=255
  249. else
  250. dec(i);
  251. res:=Copy(s,1,i);
  252. TrimB(res);
  253. TrimE(res);
  254. end;
  255. Verbose(V_Debug,'Config: '+Entry+' = "'+Res+'"');
  256. GetEntry:=true;
  257. end;
  258. end;
  259. end;
  260. begin
  261. FillChar(r,sizeof(r),0);
  262. GetConfig:=false;
  263. Verbose(V_Debug,'Reading '+fn);
  264. assign(t,fn);
  265. {$I-}
  266. reset(t);
  267. {$I+}
  268. if ioresult<>0 then
  269. begin
  270. Verbose(V_Error,'Can''t open '+fn);
  271. exit;
  272. end;
  273. r.Note:='';
  274. while not eof(t) do
  275. begin
  276. readln(t,s);
  277. if Copy(s,1,3)=#$EF#$BB#$BF then
  278. delete(s,1,3);
  279. TrimB(s);
  280. if s<>'' then
  281. begin
  282. if s[1]='{' then
  283. begin
  284. delete(s,1,1);
  285. TrimB(s);
  286. if (s<>'') and (s[1]='%') then
  287. begin
  288. delete(s,1,1);
  289. if GetEntry('OPT') then
  290. r.NeedOptions:=res
  291. else
  292. if GetEntry('DELOPT') then
  293. r.DelOptions:=res
  294. else
  295. if GetEntry('TARGET') then
  296. r.NeedTarget:=res
  297. else
  298. if GetEntry('SKIPTARGET') then
  299. r.SkipTarget:=res
  300. else
  301. if GetEntry('CPU') then
  302. r.NeedCPU:=res
  303. else
  304. if GetEntry('SKIPCPU') then
  305. r.SkipCPU:=res
  306. else
  307. if GetEntry('SKIPEMU') then
  308. r.SkipEmu:=res
  309. else
  310. if GetEntry('VERSION') then
  311. r.MinVersion:=res
  312. else
  313. if GetEntry('MAXVERSION') then
  314. r.MaxVersion:=res
  315. else
  316. if GetEntry('RESULT') then
  317. Val(res,r.ResultCode,code)
  318. else
  319. if GetEntry('GRAPH') then
  320. r.UsesGraph:=true
  321. else
  322. if GetEntry('FAIL') then
  323. r.ShouldFail:=true
  324. else
  325. if GetEntry('RECOMPILE') then
  326. begin
  327. r.NeedRecompile:=true;
  328. r.RecompileOpt:=res;
  329. end
  330. else
  331. if GetEntry('NORUN') then
  332. r.NoRun:=true
  333. else
  334. if GetEntry('NEEDLIBRARY') then
  335. r.NeedLibrary:=true
  336. else
  337. if GetEntry('NEEDEDAFTER') then
  338. r.NeededAfter:=true
  339. else
  340. if GetEntry('KNOWNRUNERROR') then
  341. begin
  342. r.IsKnownRunError:=true;
  343. if res<>'' then
  344. begin
  345. val(res,l,code);
  346. if code>1 then
  347. begin
  348. part:=code;
  349. val(copy(res,1,code-1),l,code);
  350. delete(res,1,part);
  351. end;
  352. if code=0 then
  353. r.KnownRunError:=l;
  354. if res<>'' then
  355. r.KnownRunNote:=res;
  356. end;
  357. end
  358. else
  359. if GetEntry('KNOWNCOMPILEERROR') then
  360. begin
  361. r.IsKnownCompileError:=true;
  362. if res<>'' then
  363. begin
  364. val(res,l,code);
  365. if code>1 then
  366. begin
  367. part:=code;
  368. val(copy(res,1,code-1),l,code);
  369. delete(res,1,part);
  370. end;
  371. if code=0 then
  372. r.KnownCompileError:=l;
  373. if res<>'' then
  374. r.KnownCompileNote:=res;
  375. end;
  376. end
  377. else
  378. if GetEntry('INTERACTIVE') then
  379. r.IsInteractive:=true
  380. else
  381. if GetEntry('NOTE') then
  382. begin
  383. R.Note:='Note: '+res;
  384. Verbose(V_Normal,r.Note);
  385. end
  386. else
  387. if GetEntry('TIMEOUT') then
  388. Val(res,r.Timeout,code)
  389. else
  390. if GetEntry('FILES') then
  391. r.Files:=res
  392. else
  393. if GetEntry('CONFIGFILE') then
  394. begin
  395. l:=Pos(' ',res);
  396. if l>0 then
  397. begin
  398. r.ConfigFileSrc:=Copy(res,1,l-1);
  399. r.ConfigFileDst:=Copy(res,l+1,Length(res)-l+1);
  400. if r.ConfigFileSrc='' then
  401. Verbose(V_Error,'Config file source is empty');
  402. if r.ConfigFileDst='' then
  403. Verbose(V_Error,'Config file destination is empty');
  404. end
  405. else
  406. begin
  407. r.ConfigFileSrc:=res;
  408. r.ConfigFileDst:=res;
  409. end;
  410. end
  411. else
  412. if GetEntry('WPOPARAS') then
  413. r.wpoparas:=res
  414. else
  415. if GetEntry('WPOPASSES') then
  416. val(res,r.wpopasses,code)
  417. else
  418. if GetEntry('DELFILES') then
  419. r.DelFiles:=res
  420. else
  421. Verbose(V_Error,'Unknown entry: '+s);
  422. end;
  423. end
  424. else
  425. break;
  426. end;
  427. end;
  428. close(t);
  429. GetConfig:=true;
  430. end;
  431. Function GetFileContents (FN : String) : String;
  432. Var
  433. F : Text;
  434. S : String;
  435. begin
  436. Result:='';
  437. Assign(F,FN);
  438. {$I-}
  439. Reset(F);
  440. If IOResult<>0 then
  441. Exit;
  442. {$I+}
  443. While Not(EOF(F)) do
  444. begin
  445. ReadLn(F,S);
  446. if length(Result)<MaxLogSize then
  447. Result:=Result+S+LineEnding;
  448. end;
  449. Close(F);
  450. end;
  451. end.