# vim:ts=4:sw=4:et:
# Copyright 2016-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 os.path
import signal
import subprocess

import Interrupt
import WatchmanInstance


try:
    import unittest2 as unittest
except ImportError:
    import unittest

WATCHMAN_SRC_DIR = os.environ.get("WATCHMAN_SRC_DIR", os.getcwd())
TEST_BINARY = (
    os.environ["WATCHMAN_CPPCLIENT_BINARY"]
    if "WATCHMAN_CPPCLIENT_BINARY" in os.environ.keys()
    else os.path.join(WATCHMAN_SRC_DIR, "tests/integration/cppclient.t")
)


class TestCppClient(unittest.TestCase):
    @unittest.skipIf(not os.path.isfile(TEST_BINARY), "test binary not built")
    def test_cppclient(self):
        env = os.environ.copy()
        env["WATCHMAN_SOCK"] = (
            WatchmanInstance.getSharedInstance().getSockPath().legacy_sockpath()
        )
        proc = subprocess.Popen(
            TEST_BINARY, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        (stdout, stderr) = proc.communicate()
        status = proc.poll()

        if status == -signal.SIGINT:
            Interrupt.setInterrupted()
            self.fail("Interrupted by SIGINT")
            return

        if status != 0:
            self.fail(
                "Exit status %d\n%s\n%s\n"
                % (status, stdout.decode("utf-8"), stderr.decode("utf-8"))
            )
            return

        self.assertTrue(True, TEST_BINARY)

@@ -107,96 +107,8 @@
 .0%0A%0A
-# no unicode literals%0Afrom __future__ import absolute_import, division, print_function%0A%0A
 impo
@@ -159,16 +159,48 @@
 bprocess
+%0Aimport tempfile%0Aimport unittest
 %0A%0Aimport
@@ -239,88 +239,8 @@
 ce%0A%0A
-%0Atry:%0A    import unittest2 as unittest%0Aexcept ImportError:%0A    import unittest%0A%0A
 WATC
@@ -535,16 +535,230 @@
 tCase):%0A
+    def setUp(self):%0A        self.tmpDirCtx = tempfile.TemporaryDirectory()  # noqa P201%0A        self.tmpDir = self.tmpDirCtx.__enter__()%0A%0A    def tearDown(self):%0A        self.tmpDirCtx.__exit__(None, None, None)%0A%0A
     @uni
@@ -1075,17 +1075,41 @@
 ARY,
- env=env,
+%0A            env=env,%0A           
  std
@@ -1128,16 +1128,28 @@
 ss.PIPE,
+%0A           
  stderr=
@@ -1163,16 +1163,46 @@
 ess.PIPE
+,%0A            cwd=self.tmpDir,
 %0A       

fix cppclient test by running it in a temporary directory