SASUnit Examples  Version 1.5.0
runsasunit.sas
Go to the documentation of this file.
1 
40 %MACRO runSASUnit(i_source =
41  ,i_recursive = 0
42  );
43  %GLOBAL
44  d_macroList
45  d_listcalling
46  ;
47  %LOCAL
48  d_dependency
49  d_dir
50  d_examinee
51  d_scenariosToRun
52  d_scn_pre
53  i
54  l_auto
55  l_autonr
56  l_c_scnid
57  l_dorun
58  l_error_count
59  l_filename
60  l_filled
61  l_lstfile
62  l_macname
63  l_nscn
64  l_nscncount
65  l_rc
66  l_result
67  l_result0
68  l_result1
69  l_result2
70  l_scn
71  l_scndesc
72  l_scnid
73  l_scnlogfullpath
74  l_source
75  l_sysrc
76  l_sysrc
77  l_warning_count
78  ;
79 
80  %LET l_macname=&sysmacroname;
81 
82  %_tempFileName(d_dependency);
83  %_tempFileName(d_dir);
84  %_tempFileName(d_examinee);
85  %_tempFileName(d_listcalling);
86  %_tempFileName(d_macroList);
87  %_tempFileName(d_scenariosToRun);
88  %_tempFileName(d_scn_pre);
89 
90  /*-- check if testdatabase can be accessed -----------------------------------*/
91  %IF %_handleError(&l_macname.
92  ,NoTestDB
93  ,NOT %sysfunc(exist(target.tsu)) OR NOT %symexist(g_project)
94  ,%nrstr(test database cannot be accessed, call initSASUnit before runSASUnit)
95  ,i_verbose=&g_verbose.
96  )
97  %THEN %GOTO errexit;
98 
99  /*-- parameter i_recursive ---------------------------------------------------*/
100  %IF "&i_recursive" NE "1" %THEN %LET i_recursive=0;
101 
102  /*-- find out all test scenarios ---------------------------------------------*/
103  %LET l_source = %_abspath(&g_root, &i_source);
104  %_dir(i_path=&l_source, i_recursive=&i_recursive, o_out=&d_dir)
105  %IF %_handleError(&l_macname.
106  ,NoSourceFiles
107  ,%_nobs(&d_dir) EQ 0
108  ,Error in parameter i_source: no test scenarios found
109  ,i_verbose=&g_verbose.
110  )
111  %THEN %GOTO errexit;
112 
113  data &d_scn_pre.;
114  set &d_dir.;
115  run;
116 
117  /*-- find out all possible units under test ----------------------------------*/
118  %LET l_auto=&g_sasautos;
119  %LET l_autonr=0;
120  %DO %WHILE("&l_auto" ne "");
121  %LET l_auto=%quote(&l_auto/);
122  %_dir(i_path=&l_auto.*.sas, o_out=&d_dir);
123  data &d_examinee;
124  set %IF &l_autonr>0 %THEN &d_examinee; &d_dir(in=indir);
125  if indir then DO;
126  auton=&l_autonr.+2;
127  source=symgetc ("l_auto");
128  end;
129  run;
130  %LET l_autonr = %eval(&l_autonr+1);
131  %LET l_auto=;
132  %IF %symexist(g_sasautos&l_autonr) %THEN %LET l_auto=&&g_sasautos&l_autonr;
133  %END;
134  %IF (&g_crossrefsasunit.) %THEN %DO;
135  %LET l_auto=&g_sasunit;
136  %LET l_auto=%quote(&l_auto/);
137  %_dir(i_path=&l_auto.*.sas, o_out=&d_dir);
138  data &d_examinee;
139  set &d_examinee &d_dir(in=indir);
140  if indir then DO;
141  auton=0;
142  source=symgetc ("l_auto");
143  end;
144  run;
145  %LET l_auto=&g_sasunit_os;
146  %LET l_auto=%quote(&l_auto/);
147  %_dir(i_path=&l_auto.*.sas, o_out=&d_dir);
148  data &d_examinee;
149  set &d_examinee &d_dir(in=indir);
150  if indir then DO;
151  auton=1;
152  source=symgetc ("l_auto");
153  end;
154  run;
155  %END;
156 
157  /* Create cross-reference */
158  %_crossreference(i_includeSASUnit = &g_crossrefsasunit.
159  ,i_examinee = &d_examinee.
160  ,o_listcalling = &d_listcalling.
161  ,o_dependency = &d_dependency.
162  ,o_macroList = &d_macroList.
163  );
164 
165  /* check which test scenarios must be run */
166  %_checkScenario(i_examinee = &d_examinee.
167  ,i_scn_pre = &D_SCN_PRE.
168  ,i_dependency = &d_dependency.
169  ,i_scenariosToRun = &d_scenariosToRun.
170  );
171 
172  /*-- if scenario already exists and has been changed: delete scenario files-----*/
173  %_deletescenariofiles(i_scenariosToRun=&d_scenariosToRun.
174  );
175 
176  PROC SQL NOPRINT;
177  DELETE * FROM target.cas WHERE cas_scnid in (select scn_id from &d_scenariosToRun where dorun=1);
178  DELETE * FROM target.tst WHERE tst_scnid in (select scn_id from &d_scenariosToRun where dorun=1);
179  QUIT;
180 
181  /*-- if scenario not present in test database: create new scenario --------*/
182  DATA target.scn;
183  SET target.scn &d_scenariosToRun(where=(insertIntoDB=1) in=add);
184  IF add=1 THEN DO;
185  scn_path = resolve('%_stdPath(&g_root,' || filename || ')');
186  drop filename dorun insertIntoDB name;
187  END;
188  RUN;
189 
190  /* Prepare Loop */
191  PROC SQL noprint;
192  select count(scn_id) into :l_nscn
193  from &d_scenariosToRun
194  ;
195  QUIT;
196 
197  /*-- loop over all test scenarios --------------------------------------------*/
198  %DO i=1 %TO &l_nscn;
199 
200  DATA _NULL_;
201  in = &i.;
202  set &d_scenariosToRun point=in;
203  Call Symputx('l_scnid',scn_id, 'L');
204  Call Symputx('l_dorun',dorun, 'L');
205  Call Symputx('l_filename',filename, 'L');
206  stop;
207  RUN;
208 
209  %LET l_scn = %_stdPath(&g_root,&l_filename);
210 
211  %IF &l_dorun %THEN %DO;
212  %PUT ======== test scenario &l_scnid (&l_scn) will be run ========;
213  %PUT;
214  %PUT;
215  %END;
216  %ELSE %DO;
217  %PUT ======== test scenario &l_scnid (&l_scn) will not be run ==;
218  %PUT;
219  %PUT;
220  %END;
221 
222  /*-- start test scenario if necessary -------------------------------------*/
223  %IF &l_dorun %THEN %DO;
224 
225  /*-- save description and start date and time of scenario --------------*/
226  %_getPgmDesc (i_pgmfile=&l_filename, r_desc=l_scndesc)
227  PROC SQL NOPRINT;
228  UPDATE target.scn SET
229  scn_desc = "&l_scndesc"
230  ,scn_start = %sysfunc(datetime())
231  WHERE scn_id = &l_scnid
232  ;
233  QUIT;
234 
235  %LET l_c_scnid = %substr(00&l_scnid.,%length(&l_scnid));
236  %LET l_scnlogfullpath = &g_log/&l_c_scnid..log;
237  %_runProgramSpawned(i_program =&l_filename
238  ,i_scnid =&l_c_scnid.
239  ,i_generateMcoverage=&g_testcoverage.
240  ,r_sysrc =l_sysrc
241  );
242 
243  /*-- delete listing if empty -------------------------------------------*/
244  %LET l_filled=0;
245  %LET l_lstfile=&g_testout/%substr(00&l_scnid,%length(&l_scnid)).lst;
246  %IF %SYSFUNC(FILEEXIST("&l_lstfile")) %THEN %DO;
247  DATA _null_;
248  INFILE "&l_lstfile";
249  INPUT;
250  CALL symput ('l_filled','1');
251  STOP;
252  RUN;
253  %END;
254  %IF NOT &l_filled %THEN %DO;
255  %LET l_filled=%_delfile(&l_lstfile);
256  %END;
257 
258  /*-- save metadata of test scenario ------------------------------------*/
259  /* scan log for errors outside test cases */
260  %_checklog (
261  i_logfile = &l_scnlogfullpath.
262  ,i_error = &g_error.
263  ,i_warning = &g_warning.
264  ,r_errors = l_error_count
265  ,r_warnings= l_warning_count
266  )
267 
268  PROC SQL NOPRINT;
269  /* determine results of the test cases */
270  %*** Treat missing scenario as error ***;
271  %let l_result=2;
272 
273  SELECT max (cas_res) INTO :l_result FROM target.cas WHERE cas_scnid=&l_scnid;
274 
275  %*** Treat missing scenario as failed and treat scenario with errors in scenario log as failed ***;
276  %if (&l_result. = . or &l_error_count. > 0) %then %let l_result=2;
277 
278  UPDATE target.scn
279  SET
280  scn_end = %sysfunc(datetime())
281  ,scn_rc = &l_sysrc.
282  ,scn_errorcount = &l_error_count.
283  ,scn_warningcount = &l_warning_count.
284  ,scn_res = &l_result.
285  WHERE
286  scn_id = &l_scnid.
287  ;
288 
289  UPDATE &d_scenariosToRun
290  SET
291  dorun = 0
292  WHERE
293  scn_id = &l_scnid.
294  ; QUIT;
295 
296  %END; /* run scenario */
297  %END; /* loop for all scenarios */
298 
299  %GOTO exit;
300 %errexit:
301  %PUT;
302  %PUT =========================== Error! runSASUnit aborted! ==========================================;
303  %PUT;
304  %PUT;
305 
306  %IF %EVAL("%UPCASE(&g_error_code.)" EQ "%UPCASE(NoSourceFiles)") %THEN %DO;
307 
308  /* ensure that dummy entry for inexisting scenario is present in test database, to be able to report it later
309  */
310  %LET l_scn = %_stdPath(&g_root., &l_source.);
311 
312  %LET l_nscncount = 0;
313  PROC SQL NOPRINT;
314  SELECT Count(scn_id)
315  INTO :l_nscncount SEPARATED BY ''
316  FROM target.scn
317  WHERE Upcase(scn_path) = "%UPCASE(&l_scn.)";
318  QUIT;
319 
320  %IF %EVAL(&l_nscncount. EQ 0) %THEN %DO;
321 
322  %LET l_scndesc = %STR(Scenario not found - has to fail!);
323 
324  PROC SQL NOPRINT;
325  SELECT max(scn_id) INTO :l_scnid FROM target.scn;
326  %IF &l_scnid=. %THEN %LET l_scnid=0;
327  %LET l_scnid = %eval(&l_scnid+1);
328  INSERT INTO target.scn
329  (
330  scn_id
331  ,scn_path
332  ,scn_desc
333  ,scn_start
334  ,scn_end
335  ,scn_rc
336  ,scn_errorcount
337  ,scn_warningcount
338  ,scn_res
339  )
340  VALUES
341  (
342  &l_scnid
343  ,"&l_scn."
344  ,"&l_scndesc."
345  ,.
346  ,.
347  ,.
348  ,.
349  ,.
350  ,2
351  )
352  ;
353  QUIT;
354  %END; /* if scenario is not present in database */
355  %END;
356 
357 %exit:
358  PROC DATASETS NOLIST NOWARN LIB=%scan(&d_dependency,1,.);
359  DELETE %scan(&d_dependency,2,.);
360  DELETE %scan(&d_dir,2,.);
361  DELETE %scan(&d_examinee,2,.);
362  DELETE %scan(&d_scenariosToRun,2,.);
363  DELETE %scan(&d_scn_pre,2,.);
364  QUIT;
365 %MEND runSASUnit;