From 36aa466561fed034b67f388a640c94363a5c4a0a Mon Sep 17 00:00:00 2001 From: munja Date: Thu, 3 Feb 2022 19:44:21 +0100 Subject: [PATCH 1/2] fix: avoiding error and including a test --- all.sas | 39 +++++++++++++--------- base/mp_streamfile.sas | 39 +++++++++++++--------- tests/crossplatform/mp_streamfile.test.sas | 28 ++++++++++++++++ tests/testterm.sas | 10 ++++++ 4 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 tests/crossplatform/mp_streamfile.test.sas diff --git a/all.sas b/all.sas index 6857b89..0a2c71d 100644 --- a/all.sas +++ b/all.sas @@ -10803,7 +10803,10 @@ create table &outds as @param [in] contenttype= (TEXTS) Either TEXT, ZIP, CSV, EXCEL @param [in] inloc= /path/to/file.ext to be sent @param [in] inref= fileref of file to be sent (if provided, overrides `inloc`) + @param [in] iftrue= (1=1) Provide a condition under which to execute. @param [out] outname= the name of the file, as downloaded by the browser + @param [out] outref= (_webout) The destination where the file should be + streamed.

SAS Macros

@li mf_getplatform.sas @@ -10817,12 +10820,16 @@ create table &outds as contenttype=TEXT ,inloc= ,inref=0 + ,iftrue=%str(1=1) ,outname= + ,outref=_webout )/*/STORE SOURCE*/; -%let contentype=%upcase(&contenttype); -%local platform; %let platform=%mf_getplatform(); +%if not(%eval(%unquote(&iftrue))) %then %return; +%let contentype=%upcase(&contenttype); +%let outref=%upcase(&outref); +%local platform; %let platform=%mf_getplatform(); /** * check engine type to avoid the below err message: @@ -10831,7 +10838,7 @@ create table &outds as %local streamweb; %let streamweb=0; data _null_; - set sashelp.vextfl(where=(upcase(fileref)="_WEBOUT")); + set sashelp.vextfl(where=(upcase(fileref)="&outref")); if xengine='STREAM' then call symputx('streamweb',1,'l'); run; @@ -10843,7 +10850,7 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' contenttype='application/csv' contentdisp="attachment; filename=&outname"; %end; @@ -10857,14 +10864,14 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' contenttype='application/vnd.ms-excel' contentdisp="attachment; filename=&outname"; %end; %end; %else %if &contentype=HTML %then %do; %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json" + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json" contenttype="text/html"; %end; %end; @@ -10876,15 +10883,11 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' contenttype='application/text' contentdisp="attachment; filename=&outname"; %end; %end; -%else %do; - %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!; - %return; -%end; %else %if &contentype=WOFF or &contentype=WOFF2 or &contentype=TTF %then %do; %if &platform=SASMETA and &streamweb=1 %then %do; data _null_; @@ -10892,7 +10895,7 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" contenttype="font/%lowcase(&contenttype)"; %end; %end; @@ -10905,7 +10908,7 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' contenttype= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' contentdisp="attachment; filename=&outname"; @@ -10919,17 +10922,21 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip' contenttype='application/zip' contentdisp="attachment; filename=&outname"; %end; %end; +%else %do; + %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!; + %return; +%end; %if &inref ne 0 %then %do; - %mp_binarycopy(inref=&inref,outref=_webout) + %mp_binarycopy(inref=&inref,outref=&outref) %end; %else %do; - %mp_binarycopy(inloc="&inloc",outref=_webout) + %mp_binarycopy(inloc="&inloc",outref=&outref) %end; %mend mp_streamfile; diff --git a/base/mp_streamfile.sas b/base/mp_streamfile.sas index 0c98730..037cd2e 100644 --- a/base/mp_streamfile.sas +++ b/base/mp_streamfile.sas @@ -15,7 +15,10 @@ @param [in] contenttype= (TEXTS) Either TEXT, ZIP, CSV, EXCEL @param [in] inloc= /path/to/file.ext to be sent @param [in] inref= fileref of file to be sent (if provided, overrides `inloc`) + @param [in] iftrue= (1=1) Provide a condition under which to execute. @param [out] outname= the name of the file, as downloaded by the browser + @param [out] outref= (_webout) The destination where the file should be + streamed.

SAS Macros

@li mf_getplatform.sas @@ -29,12 +32,16 @@ contenttype=TEXT ,inloc= ,inref=0 + ,iftrue=%str(1=1) ,outname= + ,outref=_webout )/*/STORE SOURCE*/; -%let contentype=%upcase(&contenttype); -%local platform; %let platform=%mf_getplatform(); +%if not(%eval(%unquote(&iftrue))) %then %return; +%let contentype=%upcase(&contenttype); +%let outref=%upcase(&outref); +%local platform; %let platform=%mf_getplatform(); /** * check engine type to avoid the below err message: @@ -43,7 +50,7 @@ %local streamweb; %let streamweb=0; data _null_; - set sashelp.vextfl(where=(upcase(fileref)="_WEBOUT")); + set sashelp.vextfl(where=(upcase(fileref)="&outref")); if xengine='STREAM' then call symputx('streamweb',1,'l'); run; @@ -55,7 +62,7 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' contenttype='application/csv' contentdisp="attachment; filename=&outname"; %end; @@ -69,14 +76,14 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' contenttype='application/vnd.ms-excel' contentdisp="attachment; filename=&outname"; %end; %end; %else %if &contentype=HTML %then %do; %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json" + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json" contenttype="text/html"; %end; %end; @@ -88,15 +95,11 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt' contenttype='application/text' contentdisp="attachment; filename=&outname"; %end; %end; -%else %do; - %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!; - %return; -%end; %else %if &contentype=WOFF or &contentype=WOFF2 or &contentype=TTF %then %do; %if &platform=SASMETA and &streamweb=1 %then %do; data _null_; @@ -104,7 +107,7 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" contenttype="font/%lowcase(&contenttype)"; %end; %end; @@ -117,7 +120,7 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls' contenttype= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' contentdisp="attachment; filename=&outname"; @@ -131,17 +134,21 @@ run; run; %end; %else %if &platform=SASVIYA %then %do; - filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip' + filename &outref filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip' contenttype='application/zip' contentdisp="attachment; filename=&outname"; %end; %end; +%else %do; + %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!; + %return; +%end; %if &inref ne 0 %then %do; - %mp_binarycopy(inref=&inref,outref=_webout) + %mp_binarycopy(inref=&inref,outref=&outref) %end; %else %do; - %mp_binarycopy(inloc="&inloc",outref=_webout) + %mp_binarycopy(inloc="&inloc",outref=&outref) %end; %mend mp_streamfile; diff --git a/tests/crossplatform/mp_streamfile.test.sas b/tests/crossplatform/mp_streamfile.test.sas new file mode 100644 index 0000000..60be755 --- /dev/null +++ b/tests/crossplatform/mp_streamfile.test.sas @@ -0,0 +1,28 @@ +/** + @file + @brief Testing mp_streamfile.sas macro + @details This is tricky to test as it streams to webout. For now just + check the compilation, and for macro leakage. + +

SAS Macros

+ @li mp_assert.sas + @li mp_assertscope.sas + @li mp_streamfile.sas + +**/ + +%mp_assertscope(SNAPSHOT) + +%mp_streamfile(iftrue(1=0) + ,contenttype=csv,inloc=/some/where.txt + ,outname=myfile.txt +) + +%mp_assertscope(COMPARE) + +%mp_assert( + iftrue=(&syscc=0), + desc=Checking error condition, + outds=work.test_results +) + diff --git a/tests/testterm.sas b/tests/testterm.sas index 2e5deb7..bc0e432 100644 --- a/tests/testterm.sas +++ b/tests/testterm.sas @@ -2,8 +2,18 @@ @file @brief term file for tests +

SAS Macros

+ @li mp_assert.sas + **/ +%mp_assert( + iftrue=(&syscc=0), + desc=Checking final error condition, + outds=work.test_results +) + + %webout(OPEN) %webout(OBJ, TEST_RESULTS) %webout(CLOSE) \ No newline at end of file From c5a8bc745de55620ce4f3e56c3acb8867a3bc274 Mon Sep 17 00:00:00 2001 From: munja Date: Thu, 3 Feb 2022 19:58:12 +0100 Subject: [PATCH 2/2] chore: fix test --- tests/crossplatform/mp_streamfile.test.sas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/crossplatform/mp_streamfile.test.sas b/tests/crossplatform/mp_streamfile.test.sas index 60be755..71f2ca1 100644 --- a/tests/crossplatform/mp_streamfile.test.sas +++ b/tests/crossplatform/mp_streamfile.test.sas @@ -13,7 +13,7 @@ %mp_assertscope(SNAPSHOT) -%mp_streamfile(iftrue(1=0) +%mp_streamfile(iftrue=(1=0) ,contenttype=csv,inloc=/some/where.txt ,outname=myfile.txt )