dotest.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. {
  2. $Id$
  3. }
  4. program dotest;
  5. uses
  6. dos,
  7. redir;
  8. const
  9. {$ifdef UNIX}
  10. ExeExt='';
  11. {$else UNIX}
  12. ExeExt='exe';
  13. {$endif UNIX}
  14. type
  15. TVerboseLevel=(V_Abort,V_Error,V_Warning,V_Normal,V_Debug);
  16. TConfig = record
  17. NeedOptions,
  18. NeedCPU,
  19. NeedVersion : string;
  20. ResultCode : longint;
  21. NeedRecompile : boolean;
  22. IsInteractive : boolean;
  23. IsKnown : boolean;
  24. NoRun : boolean;
  25. UsesGraph : boolean;
  26. ShouldFail : boolean;
  27. Category : string;
  28. end;
  29. var
  30. Config : TConfig;
  31. CompilerBin : string;
  32. CompilerCPU : string;
  33. CompilerVersion : string;
  34. PPFile : string;
  35. TestName : string;
  36. Note : string;
  37. const
  38. ResLogfile : string[32] = 'log';
  39. LongLogfile : string[32] = 'longlog';
  40. FailLogfile : string[32] = 'faillist';
  41. DoVerbose : boolean = false;
  42. DoGraph : boolean = false;
  43. DoInteractive : boolean = false;
  44. DoExecute : boolean = false;
  45. DoKnown : boolean = false;
  46. procedure Verbose(lvl:TVerboseLevel;const s:string);
  47. begin
  48. case lvl of
  49. V_Normal :
  50. writeln(s);
  51. V_Debug :
  52. if DoVerbose then
  53. writeln('Debug: ',s);
  54. V_Warning :
  55. writeln('Warning: ',s);
  56. V_Error :
  57. begin
  58. writeln('Error: ',s);
  59. halt(1);
  60. end;
  61. V_Abort :
  62. begin
  63. writeln('Abort: ',s);
  64. halt(0);
  65. end;
  66. end;
  67. end;
  68. Function FileExists (Const F : String) : Boolean;
  69. {
  70. Returns True if the file exists, False if not.
  71. }
  72. Var
  73. info : searchrec;
  74. begin
  75. FindFirst (F,anyfile,Info);
  76. FileExists:=DosError=0;
  77. FindClose (Info);
  78. end;
  79. function ToStr(l:longint):string;
  80. var
  81. s : string;
  82. begin
  83. Str(l,s);
  84. ToStr:=s;
  85. end;
  86. procedure TrimB(var s:string);
  87. begin
  88. while (s<>'') and (s[1] in [' ',#9]) do
  89. delete(s,1,1);
  90. end;
  91. procedure TrimE(var s:string);
  92. begin
  93. while (s<>'') and (s[length(s)] in [' ',#9]) do
  94. delete(s,length(s),1);
  95. end;
  96. function upper(const s : string) : string;
  97. var
  98. i : longint;
  99. begin
  100. for i:=1 to length(s) do
  101. if s[i] in ['a'..'z'] then
  102. upper[i]:=char(byte(s[i])-32)
  103. else
  104. upper[i]:=s[i];
  105. upper[0]:=s[0];
  106. end;
  107. function SplitPath(const s:string):string;
  108. var
  109. i : longint;
  110. begin
  111. i:=Length(s);
  112. while (i>0) and not(s[i] in ['/','\']) do
  113. dec(i);
  114. SplitPath:=Copy(s,1,i);
  115. end;
  116. function ForceExtension(Const HStr,ext:String):String;
  117. {
  118. Return a filename which certainly has the extension ext
  119. }
  120. var
  121. j : longint;
  122. begin
  123. j:=length(Hstr);
  124. while (j>0) and (Hstr[j]<>'.') do
  125. dec(j);
  126. if j=0 then
  127. j:=255;
  128. if Ext<>'' then
  129. ForceExtension:=Copy(Hstr,1,j-1)+'.'+Ext
  130. else
  131. ForceExtension:=Copy(Hstr,1,j-1);
  132. end;
  133. procedure Copyfile(const fn1,fn2:string;append:boolean);
  134. const
  135. bufsize = 16384;
  136. var
  137. f,g : file;
  138. i : longint;
  139. buf : pointer;
  140. begin
  141. if Append then
  142. Verbose(V_Debug,'Appending '+fn1+' to '+fn2)
  143. else
  144. Verbose(V_Debug,'Copying '+fn1+' to '+fn2);
  145. assign(f,fn1);
  146. assign(g,fn2);
  147. {$I-}
  148. reset(f,1);
  149. {$I+}
  150. if ioresult<>0 then
  151. Verbose(V_Error,'Can''t open '+fn1);
  152. if append then
  153. begin
  154. {$I-}
  155. reset(g,1);
  156. {$I+}
  157. if ioresult<>0 then
  158. append:=false
  159. else
  160. seek(g,filesize(g));
  161. end;
  162. if not append then
  163. begin
  164. {$I-}
  165. rewrite(g,1);
  166. {$I+}
  167. if ioresult<>0 then
  168. Verbose(V_Error,'Can''t open '+fn2+' for output');
  169. end;
  170. getmem(buf,bufsize);
  171. repeat
  172. blockread(f,buf^,bufsize,i);
  173. blockwrite(g,buf^,i);
  174. until i<bufsize;
  175. freemem(buf,bufsize);
  176. close(f);
  177. close(g);
  178. end;
  179. procedure AddLog(const logfile,s:string);
  180. var
  181. t : text;
  182. begin
  183. assign(t,logfile);
  184. {$I-}
  185. append(t);
  186. {$I+}
  187. if ioresult<>0 then
  188. begin
  189. {$I-}
  190. rewrite(t);
  191. {$I+}
  192. if ioresult<>0 then
  193. Verbose(V_Abort,'Can''t append to '+logfile);
  194. end;
  195. writeln(t,s);
  196. close(t);
  197. end;
  198. function GetConfig(const fn:string;var r:TConfig):boolean;
  199. var
  200. t : text;
  201. code : integer;
  202. s,res : string;
  203. function GetEntry(const entry:string):boolean;
  204. var
  205. i : longint;
  206. begin
  207. Getentry:=false;
  208. Res:='';
  209. if Upper(Copy(s,1,length(entry)))=Upper(entry) then
  210. begin
  211. Delete(s,1,length(entry));
  212. TrimB(s);
  213. if (s<>'') then
  214. begin
  215. if (s[1]='=') then
  216. begin
  217. delete(s,1,1);
  218. i:=pos('}',s);
  219. if i=0 then
  220. i:=255
  221. else
  222. dec(i);
  223. res:=Copy(s,1,i);
  224. TrimB(res);
  225. TrimE(res);
  226. end;
  227. Verbose(V_Debug,'Config: '+Entry+' = "'+Res+'"');
  228. GetEntry:=true;
  229. end;
  230. end;
  231. end;
  232. begin
  233. FillChar(r,sizeof(r),0);
  234. GetConfig:=false;
  235. Verbose(V_Debug,'Reading '+fn);
  236. assign(t,fn);
  237. {$I-}
  238. reset(t);
  239. {$I+}
  240. if ioresult<>0 then
  241. begin
  242. Verbose(V_Error,'Can''t open '+fn);
  243. exit;
  244. end;
  245. Note:='';
  246. while not eof(t) do
  247. begin
  248. readln(t,s);
  249. if s<>'' then
  250. begin
  251. if s[1]='{' then
  252. begin
  253. delete(s,1,1);
  254. TrimB(s);
  255. if (s<>'') and (s[1]='%') then
  256. begin
  257. delete(s,1,1);
  258. if GetEntry('OPT') then
  259. r.NeedOptions:=res
  260. else
  261. if GetEntry('CPU') then
  262. r.NeedCPU:=res
  263. else
  264. if GetEntry('VERSION') then
  265. r.NeedVersion:=res
  266. else
  267. if GetEntry('RESULT') then
  268. Val(res,r.ResultCode,code)
  269. else
  270. if GetEntry('GRAPH') then
  271. r.UsesGraph:=true
  272. else
  273. if GetEntry('FAIL') then
  274. r.ShouldFail:=true
  275. else
  276. if GetEntry('RECOMPILE') then
  277. r.NeedRecompile:=true
  278. else
  279. if GetEntry('NORUN') then
  280. r.NoRun:=true
  281. else
  282. if GetEntry('KNOWN') then
  283. r.IsKnown:=true
  284. else
  285. if GetEntry('INTERACTIVE') then
  286. r.IsInteractive:=true
  287. else
  288. if GetEntry('NOTE') then
  289. begin
  290. Note:='Note: '+res;
  291. Verbose(V_Normal,Note);
  292. end
  293. else
  294. Verbose(V_Error,'Unknown entry: '+s);
  295. end;
  296. end
  297. else
  298. break;
  299. end;
  300. end;
  301. close(t);
  302. GetConfig:=true;
  303. end;
  304. function GetCompilerVersion:boolean;
  305. var
  306. t : text;
  307. begin
  308. GetCompilerVersion:=false;
  309. ExecuteRedir(CompilerBin,'-iV','','out','');
  310. assign(t,'out');
  311. {$I-}
  312. reset(t);
  313. readln(t,CompilerVersion);
  314. close(t);
  315. erase(t);
  316. {$I+}
  317. if ioresult<>0 then
  318. Verbose(V_Error,'Can''t get Compiler Version')
  319. else
  320. begin
  321. Verbose(V_Debug,'Current Compiler Version: '+CompilerVersion);
  322. GetCompilerVersion:=true;
  323. end;
  324. end;
  325. function GetCompilerCPU:boolean;
  326. var
  327. t : text;
  328. begin
  329. GetCompilerCPU:=false;
  330. ExecuteRedir(CompilerBin,'-iTP','','out','');
  331. assign(t,'out');
  332. {$I-}
  333. reset(t);
  334. readln(t,CompilerCPU);
  335. close(t);
  336. erase(t);
  337. {$I+}
  338. if ioresult<>0 then
  339. Verbose(V_Error,'Can''t get Compiler CPU Target')
  340. else
  341. begin
  342. Verbose(V_Debug,'Current Compiler CPU Target: '+CompilerCPU);
  343. GetCompilerCPU:=true;
  344. end;
  345. end;
  346. function RunCompiler:boolean;
  347. var
  348. outname,
  349. args : string;
  350. begin
  351. RunCompiler:=false;
  352. OutName:=ForceExtension(PPFile,'log');
  353. args:='-Fuunits';
  354. if Config.NeedOptions<>'' then
  355. args:=args+' '+Config.NeedOptions;
  356. args:=args+' '+ppfile;
  357. Verbose(V_Debug,'Executing '+compilerbin+' '+args);
  358. { also get the output from as and ld that writes to stderr sometimes }
  359. ExecuteRedir(CompilerBin,args,'',OutName,OutName);
  360. Verbose(V_Debug,'Exitcode '+ToStr(ExecuteResult));
  361. { Shoud the compile fail ? }
  362. if Config.ShouldFail then
  363. begin
  364. if ExecuteResult<>0 then
  365. begin
  366. AddLog(ResLogFile,'Success, compilation failed '+PPFile);
  367. RunCompiler:=true;
  368. end
  369. else
  370. begin
  371. AddLog(FailLogFile,TestName);
  372. if Note<>'' then
  373. AddLog(FailLogFile,Note);
  374. AddLog(ResLogFile,'Failed, compilation successfull '+PPFile);
  375. AddLog(LongLogFile,'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  376. AddLog(LongLogFile,'Failed, compilation successfull '+PPFile);
  377. if Note<>'' then
  378. AddLog(LongLogFile,Note);
  379. CopyFile(OutName,LongLogFile,true);
  380. end;
  381. end
  382. else
  383. begin
  384. if ExecuteResult<>0 then
  385. begin
  386. AddLog(FailLogFile,TestName);
  387. if Note<>'' then
  388. AddLog(FailLogFile,Note);
  389. AddLog(ResLogFile,'Failed to compile '+PPFile);
  390. AddLog(LongLogFile,'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  391. AddLog(LongLogFile,'Failed to compile '+PPFile);
  392. if Note<>'' then
  393. AddLog(LongLogFile,Note);
  394. CopyFile(OutName,LongLogFile,true);
  395. Verbose(V_Abort,'Exitcode: '+ToStr(ExecuteResult)+' (expected 0)');
  396. end
  397. else
  398. begin
  399. AddLog(ResLogFile,'Successfully compiled '+PPFile);
  400. RunCompiler:=true;
  401. end;
  402. end;
  403. end;
  404. function RunExecutable:boolean;
  405. var
  406. outname,
  407. TestExe : string;
  408. begin
  409. RunExecutable:=false;
  410. TestExe:=ForceExtension(PPFile,ExeExt);
  411. OutName:=ForceExtension(PPFile,'elg');
  412. Verbose(V_Debug,'Executing '+TestExe);
  413. ExecuteRedir(TestExe,'','',OutName,'');
  414. Verbose(V_Debug,'Exitcode '+ToStr(ExecuteResult));
  415. if ExecuteResult<>Config.ResultCode then
  416. begin
  417. AddLog(FailLogFile,TestName);
  418. AddLog(ResLogFile,'Failed to run '+PPFile);
  419. AddLog(LongLogFile,'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  420. AddLog(LongLogFile,'Failed to run '+PPFile+' ('+ToStr(ExecuteResult)+')');
  421. Copyfile(OutName,LongLogFile,true);
  422. Verbose(V_Abort,'Exitcode: '+ToStr(ExecuteResult)+' (expected '+ToStr(Config.ResultCode)+')');
  423. end
  424. else
  425. begin
  426. AddLog(ResLogFile,'Successfully run '+PPFile);
  427. RunExecutable:=true;
  428. end;
  429. end;
  430. procedure getargs;
  431. var
  432. ch : char;
  433. para : string;
  434. i : longint;
  435. procedure helpscreen;
  436. begin
  437. writeln('dotest [Options] <File>');
  438. writeln;
  439. writeln('Options can be:');
  440. writeln(' -C<compiler> set compiler to use');
  441. writeln(' -V verbose');
  442. writeln(' -E execute test also');
  443. writeln(' -A include ALL tests');
  444. writeln(' -G include graph tests');
  445. writeln(' -K include known bug tests');
  446. writeln(' -I include interactive tests');
  447. halt(1);
  448. end;
  449. begin
  450. PPFile:='';
  451. if exeext<>'' then
  452. CompilerBin:='ppc386.'+exeext
  453. else
  454. CompilerBin:='ppc386';
  455. for i:=1 to paramcount do
  456. begin
  457. para:=Paramstr(i);
  458. if (para[1]='-') then
  459. begin
  460. ch:=Upcase(para[2]);
  461. delete(para,1,2);
  462. case ch of
  463. 'A' :
  464. begin
  465. DoGraph:=true;
  466. DoInteractive:=true;
  467. DoKnown:=true;
  468. end;
  469. 'C' : CompilerBin:=Para;
  470. 'E' : DoExecute:=true;
  471. 'G' : DoGraph:=true;
  472. 'I' : DoInteractive:=true;
  473. 'V' : DoVerbose:=true;
  474. 'K' : DoKnown:=true;
  475. end;
  476. end
  477. else
  478. begin
  479. PPFile:=ForceExtension(Para,'pp');
  480. end;
  481. end;
  482. if (PPFile='') then
  483. HelpScreen;
  484. TestName:=Copy(PPFile,1,Pos('.pp',PPFile)-1);
  485. Verbose(V_Debug,'Running test '+TestName+', file '+PPFile);
  486. end;
  487. procedure RunTest;
  488. var
  489. Res : boolean;
  490. begin
  491. Res:=GetConfig(ppfile,Config);
  492. if Res then
  493. begin
  494. if Config.UsesGraph and (not DoGraph) then
  495. begin
  496. Verbose(V_Abort,'Skipping test because it uses graph');
  497. Res:=false;
  498. end;
  499. end;
  500. if Res then
  501. begin
  502. if Config.IsInteractive and (not DoInteractive) then
  503. begin
  504. Verbose(V_Abort,'Skipping test because it is interactive');
  505. Res:=false;
  506. end;
  507. end;
  508. if Res then
  509. begin
  510. if Config.IsKnown and (not DoKnown) then
  511. begin
  512. Verbose(V_Abort,'Skipping test because it is a known bug');
  513. Res:=false;
  514. end;
  515. end;
  516. if Res then
  517. begin
  518. if Config.NeedVersion<>'' then
  519. begin
  520. Verbose(V_Debug,'Required compiler version: '+Config.NeedVersion);
  521. Res:=GetCompilerVersion;
  522. if CompilerVersion<Config.NeedVersion then
  523. begin
  524. Verbose(V_Abort,'Compiler version too low '+CompilerVersion+' < '+Config.NeedVersion);
  525. Res:=false;
  526. end;
  527. end;
  528. end;
  529. if Res then
  530. begin
  531. if Config.NeedCPU<>'' then
  532. begin
  533. Verbose(V_Debug,'Required compiler cpu: '+Config.NeedCPU);
  534. Res:=GetCompilerCPU;
  535. if Upper(Config.NeedCPU)<>Upper(CompilerCPU) then
  536. begin
  537. Verbose(V_Abort,'Compiler cpu wrong '+CompilerCPU+' <> '+Config.NeedCPU);
  538. Res:=false;
  539. end;
  540. end;
  541. end;
  542. if Res then
  543. begin
  544. Res:=RunCompiler;
  545. if Res and Config.NeedRecompile then
  546. Res:=RunCompiler;
  547. end;
  548. if Res then
  549. begin
  550. if (Config.NoRun) then
  551. begin
  552. Verbose(V_Debug,'Skipping run test');
  553. end
  554. else
  555. begin
  556. if (not Config.ShouldFail) and DoExecute then
  557. begin
  558. if FileExists(ForceExtension(PPFile,'ppu')) or
  559. FileExists(ForceExtension(PPFile,'ppw')) then
  560. Verbose(V_Debug,'Unit found, skipping run test')
  561. else
  562. Res:=RunExecutable;
  563. end;
  564. end;
  565. end;
  566. end;
  567. begin
  568. GetArgs;
  569. RunTest;
  570. end.
  571. {
  572. $Log$
  573. Revision 1.10 2001-07-31 09:00:16 pierre
  574. + %Note= comment added
  575. Revision 1.9 2001/07/04 11:23:39 florian
  576. * spelling mistake fixed
  577. Revision 1.8 2001/06/02 00:41:36 peter
  578. * write exitcode for all executed programs in debug mode
  579. Revision 1.7 2000/12/09 16:01:10 peter
  580. + known bug flag
  581. + norun flag
  582. + recompile flag
  583. Revision 1.6 2000/12/04 22:06:25 peter
  584. * fixed stupid c&p bug for CPU check
  585. Revision 1.5 2000/12/03 22:59:10 florian
  586. * some problems for go32v2 fixed
  587. }