Ejemplo n.º 1
0
 def test_found_posix(self):
     sys.platform = 'posix'
     tempdir = os.environ['PATH'] = mkdtemp(self)
     f = join(tempdir, 'binary')
     with open(f, 'w'):
         pass
     os.chmod(f, 0o777)
     self.assertEqual(which('binary'), f)
     os.environ['PATH'] = ''
     self.assertEqual(which('binary', path=tempdir), f)
Ejemplo n.º 2
0
 def test_found_posix(self):
     sys.platform = 'posix'
     tempdir = os.environ['PATH'] = mkdtemp(self)
     f = join(tempdir, 'binary')
     with open(f, 'w'):
         pass
     os.chmod(f, 0o777)
     self.assertEqual(which('binary'), f)
     self.assertEqual(which(f), f)
     os.environ['PATH'] = ''
     self.assertEqual(which('binary', path=tempdir), f)
     self.assertEqual(which(f, path=tempdir), f)
Ejemplo n.º 3
0
 def setUpClass(cls):
     # nosetest will still execute setUpClass, so the test condition
     # will need to be checked here also.
     if which('npm') is None:  # pragma: no cover
         return
     setup_class_install_environment(cls,
                                     Driver, ['nunja'],
                                     production=True)
Ejemplo n.º 4
0
    def which(self):
        """
        Figure out which binary this will execute.
        """

        if self.binary is None:
            return None

        return which(self.binary, path=self.env_path)
Ejemplo n.º 5
0
 def test_found_posix_relpath(self):
     remember_cwd(self)
     sys.platform = 'posix'
     os.chdir(mkdtemp(self))
     os.mkdir('bin')
     bin_dir = os.environ['PATH'] = join(os.path.curdir, 'bin')
     f = join(bin_dir, 'binary')
     with open(f, 'w'):
         pass
     os.chmod(f, 0o777)
     self.assertEqual(which(f), f)
Ejemplo n.º 6
0
    def which(self):
        """
        Figure out which binary this will execute.

        Returns None if the binary is not found.
        """

        if self.binary is None:
            return None

        return which(self.binary, path=self.env_path)
Ejemplo n.º 7
0
    def test_found_win32(self):
        sys.platform = 'win32'
        tempdir = os.environ['PATH'] = mkdtemp(self)
        os.environ['PATHEXT'] = pathsep.join(('.com', '.exe', '.bat'))
        f = join(tempdir, 'binary.exe')
        with open(f, 'w'):
            pass
        os.chmod(f, 0o777)
        self.assertEqual(which('binary'), f)
        self.assertEqual(which('binary.exe'), f)
        self.assertEqual(which(f), f)
        self.assertIsNone(which('binary.com'))

        os.environ['PATH'] = ''
        self.assertEqual(which('binary', path=tempdir), f)
        self.assertEqual(which('binary.exe', path=tempdir), f)
        self.assertEqual(which(f, path=tempdir), f)
        self.assertIsNone(which('binary.com', path=tempdir))
Ejemplo n.º 8
0
def _get_exec_binary(binary, kw):
    """
    On win32, the subprocess module can only reliably resolve the
    target binary if it's actually a binary; as for a Node.js script
    it seems to only work iff shell=True was specified, presenting
    a security risk.  Resolve the target manually through which will
    account for that.

    The kw argument is the keyword arguments that will be passed into
    whatever respective subprocess.Popen family of methods.  The PATH
    environment variable will be used if available.
    """

    binary = which(binary, path=kw.get('env', {}).get('PATH'))
    if binary is None:
        raise_os_error(errno.ENOENT)
    return binary
Ejemplo n.º 9
0
    def which_with_node_modules(self):
        """
        Which with node_path and node_modules
        """

        if self.binary is None:
            return None

        # first, log down the pedantic things...
        if isdir(self.join_cwd(NODE_MODULES)):
            logger.debug(
                "'%s' instance will attempt to locate '%s' binary from "
                "%s%s%s%s%s, located through the working directory",
                self.__class__.__name__,
                self.binary,
                self.join_cwd(),
                sep,
                NODE_MODULES,
                sep,
                NODE_MODULES_BIN,
            )
        if self.node_path:
            logger.debug(
                "'%s' instance will attempt to locate '%s' binary from "
                "its %s of %s",
                self.__class__.__name__,
                self.binary,
                NODE_PATH,
                self.node_path,
            )

        paths = self.find_node_modules_basedir()
        whichpaths = pathsep.join(join(p, NODE_MODULES_BIN) for p in paths)

        if paths:
            logger.debug(
                "'%s' instance located %d possible paths to the '%s' binary, "
                "which are %s",
                self.__class__.__name__,
                len(paths),
                self.binary,
                whichpaths,
            )

        return which(self.binary, path=whichpaths)
