From 7406288d79a002ec3ab6145bc693be5c065c96c3 Mon Sep 17 00:00:00 2001 From: Allan Bowe Date: Thu, 16 Sep 2021 18:30:17 +0100 Subject: [PATCH] fix: mp_gsubfile() now works with multiline files (and we have a multiline test to go with it) --- all.sas | 6 ++--- build.py | 2 +- lua/gsubfile.lua | 2 +- lua/ml_gsubfile.sas | 4 +-- lua/ml_json.sas | 2 +- tests/crossplatform/mp_gsubfile.test.sas | 33 ++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/all.sas b/all.sas index 72af851..d18573d 100644 --- a/all.sas +++ b/all.sas @@ -18831,7 +18831,7 @@ data _null_; put ' '; put '-- open file and perform the substitution '; put 'file = io.open(fpath,"r") '; - put 'fcontent = file:read() '; + put 'fcontent = file:read("*all") '; put 'file:close() '; put 'fcontent = string.gsub( '; put ' fcontent, '; @@ -18846,7 +18846,7 @@ data _null_; put 'io.close(file) '; run; -%inc "%sysfunc(pathname(work))/ml_gsubfile.lua"; +%inc "%sysfunc(pathname(work))/ml_gsubfile.lua" /source2; %mend ml_gsubfile; /** @@ -19240,7 +19240,7 @@ data _null_; put '-- JSON.LUA ENDS HERE '; run; -%inc "%sysfunc(pathname(work))/ml_json.lua"; +%inc "%sysfunc(pathname(work))/ml_json.lua" /source2; %mend ml_json; /** diff --git a/build.py b/build.py index 1ad3046..94761eb 100755 --- a/build.py +++ b/build.py @@ -22,7 +22,7 @@ for file in files: for line in infile: ml.write(" put '" + line.rstrip().replace("'","''") + " ';\n") ml.write("run;\n\n") - ml.write("%inc \"%sysfunc(pathname(work))/" + name + ".lua\";\n\n") + ml.write("%inc \"%sysfunc(pathname(work))/" + name + ".lua\" /source2;\n\n") ml.write("%mend " + name + ";\n") ml.close() diff --git a/lua/gsubfile.lua b/lua/gsubfile.lua index 004ac70..80e747b 100644 --- a/lua/gsubfile.lua +++ b/lua/gsubfile.lua @@ -10,7 +10,7 @@ end -- open file and perform the substitution file = io.open(fpath,"r") -fcontent = file:read() +fcontent = file:read("*all") file:close() fcontent = string.gsub( fcontent, diff --git a/lua/ml_gsubfile.sas b/lua/ml_gsubfile.sas index a39c122..41eef30 100644 --- a/lua/ml_gsubfile.sas +++ b/lua/ml_gsubfile.sas @@ -24,7 +24,7 @@ data _null_; put ' '; put '-- open file and perform the substitution '; put 'file = io.open(fpath,"r") '; - put 'fcontent = file:read() '; + put 'fcontent = file:read("*all") '; put 'file:close() '; put 'fcontent = string.gsub( '; put ' fcontent, '; @@ -39,6 +39,6 @@ data _null_; put 'io.close(file) '; run; -%inc "%sysfunc(pathname(work))/ml_gsubfile.lua"; +%inc "%sysfunc(pathname(work))/ml_gsubfile.lua" /source2; %mend ml_gsubfile; diff --git a/lua/ml_json.sas b/lua/ml_json.sas index cf2bf02..08f0bfb 100644 --- a/lua/ml_json.sas +++ b/lua/ml_json.sas @@ -389,6 +389,6 @@ data _null_; put '-- JSON.LUA ENDS HERE '; run; -%inc "%sysfunc(pathname(work))/ml_json.lua"; +%inc "%sysfunc(pathname(work))/ml_json.lua" /source2; %mend ml_json; diff --git a/tests/crossplatform/mp_gsubfile.test.sas b/tests/crossplatform/mp_gsubfile.test.sas index 34c5155..9695392 100644 --- a/tests/crossplatform/mp_gsubfile.test.sas +++ b/tests/crossplatform/mp_gsubfile.test.sas @@ -30,4 +30,37 @@ run; iftrue=("&str1"="&str"), desc=Check that simple replacement was successful, outds=work.test_results +) + +/** + * test 2 - replace from additional line + */ +%global str2 strcheck2 strcheck2b; +%let file2=%sysfunc(pathname(work))/file2.txt; +%let pat2=replace/me; +%let str2=with/this; +data _null_; + file "&file2"; + put 'line1';output; + put "&pat2";output; + put "&pat2";output; +run; +%mp_gsubfile(file=&file2, patternvar=pat2, replacevar=str2) +data _null_; + infile "&file2"; + input; + if _n_=2 then call symputx('strcheck2',_infile_); + if _n_=3 then call symputx('strcheck2b',_infile_); + putlog _infile_; +run; + +%mp_assert( + iftrue=("&strcheck2"="&str2"), + desc=Check that multi line replacement was successful (line2), + outds=work.test_results +) +%mp_assert( + iftrue=("&strcheck2b"="&str2"), + desc=Check that multi line replacement was successful (line3), + outds=work.test_results ) \ No newline at end of file