SASUnit Examples  Version 1.2.1
runsasunit.sas
Go to the documentation of this file.
1 
39 /* change log
40  14.02.2013 PW/KL Modified for LINUX
41  08.02.2013 PW implementation of test coverage assessment
42  29.01.2013 KL changed link from _sasunit_doc.sas to Sourceforge SASUnit User's Guide
43  28.01.2013 KL Adjusted descriptions of testcases
44  02.10.2008 NA Modified for LINUX
45  11.08.2008 AM Fehler bereinigt für den Fall, dass in der aufrufenden Sitzung keine config-Option angegeben ist
46  und die mit getoption abgefragte config-option dann Werte (mit Klammern) enthält,
47  die nicht an die gestartete SAS-Sitzung übergeben werden können
48  27.06.2008 AM config-option aus Aufruf ggfs. an die SAS-Sitzungen der Testszenarien weitergeben
49  29.12.2007 AM SASUSER nach Aufruf Testszenario wieder löschen
50  18.12.2007 KL Bugfixing beim Ermitteln der möglichen Prüflinge
51  15.12.2007 AM Logik für Überprüfung auf auszuführende Testszenarien neu implementiert (checkSzenario)
52 */
53 
54 %MACRO runSASUnit(
55  i_source =
56  ,i_recursive = 0
57 );
58 
59 %LOCAL
60  l_macname
61  d_dir
62  d_examinee
63  l_source
64  l_nscn
65  i
66  l_auto
67  l_autonr
68  l_scn
69  l_scnid
70  l_dorun
71  l_scndesc
72  l_sysrc
73  l_parms
74  l_parenthesis
75  l_rc
76  l_scnlogfullpath
77  l_filled
78  l_lstfile
79  l_error_count
80  l_warning_count
81  l_result0
82  l_result1
83  l_result2
84  l_result
85  l_nscncount
86  l_tcgFilePath
87  l_tcgOptionsString
88  l_tcgOptionsStringLINUX
89 ;
90 
91 %LET l_macname=&sysmacroname;
92 
93 %_sasunit_tempFileName(d_dir);
94 %_sasunit_tempFileName(d_examinee);
95 
96 /*-- check if testdatabase can be accessed -----------------------------------*/
97 %IF %_sasunit_handleError(&l_macname, NoTestDB,
98  NOT %sysfunc(exist(target.tsu)) OR NOT %symexist(g_project),
99  %nrstr(test database cannot be accessed, call %initSASUnit before %runSASUnit))
100  %THEN %GOTO errexit;
101 
102 /*-- parameter i_recursive ---------------------------------------------------*/
103 %IF "&i_recursive" NE "1" %THEN %LET i_recursive=0;
104 
105 /*-- find out all test scenarios ---------------------------------------------*/
106 %LET l_source = %_sasunit_abspath(&g_root, &i_source);
107 %_sasunit_dir(i_path=&l_source, i_recursive=&i_recursive, o_out=&d_dir)
108 %IF %_sasunit_handleError(&l_macname, NoSourceFiles,
109  %_sasunit_nobs(&d_dir) EQ 0,
110  Error in parameter i_source: no test scenarios found)
111  %THEN %GOTO errexit;
112 
113 %DO i=1 %TO %_sasunit_nobs(&d_dir);
114  %LOCAL
115  l_scnfile&i
116  l_scnchanged&i
117  ;
118 %END;
119 
120 DATA _null_;
121  SET &d_dir;
122  CALL symput ('l_scnfile' !! left(put(_n_,8.)), trim(filename));
123  CALL symput ('l_scnchanged' !! left(put(_n_,8.)), compress(put(changed,12.)));
124  CALL symput ('l_nscn', compress(put(_n_,8.)));
125 RUN;
126 
127 /*-- find out all possible units under test ----------------------------------*/
128 %LET l_auto=&g_sasautos;
129 %LET l_autonr=0;
130 %DO %WHILE("&l_auto" ne "");
131  %LET l_auto=%quote(&l_auto/);
132  %_sasunit_dir(i_path=&l_auto.*.sas, o_out=&d_dir)
133  data &d_examinee;
134  set %IF &l_autonr>0 %THEN &d_examinee; &d_dir(in=indir);
135  if indir then auton=&l_autonr;
136  run;
137  %LET l_autonr = %eval(&l_autonr+1);
138  %LET l_auto=;
139  %IF %symexist(g_sasautos&l_autonr) %THEN %LET l_auto=&&g_sasautos&l_autonr;
140 %END;
141 
142 /*-- loop over all test scenarios --------------------------------------------*/
143 %DO i=1 %TO &l_nscn;
144 
145  %LET l_scn = %_sasunit_stdPath(&g_root, &&l_scnfile&i);
146 
147  /* check if test scenario must be run */
148  %_sasunit_checkScenario(
149  i_scnfile = &&l_scnfile&i
150  ,i_changed = &&l_scnchanged&i
151  ,i_dir = &d_examinee
152  ,r_scnid = l_scnid
153  ,r_run = l_dorun
154  )
155 
156  /*-- if scenario not present in test database: create new scenario --------*/
157  %IF &l_scnid = 0 %THEN %DO;
158  PROC SQL NOPRINT;
159  SELECT max(scn_id) INTO :l_scnid FROM target.scn;
160  %IF &l_scnid=. %THEN %LET l_scnid=0;
161  %LET l_scnid = %eval(&l_scnid+1);
162  INSERT INTO target.scn VALUES (
163  &l_scnid
164  ,"&l_scn"
165  ,"",.,.,.,.,.,.
166  );
167  QUIT;
168  %END;
169  /*-- if scenario already exists and has been changed: delete scenario -----*/
170  %ELSE %IF &l_dorun %THEN %DO;
171  PROC SQL NOPRINT;
172  DELETE FROM target.cas WHERE cas_scnid = &l_scnid;
173  DELETE FROM target.tst WHERE tst_scnid = &l_scnid;
174  QUIT;
175  %END;
176 
177  %IF &l_dorun %THEN %DO;
178  %PUT ======== test scenario &l_scnid (&l_scn) will be run ========;
179  %PUT;
180  %PUT;
181  %END;
182  %ELSE %DO;
183  %PUT ======== test scenario &l_scnid (&l_scn) will not be run ==;
184  %PUT;
185  %PUT;
186  %END;
187 
188  /*-- start test scenario if necessary -------------------------------------*/
189  %IF &l_dorun %THEN %DO;
190 
191  /*-- save description and start date and time of scenario --------------*/
192  %_sasunit_getPgmDesc (i_pgmfile=&&l_scnfile&i, r_desc=l_scndesc)
193  PROC SQL NOPRINT;
194  UPDATE target.scn SET
195  scn_desc = "&l_scndesc"
196  ,scn_start = %sysfunc(datetime())
197  WHERE scn_id = &l_scnid
198  ;
199  QUIT;
200 
201  /*-- prepare sasuser ---------------------------------------------------*/
202  DATA _null_;
203  FILE "%sysfunc(pathname(work))/x.cmd";
204  PUT "&g_removedir ""%sysfunc(pathname(work))/sasuser""&g_endcommand";
205  PUT "&g_makedir ""%sysfunc(pathname(work))/sasuser""&g_endcommand";
206  %IF %length(&g_sasuser) %THEN %DO;
207  PUT "&g_copydir ""&g_sasuser"" ""%sysfunc(pathname(work))/sasuser""&g_endcommand";
208  %END;
209  RUN;
210  %if &sysscp. = LINUX %then %do;
211  %_sasunit_xcmd(chmod u+x "%sysfunc(pathname(work))/x.cmd")
212  %end;
213  %_sasunit_xcmd("%sysfunc(pathname(work))/x.cmd")
214  %LET l_rc=_sasunit_delfile(%sysfunc(pathname(work))/x.cmd);
215 
216  /*-- run test scenario in a new process --------------------------------*/
217  %LET l_parms=;
218  %LET l_parenthesis=(;
219  %IF "&g_autoexec" NE "" %THEN %DO;
220  %LET l_parms=&l_parms -autoexec ""&g_autoexec"";
221  %END;
222  %IF &sysscp. = LINUX %THEN %DO;
223  %IF "&g_sascfg" NE "" %THEN %DO;
224  options SET=SASCFGPATH "&g_sascfg.";
225  %END;
226  %END;
227  %ELSE %DO;
228  %IF "&g_sascfg" NE "" %THEN %DO;
229  %LET l_parms=&l_parms -config ""&g_sascfg"";
230  %END;
231  %ELSE %IF %length(%sysfunc(getoption(config))) NE 0 AND %index(%quote(%sysfunc(getoption(config))),%bquote(&l_parenthesis)) NE 1 %THEN %DO;
232  %LET l_parms=&l_parms -config ""%sysfunc(getoption(config))"";
233  %END;
234  %END;
235 
236  %LET l_scnlogfullpath = &g_log/%substr(00&l_scnid.,%length(&l_scnid)).log;
237 
238  %IF &g_testcoverage. EQ 1 %THEN %DO;
239  /*-- generate a local macro variable containing the
240  path to the generated coverage file if necessary ---------------*/
241  %LET l_tcgFilePath = &g_log/%substr(00&l_scnid.,%length(&l_scnid)).tcg;
242  %LET l_tcgOptionsString = -mcoverage -mcoverageloc = ""&l_tcgFilePath."";
243  %LET l_tcgOptionsStringLINUX = options mcoverage mcoverageloc='&l_tcgFilePath.';
244  %END;
245 
246  DATA _null_;
247  ATTRIB
248  _sCmdString LENGTH = $32000
249  ;
250  FILE
251  "%sysfunc(pathname(work))/xx.cmd"
252  LRECL=32000
253  ;
254  %IF &sysscp. = LINUX %THEN %DO;
255  _sCmdString =
256  "" !! &g_sasstart.
257  !! " "
258  !! "&l_parms. "
259  !! "-sysin &&l_scnfile&i "
260  !! "-initstmt "" &l_tcgOptionsStringLINUX.; %nrstr(%%_sasunit_scenario%(io_target=)&g_target%nrstr(%);%%let g_scnid=)&l_scnid.;"" "
261  !! "-log &l_scnlogfullpath. "
262  !! "-print &g_testout/%substr(00&l_scnid.,%length(&l_scnid)).lst "
263  !! "-noovp "
264  !! "-nosyntaxcheck "
265  !! "-mautosource "
266  !! "-mcompilenote all "
267  !! "-sasautos &g_sasunit "
268  !! "-sasuser %sysfunc(pathname(work))/sasuser "
269  !! "-termstmt ""%nrstr(%%_sasunit_termScenario())"" "
270  !! "";
271  %END;
272  %ELSE %DO;
273  _sCmdString =
274  """" !! &g_sasstart !! """"
275  !! " "
276  !! "&l_parms. "
277  !! "-sysin ""&&l_scnfile&i"" "
278  !! "-initstmt ""%nrstr(%%%_sasunit_scenario%(io_target=)&g_target%nrstr(%);%%%let g_scnid=)&l_scnid.;"" "
279  !! "-log ""&l_scnlogfullpath."" "
280  !! "-print ""&g_testout/%substr(00&l_scnid.,%length(&l_scnid)).lst"" "
281  !! "&g_splash "
282  !! "-noovp "
283  !! "-nosyntaxcheck "
284  !! "-mautosource "
285  !! "-mcompilenote all "
286  !! "-sasautos ""&g_sasunit"" "
287  !! "-sasuser ""%sysfunc(pathname(work))/sasuser"" "
288  !! "-termstmt ""%nrstr(%%%_sasunit_termScenario())"" "
289  !! "&l_tcgOptionsString. "
290  !! "";
291  %END;
292  PUT
293  _sCmdString
294  ;
295  RUN;
296  %IF &sysscp. = LINUX %THEN %DO;
297  %_sasunit_xcmd(chmod u+x "%sysfunc(pathname(work))/xx.cmd");
298  %_sasunit_xcmd(sed -i -e 's/\r//g' %sysfunc(pathname(work))/xx.cmd);
299  %END;
300  %_sasunit_xcmd("%sysfunc(pathname(work))/xx.cmd")
301 
302 
303  %LET l_rc=_sasunit_delfile(%sysfunc(pathname(work))/xx.cmd);
304  %LET l_sysrc = &sysrc;
305 
306  /*-- delete sasuser ----------------------------------------------------*/
307  DATA _null_;
308  FILE "%sysfunc(pathname(work))/x.cmd";
309  PUT "&g_removedir ""%sysfunc(pathname(work))/sasuser""&g_endcommand";
310  RUN;
311  %if &sysscp. = LINUX %then %do;
312  %_sasunit_xcmd(chmod u+x "%sysfunc(pathname(work))/x.cmd")
313  %end;
314 
315  %_sasunit_xcmd("%sysfunc(pathname(work))/x.cmd")
316  %LET l_rc=_sasunit_delfile(%sysfunc(pathname(work))/x.cmd);
317 
318  /*-- delete listing if empty -------------------------------------------*/
319  %LET l_filled=0;
320  %LET l_lstfile=&g_testout/%substr(00&l_scnid,%length(&l_scnid)).lst;
321  %IF %SYSFUNC(FILEEXIST("&l_lstfile")) %THEN %DO;
322  DATA _null_;
323  INFILE "&l_lstfile";
324  INPUT;
325  CALL symput ('l_filled','1');
326  STOP;
327  RUN;
328  %END;
329  %IF NOT &l_filled %THEN %DO;
330  %LET l_filled=%_sasunit_delfile(&l_lstfile);
331  %END;
332 
333  /*-- save metadata of test scenario ------------------------------------*/
334  /* scan log for errors outside test cases */
335  %_sasunit_checklog (
336  i_logfile = &l_scnlogfullpath.
337  ,i_error = &g_error.
338  ,i_warning = &g_warning.
339  ,r_errors = l_error_count
340  ,r_warnings= l_warning_count
341  )
342 
343  PROC SQL NOPRINT;
344  /* determine results of the test cases */
345  SELECT count(*) INTO :l_result0 FROM target.cas WHERE cas_scnid=&l_scnid AND cas_res=0;
346  SELECT count(*) INTO :l_result1 FROM target.cas WHERE cas_scnid=&l_scnid AND cas_res=1;
347  SELECT count(*) INTO :l_result2 FROM target.cas WHERE cas_scnid=&l_scnid AND cas_res=2;
348 
349  %IF &l_result1 GT 0 OR &l_error_count. GT 0 %THEN %DO;
350  %LET l_result=1; /* error occured */
351  %END;
352  %ELSE %IF &l_result2 GT 0 %THEN %DO;
353  %LET l_result=2; /* manual occured */
354  %END;
355  %ELSE %IF %EVAL(%SYSFUNC(sum(&l_result0., &l_result1., &l_result2.)) EQ 0) %THEN %DO;
356  %LET l_result=1; /* no test cases -> show as error occurred */
357  %END;
358  %ELSE %IF &l_error_count. GT 0 %THEN %DO;
359  %LET l_result=1; /* error(s) in scenario log -> show as error occurred */
360  %END;
361  %ELSE %DO;
362  %LET l_result=0; /* everything OK */
363  %END;
364 
365  UPDATE target.scn
366  SET
367  scn_end = %sysfunc(datetime())
368  ,scn_rc = &l_sysrc.
369  ,scn_errorcount = &l_error_count.
370  ,scn_warningcount = &l_warning_count.
371  ,scn_res = &l_result.
372  WHERE
373  scn_id = &l_scnid
374  ;
375  QUIT;
376 
377  %END; /* run scenario */
378 %END; /* loop for all scenarios */
379 
380 %GOTO exit;
381 %errexit:
382  %PUT;
383  %PUT =========================== Error! runSASUnit aborted! ==========================================;
384  %PUT;
385  %PUT;
386 
387  %IF %EVAL("%UPCASE(&g_error_code.)" EQ "%UPCASE(NoSourceFiles)") %THEN %DO;
388 
389  /* ensure that dummy entry for inexisting scenario is present in test database, to be able to report it later
390  */
391  %LET l_scn = %_sasunit_stdPath(&g_root., &l_source.);
392 
393  %LET l_nscncount = 0;
394  PROC SQL NOPRINT;
395  SELECT Count(scn_id)
396  INTO :l_nscncount SEPARATED BY ''
397  FROM target.scn
398  WHERE Upcase(scn_path) = "%UPCASE(&l_scn.)";
399  QUIT;
400 
401  %IF %EVAL(&l_nscncount. EQ 0) %THEN %DO;
402 
403  %LET l_scndesc = %STR(Scenario not found - has to fail!);
404 
405  PROC SQL NOPRINT;
406  SELECT max(scn_id) INTO :l_scnid FROM target.scn;
407  %IF &l_scnid=. %THEN %LET l_scnid=0;
408  %LET l_scnid = %eval(&l_scnid+1);
409  INSERT INTO target.scn
410  (
411  scn_id
412  ,scn_path
413  ,scn_desc
414  ,scn_start
415  ,scn_end
416  ,scn_rc
417  ,scn_errorcount
418  ,scn_warningcount
419  ,scn_res
420  )
421  VALUES
422  (
423  &l_scnid
424  ,"&l_scn."
425  ,"&l_scndesc."
426  ,.
427  ,.
428  ,.
429  ,.
430  ,.
431  ,1
432  )
433  ;
434  QUIT;
435 
436  %END; /* if scenario is not present in database */
437 
438  %END;
439 
440 %exit:
441 PROC DATASETS NOLIST NOWARN LIB=%scan(&d_dir,1,.);
442  DELETE %scan(&d_dir,2,.);
443  DELETE %scan(&d_examinee,2,.);
444 QUIT;
445 %MEND runSASUnit;