Skip to content

Instantly share code, notes, and snippets.

@wez

wez/-

Created August 29, 2015 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wez/ccc7969f6714030886ee to your computer and use it in GitHub Desktop.
Save wez/ccc7969f6714030886ee to your computer and use it in GitHub Desktop.
diff --git a/tests/integration/WatchmanTestCase.py b/tests/integration/WatchmanTestCase.py
index 7a9f756..077dadb 100644
--- a/tests/integration/WatchmanTestCase.py
+++ b/tests/integration/WatchmanTestCase.py
@@ -10,6 +10,7 @@ import os.path
import os
import WatchmanInstance
import copy
+import sys
def norm_path(name):
return os.path.normcase(os.path.normpath(name))
@@ -39,6 +40,11 @@ class WatchmanTestCase(unittest.TestCase):
def mkdtemp(self):
return self.normPath(tempfile.mkdtemp(dir=self.tempdir))
+ def mktemp(self, prefix=''):
+ f, name = tempfile.mkstemp(prefix=prefix, dir=self.tempdir)
+ os.close(f)
+ return name
+
def run(self, result):
if result is None:
raise Exception('MUST be a runtests.py:Result instance')
@@ -52,6 +58,11 @@ class WatchmanTestCase(unittest.TestCase):
result.setFlavour(self.transport, self.encoding)
super(WatchmanTestCase, self).run(result)
finally:
+ try:
+ self.watchmanCommand('log-level', 'off')
+ self.getClient().getLog(remove=True)
+ except:
+ pass
self.__logTestInfo(id, 'END')
self.__clearWatches()
diff --git a/tests/integration/test_trigger.py b/tests/integration/test_trigger.py
new file mode 100644
index 0000000..56d9d33
--- /dev/null
+++ b/tests/integration/test_trigger.py
@@ -0,0 +1,68 @@
+# vim:ts=4:sw=4:et:
+# Copyright 2012-present Facebook, Inc.
+# Licensed under the Apache License, Version 2.0
+import WatchmanTestCase
+import os
+import os.path
+import sys
+import re
+
+class TestTrigger(WatchmanTestCase.WatchmanTestCase):
+ def matchTriggerInLog(self, logs, root, triggerName):
+ r = re.compile(' trigger %s:%s pid=' % (re.escape(root), triggerName))
+ for line in logs:
+ if r.search(line):
+ return True
+ return False
+
+ def hasTriggerInLogs(self, root, triggerName):
+ client = self.getClient()
+ logs = client.getLog(remove=False)
+ if self.matchTriggerInLog(logs, root, triggerName):
+ return True
+ res = client.receive()
+ while client.isUnilateralResponse(res):
+ logs = client.getLog(remove=False)
+ if self.matchTriggerInLog(logs, root, triggerName):
+ return True
+ res = client.receive()
+ return False
+
+
+ def test_triggerIssue141(self):
+ if self.transport == 'cli':
+ self.skipTest('cli transport has no log subscriptions')
+
+ root = self.mkdtemp()
+ self.touchRelative(root, 'foo.js')
+
+ self.watchmanCommand('watch', root)
+ self.assertFileList(root, ['foo.js'])
+
+ touch = os.path.join(os.path.dirname(__file__), 'touch.py')
+ logs = self.mkdtemp()
+ first_log = os.path.join(logs, 'first')
+ second_log = os.path.join(logs, 'second')
+
+ res = self.watchmanCommand('trigger', root, 'first', '*.js', '--',
+ sys.executable, touch, first_log)
+ self.assertEqual(res['triggerid'], 'first')
+
+ res = self.watchmanCommand('trigger', root, 'second', '*.js', '--',
+ sys.executable, touch, second_log)
+ self.assertEqual(res['triggerid'], 'second')
+
+ self.assertWaitFor(lambda: os.path.exists(first_log) and
+ os.path.exists(second_log),
+ message='both triggers fire at start')
+
+ # start collecting logs
+ self.watchmanCommand('log-level', 'debug')
+ client = self.getClient()
+
+ self.touchRelative(root, 'foo.js')
+
+ self.assertWaitFor(lambda: self.hasTriggerInLogs(root, 'first') and
+ self.hasTriggerInLogs(root, 'second'),
+ message='both triggers fired on update')
+
diff --git a/tests/integration/touch.py b/tests/integration/touch.py
new file mode 100644
index 0000000..199a2a3
--- /dev/null
+++ b/tests/integration/touch.py
@@ -0,0 +1,16 @@
+# Portable simple implementation of `touch`
+import os
+import sys
+import errno
+
+fname = sys.argv[1]
+
+try:
+ os.utime(fname, None)
+except OSError as e:
+ if e.errno == errno.ENOENT:
+ with open(fname, 'a'):
+ os.utime(fname, None)
+ else:
+ raise
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment