Example #1
0
 def test_set_env_path_with_node_modules_undefined(self):
     driver = base.BaseDriver()
     with self.assertRaises(ValueError) as e:
         driver._set_env_path_with_node_modules()
     self.assertEqual(
         str(e.exception),
         "binary undefined for 'calmjs.base:BaseDriver' instance")
Example #2
0
    def test_find_node_modules_basedir(self):
        driver = base.BaseDriver()
        # ensure that NODE_PATH is initially None
        driver.node_path = None
        driver.working_dir = mkdtemp(self)
        # initially should be empty, since no node_modules in either
        # directories that it should check
        self.assertEqual([], driver.find_node_modules_basedir())

        # having the NODE_PATH defined will result in such
        p1 = mkdtemp(self)
        p2 = mkdtemp(self)
        driver.node_path = pathsep.join([p1, p2])
        self.assertEqual([p1, p2], driver.find_node_modules_basedir())

        # create the node_modules in the working directory defined for
        # the driver instance, and unset NODE_PATH
        driver.node_path = None
        dwd_wd_nm = join(driver.working_dir, 'node_modules')
        os.mkdir(dwd_wd_nm)
        self.assertEqual([dwd_wd_nm], driver.find_node_modules_basedir())

        # combine the two, they should be in this order, where the
        # working directory has higher precedence over NODE_PATH
        driver.node_path = p1
        self.assertEqual([dwd_wd_nm, p1], driver.find_node_modules_basedir())
Example #3
0
 def test_construction(self):
     driver = base.BaseDriver(
         node_path='node_path', env_path='env_path', working_dir='cwd',
         indent=2, separators=(', ', ':'))
     self.assertEqual(driver.node_path, 'node_path')
     self.assertEqual(driver.env_path, 'env_path')
     self.assertEqual(driver.working_dir, 'cwd')
     self.assertEqual(driver.indent, 2)
     self.assertEqual(driver.separators, (', ', ':'))
Example #4
0
    def test_which_with_node_modules(self):
        driver = base.BaseDriver()
        # ensure that NODE_PATH is initially None
        driver.node_path = None
        driver.working_dir = mkdtemp(self)
        # initially should be empty, since no node_modules in either
        # directories that it should check
        with pretty_logging(stream=mocks.StringIO()) as s:
            self.assertIsNone(driver.which_with_node_modules())
        # should not generate extra log messages.
        self.assertNotIn('will attempt', s.getvalue())

        # having the NODE_PATH defined will result in such
        p1 = mkdtemp(self)
        p2 = mkdtemp(self)
        driver.node_path = pathsep.join([p1, p2])
        with pretty_logging(stream=mocks.StringIO()) as s:
            self.assertIsNone(driver.which_with_node_modules())

        # should not generate extra log messages, binary still not
        # assigned.
        self.assertNotIn('will attempt', s.getvalue())

        driver.binary = 'dummy'
        with pretty_logging(stream=mocks.StringIO()) as s:
            self.assertIsNone(driver.which_with_node_modules())

        # now the log should show what attempted.
        log = s.getvalue()
        self.assertIn(
            "'BaseDriver' instance will attempt to locate 'dummy' binary from "
            "its NODE_PATH of", log)
        self.assertIn(p1, log)
        self.assertIn(p2, log)
        self.assertIn("'BaseDriver' instance located 2 possible paths", log)

        # try again with working directory
        driver.node_path = None
        dwd_wd_nm = join(driver.working_dir, 'node_modules')
        os.mkdir(dwd_wd_nm)
        with pretty_logging(stream=mocks.StringIO()) as s:
            self.assertIsNone(driver.which_with_node_modules())

        log = s.getvalue()
        # now the log should show what attempted.
        self.assertIn(
            "'BaseDriver' instance will attempt to locate 'dummy' binary from",
            log,
        )
        self.assertIn(dwd_wd_nm, log)
        self.assertIn("located through the working directory", log)
        self.assertIn("'BaseDriver' instance located 1 possible paths", log)
Example #5
0
    def test_join_cwd(self):
        driver = base.BaseDriver()
        self.assertEqual(driver.cwd, os.getcwd())
        self.assertTrue(driver.join_cwd('bar').startswith(os.getcwd()))
        driver.working_dir = mkdtemp(self)

        self.assertEqual(driver.cwd, driver.working_dir)
        result = driver.join_cwd('bar')
        self.assertTrue(result.startswith(driver.working_dir))
        self.assertTrue(result.endswith('bar'))

        result = driver.join_cwd()
        self.assertEqual(result, driver.working_dir)
Example #6
0
 def test_which(self):
     driver = base.BaseDriver()
     # no binary, no nothing.
     self.assertIsNone(driver.which())
Example #7
0
 def test_dumps(self):
     driver = base.BaseDriver()
     self.assertEqual(driver.dumps({'a': 1}), '{\n    "a": 1\n}')
Example #8
0
 def test_dump(self):
     driver = base.BaseDriver()
     stream = mocks.StringIO()
     driver.dump({'a': 1}, stream)
     self.assertEqual(stream.getvalue(), '{\n    "a": 1\n}')