# -*-Perl-*-
# $Id: trace,v 1.9 2005/12/24 16:17:52 rockyb Exp $
$description = "The following checks tracing.\n";

$details = "We test line tracing, showing a target stack, line numbers\n"
         . "when we have multiple commands in a target\n";

# TEST #1
# -------

open(MAKEFILE,"> $makefile");

# The Contents of the MAKEFILE ...

print MAKEFILE <<\EOF;
# Test #1 simple tracing
all: foo

foo:
	@echo FOO here
EOF
# END of Contents of MAKEFILE

close(MAKEFILE);

&run_make_with_options($makefile,"--trace --basename-filenames",
                       &get_logfile);

# Create the answer to what should be produced by this Makefile

$answer = "Reading makefiles...
Updating goal targets....
 work/debugger/trace.mk:2	File `all' does not exist.
   work/debugger/trace.mk:4	File `foo' does not exist.
  work/debugger/trace.mk:4	Must remake target `foo'.
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
work/debugger/trace.mk:5: foo
echo FOO here
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+ echo FOO here
FOO here
  work/debugger/trace.mk:4	Successfully remade target file `foo'.
work/debugger/trace.mk:2	Must remake target `all'.
work/debugger/trace.mk:2	Successfully remade target file `all'.
";

# COMPARE RESULTS

&compare_output($answer,&get_logfile(1));

# TEST #2 Test #2 tracing with multiple commands and show call stack
# dump
# -------

open(MAKEFILE,"> $makefile");

# The Contents of the MAKEFILE ...

print MAKEFILE <<\EOF;
# Test #2 tracing with multiple commands and show call stack dump
all: foo
foo:
	@echo hi > /dev/null
	@exit 5
EOF
# END of Contents of MAKEFILE

close(MAKEFILE);

# We used option --trace in test 1, use short option name -x this time.
&run_make_with_options($makefile, "-x -i --basename-filenames",
		       &get_logfile);

# Create the answer to what should be produced by this Makefile

$answer = "Reading makefiles...
Updating goal targets....
 work/debugger/trace.mk:2	File `all' does not exist.
   work/debugger/trace.mk:3	File `foo' does not exist.
  work/debugger/trace.mk:3	Must remake target `foo'.
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
work/debugger/trace.mk:4: foo
echo hi > /dev/null
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+ echo hi
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
work/debugger/trace.mk:5: foo
exit 5
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+ exit 5
work/debugger/trace.mk:5: [foo] Error 5 (ignored)

#0  foo at work/debugger/trace.mk:5
#1  all at work/debugger/trace.mk:2
  work/debugger/trace.mk:3	Successfully remade target file `foo'.
work/debugger/trace.mk:2	Must remake target `all'.
work/debugger/trace.mk:2	Successfully remade target file `all'.
";

# COMPARE RESULTS

&compare_output($answer,&get_logfile(1));

# TEST #3 Test #3 "tracing" vs "silent"
# dump
# -------

open(MAKEFILE,"> $makefile");

# The Contents of the MAKEFILE ...

print MAKEFILE <<\EOF;
# Test #3 "tracing" versus "silent"
.SILENT: foo

all: foo

foo:
	if [ -n FOO ] ; \
	then echo "test okay"; fi
EOF
# END of Contents of MAKEFILE

close(MAKEFILE);

# Shells vary in output. For example, for echo "test okay"
# some give 'test okay' while other omit the quotes.
# So we'll issue the command that make should issue
# and compare against that.

# Pick up what the name of the shell is. Perhaps this
# should done more globally.

my @lines = `echo 'all: ;\@echo \$(SHELL)' | $make_path -f -`;
if (@lines < 1) {
  return -1;
}

chomp @lines;
# Well, actually we want shell plus tracing.
$shell = "$lines[0] -x";

# Compute what the output should be for this shell.
my $foo_lines = "if [ -n FOO ] ; then echo \"test okay\"; fi";
my $foo_output = `echo '$foo_lines' | $shell 2>&1`;
chomp $foo_output;

# We used option --trace in test 1, use short option name -x this time.
&run_make_with_options($makefile, "-x -i --basename-filenames",
		       &get_logfile);

# Create the answer to what should be produced by this Makefile

$answer = "Reading makefiles...
Updating goal targets....
 work/debugger/trace.mk:4	File `all' does not exist.
   work/debugger/trace.mk:6	File `foo' does not exist.
  work/debugger/trace.mk:6	Must remake target `foo'.
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
work/debugger/trace.mk:7: foo
if [ -n FOO ] ; \\
then echo \"test okay\"; fi
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$foo_output
  work/debugger/trace.mk:6	Successfully remade target file `foo'.
work/debugger/trace.mk:4	Must remake target `all'.
work/debugger/trace.mk:4	Successfully remade target file `all'.
";

# COMPARE RESULTS

&compare_output($answer,&get_logfile(1));