Ejemplo n.º 10
0
    def _set_env_path_with_node_modules(self):
        """
        Attempt to locate and set the paths to the binary with the
        working directory defined for this instance.
        """

        modcls_name = ':'.join(
            (self.__class__.__module__, self.__class__.__name__))

        if self.binary is None:
            raise ValueError("binary undefined for '%s' instance" %
                             modcls_name)

        logger.debug(
            "locating '%s' node binary for %s instance...",
            self.binary,
            modcls_name,
        )

        default = self.which()
        if default is not None:
            logger.debug(
                "found '%s'; "
                "not modifying PATH environment variable in instance of '%s'.",
                realpath(default), modcls_name)
            return True

        node_path = self.node_path
        if node_path:
            logger.debug(
                "environment variable '%s' defined '%s'; "
                "their bin directories will be searched.",
                NODE_PATH,
                node_path,
            )
        else:
            node_path = self.join_cwd('node_modules')
            logger.debug(
                "environment variable '%s' undefined; using instance's "
                "working directory's node_modules (%s) as base directory for "
                "finding node binaries.",
                NODE_PATH,
                node_path,
            )

        target = which(self.binary,
                       path=pathsep.join(
                           join(p, '.bin') for p in node_path.split(pathsep)))

        if target:
            # Only setting the path specific for the binary; side effect
            # will be whoever else borrowing the _exec in here might not
            # get the binary they want.  That's why it's private.
            self.env_path = dirname(target)
            logger.debug(
                "located '%s' binary at '%s'; setting PATH environment "
                "variable for '%s' instance.", self.binary, self.env_path,
                modcls_name)
            return True
        else:
            logger.debug(
                "Unable to locate '%s'; not modifying PATH environment "
                "variable for instance of '%s'.", self.binary, modcls_name)
            return False
Ejemplo n.º 11
0
 def test_dupe_skip(self):
     os.environ['PATH'] = pathsep.join(
         (os.environ['PATH'], os.environ['PATH']))
     which('ls')
     which('cmd')
Ejemplo n.º 12
0
from calmjs.utils import pretty_logging
from calmjs.utils import finalize_env
from calmjs.utils import which
from calmjs.testing import mocks
from calmjs.testing.mocks import MockProvider
from calmjs.testing.utils import create_fake_bin
from calmjs.testing.utils import fake_error
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_mod_check_output
from calmjs.testing.utils import stub_os_environ

which_node = which('node')


class CliGenerateMergeDictTestCase(unittest.TestCase):
    def test_merge(self):
        result = cli.generate_merge_dict(['key'], {'key': {
            'foo': 1
        }}, {'baz': 1}, {'key': {
            'bar': 1
        }})
        self.assertEqual(result, {'key': {
            'foo': 1,
            'bar': 1,
        }})

    def test_merge_multi(self):
Ejemplo n.º 13
0
from calmjs.utils import pretty_logging
from calmjs.utils import which

from calmjs.testing.mocks import StringIO
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import make_dummy_dist
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_check_interactive
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_os_environ
from calmjs.testing.utils import stub_stdin
from calmjs.testing.utils import stub_stdouts

which_npm = which('npm')


