views:

398

answers:

3

I'd like to start writing some unit tests for my Mathematica programs and control everything from the command line with some Makefiles.

It seems like Mathematica can be run from the command line but I can't see any basic instructions on getting started with doing this on Mac OS X — has anyone done this before?


Update:

Creating a test file like this:

Print["hello"];
x := 1;
y = x+1;
z = y+1;
Print["y="ToString@y];
Print["z="ToString@z];
Quit[];

And running it with

/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt < test.m

is the closest I can get to some sort of batch processing. The output looks ugly, though; newlines are added for every line of the script!


"hello"




"y=2"

"z=3"

Is this the closest thing I can get to a script that can still output information to the console output? I'm only using Mathematica 6, but I hope that doesn't make a difference.

+2  A: 

With some experimentation, I found that /Applications/Mathematica.app/Contents/MacOS/MathKernel can be launched from the command-line. It doesn't seem to accept the usual -h or --help command-line flags, though.

las3rjock
+3  A: 

This, finally, gives output like I'd expect it to:

/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "<<test.m"

Makes sense, I suppose. Adding this to my .bash_profile allows easy execution (as in mma test.m):

mma () { /Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "<<$1" ; }

See also dreeves's mash Perl script, which may offer advantages over this approach.

Will Robertson
A: 

Thanks to Pillsy and Will Robertson for the MASH plug! Here's the relevant StackOverflow question: http://stackoverflow.com/questions/148057/calling-a-mathematica-program-from

If you don't use MASH, you may want to use the following utility functions that MASH defines. For example, the standard Print will print strings with quotation marks -- not usually what you want in scripts.

ARGV = args = Drop[$CommandLine, 4];         (* Command line args.            *)
pr = WriteString["stdout", ##]&;             (* More                          *)
prn = pr[##, "\n"]&;                         (*  convenient                   *)
perr = WriteString["stderr", ##]&;           (*   print                       *)
perrn = perr[##, "\n"]&;                     (*    statements.                *)
EOF = EndOfFile;                             (* I wish mathematica            *)
eval = ToExpression;                         (*  weren't so damn              *)
re = RegularExpression;                      (*   verbose!                    *)
read[] := InputString[""];                   (* Grab a line from stdin.       *)
doList[f_, test_] :=                         (* Accumulate list of what f[]   *)
  Most@NestWhileList[f[]&, f[], test];       (*  returns while test is true.  *)
readList[] := doList[read, #=!=EOF&];        (* Slurp list'o'lines from stdin *)

To use MASH, just grab that perl file, mash.pl, and then make your test.m like the following:

#!/usr/bin/env /path/to/mash.pl

prn["hello"];
x := 1;
y = x+1;
z = y+1;
prn["y=", y];
prn["z=", z];
dreeves