Esempio n. 1
0
    def test_add_import_for_test_uuid_tempest(self):
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        test1 = ("    def test_test():\n" "        pass\n")
        test2 = ("    def test_another_test():\n" "        pass\n")
        source_code = test1 + test2
        fake_file.write(source_code)
        fake_file.close()

        def fake_import_name(node):
            return node.name

        checker._import_name = fake_import_name

        class Fake_node():
            def __init__(self, lineno, col_offset, name):
                self.lineno = lineno
                self.col_offset = col_offset
                self.name = name

        class Fake_src_parsed():
            body = [
                Fake_node(1, 4, 'tempest.a_fake_module'),
                Fake_node(3, 4, 'another_fake_module')
            ]

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed(),
                                          fake_file.name)
        (patch_id, patch), = patcher.patches.items()
        self.assertEqual(patcher._quote(check_uuid.IMPORT_LINE + '\n'), patch)
        expected_source = patcher._quote(test1) + '{' + patch_id + ':s}' +\
            patcher._quote(test2)
        self.assertEqual(expected_source, patcher.source_files[fake_file.name])
Esempio n. 2
0
    def test_add_patch(self):
        patcher = check_uuid.SourcePatcher()
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        file_contents = 'first_line\nsecond_line'
        fake_file.write(file_contents)
        fake_file.close()
        patcher.add_patch(fake_file.name, 'patch', 2)

        source_file = patcher.source_files[fake_file.name]
        self.assertEqual(1, len(patcher.patches))
        (patch_id, patch), = patcher.patches.items()
        self.assertEqual(patcher._quote('patch\n'), patch)
        self.assertEqual('first_line\n{%s:s}second_line' % patch_id,
                         patcher._unquote(source_file))
Esempio n. 3
0
    def test_add_import_no_import(self):
        patcher = check_uuid.SourcePatcher()
        patcher.add_patch = mock.Mock()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        fake_file.close()

        class Fake_src_parsed:
            body = []

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed,
                                          fake_file.name)

        self.assertTrue(not patcher.add_patch.called)
Esempio n. 4
0
    def test_apply_patches(self):
        fake_file = tempfile.NamedTemporaryFile("w+t")
        patcher = check_uuid.SourcePatcher()
        patcher.patches = {'fake-uuid': patcher._quote('patch\n')}
        patcher.source_files = {
            fake_file.name: patcher._quote('first_line\n') +
            '{fake-uuid:s}second_line'}
        with mock.patch('sys.stdout'):
            patcher.apply_patches()

        lines = fake_file.read().split('\n')
        fake_file.close()
        self.assertEqual(['first_line', 'patch', 'second_line'], lines)
        self.assertFalse(patcher.patches)
        self.assertFalse(patcher.source_files)
Esempio n. 5
0
    def test_add_import_for_test_uuid_no_tempest(self):
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t")

        class Fake_src_parsed():
            body = ['test_node']

        checker._import_name = mock.Mock(return_value='fake_module')

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed(),
                                          fake_file.name)
        (patch_id, patch), = patcher.patches.items()
        self.assertEqual(patcher._quote('\n' + check_uuid.IMPORT_LINE + '\n'),
                         patch)
        self.assertEqual('{%s:s}' % patch_id,
                         patcher.source_files[fake_file.name])
Esempio n. 6
0
    def test_add_import_for_test_uuid_tempest(self):
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        source_code = "from tempest import a_fake_module\n"
        fake_file.write(source_code)
        fake_file.close()

        class Fake_src_parsed:
            body = [TestTestChecker.get_mocked_ast_object(
                1, 4, 'tempest', 'a_fake_module', ast.ImportFrom)]

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed,
                                          fake_file.name)
        patcher.apply_patches()

        with open(fake_file.name, "r") as f:
            expected_result = source_code + TestTestChecker.IMPORT_LINE
            self.assertTrue(expected_result == f.read())
Esempio n. 7
0
    def _test_add_uuid_to_test(self, source_file):
        class Fake_test_node():
            lineno = 1
            col_offset = 4
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        fake_file.write(source_file)
        fake_file.close()
        checker._add_uuid_to_test(patcher, Fake_test_node(), fake_file.name)

        self.assertEqual(1, len(patcher.patches))
        self.assertEqual(1, len(patcher.source_files))
        (patch_id, patch), = patcher.patches.items()
        changed_source_file, = patcher.source_files.values()
        self.assertEqual('{%s:s}%s' % (patch_id, patcher._quote(source_file)),
                         changed_source_file)
        expected_patch_start = patcher._quote(
            '    ' + check_uuid.DECORATOR_TEMPLATE.split('(')[0])
        self.assertTrue(patch.startswith(expected_patch_start))