###############################################################################
#
# exec.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################

unset -nocomplain test_log_if
set test_log_if [expr {[info exists test_log] ? $test_log : ""}]

###############################################################################

unset -nocomplain comSpec
set comSpec [expr {[info exists env(ComSpec)] ? $env(ComSpec) : ""}]

###############################################################################

runTest {test exec-1.1 {cancel exec with events, kill process} -setup  [subst \
    -nocommands -nobackslashes {
  #
  # NOTE: Set the channel to use for test output, if necessary.
  #
  if {![info exists test_channel]} then {
    set test_channel {$test_channel}
  }

  #
  # NOTE: Check for and load the Eagle test package, if necessary.
  #
  if {[catch {package present Eagle.Test}]} then {
    package require Eagle.Test
  }

  #
  # NOTE: Set the log file name to use for test output, if necessary.
  #
  if {![info exists test_log]} then {
    set test_log {$test_log_if}
  }

  #
  # NOTE: Set the operating system command shell to use for the test.
  #
  if {![info exists comSpec]} then {
    set comSpec {$comSpec}
  }

  set savedFlags [after flags]
  after flags =Immediate
}] -body {
  set pid 0
  set code [catch {
    after 1000 [list interp cancel]
    testExec $comSpec [list -nocapture -processid pid]
  } msg]

  set y [list $code $msg [expr {$pid != 0 && $pid == [info previouspid] ? \
      [kill -force $pid] : "cannot kill process, wrong pid"}]]
} -cleanup {
  catch {kill -force [info previouspid]}
  catch {info previouspid true}

  after flags =$savedFlags
  unset -nocomplain y code pid msg savedFlags
} -constraints {eagle windows testExec interactive tip285} -isolationLevel \
Interpreter -match regexp -result [string map [list cmd.exe [appendArgs \
.*\\\\ [file tail $comSpec]]] {^1 \{eval canceled\} \{(killed \{\d+\
\{cmd.exe\}\})|(killed \d+)\}$}]}

###############################################################################

unset -nocomplain comSpec

###############################################################################

unset -nocomplain test_log_if

###############################################################################

runTest {test exec-1.2 {-debug option} -setup {
  unset -nocomplain output pid
} -body {
  set pid 0
  list [catch {
    testExec fossil [list -debug -processid pid] version
  } output] $pid [string trim $output]
} -cleanup {
  catch {info previouspid true}

  unset -nocomplain output pid
} -constraints {eagle testExec fossil_version} -match regexp -result \
{^0 \d+ \{This is fossil version \d+\.\d+ \[[0-9a-f]{10}\] \d{4}-\d{2}-\d{2}\
\d{2}:\d{2}:\d{2} UTC\}$}}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
