Ejemplo n.º 1
0
class TestBuildSystemGenerator(TestCase):
    @classmethod
    def setUp(self):
        self.bsg = BuildSystemGenerator()

        self.funcs = {
            'dumb': dummy_function,
            'dump_new': dump_args_to_json_file_with_newlines,
            'dump': dump_args_to_json_file
        }

    def test_initiation_funcs_empty(self):
        self.assertEqual(self.bsg.funcs, {})

    def test_initiation_funcs_initialized(self):
        func = {'a': dummy_function}
        bs = BuildSystemGenerator(func)
        self.assertEqual(bs.funcs, func)

    def test_initiation_funcs_initialized_tuple(self):
        func = ('a', dummy_function)
        bs = BuildSystemGenerator(func)
        self.assertEqual(bs.funcs, {})

    def test_initiation_internal_stages(self):
        self.assertTrue(isinstance(self.bsg._stages, BuildSystem))

    def test_initiation_dependency_checking_object(self):
        self.assertTrue(isinstance(self.bsg.check, DependencyChecks))

    def test_initiation_empty_system(self):
        self.assertEqual(self.bsg.system, None)

    def test_initiation_of_tree_structures(self):
        self.assertTrue(isinstance(self.bsg._process_jobs, dict))
        self.assertTrue(isinstance(self.bsg._process_tree, dict))

    def test_initial_object_is_open(self):
        self.assertFalse(self.bsg._final)

    def test_check_method_correct_setting(self):
        self.assertEqual(self.bsg.check._check, self.bsg.check_method)

    def test_check_method_correct_value(self):
        self.assertTrue(self.bsg.check_method in self.bsg.check.checks)

    def test_check_method_resistance(self):
        o = self.bsg.check_method

        self.bsg.check_method = '_abcdefg'
        self.assertEqual(o, self.bsg.check_method)

    def test_check_method_change_success(self):
        o = self.bsg.check_method

        self.bsg.check_method = 'hash'
        self.assertNotEqual(o, self.bsg.check_method)

    def test_process_strings_no_token(self):
        string = 'this string has no replacement tokens'
        self.assertEqual(self.bsg.process_strings(string, {}), string)

    def test_process_replacement_strings_not_dict(self):
        with self.assertRaises(TypeError):
            self.bsg.process_strings(['a'], 'f{o}o')

    def test_process_successful_replacement(self):
        new = 'this is not a car.'
        old = 'this {is} a car.'

        p = self.bsg.process_strings(old, {'is': 'is not'})
        self.assertEqual(p, new)

    def test_process_irrelevant_spec(self):
        old = 'this {is} a car.'

        with self.assertRaises(InvalidJob):
            p = self.bsg.process_strings(old, {"isn't'": 'is not'})

    def test_process_half_relevant_spec(self):
        old = 'this {works} or {not}'
        new = 'this wwworks or {not}'
        strings = {'works': 'wwworks', 'car': 'cccar'}

        with self.assertRaises(InvalidJob):
            self.assertEqual(self.bsg.process_strings(old, strings), new)

    def test_process_replacements_in_dict(self):
        old = {'message': 'this {car} {works}'}
        new = {'message': 'this cccar wwworks'}
        strings = {'works': 'wwworks', 'car': 'cccar'}

        ret = self.bsg.process_strings(old, strings)
        self.assertEqual(ret, new)

        alt = {'message': old['message'].format(**strings)}
        self.assertEqual(alt, new)

    def test_generate_job_list(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({
            'job': 'dumb',
            'args': [1, 2]
        }, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_tuple(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': 'dumb', 'args': args}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_dict(self):
        args = {'a': 1, 'b': 2}
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': 'dumb', 'args': args}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_malformed_args(self):
        args = BuildSystem()

        with self.assertRaises(InvalidJob):
            self.bsg.generate_job({'job': 'dumb', 'args': args}, self.funcs)

    def test_generate_job_non_extant_function(self):
        args = (1, 2)

        with self.assertRaises(InvalidJob):
            self.bsg.generate_job({'job': 'Nope', 'args': args}, self.funcs)

    def test_generate_job_list_func_callable(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({
            'job': dummy_function,
            'args': [1, 2]
        }, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_tuple_func_callable(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({
            'job': dummy_function,
            'args': args
        }, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_dict_func_callable(self):
        args = {'a': 1, 'b': 2}
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({
            'job': dummy_function,
            'args': args
        }, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_malformed_args_func_callable(self):
        args = BuildSystem()

        with self.assertRaises(InvalidJob):
            self.bsg.generate_job({
                'job': dummy_function,
                'args': args
            }, self.funcs)

    def test_adding_task_to_funcs(self):
        self.assertEqual(self.bsg.funcs, {})
        self.bsg.add_task('foo', dummy_function)
        self.assertEqual(self.bsg.funcs, {'foo': dummy_function})

    def test_adding_task_to_funcs_invalid_job(self):
        self.assertEqual(self.bsg.funcs, {})
        with self.assertRaises(InvalidJob):
            self.bsg.add_task('foo', BuildSystemGenerator)
        self.assertEqual(self.bsg.funcs, {})

    # TODO tests for ingest_yaml
    # TODO tests for ingest_json

    def test_dependency_string_full_name(self):
        spec = {
            'dependency': 'a b c d',
            'target': '/tmp/files',
            'msg': 'alpha'
        }
        expected = ['a', 'b', 'c', 'd']

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_string_shortest(self):
        spec = {'dep': 'a b c d', 'target': '/tmp/files', 'msg': 'alpha'}
        expected = ['a', 'b', 'c', 'd']

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_string_short_alt(self):
        spec = {'deps': 'a b c d', 'target': '/tmp/files', 'msg': 'alpha'}
        expected = ['a', 'b', 'c', 'd']

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_list_full_name(self):
        spec = {
            'dependency': ['a', 'b', 'c', 'd'],
            'target': '/tmp/files',
            'msg': 'alpha'
        }
        expected = ['a', 'b', 'c', 'd']

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_list_shortest(self):
        spec = {
            'dep': ['a', 'b', 'c', 'd'],
            'target': '/tmp/files',
            'msg': 'alpha'
        }
        expected = ['a', 'b', 'c', 'd']

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_list_short_alt(self):
        spec = {
            'deps': ['a', 'b', 'c', 'd'],
            'target': '/tmp/files',
            'msg': 'alpha'
        }
        expected = ['a', 'b', 'c', 'd']

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_process_job_with_stage_and_target(self):
        spec = {
            'target': 'other',
            'dependency': 'string',
            'job': 'dumb',
            'args': None,
            'stage': 'one',
        }

        with self.assertRaises(InvalidJob):
            self.bsg._process_job(spec)

    def test_process_dependency_list(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'force'

        spec = {
            'deps': ['a', 'b', 'c', 'd'],
            'stage': 'test',
            'target': '/tmp/files0',
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.bsg._process_dependency(spec)
        self.assertEqual(spec['deps'], self.bsg._process_tree[spec['target']])

    def test_process_dependency_string(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'force'

        spec = {
            'deps': 'a b c d',
            'stage': 'test',
            'target': '/tmp/files1',
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.bsg._process_dependency(spec)
        self.assertEqual(spec['deps'].split(),
                         self.bsg._process_tree[spec['target']])

    def test_process_dependency_rebuild_needed(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'force'

        spec = {
            'deps': 'a b c d',
            'stage': 'test',
            'target': '/tmp/files2',
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.bsg._process_dependency(spec)
        self.assertTrue(self.bsg._process_jobs[spec['target']][1])

    def test_process_dependency_rebuild_not_needed(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'ignore'

        spec = {
            'deps': 'a b c d',
            'stage': 'test',
            'target': '/tmp/files3',
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.bsg._process_dependency(spec)
        self.assertFalse(self.bsg._process_jobs[spec['target']][1])

    def test_job_processing_dep_target(self):
        spec = {
            'target': '/tmp/files3',
            'dependency': ['a', 'b'],
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_no_dep_target(self):
        spec = {
            'target': '/tmp/files3',
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_dep_stage(self):
        spec = {
            'stage': '/tmp/files3',
            'dependency': ['a', 'b'],
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_no_dep_stage(self):
        spec = {
            'stage': '/tmp/files3',
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_invalid_with_stage_and_target(self):
        spec = {
            'stage': 'test',
            'target': '/tmp/files3',
            'dependency': ['a', 'b'],
            'dir': '/tmp',
            'cmd': 'touch',
            'args': ['a', 'b'],
            'msg': 'alpha'
        }

        with self.assertRaises(InvalidJob):
            self.bsg._process_job(spec)
Ejemplo n.º 2
0
class TestBuildSystemGenerator(TestCase):
    @classmethod
    def setUp(self):
        self.bsg = BuildSystemGenerator()

        self.funcs = { 'dumb': dummy_function,
                       'dump_new': dump_args_to_json_file_with_newlines,
                       'dump': dump_args_to_json_file }

    def test_initiation_funcs_empty(self):
        self.assertEqual(self.bsg.funcs, {})

    def test_initiation_funcs_initialized(self):
        func = { 'a': dummy_function }
        bs = BuildSystemGenerator(func)
        self.assertEqual(bs.funcs, func)

    def test_initiation_funcs_initialized_tuple(self):
        func = ('a', dummy_function)
        bs = BuildSystemGenerator(func)
        self.assertEqual(bs.funcs, {})

    def test_initiation_internal_stages(self):
        self.assertTrue(isinstance(self.bsg._stages, BuildSystem))

    def test_initiation_dependency_checking_object(self):
        self.assertTrue(isinstance(self.bsg.check, DependencyChecks))

    def test_initiation_empty_system(self):
        self.assertEqual(self.bsg.system, None)

    def test_initiation_of_tree_structures(self):
        self.assertTrue(isinstance(self.bsg._process_jobs, dict))
        self.assertTrue(isinstance(self.bsg._process_tree, dict))

    def test_initial_object_is_open(self):
        self.assertFalse(self.bsg._final)

    def test_check_method_correct_setting(self):
        self.assertEqual(self.bsg.check._check, self.bsg.check_method)

    def test_check_method_correct_value(self):
        self.assertTrue(self.bsg.check_method in self.bsg.check.checks)

    def test_check_method_resistance(self):
        o = self.bsg.check_method

        self.bsg.check_method = '_abcdefg'
        self.assertEqual(o, self.bsg.check_method)

    def test_check_method_change_success(self):
        o = self.bsg.check_method

        self.bsg.check_method = 'hash'
        self.assertNotEqual(o, self.bsg.check_method)


    def test_process_strings_no_token(self):
        string = 'this string has no replacement tokens'
        self.assertEqual(self.bsg.process_strings(string, {}), string)

    def test_process_replacement_strings_not_dict(self):
        with self.assertRaises(TypeError):
            self.bsg.process_strings(['a'], 'f{o}o')

    def test_process_successful_replacement(self):
        new = 'this is not a car.'
        old = 'this {is} a car.'

        p = self.bsg.process_strings(old, { 'is': 'is not'})
        self.assertEqual(p, new)

    def test_process_irrelevant_spec(self):
        old = 'this {is} a car.'

        with self.assertRaises(InvalidJob):
            p = self.bsg.process_strings(old,{ "isn't'": 'is not'})

    def test_process_half_relevant_spec(self):
        old = 'this {works} or {not}'
        new = 'this wwworks or {not}'
        strings = {'works': 'wwworks', 'car': 'cccar'}

        with self.assertRaises(InvalidJob):
            self.assertEqual(self.bsg.process_strings(old, strings), new)

    def test_process_replacements_in_dict(self):
        old = {'message': 'this {car} {works}'}
        new = {'message': 'this cccar wwworks'}
        strings = {'works': 'wwworks', 'car': 'cccar'}

        ret = self.bsg.process_strings(old, strings)
        self.assertEqual(ret, new)

        alt = { 'message': old['message'].format(**strings)}
        self.assertEqual(alt, new)

    def test_generate_job_list(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': 'dumb', 'args': [1, 2]}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_tuple(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': 'dumb', 'args': args}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_dict(self):
        args = {'a': 1, 'b': 2}
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': 'dumb', 'args': args}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_malformed_args(self):
        args = BuildSystem()

        with self.assertRaises(InvalidJob):
            self.bsg.generate_job({'job': 'dumb', 'args': args}, self.funcs)

    def test_generate_job_non_extant_function(self):
        args = (1, 2)

        with self.assertRaises(InvalidJob):
            self.bsg.generate_job({'job': 'Nope', 'args': args}, self.funcs)

    def test_generate_job_list_func_callable(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': dummy_function, 'args': [1, 2]}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_tuple_func_callable(self):
        args = (1, 2)
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': dummy_function, 'args': args}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_dict_func_callable(self):
        args = {'a': 1, 'b': 2}
        expected = (dummy_function, args)
        ret = self.bsg.generate_job({'job': dummy_function, 'args': args}, self.funcs)

        self.assertEqual(expected, ret)

    def test_generate_job_malformed_args_func_callable(self):
        args = BuildSystem()

        with self.assertRaises(InvalidJob):
            self.bsg.generate_job({'job': dummy_function, 'args': args}, self.funcs)

    def test_adding_task_to_funcs(self):
        self.assertEqual(self.bsg.funcs, {})
        self.bsg.add_task('foo', dummy_function)
        self.assertEqual(self.bsg.funcs, {'foo': dummy_function})

    def test_adding_task_to_funcs_invalid_job(self):
        self.assertEqual(self.bsg.funcs, {})
        with self.assertRaises(InvalidJob):
            self.bsg.add_task('foo', BuildSystemGenerator)
        self.assertEqual(self.bsg.funcs, {})

    # TODO tests for ingest_yaml
    # TODO tests for ingest_json

    def test_dependency_string_full_name(self):
        spec = { 'dependency': 'a b c d',
                 'target': '/tmp/files',
                 'msg': 'alpha' }
        expected = [ 'a', 'b', 'c', 'd' ]

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_string_shortest(self):
        spec = { 'dep': 'a b c d',
                 'target': '/tmp/files',
                 'msg': 'alpha' }
        expected = [ 'a', 'b', 'c', 'd' ]

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_string_short_alt(self):
        spec = { 'deps': 'a b c d',
                 'target': '/tmp/files',
                 'msg': 'alpha' }
        expected = [ 'a', 'b', 'c', 'd' ]

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_list_full_name(self):
        spec = { 'dependency': ['a', 'b', 'c', 'd'],
                 'target': '/tmp/files',
                 'msg': 'alpha' }
        expected = [ 'a', 'b', 'c', 'd' ]

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_list_shortest(self):
        spec = { 'dep': ['a', 'b', 'c', 'd'],
                 'target': '/tmp/files',
                 'msg': 'alpha' }
        expected = [ 'a', 'b', 'c', 'd' ]

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_dependency_list_short_alt(self):
        spec = { 'deps': ['a', 'b', 'c', 'd'],
                 'target': '/tmp/files',
                 'msg': 'alpha' }
        expected = [ 'a', 'b', 'c', 'd' ]

        self.assertEqual(self.bsg.get_dependency_list(spec), expected)

    def test_process_job_with_stage_and_target(self):
        spec = {
            'target': 'other',
            'dependency': 'string',
            'job': 'dumb',
            'args': None,
            'stage': 'one',
        }

        with self.assertRaises(InvalidJob):
            self.bsg._process_job(spec)

    def test_process_dependency_list(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'force'

        spec = { 'deps': ['a', 'b', 'c', 'd'],
                 'stage': 'test',
                 'target': '/tmp/files0',
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.bsg._process_dependency(spec)
        self.assertEqual(spec['deps'], self.bsg._process_tree[spec['target']])

    def test_process_dependency_string(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'force'

        spec = { 'deps': 'a b c d',
                 'stage': 'test',
                 'target': '/tmp/files1',
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.bsg._process_dependency(spec)
        self.assertEqual(spec['deps'].split(),
                         self.bsg._process_tree[spec['target']])

    def test_process_dependency_rebuild_needed(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'force'

        spec = { 'deps': 'a b c d',
                 'stage': 'test',
                 'target': '/tmp/files2',
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.bsg._process_dependency(spec)
        self.assertTrue(self.bsg._process_jobs[spec['target']][1])

    def test_process_dependency_rebuild_not_needed(self):
        self.assertEqual(self.bsg._process_jobs, {})
        self.assertEqual(self.bsg._process_tree, {})

        self.bsg.check.check_method = 'ignore'

        spec = { 'deps': 'a b c d',
                 'stage': 'test',
                 'target': '/tmp/files3',
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.bsg._process_dependency(spec)
        self.assertFalse(self.bsg._process_jobs[spec['target']][1])

    def test_job_processing_dep_target(self):
        spec = { 'target': '/tmp/files3',
                 'dependency': ['a', 'b'],
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_no_dep_target(self):
        spec = { 'target': '/tmp/files3',
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_dep_stage(self):
        spec = { 'stage': '/tmp/files3',
                 'dependency': ['a', 'b'],
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_no_dep_stage(self):
        spec = { 'stage': '/tmp/files3',
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        self.assertTrue(self.bsg._process_job(spec))

    def test_job_processing_invalid_with_stage_and_target(self):
        spec = { 'stage': 'test',
                 'target': '/tmp/files3',
                 'dependency': ['a', 'b'],
                 'dir': '/tmp',
                 'cmd': 'touch',
                 'args': ['a', 'b'],
                 'msg': 'alpha' }

        with self.assertRaises(InvalidJob):
            self.bsg._process_job(spec)
Ejemplo n.º 3
0
class TestBuildGeneratorProcessStageSpecs(TestCase):
    @classmethod
    def setUp(self):
        self.bsg = BuildSystemGenerator()
        self.bsg.add_task('dumb', dummy_function)
        self.bsg.add_task('dumb_json', dump_args_to_json_file)
        self.bsg.add_task('dumb_json_newline',
                          dump_args_to_json_file_with_newlines)
        self.bsg.add_task('shell', subprocess.call)

        self.funcs = {
            'dumb': dummy_function,
            'dump_json_newline': dump_args_to_json_file_with_newlines,
            'shell': subprocess.call,
            'dumb_json': dump_args_to_json_file
        }

        self.strings = {'is': 'is not', 'can': 'cannot'}

    def test_process_stage_python_job(self):
        spec = {
            'job': 'dumb',
            'args': [1, 2, 3],
            'stage': 'test',
            'msg': 'wtf'
        }
        ret = self.bsg._process_stage(spec)
        expected = self.bsg.generate_job(spec, self.funcs)
        self.assertEqual(ret, expected)

    def test_process_stage_python_job_with_replacement(self):
        spec = {
            'job': 'dumb',
            'args': [1, 2, 3],
            'stage': 'test',
            'msg': 'wtf {can} this be or {is}'
        }

        ret = self.bsg._process_stage(spec, strings=self.strings)

        ex_spec = self.bsg.process_strings(spec, self.strings)
        expected = self.bsg.generate_job(ex_spec, self.funcs)

        self.assertEqual(ret, expected)

    def test_process_stage_shell_job(self):
        spec = {
            'dir': '/tmp',
            'args': [1, 2, 3],
            'cmd': 'cat',
            'stage': 'test',
            'msg': 'wtf'
        }

        ret = self.bsg._process_stage(spec)
        expected = self.bsg.generate_shell_job(spec)
        self.assertEqual(ret, expected)

    def test_process_stage_shell_job_with_replacement(self):
        spec = {
            'dir': '/tmp',
            'args': [1, 2, 3],
            'cmd': 'cat',
            'stage': 'test',
            'msg': 'wtf {can} this be or {is}'
        }

        ex_spec = self.bsg.process_strings(spec, self.strings)
        ret = self.bsg._process_stage(spec, strings=self.strings)
        expected = self.bsg.generate_shell_job(ex_spec)
        self.assertEqual(ret, expected)

    def test_process_stage_task_sequence(self):
        spec = {
            'stage':
            'test',
            'tasks': [{
                'dir': '/tmp',
                'args': [1, 2, 3],
                'cmd': 'cat',
                'stage': 'test',
                'msg': 'wtf {can} this be or {is}'
            }, {
                'job': 'dumb',
                'args': [1, 2, 3],
                'stage': 'test',
                'msg': 'wtf'
            }],
            'msg':
            'wtf {can} this be or {is}'
        }

        ret = self.bsg._process_stage(spec)
        expected = (self.bsg.generate_sequence(spec, self.funcs).run, None)
        self.assertEqual(str(ret)[:-18], str(expected)[:-18])

    def test_process_stage_task_sequence_replacement(self):
        spec = {
            'stage':
            'test',
            'tasks': [{
                'dir': '/tmp',
                'args': [1, 2, 3],
                'cmd': 'cat',
                'stage': 'test',
                'msg': 'wtf {can} this be or {is}'
            }, {
                'job': 'dumb',
                'args': [1, 2, 3],
                'stage': 'test',
                'msg': 'wtf'
            }],
            'msg':
            'wtf {can} this be or {is}'
        }

        ex_spec = self.bsg.process_strings(spec, self.strings)
        ret = self.bsg._process_stage(spec, strings=self.strings)
        expected = (self.bsg.generate_sequence(ex_spec, self.funcs).run, None)
        self.assertEqual(str(ret)[:-18], str(expected)[:-18])

    def test_invalid_spec(self):
        spec = {'is': 'is not', 'can': 'cannot'}

        with self.assertRaises(InvalidJob):
            self.bsg._process_stage(spec)

    def test_invalid_spec_with_keys(self):
        spec_keys = set(['stage', 'task'])

        spec = {'stage': 'is not', 'test': 'cannot', 'msg': 'stage'}

        with self.assertRaises(InvalidJob):
            self.bsg._process_stage(spec, spec_keys=spec_keys)

    def test_invalid_spec_with_keys(self):
        spec_keys = set(['stage', 'task'])

        spec = {'is': 'is not', 'can': 'cannot'}

        with self.assertRaises(InvalidJob):
            self.bsg._process_stage(spec, spec_keys=spec_keys)

    def test_process_stage_shell_job_with_keys(self):
        spec_keys = set(['dir', 'cmd', 'args', 'stage'])

        spec = {
            'dir': '/tmp',
            'args': [1, 2, 3],
            'cmd': 'cat',
            'stage': 'test',
            'msg': 'wtf'
        }

        ret = self.bsg._process_stage(spec)
        expected = self.bsg.generate_shell_job(spec)
        self.assertEqual(ret, expected)
Ejemplo n.º 4
0
class TestBuildGeneratorProcessStageSpecs(TestCase):
    @classmethod
    def setUp(self):
        self.bsg = BuildSystemGenerator()
        self.bsg.add_task('dumb', dummy_function)
        self.bsg.add_task('dumb_json', dump_args_to_json_file)
        self.bsg.add_task('dumb_json_newline', dump_args_to_json_file_with_newlines)
        self.bsg.add_task('shell', subprocess.call)

        self.funcs = { 'dumb': dummy_function,
                       'dump_json_newline': dump_args_to_json_file_with_newlines,
                       'shell': subprocess.call,
                       'dumb_json': dump_args_to_json_file }

        self.strings = { 'is': 'is not',
                         'can': 'cannot' }

    def test_process_stage_python_job(self):
        spec = { 'job': 'dumb',
                 'args': [1,2,3],
                 'stage': 'test',
                 'msg': 'wtf'}
        ret = self.bsg._process_stage(spec)
        expected = self.bsg.generate_job(spec, self.funcs)
        self.assertEqual(ret, expected)

    def test_process_stage_python_job_with_replacement(self):
        spec = { 'job': 'dumb',
                 'args': [1,2,3],
                 'stage': 'test',
                 'msg': 'wtf {can} this be or {is}'}

        ret = self.bsg._process_stage(spec, strings=self.strings)

        ex_spec = self.bsg.process_strings(spec, self.strings)
        expected = self.bsg.generate_job(ex_spec, self.funcs)

        self.assertEqual(ret, expected)

    def test_process_stage_shell_job(self):
        spec = { 'dir': '/tmp',
                 'args': [1,2,3],
                 'cmd': 'cat',
                 'stage': 'test',
                 'msg': 'wtf'}

        ret = self.bsg._process_stage(spec)
        expected = self.bsg.generate_shell_job(spec)
        self.assertEqual(ret, expected)

    def test_process_stage_shell_job_with_replacement(self):
        spec = { 'dir': '/tmp',
                 'args': [1,2,3],
                 'cmd': 'cat',
                 'stage': 'test',
                 'msg': 'wtf {can} this be or {is}'}

        ex_spec = self.bsg.process_strings(spec, self.strings)
        ret = self.bsg._process_stage(spec, strings=self.strings)
        expected = self.bsg.generate_shell_job(ex_spec)
        self.assertEqual(ret, expected)

    def test_process_stage_task_sequence(self):
        spec = { 'stage': 'test',
                 'tasks': [
                     { 'dir': '/tmp',
                       'args': [1,2,3],
                       'cmd': 'cat',
                       'stage': 'test',
                       'msg': 'wtf {can} this be or {is}'},
                     { 'job': 'dumb',
                       'args': [1,2,3],
                       'stage': 'test',
                       'msg': 'wtf' }
                    ],
                 'msg': 'wtf {can} this be or {is}'}

        ret = self.bsg._process_stage(spec)
        expected = (self.bsg.generate_sequence(spec, self.funcs).run, None)
        self.assertEqual(str(ret)[:-18], str(expected)[:-18])

    def test_process_stage_task_sequence_replacement(self):
        spec = { 'stage': 'test',
                 'tasks': [
                     { 'dir': '/tmp',
                       'args': [1,2,3],
                       'cmd': 'cat',
                       'stage': 'test',
                       'msg': 'wtf {can} this be or {is}'},
                     { 'job': 'dumb',
                       'args': [1,2,3],
                       'stage': 'test',
                       'msg': 'wtf' }
                    ],
                 'msg': 'wtf {can} this be or {is}'}

        ex_spec = self.bsg.process_strings(spec, self.strings)
        ret = self.bsg._process_stage(spec, strings=self.strings)
        expected = (self.bsg.generate_sequence(ex_spec, self.funcs).run, None)
        self.assertEqual(str(ret)[:-18], str(expected)[:-18])

    def test_invalid_spec(self):
        spec = { 'is': 'is not',
                 'can': 'cannot' }

        with self.assertRaises(InvalidJob):
            self.bsg._process_stage(spec)

    def test_invalid_spec_with_keys(self):
        spec_keys = set(['stage', 'task'])

        spec = { 'stage': 'is not',
                 'test': 'cannot',
                 'msg': 'stage' }

        with self.assertRaises(InvalidJob):
            self.bsg._process_stage(spec, spec_keys=spec_keys)

    def test_invalid_spec_with_keys(self):
        spec_keys = set(['stage', 'task'])

        spec = { 'is': 'is not',
                 'can': 'cannot' }

        with self.assertRaises(InvalidJob):
            self.bsg._process_stage(spec, spec_keys=spec_keys)

    def test_process_stage_shell_job_with_keys(self):
        spec_keys = set(['dir', 'cmd', 'args', 'stage'])

        spec = { 'dir': '/tmp',
                 'args': [1,2,3],
                 'cmd': 'cat',
                 'stage': 'test',
                 'msg': 'wtf'}

        ret = self.bsg._process_stage(spec)
        expected = self.bsg.generate_shell_job(spec)
        self.assertEqual(ret, expected)