示例#1
0
    def test_find_node_module_pkg_name(self):
        remember_cwd(self)
        text_handler = loaderplugin.WebpackLoaderHandler(None, 'text')
        working_dir = mkdtemp(self)
        os.chdir(working_dir)
        toolchain = Toolchain()
        spec = Spec()

        # base test without any Node.js packages available
        self.assertEqual(
            'text-loader',
            text_handler.find_node_module_pkg_name(toolchain, spec))

        # now provide a package named simply 'text'
        create_mock_npm_package(working_dir, 'text', 'index.js')
        # which, being available, will resolve directly to 'text' due to
        # ambiguity.
        self.assertEqual(
            'text', text_handler.find_node_module_pkg_name(toolchain, spec))

        # however, if a -loader suffixed package (i.e. 'text-loader') is
        # available, the -loader version will be returned instead.
        create_mock_npm_package(working_dir, 'text-loader', 'index.js')
        self.assertEqual(
            'text-loader',
            text_handler.find_node_module_pkg_name(toolchain, spec))
示例#2
0
    def test_modname_loader_map(self):
        srcfile = join(mkdtemp(self), 'some.css')
        spec = Spec(
            build_dir=mkdtemp(self),
            calmjs_webpack_modname_loader_map={'some.css': ['style', 'css']},
        )
        toolchain = Toolchain()
        with open(srcfile, 'w') as fd:
            fd.write('.body {}')

        reg = LoaderPluginRegistry('calmjs.webpack.loaders')
        reg.records['style'] = text = loaderplugin.WebpackLoaderHandler(
            reg, 'style')
        reg.records['css'] = loaderplugin.WebpackLoaderHandler(reg, 'css')
        modpaths, targets, export_module_names = text(toolchain, spec,
                                                      'style!css!some.css',
                                                      srcfile, 'some.css',
                                                      'style!css!some.css')

        self.assertEqual({'style!css!some.css': 'style!css!some.css'},
                         modpaths)
        self.assertEqual({
            'some.css': 'some.css',
            './some.css': 'some.css',
        }, targets)
        self.assertEqual([], export_module_names)
示例#3
0
    def test_call_loader_chaining(self):
        srcfile = join(mkdtemp(self), 'some.css')
        spec = Spec(build_dir=mkdtemp(self))
        toolchain = Toolchain()
        with open(srcfile, 'w') as fd:
            fd.write('body { color: #000; }')

        reg = LoaderPluginRegistry('calmjs.webpack.loaders')
        reg.records['text'] = text = loaderplugin.WebpackLoaderHandler(
            reg, 'text')
        reg.records['css'] = loaderplugin.WebpackLoaderHandler(reg, 'css')

        modpaths, targets, export_module_names = text(toolchain, spec,
                                                      'text!css!some.css',
                                                      srcfile, 'some.css',
                                                      'text!css!some.css')

        self.assertEqual({'text!css!some.css': 'text!css!some.css'}, modpaths)
        self.assertEqual({
            'some.css': 'some.css',
            './some.css': 'some.css',
        }, targets)
        self.assertEqual(['text!css!some.css'], export_module_names)

        self.assertTrue(exists(join(spec['build_dir'], 'some.css')))
示例#4
0
    def test_call_dir_nesting(self):
        srcfile = join(mkdtemp(self), 'some.file.txt')
        tgtfile = join('dir', 'some.file.txt')
        spec = Spec(build_dir=mkdtemp(self))
        toolchain = Toolchain()
        with open(srcfile, 'w') as fd:
            fd.write('hello world')

        reg = LoaderPluginRegistry('calmjs.webpack.loaders')
        text = loaderplugin.WebpackLoaderHandler(reg, 'text')
        modpaths, targets, export_module_names = text(toolchain, spec,
                                                      'text!some.file.txt',
                                                      srcfile, tgtfile,
                                                      'text!some.file.txt')

        self.assertTrue(exists(join(spec['build_dir'], 'dir',
                                    'some.file.txt')))

        self.assertEqual({'text!some.file.txt': 'text!some.file.txt'},
                         modpaths)
        self.assertEqual(
            {
                'some.file.txt': tgtfile,
                './some.file.txt': tgtfile,
            }, targets)
        self.assertEqual(['text!some.file.txt'], export_module_names)
