@sasjs/core 3.10.0 → 3.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/mp_assertscope.sas +119 -0
- package/package.json +1 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Used to capture scope leakage of macro variables
|
|
4
|
+
@details A common 'difficult to detect' bug in macros is where a nested
|
|
5
|
+
macro over-writes variables in a higher level macro.
|
|
6
|
+
|
|
7
|
+
This assertion takes a snapshot of the macro variables before and after
|
|
8
|
+
a macro invocation. This makes it easy to detect whether any macro
|
|
9
|
+
variables were modified or changed.
|
|
10
|
+
|
|
11
|
+
Currently, the macro only checks for global scope variables. In the future
|
|
12
|
+
it may be extended to work at multiple levels of nesting.
|
|
13
|
+
|
|
14
|
+
If you would like this feature, feel free to contribute / raise an issue /
|
|
15
|
+
engage the SASjs team directly.
|
|
16
|
+
|
|
17
|
+
Example usage:
|
|
18
|
+
|
|
19
|
+
%mp_assertscope(SNAPSHOT)
|
|
20
|
+
|
|
21
|
+
%let oops=I did it again;
|
|
22
|
+
|
|
23
|
+
%mp_assertscope(COMPARE,
|
|
24
|
+
desc=Checking macro variables against previous snapshot
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
@param [in] action (SNAPSHOT) The action to take. Valid values:
|
|
28
|
+
@li SNAPSHOT - take a copy of the current macro variables
|
|
29
|
+
@li COMPARE - compare the current macro variables against previous values
|
|
30
|
+
@param [in] scope= (GLOBAL) The scope of the variables to be checked. This
|
|
31
|
+
corresponds to the values in the SCOPE column in `sashelp.vmacro`.
|
|
32
|
+
@param [in] desc= (Testing variable scope) The user provided test description
|
|
33
|
+
@param [in,out] scopeds= (work.mp_assertscope) The dataset to contain the
|
|
34
|
+
scope snapshot
|
|
35
|
+
@param [out] outds= (work.test_results) The output dataset to contain the
|
|
36
|
+
results. If it does not exist, it will be created, with the following format:
|
|
37
|
+
|TEST_DESCRIPTION:$256|TEST_RESULT:$4|TEST_COMMENTS:$256|
|
|
38
|
+
|---|---|---|
|
|
39
|
+
|User Provided description|PASS|No out of scope variables created or modified|
|
|
40
|
+
|
|
41
|
+
<h4> Related Macros </h4>
|
|
42
|
+
@li mp_assert.sas
|
|
43
|
+
@li mp_assertcols.sas
|
|
44
|
+
@li mp_assertcolvals.sas
|
|
45
|
+
@li mp_assertdsobs.sas
|
|
46
|
+
@li mp_assertscope.test.sas
|
|
47
|
+
|
|
48
|
+
@version 9.2
|
|
49
|
+
@author Allan Bowe
|
|
50
|
+
|
|
51
|
+
**/
|
|
52
|
+
|
|
53
|
+
%macro mp_assertscope(action,
|
|
54
|
+
desc=0,
|
|
55
|
+
scope=GLOBAL,
|
|
56
|
+
scopeds=work.mp_assertscope,
|
|
57
|
+
outds=work.test_results
|
|
58
|
+
)/*/STORE SOURCE*/;
|
|
59
|
+
%local ds test_result test_comments del add mod;
|
|
60
|
+
|
|
61
|
+
/* get current variables */
|
|
62
|
+
%if &action=SNAPSHOT %then %do;
|
|
63
|
+
proc sql;
|
|
64
|
+
create table &scopeds as
|
|
65
|
+
select name,offset,value
|
|
66
|
+
from dictionary.macros
|
|
67
|
+
where scope="&scope"
|
|
68
|
+
order by name,offset;
|
|
69
|
+
%end;
|
|
70
|
+
%else %if &action=COMPARE %then %do;
|
|
71
|
+
|
|
72
|
+
proc sql;
|
|
73
|
+
create table _data_ as
|
|
74
|
+
select name,offset,value
|
|
75
|
+
from dictionary.macros
|
|
76
|
+
where scope="&scope"
|
|
77
|
+
order by name,offset;
|
|
78
|
+
|
|
79
|
+
%let ds=&syslast;
|
|
80
|
+
|
|
81
|
+
proc compare base=&scopeds compare=&ds;
|
|
82
|
+
run;
|
|
83
|
+
|
|
84
|
+
%if &sysinfo=0 %then %do;
|
|
85
|
+
%let test_result=PASS;
|
|
86
|
+
%let test_comments=&scope Variables Unmodified;
|
|
87
|
+
%end;
|
|
88
|
+
%else %do;
|
|
89
|
+
proc sql noprint undo_policy=none;
|
|
90
|
+
select distinct name into: del separated by ' ' from &scopeds
|
|
91
|
+
where name not in (select name from &ds);
|
|
92
|
+
select distinct name into: add separated by ' ' from &ds
|
|
93
|
+
where name not in (select name from &scopeds);
|
|
94
|
+
select distinct a.name into: mod separated by ' '
|
|
95
|
+
from &scopeds a
|
|
96
|
+
inner join &ds b
|
|
97
|
+
on a.name=b.name
|
|
98
|
+
and a.offset=b.offset
|
|
99
|
+
where a.value ne b.value;
|
|
100
|
+
%let test_result=FAIL;
|
|
101
|
+
%let test_comments=%str(Mod:(&mod) Add:(&add) Del:(&del));
|
|
102
|
+
%end;
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
data ;
|
|
106
|
+
length test_description $256 test_result $4 test_comments $256;
|
|
107
|
+
test_description=symget('desc');
|
|
108
|
+
test_comments=symget('test_comments');
|
|
109
|
+
test_result=symget('test_result');
|
|
110
|
+
run;
|
|
111
|
+
|
|
112
|
+
%let ds=&syslast;
|
|
113
|
+
proc append base=&outds data=&ds;
|
|
114
|
+
run;
|
|
115
|
+
proc sql;
|
|
116
|
+
drop table &ds;
|
|
117
|
+
%end;
|
|
118
|
+
|
|
119
|
+
%mend mp_assertscope;
|
package/package.json
CHANGED