Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
prefix = "watchmantest"
self.temp_dir = path.get_canonical_filesystem_path(
tempfile.mkdtemp(dir=parent_dir, prefix=prefix)
)
if os.name != "nt":
# On some platforms, setting the setgid bit on a directory doesn't
# work if the user isn't a member of the directory's group. Set the
# group explicitly to avoid this.
os.chown(self.temp_dir, -1, os.getegid())
# Some environments have a weird umask that can leave state
# directories too open and break tests.
os.umask(0o022)
# Redirect all temporary files to that location
if pywatchman.compat.PYTHON3:
tempfile.tempdir = os.fsdecode(self.temp_dir)
else:
tempfile.tempdir = self.temp_dir
self.keep = keepAtShutdown
def cleanup():
if self.keep:
sys.stdout.write("Preserving output in %s\n" % self.temp_dir)
return
self._retry_rmtree(self.temp_dir)
atexit.register(cleanup)
import WatchmanInstance
try:
import unittest2 as unittest
except ImportError:
import unittest
WATCHMAN_SRC_DIR = os.environ.get("WATCHMAN_SRC_DIR", os.getcwd())
THIS_DIR = os.path.join(WATCHMAN_SRC_DIR, "tests", "integration")
@unittest.skipIf(os.name == "nt", "not supported on windows")
class TestSiteSpawn(unittest.TestCase):
if not pywatchman.compat.PYTHON3:
assertRegex = unittest.TestCase.assertRegexpMatches
def test_failingSpawner(self):
config = {
"spawn_watchman_service": os.path.join(THIS_DIR, "site_spawn_fail.py")
}
inst = WatchmanInstance.Instance(config=config)
stdout, stderr = inst.commandViaCLI(["version"])
print("stdout", stdout)
print("stderr", stderr)
stderr = stderr.decode("ascii")
self.assertEqual(b"", stdout)
self.assertRegex(stderr, "failed to start\n")
self.assertRegex(stderr, "site_spawn_fail.py: exited with status 1")
# vim:ts=4:sw=4:et:
# Copyright 2017-present Facebook, Inc.
# Licensed under the Apache License, Version 2.0
# no unicode literals
from __future__ import absolute_import, division, print_function
import os
import subprocess
import pywatchman
import WatchmanInstance
import WatchmanTestCase
if pywatchman.compat.PYTHON3:
STRING_TYPES = (str, bytes)
else:
STRING_TYPES = (str, unicode) # noqa: F821
class WatchmanSCMTestCase(WatchmanTestCase.WatchmanTestCase):
def __init__(self, methodName="run"):
super(WatchmanSCMTestCase, self).__init__(methodName)
def requiresPersistentSession(self):
return True
def skipIfNoFSMonitor(self):
""" cause the test to skip if fsmonitor is not available.
We don't call this via unittest.skip because we want
to have the skip message show the context """
def doJson(self, addNewLine, pretty=False):
sockname = self.getSockPath()
if pretty:
watchman_cmd = b'[\n"get-sockname"\n]'
else:
watchman_cmd = json.dumps(["get-sockname"])
if compat.PYTHON3:
watchman_cmd = watchman_cmd.encode("ascii")
if addNewLine:
watchman_cmd = watchman_cmd + b"\n"
cli_cmd = [
os.environ.get("WATCHMAN_BINARY", "watchman"),
"--sockname={0}".format(sockname),
"--logfile=/BOGUS",
"--statefile=/BOGUS",
"--no-spawn",
"--no-local",
"-j",
]
proc = subprocess.Popen(
cli_cmd,
stdin=subprocess.PIPE,
def start(self):
args = [self.watchmanBinary(), "--foreground", "--log-level=2"]
args.extend(self.get_state_args())
env = os.environ.copy()
env["WATCHMAN_CONFIG_FILE"] = self.cfg_file
with open(self.cli_log_file_name, "w+") as cli_log_file:
self.proc = subprocess.Popen(
args, env=env, stdin=None, stdout=cli_log_file, stderr=cli_log_file
)
if self.debug_watchman:
print("Watchman instance PID: " + str(self.proc.pid))
if pywatchman.compat.PYTHON3:
user_input = input
else:
user_input = raw_input # noqa:F821
user_input("Press Enter to continue...")
# wait for it to come up
deadline = time.time() + self.start_timeout
while time.time() < deadline:
try:
client = pywatchman.client(sockpath=self.sock_file)
self.pid = client.query("get-pid")["pid"]
break
except pywatchman.SocketConnectError:
t, val, tb = sys.exc_info()
time.sleep(0.1)
finally:
A decorator function used to create different permutations from
a given input test class.
Given a test class named "MyTest", this will create 4 separate
classes named "MyTestLocalBser", "MyTestLocalBser2",
"MyTestLocalJson" and "MyTestCliJson" that will exercise the
different transport and encoding options implied by their names.
"""
matrix = [
("local", "bser", "LocalBser2"),
("local", "json", "LocalJson"),
("cli", "json", "CliJson"),
]
if not pywatchman.compat.PYTHON3:
matrix += [("local", "bser-v1", "LocalBser")]
# We do some rather hacky things here to define new test class types
# in our caller's scope. This is needed so that the unittest TestLoader
# will find the subclasses we define.
caller_scope = inspect.currentframe().f_back.f_locals
for (transport, encoding, suffix) in matrix:
def make_class(transport, encoding, suffix):
subclass_name = test_class.__name__ + suffix
# Define a new class that derives from the input class
class MatrixTest(test_class):
def setDefaultConfiguration(self):
self.setConfiguration(transport, encoding)
def test_subscribe_unicode(self):
unicode_filename = u"\u263a"
# On Python 2, pywatchman returns bytestrings by default. On Python 3
# it returns Unicode strings. So we need to take care of that.
if not pywatchman.compat.PYTHON3:
unicode_filename = unicode_filename.encode("utf-8")
root = self.mkdtemp()
a_dir = os.path.join(root, "a")
os.mkdir(a_dir)
self.touchRelative(a_dir, "lemon")
self.touchRelative(root, "b")
self.touchRelative(root, unicode_filename)
self.watchmanCommand("watch", root)
self.assertFileList(root, files=["a", "a/lemon", "b", unicode_filename])
self.watchmanCommand("subscribe", root, "myname", {"fields": ["name"]})
self.watchmanCommand(
"subscribe", root, "relative", {"fields": ["name"], "relative_root": "a"}