summaryrefslogtreecommitdiff
path: root/test/fake-editor.tcl
diff options
context:
space:
mode:
authorcashmere <cashmere@autistici.org>2026-09-04 09:05:24 +0200
committercashmere <cashmere@autistici.org>2026-09-04 09:05:24 +0200
commit4f49b16079b27d7ce633619e4cddfdf2bffbc952 (patch)
treec296c1e7f3c3e2f6859c6d3a1b1b208779b89679 /test/fake-editor.tcl
downloadrelic-4f49b16079b27d7ce633619e4cddfdf2bffbc952.tar.gz
initial commitHEADmaster
Diffstat (limited to 'test/fake-editor.tcl')
-rw-r--r--test/fake-editor.tcl98
1 files changed, 98 insertions, 0 deletions
diff --git a/test/fake-editor.tcl b/test/fake-editor.tcl
new file mode 100644
index 0000000..a363fb7
--- /dev/null
+++ b/test/fake-editor.tcl
@@ -0,0 +1,98 @@
+#
+# Copyright (c) 2016 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/
+#
+############################################################################
+#
+# This is a fake text editor for use by tests. To customize its behavior,
+# set the FAKE_EDITOR_SCRIPT environment variable prior to evaluating this
+# script file. If FAKE_EDITOR_SCRIPT environment variable is not set, the
+# default behavior will be used. The default behavior is to append the
+# process identifier and the current time, in seconds, to the file data.
+#
+
+if {![info exists argv] || [llength $argv] != 1} {
+ error "Usage: \"[info nameofexecutable]\" \"[info script]\" <fileName>"
+}
+
+###############################################################################
+
+proc makeBinaryChannel { channel } {
+ fconfigure $channel -encoding binary -translation binary
+}
+
+proc readFile { fileName } {
+ set channel [open $fileName RDONLY]
+ makeBinaryChannel $channel
+ set result [read $channel]
+ close $channel
+ return $result
+}
+
+proc writeFile { fileName data } {
+ set channel [open $fileName {WRONLY CREAT TRUNC}]
+ makeBinaryChannel $channel
+ puts -nonewline $channel $data
+ close $channel
+ return ""
+}
+
+###############################################################################
+
+set fileName [lindex $argv 0]
+
+if {[regexp {^CYGWIN} $::tcl_platform(os)]} {
+ # Under Cygwin, we get a Windows path but must access using the unix path.
+ set fileName [exec cygpath --unix $fileName]
+}
+
+if {[file exists $fileName]} {
+ set data [readFile $fileName]
+} else {
+ set data ""
+}
+
+###############################################################################
+
+if {[info exists env(FAKE_EDITOR_SCRIPT)]} {
+ #
+ # NOTE: If an error is caught while evaluating this script, catch
+ # it and return, which will also skip writing the (possibly
+ # modified) content back to the original file.
+ #
+ set script $env(FAKE_EDITOR_SCRIPT)
+ set code [catch $script error]
+
+ if {$code != 0} {
+ if {[info exists env(FAKE_EDITOR_VERBOSE)]} {
+ if {[info exists errorInfo]} {
+ puts stdout "ERROR ($code): $errorInfo"
+ } else {
+ puts stdout "ERROR ($code): $error"
+ }
+ }
+
+ return
+ }
+} else {
+ #
+ # NOTE: The default behavior is to append the process identifier
+ # and the current time, in seconds, to the file data.
+ #
+ append data " " [pid] " " [clock seconds]
+}
+
+###############################################################################
+
+writeFile $fileName $data