Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_dirlist.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_dirlist.sas macro
4
5 <h4> SAS Macros </h4>
6 @li mf_nobs.sas
7 @li mf_getuniquefileref.sas
8 @li mf_mkdir.sas
9 @li mp_assert.sas
10 @li mp_assertscope.sas
11 @li mp_assertmodel.sas
12 @li mddl_dirlist.sas
13 @li mp_dirlist.sas
14
15**/
16
17/**
18 * make a directory structure
19 */
20
21%let root=%sysfunc(pathname(work))/top;
22%mf_mkdir(&root)
23%mf_mkdir(&root/a)
24%mf_mkdir(&root/b)
25%mf_mkdir(&root/a/d)
26%mf_mkdir(&root/a/e)
27%mf_mkdir(&root/a/e/f)
28libname test "&root/a/e/f";
29data test.ds1;
30 x=1;
31run;
32
33%mddl_dirlist(work.dirlist_schema)
34/* Keep a caller-owned fileref assigned while checking the listing's cleanup. */
35%let dirlist_userref=%mf_getuniquefileref();
36%let dirlist_userpath=%sysfunc(pathname(&dirlist_userref));
37/* The helper uses underscore plus seven digits; ignore SAS internal # refs. */
38%macro dirlist_snapshot();
39 proc sql;
40 create table work.dirlist_refs_before as
41 select distinct fileref from dictionary.extfiles
42 where substr(fileref,1,1)='_' and lengthn(fileref)=8
43 and verify(substr(fileref,2),'0123456789')=0;
44 quit;
45%mend dirlist_snapshot;
46%macro dirlist_checkrefs(desc);
47 proc sql noprint;
48 select count(distinct fileref) into :dirlist_newrefs trimmed
49 from dictionary.extfiles
50 where substr(fileref,1,1)='_' and lengthn(fileref)=8
51 and verify(substr(fileref,2),'0123456789')=0
52 and fileref not in (select fileref from work.dirlist_refs_before);
53 quit;
54 %mp_assert(iftrue=(&dirlist_newrefs=0),desc=&desc)
55%mend dirlist_checkrefs;
56
57%dirlist_snapshot()
58%mp_assertscope(SNAPSHOT)
59%mp_dirlist(path=&root, outds=myTable, maxdepth=MAX)
60%mp_assertscope(COMPARE,
61 desc=Test 1: mp_dirlist does not leak scope,
62 outds=work.test_results
63)
64%dirlist_checkrefs(Directory listing releases its temporary filerefs)
65
66%mp_assertmodel(work.mytable,base=work.dirlist_schema,
67 desc=Populated output matches directory model)
68
69%mp_assert(
70 iftrue=(%mf_nobs(work.mytable)=6),
71 desc=All levels returned,
72 outds=work.test_results
73)
74
75%mp_dirlist(path=&root, outds=myTable2, maxdepth=2)
76
77%mp_assert(
78 iftrue=(%mf_nobs(work.mytable2)=5),
79 desc=Top two levels returned,
80 outds=work.test_results
81)
82
83%mp_dirlist(path=&root, outds=work.myTable3, maxdepth=0)
84
85%mp_assert(
86 iftrue=(%mf_nobs(work.mytable3)=2),
87 desc=Top level returned,
88 outds=work.test_results
89)
90
91%dirlist_snapshot()
92%mp_dirlist(path=&root/b, outds=work.myTable4)
93%dirlist_checkrefs(Empty directory releases its temporary filerefs)
94%mp_assertmodel(work.mytable4,base=work.dirlist_schema,
95 desc=Empty output matches directory model)
96%mp_assert(
97 iftrue=(%mf_nobs(work.mytable4)=0),
98 desc=Empty table for empty directory,
99 outds=work.test_results
100)
101
102%dirlist_snapshot()
103%mp_dirlist(path=&root/notexisting, outds=work.myTable5)
104%dirlist_checkrefs(Missing directory releases its temporary filerefs)
105%mp_assert(
106 iftrue=(%mf_nobs(work.mytable5)=0),
107 desc=Empty table for non-existing directory,
108 outds=work.test_results
109)
110
111/**
112 * maxobs caps the rows returned, and the scan stops at the cap
113 */
114%mp_dirlist(path=&root, outds=work.myTable6, maxdepth=MAX, maxobs=3)
115%mp_assert(
116 iftrue=(%mf_nobs(work.mytable6)=3),
117 desc=Rows are capped by maxobs,
118 outds=work.test_results
119)
120
121%mp_dirlist(path=&root, outds=work.myTable7, maxdepth=MAX, maxobs=100)
122%mp_assert(
123 iftrue=(%mf_nobs(work.mytable7)=6),
124 desc=A maxobs above the tree size returns everything,
125 outds=work.test_results
126)
127
128/**
129 * showparent adds the starting folder itself
130 */
131%mp_dirlist(path=&root, outds=work.myTable8, maxdepth=0, showparent=YES)
132%mp_assert(
133 iftrue=(%mf_nobs(work.mytable8)=3),
134 desc=Showparent adds one row for the parent,
135 outds=work.test_results
136)
137
138/**
139 * the level column reports depth below the parent
140 */
141data _null_;
142 set work.mytable end=eof;
143 retain maxlevel 0;
144 if level>maxlevel then maxlevel=level;
145 if eof then call symputx('maxlevel',maxlevel,'l');
146run;
147%mp_assert(
148 iftrue=(&maxlevel=3),
149 desc=Deepest entry is three levels below the parent,
150 outds=work.test_results
151)
152
153/**
154 * a file is reported as a file and a folder as a folder
155 */
156data _null_;
157 set work.mytable;
158 if filename='a' then call symputx('type_a',file_or_folder,'l');
159 if filename='ds1.sas7bdat' then call symputx('type_ds1',file_or_folder,'l');
160run;
161%mp_assert(
162 iftrue=("%upcase(&type_a)"="FOLDER"),
163 desc=A directory entry is classified as a folder,
164 outds=work.test_results
165)
166%mp_assert(
167 iftrue=("%upcase(&type_ds1)"="FILE"),
168 desc=A dataset file is classified as a file,
169 outds=work.test_results
170)
171
172
173/**
174 * a blank path is the platform roots - drives on Windows, / on Unix
175 */
176%dirlist_snapshot()
177%mp_dirlist(path=, outds=work.myTable9)
178%dirlist_checkrefs(Root listing releases its temporary filerefs)
179data _null_;
180 ref=symget('dirlist_userref');
181 preserved=(fileref(ref)<=0 and pathname(ref)=symget('dirlist_userpath'));
182 call symputx('dirlist_preserved',preserved);
183run;
184%mp_assert(iftrue=(&dirlist_preserved=1),
185 desc=Listing preserves caller-owned fileref)
186%let dirlist_rc=%sysfunc(filename(dirlist_userref));
187%mp_assert(
188 iftrue=(%mf_nobs(work.mytable9)>=1),
189 desc=A blank path returns the platform roots,
190 outds=work.test_results
191)
192data _null_;
193 set work.mytable9(obs=1);
194 call symputx('rootpath',filepath,'l');
195run;
196%mp_assert(
197 iftrue=(%length(&rootpath)>=1),
198 desc=The root entry carries a path,
199 outds=work.test_results
200)
201
202/**
203 * a plain file is a file. Some platforms answer dopen on a file with an
204 * empty sysmsg(), so the message checks alone cannot tell it from an entry
205 * that could not be opened at all
206 */
207data _null_;
208 file "&root/plain.txt";
209 put 'x';
210run;
211%mp_dirlist(path=&root, outds=work.myTable10)
212data _null_;
213 set work.mytable10(where=(filename='plain.txt'));
214 call symputx('type_txt',file_or_folder,'l');
215 call symputx('msg_txt',msg,'l');
216run;
217%mp_assert(
218 iftrue=("%upcase(&type_txt)"="FILE"),
219 desc=A text file is classified as a file,
220 outds=work.test_results
221)
222
223/**
224 * dump results to the log for offline inspection
225 */
226data _null_;
227 set work.test_results;
228 putlog 'TEST_RESULT_LINE: ' test_result ' - ' test_description;
229run;