From 40b513a9e3e5185e419fd15ce04d61b096f94e3b Mon Sep 17 00:00:00 2001 From: Allan Bowe Date: Wed, 4 Aug 2021 22:00:18 +0300 Subject: [PATCH] feat: new mf_getapploc macro --- base/mf_getapploc.sas | 61 ++++++++++++++++++++++++++++++++ tests/base/mf_getapploc.test.sas | 41 +++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 base/mf_getapploc.sas create mode 100644 tests/base/mf_getapploc.test.sas diff --git a/base/mf_getapploc.sas b/base/mf_getapploc.sas new file mode 100644 index 0000000..4ff64f7 --- /dev/null +++ b/base/mf_getapploc.sas @@ -0,0 +1,61 @@ +/** + @file + @brief Returns the appLoc from the _program variable + @details When working with SASjs apps, web services / tests / jobs are always + deployed to a root (app) location in the SAS logical folder tree. + + When building apps for use in other environments, you do not necessarily know + where the backend services will be deployed. Therefore a function like this + is handy in order to dynamically figure out the appLoc, and enable other + services to be connected by a relative reference. + + SASjs apps always have the same immediate substructure (one or more of the + following): + + @li /data + @li /jobs + @li /services + @li /tests/jobs + @li /tests/services + @li /tests/macros + + This function works by testing for the existence of any of the above in the + automatic _program variable, and returning the part to the left of it. + + Usage: + + %put %mf_getapploc(&_program) + + %put %mf_getapploc(/some/location/services/admin/myservice); + %put %mf_getapploc(/some/location/jobs/extract/somejob/); + %put %mf_getapploc(/some/location/tests/jobs/somejob/); + + + @author Allan Bowe +**/ + +%macro mf_getapploc(pgm); +%if "&pgm"="" %then %do; + %put &sysmacroname: No value provided; + %return; +%end; +%local root; + +/** + * move up two levels to avoid matches on subfolder or service name + */ +%let root=%substr(&pgm,1,%length(&pgm)-%length(%scan(&pgm,-1,/))-1); +%let root=%substr(&root,1,%length(&root)-%length(%scan(&root,-1,/))-1); + +%if %index(&root,/tests/) %then %do; + %let root=%substr(&root,1,%index(&root,/tests/)-1); +%end; +%else %if %index(&root,/services) %then %do; + %let root=%substr(&root,1,%index(&root,/services)-1); +%end; +%else %if %index(&root,/jobs) %then %do; + %let root=%substr(&root,1,%index(&root,/jobs)-1); +%end; +%else %put &sysmacroname: Could not find an app location from &pgm; + &root +%mend mf_getapploc ; \ No newline at end of file diff --git a/tests/base/mf_getapploc.test.sas b/tests/base/mf_getapploc.test.sas new file mode 100644 index 0000000..0c5871a --- /dev/null +++ b/tests/base/mf_getapploc.test.sas @@ -0,0 +1,41 @@ +/** + @file + @brief Testing mf_getapploc macro + +

SAS Macros

+ @li mf_getapploc.sas + @li mp_assert.sas + +**/ + +%mp_assert( + iftrue=( + "%mf_getapploc(/some/loc/tests/services/x/service)"="/some/loc" + ), + desc=Checking test appLoc matches, + outds=work.test_results +) + +%mp_assert( + iftrue=( + "%mf_getapploc(/some/loc/tests/services/tests/service)"="/some/loc" + ), + desc=Checking nested services appLoc matches, + outds=work.test_results +) + +%mp_assert( + iftrue=( + "%mf_getapploc(/some/area/services/admin/service)"="/some/area" + ), + desc=Checking services appLoc matches, + outds=work.test_results +) + +%mp_assert( + iftrue=( + "%mf_getapploc(/some/area/jobs/jobs/job)"="/some/area" + ), + desc=Checking jobs appLoc matches, + outds=work.test_results +) \ No newline at end of file