<commit_before># no unicode literals
from __future__ import absolute_import, division, print_function

import distutils.spawn
import glob
import inspect
import os
import os.path
import re
import shutil
import signal
import subprocess

import Interrupt
import TempDir
import WatchmanInstance


try:
    import unittest2 as unittest
except ImportError:
    import unittest

node_bin = distutils.spawn.find_executable("node") or distutils.spawn.find_executable(
    "nodejs"
)
npm_bin = distutils.spawn.find_executable("npm")

WATCHMAN_SRC_DIR = os.environ.get("WATCHMAN_SRC_DIR", os.getcwd())
THIS_DIR = os.path.join(WATCHMAN_SRC_DIR, "tests", "integration")


def find_js_tests(test_class):
    """
    A decorator function used to create a class per JavaScript test script
    """

    # 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 js in glob.glob(os.path.join(THIS_DIR, "*.js")):
        base = os.path.basename(js)
        if base.startswith(".") or base.startswith("_"):
            continue

        subclass_name = base.replace(".", "_").replace("-", "_")

        def make_class(jsfile):
            # Define a new class that derives from the input class.
            # This has to be a function otherwise jsfile captures
            # the value from the last iteration of the glob loop.

            class JSTest(test_class):
                def getCommandArgs(self):
                    return [node_bin, jsfile]

            # Set the name and module information on our new subclass
            JSTest.__name__ = subclass_name
            JSTest.__qualname__ = subclass_name
            JSTest.__module__ = test_class.__module__

            caller_scope[subclass_name] = JSTest

        make_class(js)

    return None


@find_js_tests
class NodeTestCase(unittest.TestCase):
    attempt = 0

    def setAttemptNumber(self, attempt):
        """ enable flaky test retry """
        self.attempt = attempt

    @unittest.skipIf(node_bin is None or npm_bin is None, "node not installed")
    def runTest(self):
        env = os.environ.copy()
        env["WATCHMAN_SOCK"] = WatchmanInstance.getSharedInstance().getSockPath()
        dotted = (
            os.path.normpath(self.id())
            .replace(os.sep, ".")
            .replace("tests.integration.", "")
            .replace(".php", "")
        )
        if self.attempt > 0:
            dotted += "-%d" % self.attempt
        env["TMPDIR"] = os.path.join(TempDir.get_temp_dir().get_dir(), dotted)
        os.mkdir(env["TMPDIR"])

        # build the node module with npm
        node_dir = os.path.join(env["TMPDIR"], "fb-watchman")
        shutil.copytree(os.path.join(WATCHMAN_SRC_DIR, "node"), node_dir)
        subprocess.check_call(["npm", "install"], cwd=node_dir)

        env["TMP"] = env["TMPDIR"]
        env["TEMP"] = env["TMPDIR"]
        env["IN_PYTHON_HARNESS"] = "1"
        env["NODE_PATH"] = "%s:%s" % (env["TMPDIR"], env.get("NODE_PATH", ""))
        proc = subprocess.Popen(
            self.getCommandArgs(),
            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, self.getCommandArgs())<commit_msg>Refactor temp dir creation in test_nodejs<commit_after># no unicode literals
from __future__ import absolute_import, division, print_function

import distutils.spawn
import glob
import inspect
import os
import os.path
import re
import shutil
import signal
import subprocess

import Interrupt
import WatchmanInstance
import WatchmanTestCase


try:
    import unittest2 as unittest
except ImportError:
    import unittest

node_bin = distutils.spawn.find_executable("node") or distutils.spawn.find_executable(
    "nodejs"
)
npm_bin = distutils.spawn.find_executable("npm")

WATCHMAN_SRC_DIR = os.environ.get("WATCHMAN_SRC_DIR", os.getcwd())
THIS_DIR = os.path.join(WATCHMAN_SRC_DIR, "tests", "integration")


def find_js_tests(test_class):
    """
    A decorator function used to create a class per JavaScript test script
    """

    # 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 js in glob.glob(os.path.join(THIS_DIR, "*.js")):
        base = os.path.basename(js)
        if base.startswith(".") or base.startswith("_"):
            continue

        subclass_name = base.replace(".", "_").replace("-", "_")

        def make_class(jsfile):
            # Define a new class that derives from the input class.
            # This has to be a function otherwise jsfile captures
            # the value from the last iteration of the glob loop.

            class JSTest(test_class):
                def getCommandArgs(self):
                    return [node_bin, jsfile]

            # Set the name and module information on our new subclass
            JSTest.__name__ = subclass_name
            JSTest.__qualname__ = subclass_name
            JSTest.__module__ = test_class.__module__

            caller_scope[subclass_name] = JSTest

        make_class(js)

    return None


@find_js_tests
class NodeTestCase(WatchmanTestCase.TempDirPerTestMixin, unittest.TestCase):
    attempt = 0

    def setAttemptNumber(self, attempt):
        """ enable flaky test retry """
        self.attempt = attempt

    @unittest.skipIf(node_bin is None or npm_bin is None, "node not installed")
    def runTest(self):
        env = os.environ.copy()
        env["WATCHMAN_SOCK"] = WatchmanInstance.getSharedInstance().getSockPath()
        env["TMPDIR"] = self.tempdir

        # build the node module with npm
        node_dir = os.path.join(env["TMPDIR"], "fb-watchman")
        shutil.copytree(os.path.join(WATCHMAN_SRC_DIR, "node"), node_dir)
        subprocess.check_call(["npm", "install"], cwd=node_dir)

        env["TMP"] = env["TMPDIR"]
        env["TEMP"] = env["TMPDIR"]
        env["IN_PYTHON_HARNESS"] = "1"
        env["NODE_PATH"] = "%s:%s" % (env["TMPDIR"], env.get("NODE_PATH", ""))
        proc = subprocess.Popen(
            self.getCommandArgs(),
            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, self.getCommandArgs())

    def _getTempDirName(self):
        dotted = (
            os.path.normpath(self.id())
            .replace(os.sep, ".")
            .replace("tests.integration.", "")
            .replace(".php", "")
        )
        if self.attempt > 0:
            dotted += "-%d" % self.attempt
        return dotted
{'fraction_non_alphanumeric': 0.11182401466544455, 'fraction_numerical': 0.0018331805682859762, 'mean_word_length': 2.4113443501563196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2469508', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2273, 'n_words': 567}