put11.gms : Put_Utility without file handle

Description

Before GAMS 25.0 the Put_Utility statement required an open file handle. This
test makes sure, that the different utilities also work without one.

Contributor: Lutz Westermann, December 2017


Small Model of Type : GAMS


Category : GAMS Test library


Main file : put11.gms

$title Put_Utility without file handle (PUT11,SEQ=741)

$ontext
Before GAMS 25.0 the Put_Utility statement required an open file handle. This
test makes sure, that the different utilities also work without one.

Contributor: Lutz Westermann, December 2017
$offtext


$call rm -f dummy.txt shell.txt mySave.g00  myxSave.g00

put_utility 'exec' / 'touch dummy.txt';
execute '=test -e dummy.txt';
abort$(errorlevel) 'Problem with "exec"';

$echo shell > shellWant.txt
put_utility 'shell' / 'echo shell > shell.txt';
execute "diff -bw shellWant.txt shell.txt";
abort$(errorlevel) 'Problem with "shell"';

put_utility 'save' / 'mySave';
execute '=test -e mySave.g00';
abort$(errorlevel) 'Problem with "save"';

put_utility 'xsave' / 'myxSave';
execute '=test -e myxSave.g00';
abort$(errorlevel) 'Problem with "xsave"';

Set       j          / 2005*2007 /;
Parameter randoms(j)              ;
Scalar    random                  ;

loop(j,
   put_utility 'gdxOut' / 'data' j.tl:0;
   random = uniform(0,1);
   randoms(j) = random;
   execute_unload random;
);

loop(j,
   put_utility 'gdxIn' / 'data' j.tl:0 ;
   execute_load random;
   abort$(randoms(j)<>random) 'Problem with "gdxout/gdxin"';
);

* These are hard to test for correctness, just make sure that nothing crashes
put_utility 'click' / 'shell.txt' ;
put_utility 'log' / 'Hi log!';
put_utility 'msg' / 'Hi lst!';
put_utility 'msgLog' / 'Hi lstlog!';
put_utility 'title' / 'New Title';
* The default for putPW is pretty high. This ensures, that this actually works and not the classic default of 255 is applied
put_utility 'log' / '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
                    '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
                    '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
$ifi not %system.fileSys%==UNIX
   put_utility 'winMsg' / 'New Title' / "Some Message";

abort$(execerror<>0) 'There should be no errors until here';

* These actually need an open file handle, but not for put_utility itself, but for the action perfomed
put_utility 'ren' / 'one.txt';
abort$(execerror=0) 'This "ren" should have failed';
execerror = 0;

put_utility 'inc' / 'external.txt' ;
abort$(execerror=0) 'This "inc" should have failed';
execerror = 0;

put_utility 'incMsg' / 'externalX.txt' ;
abort$(execerror=0) 'This "incMsg" should have failed';
execerror = 0;

put_utility 'incLog' / 'externalX.txt' ;
abort$(execerror=0) 'This "incLog" should have failed';
execerror = 0;

put_utility 'incMsgLog' / 'externalX.txt' ;
abort$(execerror=0) 'This "incMsgLog" should have failed';
execerror = 0;

set slv / cplex, conopt /;
variable v;equation e; e.. v=e=0; model m /e/;
loop(slv,
   put_utility 'solver' / 'lp' / slv.tl:0;
   solve m us lp min v;
   if (sameas(slv,'cplex'),
      put_utility 'shell' / 'echo "' system.SString '" | grep CPLEX  > %system.nullFile%';
   else
      put_utility 'shell' / 'echo "' system.SString '" | grep CONOPT > %system.nullFile%';
   );
   abort$errorlevel 'requested solver name not in audit string';   
);

singleton set someText / 1 /;
put_utility 'assignText' / 'someText' / 'We assign some text to the label';
put_utility 'shell' / 'echo "' someText.te(someText) '" | grep "We assign some text to the label" > %system.nullFile%';
abort$errorlevel 'text expected and stored are different';

put_utility 'setenv' / 'SOMETEXT' / 'We assign some text to the environment variable';
put_utility 'shell.checkErrorLevel' / 'echo "' sysEnv.SOMETEXT '" | grep "We assign some text to the environment variable" > %system.nullFile%';
put_utility 'dropenv' / 'SOMETEXT';
* This assumes stringChk=0
put_utility 'shell.checkErrorLevel' / 'echo "' sysEnv.SOMETEXT '" | grep "sysEnv.SOMETEXT" > %system.nullFile%';