class LocatePackageTestCase(unittest.TestCase):

    def test_locate_package_entry_module_not_found(self):
        working_dir = mkdtemp(self)
        pkg_name = 'demo'
        with pretty_logging(stream=StringIO()) as stream:
            self.assertIsNone(
                npm.locate_package_entry_file(working_dir, pkg_name))

        self.assertIn(
            "could not locate package.json for the npm package 'demo' "
            "in the current working directory '%s'" % working_dir,
            stream.getvalue(),
Ejemplo n.º 14
0
 def test_dupe_skip(self):
     os.environ['PATH'] = pathsep.join(
         (os.environ['PATH'], os.environ['PATH']))
     which('ls')
     which('cmd')
Ejemplo n.º 15
0
 def test_nothing(self):
     os.environ['PATH'] = ''
     self.assertIsNone(which('ls'))
Ejemplo n.º 16
0
from calmjs import dist
from calmjs import runtime
from calmjs.utils import which
from calmjs.utils import finalize_env

from calmjs.testing import mocks
from calmjs.testing.utils import make_dummy_dist
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_mod_check_interactive
from calmjs.testing.utils import stub_stdouts

which_bower = which('bower')


class IntegrationTestCase(unittest.TestCase):
    def test_calmjs_main_console_entry_point(self):
        stub_stdouts(self)
        with self.assertRaises(SystemExit):
            runtime.main(['-h'])
        # ensure our base action module/class is registered.
        self.assertIn('bower', sys.stdout.getvalue())

    def setup_runtime(self):
        make_dummy_dist(self, (('bower.json',
                                json.dumps({
                                    'name': 'site',
                                    'dependencies': {
Ejemplo n.º 17
0
 def tearDownClass(cls):
     # Ditto, as per above.
     if which('npm') is None:  # pragma: no cover
         return
     rmtree(cls._cls_tmpdir)
Ejemplo n.º 18
0
from calmjs.utils import pretty_logging
from calmjs.utils import which

from calmjs.testing.mocks import StringIO
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import make_dummy_dist
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_check_interactive
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_os_environ
from calmjs.testing.utils import stub_stdin
from calmjs.testing.utils import stub_stdouts

which_yarn = which('yarn')


class YarnTestCase(unittest.TestCase):

    def setUp(self):
        remember_cwd(self)
        stub_os_environ(self)
        stub_check_interactive(self, True)

    def test_yarn_no_path(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)
        os.environ['PATH'] = ''
        with pretty_logging(stream=StringIO()) as stderr:
            self.assertIsNone(yarn.get_yarn_version())
Ejemplo n.º 19
0
 def test_nothing(self):
     os.environ['PATH'] = ''
     self.assertIsNone(which('ls'))
Ejemplo n.º 20
0
            bundle_sourcepath={},
        )

        with pretty_logging('nunja', stream=StringIO()) as stream:
            precompile_nunja(spec,
                             True,
                             'plugin_sourcepath',
                             'bundle_sourcepath',
                             omit_paths=('empty:', ))

        self.assertNotIn('failed', stream.getvalue())
        self.assertFalse(exists(join(build_dir, '__nunja_precompiled__.js')))
        self.assertNotIn('nunjucks', spec['bundle_sourcepath'])


@unittest.skipIf(which('npm') is None, 'npm not found.')
class SpecIntegrationTestCase(unittest.TestCase):
    """
    Actually do some real template precompilation.
    """
    @classmethod
    def setUpClass(cls):
        # nosetest will still execute setUpClass, so the test condition
        # will need to be checked here also.
        if which('npm') is None:  # pragma: no cover
            return
        setup_class_install_environment(cls,
                                        Driver, ['nunja'],
                                        production=True)

    @classmethod
Ejemplo n.º 21
0
from calmjs.utils import pretty_logging
from calmjs.utils import which

from calmjs.testing.mocks import StringIO
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import make_dummy_dist
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_check_interactive
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_os_environ
from calmjs.testing.utils import stub_stdin
from calmjs.testing.utils import stub_stdouts

which_yarn = which('yarn')


class YarnTestCase(unittest.TestCase):
    def setUp(self):
        remember_cwd(self)
        stub_os_environ(self)
        stub_check_interactive(self, True)

    def test_yarn_no_path(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)
        os.environ['PATH'] = ''
        with pretty_logging(stream=StringIO()) as stderr:
            self.assertIsNone(yarn.get_yarn_version())
            self.assertIn("failed to execute 'yarn'", stderr.getvalue())
Ejemplo n.º 22
0
from calmjs.utils import pretty_logging
from calmjs.utils import which

from calmjs.testing.mocks import StringIO
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import make_dummy_dist
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_check_interactive
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_os_environ
from calmjs.testing.utils import stub_stdin
from calmjs.testing.utils import stub_stdouts

which_npm = which('npm')


class LocatePackageTestCase(unittest.TestCase):
    def test_locate_package_entry_module_not_found(self):
        working_dir = mkdtemp(self)
        pkg_name = 'demo'
        with pretty_logging(stream=StringIO()) as stream:
            self.assertIsNone(
                npm.locate_package_entry_file(working_dir, pkg_name))

        self.assertIn(
            "could not locate package.json for the npm package 'demo' "
            "in the current working directory '%s'" % working_dir,
            stream.getvalue(),
        )
Ejemplo n.º 23
0
from calmjs.utils import finalize_env
from calmjs.utils import which
from calmjs.testing import mocks
from calmjs.testing.mocks import MockProvider
from calmjs.testing.utils import create_fake_bin
from calmjs.testing.utils import fake_error
from calmjs.testing.utils import mkdtemp
from calmjs.testing.utils import remember_cwd
from calmjs.testing.utils import stub_check_interactive
from calmjs.testing.utils import stub_item_attr_value
from calmjs.testing.utils import stub_base_which
from calmjs.testing.utils import stub_mod_call
from calmjs.testing.utils import stub_mod_check_output
from calmjs.testing.utils import stub_os_environ

which_node = which('node')
isatty = sys.stdin.isatty()


class CliGenerateMergeDictTestCase(unittest.TestCase):

    def test_merge(self):
        result = cli.generate_merge_dict(
            ['key'], {'key': {'foo': 1}}, {'baz': 1}, {'key': {'bar': 1}})
        self.assertEqual(result, {'key': {
            'foo': 1,
            'bar': 1,
        }})

    def test_merge_multi(self):
        result = cli.generate_merge_dict(