示例#1
0
 def setUp(self):
     self.cache = mock.Mock(spec=FilesListComparator)
     self.rjsb = RJSBuild('app/app', '/abc/def', '/ghi/out.js',
                          {'abcd': 'efgh'}, self.cache)
示例#2
0
class TestRJSBuild(unittest.TestCase):
    def setUp(self):
        self.cache = mock.Mock(spec=FilesListComparator)
        self.rjsb = RJSBuild('app/app', '/abc/def', '/ghi/out.js',
                             {'abcd': 'efgh'}, self.cache)

    def test_get_command(self):
        with mock.patch('sett.requirejs.which') as which:
            command = self.rjsb.get_command(abc='def', ghi='klm')

        self.assertEqual(command[0:3], [which.node, which.search.return_value, '-o'])
        self.assertEqual(set(command[3:]), {'abc=def', 'ghi=klm'})

    def test_build(self):
        process = mock.Mock(
            name='r.js',
            stdout=iter([
                b'Requirejs header\n',
                b'--------------\n',
                b'/abc/views/view.js\n',
                b'/abc/app/app.js\n',
            ]),
        )
        process.wait.return_value = 0

        with mock.patch('sett.requirejs.subprocess') as subproc:
            subproc.Popen.return_value = process
            with mock.patch.object(self.rjsb, 'get_command') as get_command:
                self.rjsb.build()

        subproc.Popen.assert_called_once_with(
            get_command.return_value,
            stdout=subproc.PIPE,
        )
        get_command.assert_called_once_with(
            baseUrl='/abc/def',
            mainConfigFile='/abc/def/config.js',
            out='/ghi/out.js',
            abcd='efgh',
        )
        self.cache.write.assert_called_once_with([
            '/abc/views/view.js',
            '/abc/app/app.js',
            '/abc/def/config.js',
        ])

    def test_error(self):
        process = mock.Mock(
            name='r.js',
            stdout=iter([
                b'Requirejs error\n',
            ]),
        )

        with mock.patch('sett.requirejs.subprocess') as subproc:
            subproc.Popen.return_value = process
            with mock.patch.object(self.rjsb, 'get_command'):
                with self.assertRaises(RuntimeError):
                    self.rjsb.build()

        process.wait.assert_called_once_with()