Published on :
Test CREATION_INTERNE

Test of the mp_dirlist macro for directory listing

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script initializes a temporary root directory in the SAS© work directory, then builds a subdirectory hierarchy (a, b, a/d, a/e, a/e/f) using the `%mf_mkdir` macro. A SAS© dataset (`test.ds1`) is created in the deepest directory (`&root/a/e/f`) to simulate content. Multiple calls to `%mp_dirlist` are made with various `maxdepth` parameters (MAX, 2, 0) to test the macro's ability to list directories at different depths. For each test, the script uses the `%mf_nobs` macros to count observations in the `mp_dirlist` output datasets and `%mp_assert` to validate that these counts match expectations. Specific cases for an empty directory (`&root/b`) and a non-existent directory (`&root/notexisting`) are also tested to ensure the macro's robustness.
Data Analysis

Type : CREATION_INTERNE


The script creates a temporary directory structure and SAS dataset for testing purposes. No external data or data from the `SASHELP` library is used directly for the testing process. Test data is generated in place.

1 Code Block
MACRO mf_mkdir / DATA STEP Data
Explanation :
This code block initializes a macro variable `root` pointing to a new 'top' subdirectory in the temporary work directory (`work`). It then repeatedly uses the `%mf_mkdir` macro to create a nested directory hierarchy. A `test` libname is defined to point to the deepest directory. Finally, a `ds1` dataset is created in this library, serving as test data.
Copied!
1%let root=%sysfunc(pathname(work))/top;
2%mf_mkdir(&root)
3%mf_mkdir(&root/a)
4%mf_mkdir(&root/b)
5%mf_mkdir(&root/a/d)
6%mf_mkdir(&root/a/e)
7%mf_mkdir(&root/a/e/f)
8LIBNAME test '&root/a/e/f';
9DATA test.ds1;
10 x=1;
11RUN;
2 Code Block
MACRO mp_dirlist / MACRO mp_assert
Explanation :
This block tests the `%mp_dirlist` macro by asking it to list all directory levels under `&root` (with `maxdepth=MAX`). The `%mf_nobs` macro is used to count observations in the output dataset `work.myTable`. `%mp_assert` verifies that the number of observations is equal to 6, which corresponds to the total number of directories created.
Copied!
1%mp_dirlist(path=&root, outds=myTable, maxdepth=MAX)
2 
3%mp_assert(
4 iftrue=(%mf_nobs(work.mytable)=6),
5 desc=All levels returned,
6 outds=work.test_results
7)
3 Code Block
MACRO mp_dirlist / MACRO mp_assert
Explanation :
This test calls `%mp_dirlist` with `maxdepth=2`, meaning that only directories from the first two depth levels (from `&root`) should be listed. `%mp_assert` is used to confirm that the `work.myTable2` dataset contains 5 observations, thus validating the depth limitation.
Copied!
1%mp_dirlist(path=&root, outds=myTable2, maxdepth=2)
2 
3%mp_assert(
4 iftrue=(%mf_nobs(work.mytable2)=5),
5 desc=Top two levels returned,
6 outds=work.test_results
7)
4 Code Block
MACRO mp_dirlist / MACRO mp_assert
Explanation :
This code block tests `%mp_dirlist` with `maxdepth=0`, indicating that only the specified directory (`&root`) itself should be returned. `%mp_assert` verifies that the `work.myTable3` dataset contains 2 observations, which is the expected behavior for this listing depth (generally the directory itself plus an entry for metadata or the parent).
Copied!
1%mp_dirlist(path=&root, outds=work.myTable3, maxdepth=0)
2 
3%mp_assert(
4 iftrue=(%mf_nobs(work.mytable3)=2),
5 desc=Top level returned,
6 outds=work.test_results
7)
5 Code Block
MACRO mp_dirlist / MACRO mp_assert
Explanation :
This test validates the behavior of `%mp_dirlist` when applied to an empty directory (`&root/b`). The `%mp_assert` macro is used to verify that the output dataset `work.myTable4` contains no observations, as expected for a directory with no subdirectories or listable files.
Copied!
1%mp_dirlist(path=&root/b, outds=work.myTable4)
2%mp_assert(
3 iftrue=(%mf_nobs(work.mytable4)=0),
4 desc=Empty TABLE for empty directory,
5 outds=work.test_results
6)
6 Code Block
MACRO mp_dirlist / MACRO mp_assert
Explanation :
The last code block tests `%mp_dirlist` with a non-existent directory path (`&root/notexisting`). `%mp_assert` is used to confirm that the `work.myTable5` output dataset is empty, indicating that the macro correctly handles invalid paths without generating an error and by returning an empty result.
Copied!
1%mp_dirlist(path=&root/notexisting, outds=work.myTable5)
2%mp_assert(
3 iftrue=(%mf_nobs(work.mytable5)=0),
4 desc=Empty TABLE for non-existing directory,
5 outds=work.test_results
6)
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de. This file is part of SASUnit, the unit testing framework for SAS(R) programs. For copyright information and terms of use under the GNU Lesser General Public License, see the included README.md file or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.