$echo zero > zeroWant.txt
$echo one  > oneWant.txt
file fx /'zero.txt'/; putclose fx 'zero';
put_utility 'ren' / 'one.txt'; putclose 'one';
execute "diff -bw zeroWant.txt zero.txt";
abort$(errorlevel) 'Problem with "ren zero"';
execute "diff -bw oneWant.txt one.txt";
abort$(errorlevel) 'Problem with "ren one"';

$echo Here we include content from an external file: > includedWant.txt
$echo I am the external content                     >> includedWant.txt
$echo I am the external content > external.txt
put_utility 'ren' / 'included.txt';
put "Here we include content from an external file:";
put_utility 'inc' / 'external.txt' ; putclose;
execute "diff -bw includedWant.txt included.txt";
abort$(errorlevel) 'Problem with "inc"';

* There was a bug in 25.0.1 which caused this to fail
file f1, f2;
put f1                 '1';
put f2                 '2';
put_utility f1 'log' / '3';

* There was a bug in 25.0.2 which caused this to fail
$echo trnsport > trnsportWant.txt
File fy /'trnsport.txt'/; fy.lCase = 1;

Singleton Set s / TRNSPORT /;
put_utility fy 'log' / s.tl:0;
putClose               s.tl:0;
execute "diff -bw trnsportWant.txt trnsport.txt";
abort$(errorlevel) 'Problem with "Trnsport lCase"';

$onechoV > inclogmsg.gms
File recall /recall.txt/;
putclose recall "I am the external content." / "I go over more than one line.";

display 'before the include';
put_utility 'inc%cmd%' / 'recall.txt' ;
display 'after the include';
$offecho
$onechoV > inclogmsgWant.txt
--- Start of include of file recall.txt
I am the external content.
I go over more than one line.
--- End of include of file recall.txt
$offecho

execute.checkErrorLevel "gams inclogmsg.gms --cmd=log lo=2"
execute.checkErrorLevel 'awk "/Start of include of file recall.txt/,/End of include of file recall.txt/" inclogmsg.lst > inclogmsgHave.txt';
execute 'diff -bw inclogmsgHave.txt inclogmsgWant.txt > %system.nullfile%';
abort$(not errorlevel) 'Problem with inclogmsg case log (lst)';
execute.checkErrorLevel 'awk "/Start of include of file recall.txt/,/End of include of file recall.txt/" inclogmsg.log > inclogmsgHave.txt';
execute 'diff -bw inclogmsgHave.txt inclogmsgWant.txt > %system.nullfile%';
abort$(errorlevel) 'Problem with inclogmsg case log (log)';
execute.checkErrorLevel "gams inclogmsg.gms --cmd=msg lo=2"
execute.checkErrorLevel 'awk "/Start of include of file recall.txt/,/End of include of file recall.txt/" inclogmsg.lst > inclogmsgHave.txt';
execute 'diff -bw inclogmsgHave.txt inclogmsgWant.txt > %system.nullfile%';
abort$(errorlevel) 'Problem with inclogmsg case msg (lst)';
execute.checkErrorLevel 'awk "/Start of include of file recall.txt/,/End of include of file recall.txt/" inclogmsg.log  > inclogmsgHave.txt';
execute 'diff -bw inclogmsgHave.txt inclogmsgWant.txt > %system.nullfile%';
abort$(not errorlevel) 'Problem with inclogmsg case msg (log)';
execute.checkErrorLevel "gams inclogmsg.gms --cmd=msglog lo=2"
execute.checkErrorLevel 'awk "/Start of include of file recall.txt/,/End of include of file recall.txt/" inclogmsg.lst > inclogmsgHave.txt';
execute 'diff -bw inclogmsgHave.txt inclogmsgWant.txt > %system.nullfile%';
abort$(errorlevel) 'Problem with inclogmsg case msglog (lst)';
execute.checkErrorLevel 'awk "/Start of include of file recall.txt/,/End of include of file recall.txt/" inclogmsg.log > inclogmsgHave.txt';
execute 'diff -bw inclogmsgHave.txt inclogmsgWant.txt > %system.nullfile%';
abort$(errorlevel) 'Problem with inclogmsg case msglog (log)';

* Test stdOut and stderr
$onEchoV > pu.gms
put_utility 'std%outOrErr%' / 'Teststring';
$offEcho
$echo Teststring > expected.txt
$call gams pu.gms lo=0 --outOrErr=out > test.txt
$call.checkErrorLevel diff -q expected.txt test.txt
$call gams pu.gms lo=0 --outOrErr=err 2> test.txt
$call.checkErrorLevel diff -q expected.txt test.txt