-- Note that a significant part of the logic for this test lives in
-- extra_hooks.lua

mtn_setup()

get("extra_hooks.lua")
get("utf8.txt")
get("euc-jp.txt")

addfile("testfile", "blah blah")

set_env("CHARSET", "euc-jp")
check(mtn("attr", "set", "testfile", "testattr", readfile("euc-jp.txt")),
      0, false, false)

-- First, we do a commit that should _fail_, because validate_commit_message
-- fires.  but it should trigger a writeout of the log message to _MTN/log
f = io.open("fail_comment", "w")
f:close()
check(exists("fail_comment"))
check(mtn("commit", "--rcfile", "extra_hooks.lua"), 1, false, true)
check(qgrep("EDIT: BASE GOOD", "stderr"))
check(qgrep("EDIT: MSG NONESUCH", "stderr"))
check(qgrep("VALIDATE: MSG GOOD", "stderr"))
check(qgrep("VALIDATE: REV GOOD", "stderr"))

-- Now the localized message should have been written out to _MTN/log
check(readfile("euc-jp.txt") == readfile("_MTN/log"))

-- and if we commit again, both hooks should succeed
remove("fail_comment")
check(mtn("commit", "--rcfile", "extra_hooks.lua"), 0, false, true)
check(qgrep("EDIT: BASE GOOD", "stderr"))
check(qgrep("EDIT: MSG GOOD", "stderr"))
check(qgrep("VALIDATE: MSG GOOD", "stderr"))
check(qgrep("VALIDATE: REV GOOD", "stderr"))

check(mtn("log"), 0, true, false)
eucjp = string.gsub(readfile("euc-jp.txt"), "\n", "")
check(qgrep(eucjp, "stdout"))

-- and if we look at the cert, it should be in unicode
rev = base_revision()
check(mtn("automate", "certs", rev), 0, true, false)
utf8 = string.gsub(readfile("utf8.txt"), "\n", "")
check(qgrep(utf8, "stdout"))