示例#5
0
    def test_find_node_module_pkg_name_full_suffix(self):
        remember_cwd(self)
        # this one is fully named 'text-loader'
        text_handler = loaderplugin.WebpackLoaderHandler(None, 'text-loader')
        working_dir = mkdtemp(self)
        os.chdir(working_dir)
        toolchain = Toolchain()
        spec = Spec()

        # base test without any Node.js packages available
        self.assertEqual(
            'text-loader',
            text_handler.find_node_module_pkg_name(toolchain, spec))

        # not affected by a prefix-free package
        create_mock_npm_package(working_dir, 'text', 'index.js')
        self.assertEqual(
            'text-loader',
            text_handler.find_node_module_pkg_name(toolchain, spec))
示例#6
0
 def setUp(self):
     self.toolchain = Toolchain()
示例#7
0
class ToolchainTestCase(unittest.TestCase):
    """
    Base toolchain class test case.
    """
    def setUp(self):
        self.toolchain = Toolchain()

    def tearDown(self):
        pass

    def test_toolchain_calf_not_spec(self):
        # can't just use a normal dict
        with self.assertRaises(TypeError):
            self.toolchain({})

    def test_toolchain_standard_not_implemented(self):
        spec = Spec()

        with self.assertRaises(NotImplementedError):
            self.toolchain(spec)

        with self.assertRaises(NotImplementedError):
            self.toolchain.assemble(spec)

        with self.assertRaises(NotImplementedError):
            self.toolchain.link(spec)

        # Check that the build_dir is set on the spec based on tempfile
        self.assertTrue(spec['build_dir'].startswith(
            realpath(tempfile.gettempdir())))
        # Also that it got deleted properly.
        self.assertFalse(exists(spec['build_dir']))

    def test_toolchain_calf_with_build_dir_null(self):
        spec = Spec(build_dir=None)

        with self.assertRaises(NotImplementedError):
            self.toolchain(spec)

        # While build_dir is defined, no value was assigned.  See that
        # the process will give it a new one.
        self.assertTrue(spec['build_dir'].startswith(
            realpath(tempfile.gettempdir())))
        # Also that it got deleted properly.
        self.assertFalse(exists(spec['build_dir']))

    def test_toolchain_standard_compile_all(self):
        spec = Spec()
        self.toolchain.compile_all(spec)
        self.assertEqual(spec['compiled_paths'], {})
        self.assertEqual(spec['bundled_paths'], {})
        self.assertEqual(spec['module_names'], [])

    def test_toolchain_standard_good(self):
        # good, with a mock
        called = []

        def mockcall(spec):
            called.append(True)

        spec = Spec()
        self.toolchain.assemble = mockcall
        self.toolchain.link = mockcall

        self.toolchain(spec)

        self.assertEqual(len(called), 2)

    def test_toolchain_standard_build_dir_set(self):
        spec = Spec()
        spec['build_dir'] = mkdtemp(self)

        with self.assertRaises(NotImplementedError):
            self.toolchain(spec)

        # Manually specified build_dirs do not get deleted from the
        # filesystem
        self.assertTrue(exists(spec['build_dir']))

        not_exist = join(spec['build_dir'], 'not_exist')

        spec['build_dir'] = not_exist
        with self.assertRaises(OSError):
            # well, dir does not exist
            self.toolchain(spec)

        # Manually specified build_dirs do not get modified if they just
        # simply don't exist.
        self.assertEqual(spec['build_dir'], not_exist)

    def test_toolchain_standard_build_dir_remapped(self):
        """
        This can either be caused by relative paths or symlinks.  Will
        result in the manually specified build_dir being remapped to its
        real location
        """

        fake = mkdtemp(self)
        real = mkdtemp(self)
        real_base = basename(real)
        spec = Spec()
        spec['build_dir'] = join(fake, pardir, real_base)

        with pretty_logging(stream=StringIO()) as s:
            with self.assertRaises(NotImplementedError):
                self.toolchain(spec)

        self.assertIn('realpath of build_dir resolved to', s.getvalue())
        self.assertEqual(spec['build_dir'], real)

    def test_toolchain_target_build_dir_inside(self):
        """
        Mostly a sanity check; who knows if anyone will write some
        config that will break this somehow.
        """

        source = mkdtemp(self)
        build_dir = mkdtemp(self)

        with open(join(source, 'source.js'), 'w') as fd:
            fd.write('Hello world.')

        spec = Spec(
            build_dir=build_dir,
            transpile_source_map={
                # lol ``.`` being valid namespace in node
                '../source': join(source, 'source'),
            },
        )
        with self.assertRaises(ValueError):
            self.toolchain(spec)