33.
How to copy executed commands output to the journal
Question
I have some test cases that use the C API.
As part of my test program, I need to execute a shell script
using
system().
I would like the output of shell script to go into the journal file.
I tried setting
TET_OUTPUT_CAPTURE=True
in my
tetexec.cfg
file and ran the test.
It just hangs.
Is there anything else that I need to do to solve this problem?
Is there any other way other than by setting
TET_OUTPUT_CAPTURE?
Answer
There are a couple of ways that you can do this -
a simple way where
tcc
does the output capture for you, and a more
complicated way where you do the output capture within the test case.
Method 1 - have tcc do the work for you
Recall that the value of
TET_OUTPUT_CAPTURE
provides a default value for
TET_API_COMPLIANT.
The value of
TET_API_COMPLIANT
must be True if your test case uses a
TETware API, otherwise
tcc
processes your test case as a non
API-conforming test case.
So if you want to run an API-conforming test case with output capture
mode enabled, you must set both of these variables explicitly, thus:
TET_OUTPUT_CAPTURE=true
TET_API_COMPLIANT=true
When you do this,
tcc
captures all the output from each test case and
inserts it in the journal before any of the API-generated output from
the test case.
The API doesn't indicate which section of the output comes from each
test purpose function.
Method 2 - do the output capture within the test case
If you want output from your shell script to appear in the journal
between the appropriate TP Start and TP Result lines, you will need to
redirect your shell script's
stdout
and
stderr
to a file, then collect
the contents of this file in your test code and use
tet_infoline()
to
print it to the journal.
When you do this, you don't need to set
TET_OUTPUT_CAPTURE=true.
For example, the following function will do this on a UNIX system:
int system_with_capture(cmd)
char *cmd;
{
static char template[] = "/tmp/capXXXXXX";
char capfile[sizeof template];
char buf[BUFSIZ];
FILE *fp;
char *p;
int rc;
(void) strcpy(capfile, template);
(void) mktemp(capfile);
(void) sprintf(buf, "%s > %s 2>&1", cmd, capfile);
(void) unlink(capfile);
if ((rc = system(buf)) == 127 || rc == -1) return(rc);
if ((fp = fopen(capfile, "r")) == (FILE *) 0)
tet_printf("can't open output capture file %s, \
errno = %d", capfile, errno);
else {
tet_printf("output from \"%s\":", cmd);
tet_infoline("------------------------------------");
while (fgets(buf, sizeof buf, fp) != (char *) 0) {
for (p = buf; *p; p++)
if (*p == '\n')
*p = '\0';
tet_infoline(buf);
}
tet_infoline("------------------------------------");
(void) fclose(fp);
(void) unlink(capfile);
}
return(rc);
}
Which method you choose depends on how important it is for the captured
output to be identified with a particular test purpose function.
See also
-
"Execute mode processing'' and
"Configuration variables which modify TETware's operation'' in
the TETware Programmers Guide.
-
"Making journal entries'' in each chapter that describes API
functions in the TETware Programmers Guide.
|