Tests: expose 'args' passed to hook scripts

- Useful to validate that all the expected arguments were passed with
  correct escaping.
- Update test/template.t with one use-case for 'args'
This commit is contained in:
Renato Alves 2015-04-06 13:22:40 +01:00
parent 09d86eb165
commit fd17a68930
4 changed files with 12 additions and 8 deletions

View file

@ -10,7 +10,6 @@ try:
except ImportError: except ImportError:
import json import json
from copy import deepcopy
from datetime import datetime from datetime import datetime
from .utils import DEFAULT_HOOK_PATH from .utils import DEFAULT_HOOK_PATH
from .exceptions import HookError from .exceptions import HookError
@ -396,7 +395,7 @@ class LoggedHook(Hook):
It should look something like this: It should look something like this:
## STDIN file ## STDIN file
% Called at 1414874711 % Called at 1414874711 with 'arg1 arg2 ...'
{... JSON received by the hook ... } {... JSON received by the hook ... }
{... more JSON ...} {... more JSON ...}
@ -424,12 +423,14 @@ class LoggedHook(Hook):
for i, line in enumerate(fh): for i, line in enumerate(fh):
line = line.rstrip("\n") line = line.rstrip("\n")
if line.startswith("%"): if line.startswith("%"):
tstamp, args = line.split(" with ")
# Timestamp includes nanosecond resolution # Timestamp includes nanosecond resolution
timestamp = line.split(" ")[-1] timestamp = tstamp.split(" ")[-1]
# convert timestamp to python datetime object # convert timestamp to python datetime object
log["calls"].append( log["calls"].append({
datetime.fromtimestamp(float(timestamp)) "timestamp": datetime.fromtimestamp(float(timestamp)),
) "args": args,
})
elif line.startswith("{"): elif line.startswith("{"):
# Decode json input (to hook) # Decode json input (to hook)
log["input"]["json"].append(json_decoder(line)) log["input"]["json"].append(json_decoder(line))

View file

@ -29,7 +29,6 @@
import sys import sys
import os import os
import unittest import unittest
from datetime import datetime
# Ensure python finds the local simpletap module # Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__))) sys.path.append(os.path.dirname(os.path.abspath(__file__)))
@ -63,6 +62,7 @@ class TestHooksOnLaunch(TestCase):
self.assertEqual('data' in taskenv, True, 'data:...') self.assertEqual('data' in taskenv, True, 'data:...')
self.assertEqual('version' in taskenv, True, 'version:...') self.assertEqual('version' in taskenv, True, 'version:...')
if __name__ == "__main__": if __name__ == "__main__":
from simpletap import TAPTestRunner from simpletap import TAPTestRunner
unittest.main(testRunner=TAPTestRunner()) unittest.main(testRunner=TAPTestRunner())

View file

@ -157,6 +157,9 @@ sys.exit(0)
# (according to python's JSON parser) # (according to python's JSON parser)
hook.assertValidJSONOutput() hook.assertValidJSONOutput()
# Checking which arguments were passed to the hook
self.assertIn("/Hello/Greetings/", logs["calls"][0]["args"])
# Some message output from the hook # Some message output from the hook
self.assertEqual(logs["output"]["msgs"][0], self.assertEqual(logs["output"]["msgs"][0],
"Hello from the template hook") "Hello from the template hook")

View file

@ -6,7 +6,7 @@ IN="${ORIGINALHOOK}.log.in"
OUT="${ORIGINALHOOK}.log.out" OUT="${ORIGINALHOOK}.log.out"
# Let it know that we were executed # Let it know that we were executed
echo "% Called at $(python -c 'import time; print(time.time())')" >> ${IN} echo "% Called at $(python -c 'import time; print(time.time())') with '$@'" >> ${IN}
# Log what arrives via stdin to ${IN} and what comes via stdout to ${OUT} # Log what arrives via stdin to ${IN} and what comes via stdout to ${OUT}
$ORIGINALHOOK "$@" < <(tee -a ${IN}) > >(tee -a ${OUT}) $ORIGINALHOOK "$@" < <(tee -a ${IN}) > >(tee -a ${OUT})