#
# Copyright (c) 2011 D. Richard Hipp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Simplified BSD License (also
# known as the "2-Clause License" or "FreeBSD License".)
#
# This program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose.
#
# Author contact information:
# drh@hwaci.com
# http://www.hwaci.com/drh/
#
############################################################################
#
# TH1 Hooks
#
fossil test-th-eval "hasfeature th1Hooks"
if {$::RESULT ne "1"} then {
puts "Fossil was not compiled with TH1 hooks support."; return
}
###############################################################################
set env(TH1_ENABLE_HOOKS) 1; # TH1 hooks must be enabled for this test.
###############################################################################
proc fossil_th1_hook_http { repository url } {
set suffix [appendArgs [pid] - [clock seconds] .txt]
set inFileName [file join $::tempPath [appendArgs test-http-in- $suffix]]
set outFileName [file join $::tempPath [appendArgs test-http-out- $suffix]]
set data [subst [read_file [file join $::testdir th1-hooks-input.txt]]]
write_file $inFileName $data
fossil http $repository $inFileName $outFileName 127.0.0.1
set result [expr {[file exists $outFileName] ? [read_file $outFileName] : ""}]
if {1} then {
catch {file delete $inFileName}
catch {file delete $outFileName}
}
return $result
}
proc first_data_line {} {
return [lindex [split [string trim $::RESULT] \r\n] 0]
}
proc second_data_line {} {
return [lindex [split [string trim $::RESULT] \r\n] 1]
}
proc third_data_line {} {
return [lindex [split [string trim $::RESULT] \r\n] 2]
}
proc last_data_line {} {
return [lindex [split [string trim $::RESULT] \r\n] end]
}
proc next_to_last_data_line {} {
return [lindex [split [string trim $::RESULT] \r\n] end-1]
}
###############################################################################
set testTh1Setup {
proc initialize_hook_log {} {
if {![info exists ::hook_log]} {
set ::hook_log ""
}
}
proc append_hook_log { args } {
initialize_hook_log
if {[string length $::hook_log] > 0} {
set ::hook_log "$::hook_log "
}
for {set i 0} {$i < [llength $args]} {set i [expr {$i + 1}]} {
set ::hook_log $::hook_log[lindex $args $i]
}
}
proc emit_hook_log {} {
initialize_hook_log
html "\n<h1><b>$::hook_log</b></h1>\n"
}
proc command_hook {} {
append_hook_log command_hook " " $::cmd_name
if {$::cmd_name eq "test1"} {
puts [repository]; continue
} elseif {$::cmd_name eq "test2"} {
error "unsupported command"
} elseif {$::cmd_name eq "test3"} {
emit_hook_log
break "TH_BREAK return code"
} elseif {$::cmd_name eq "test4"} {
emit_hook_log
return -code 2 "TH_RETURN return code"
} elseif {$::cmd_name eq "timeline"} {
if {$::cmd_args eq "custom"} {
emit_hook_log
return "custom timeline"
} else {
emit_hook_log
error "unsupported timeline"
}
}
}
proc command_notify {} {
append_hook_log command_notify " " $::cmd_name
emit_hook_log
}
proc webpage_hook {} {
append_hook_log webpage_hook " " $::web_name
if {$::web_name eq "test1"} {
puts [repository]; continue
}
}
proc webpage_notify {} {
append_hook_log webpage_notify " " $::web_name
emit_hook_log
}
}
###############################################################################
set data [fossil info]
regexp -line -- {^repository: (.*)$} $data dummy repository
if {[string length $repository] == 0 || ![file exists $repository]} then {
error "unable to locate repository"
}
###############################################################################
saveTh1SetupFile; writeTh1SetupFile $testTh1Setup
###############################################################################
fossil timeline custom; # NOTE: Bad "WHEN" argument.
test th1-cmd-hooks-1a {[string map [list \r\n \n] [string trim $RESULT]] eq {<h1><b>command_hook timeline</b></h1>
ERROR: unsupported timeline
+++ no more data (0) +++
<h1><b>command_hook timeline command_notify timeline</b></h1>}}
###############################################################################
fossil timeline
test th1-cmd-hooks-2a {[first_data_line] eq {<h1><b>command_hook timeline</b></h1>}}
test th1-cmd-hooks-2b {[second_data_line] eq {ERROR: unsupported timeline}}
test th1-cmd-hooks-2c {[regexp -- {=== \d{4}-\d{2}-\d{2} ===} [third_data_line]]}
test th1-cmd-hooks-2d {[last_data_line] eq {<h1><b>command_hook timeline command_notify timeline</b></h1>}}
###############################################################################
fossil test1
test th1-custom-cmd-1a {[next_to_last_data_line] eq $repository}
test th1-custom-cmd-1b {[last_data_line] eq {<h1><b>command_hook test1 command_notify test1</b></h1>}}
###############################################################################
fossil test2
test th1-custom-cmd-2a {[first_data_line] eq {ERROR: unsupported command}}
###############################################################################
fossil test3
test th1-custom-cmd-3a {[string trim $RESULT] eq {<h1><b>command_hook test3</b></h1>}}
###############################################################################
fossil test4
test th1-custom-cmd-4a {[string trim $RESULT] eq {<h1><b>command_hook test4</b></h1>}}
###############################################################################
set RESULT [fossil_th1_hook_http $repository /timeline]
test th1-web-hooks-1a {[regexp {<title>Fossil: Timeline</title>} $RESULT]}
test th1-web-hooks-1b {[regexp {<h1><b>command_hook http webpage_hook timeline webpage_notify timeline</b></h1>} $RESULT]}
###############################################################################
set RESULT [fossil_th1_hook_http $repository /test1]
test th1-custom-web-1a {[next_to_last_data_line] eq $repository}
test th1-custom-web-1b {[last_data_line] eq {<h1><b>command_hook http webpage_hook test1 webpage_notify test1</b></h1>}}
###############################################################################
restoreTh1SetupFile