Exemplo n.º 1
0
# Copyright (C) 2020 Daniel Lutz <*****@*****.**>

import testenv  # also sets up module search paths
import dlb.ex
import dlb_contrib.generic
import dlb_contrib.gcc
import dlb_contrib.gnubinutils
import os.path
import unittest


class ThisIsAUnitTest(unittest.TestCase):
    pass


@unittest.skipIf(not testenv.has_executable_in_path('gcc'),
                 'requires gcc in $PATH')
@unittest.skipIf(not testenv.has_executable_in_path('ar'),
                 'requires ar in $PATH')
class ArTest(testenv.TemporaryWorkingDirectoryTestCase):
    def test_example(self):
        with open('a.c', 'w') as f:
            f.write('int f() {return 0;}\n')
        with open('b.c', 'w') as f:
            f.write('int g() {return 0;}\n')

        import dlb.di
        import dlb.cf
        dlb.cf.level.helper_execution = dlb.di.ERROR

        with dlb.ex.Context():
Exemplo n.º 2
0
            dlb_contrib.gcc.ObjectOrArchivePath('a:b.src')

    def test_fails_for_invalid_linkable_file(self):
        with self.assertRaises(ValueError):
            dlb_contrib.gcc.ObjectOrArchivePath('')
        with self.assertRaises(ValueError):
            dlb_contrib.gcc.ObjectOrArchivePath('.o')
        with self.assertRaises(ValueError):
            dlb_contrib.gcc.ObjectOrArchivePath('.a')
        with self.assertRaises(ValueError):
            dlb_contrib.gcc.ObjectOrArchivePath('..o')
        with self.assertRaises(ValueError):
            dlb_contrib.gcc.ObjectOrArchivePath('..a')


@unittest.skipIf(not testenv.has_executable_in_path('gcc'), 'requires gcc in $PATH')
class CTest(testenv.TemporaryWorkingDirectoryTestCase):

    def test_example(self):
        os.mkdir('i')

        with open('a.c', 'w', encoding='utf-8') as f:
            f.write(textwrap.dedent(
                '''
                #include "a.h"
                
                #ifndef ONE
                    #error "ONE is not defined"
                #endif
                
                #ifdef __GNUC__
Exemplo n.º 3
0
        if result.commit_number_from_tag_to_latest_commit:
            wd_version += f'-dev{result.commit_number_from_tag_to_latest_commit}' \
                          f'+{result.latest_commit_hash[:shortened_commit_hash_length]}'
        if result.has_changes_in_tracked_files:
            wd_version += '?'

        result.wd_version = wd_version
        result.version_components = (
            int(m.group('major')), int(m.group('minor')), int(m.group('micro')),
            m.group('post'), None if m.group('post_number') is None else int(m.group('post_number'))
        )

        return True


@unittest.skipIf(not testenv.has_executable_in_path('git'), 'requires git in $PATH')
@unittest.skipIf(not testenv.has_executable_in_path('sh'), 'requires sh in $PATH')
class GitDescribeWorkingDirectoryTest(testenv.TemporaryWorkingDirectoryTestCase):

    def test_line_output(self):
        with dlb.ex.Context():
            class AddLightWeightTag(dlb_contrib.sh.ShScriptlet):
                SCRIPTLET = 'git tag v2'  # light-weight tag does not affect 'git describe'

            PrepareGitRepo().start().complete()
            AddLightWeightTag().start().complete()
            result = DescribeWorkingDirectory().start()

        dlb.di.inform(f"version: {result.version_components!r}, wd version: {result.wd_version!r}")
        dlb.di.inform(f"changed: {result.modification_by_file.keys()!r}")
        self.assertEqual({
Exemplo n.º 4
0
            def process(self, chunk: bytes, is_last: bool):
                if b'i' in chunk:
                    self.result.append(chunk)

        return Processor()


class QuoteTest(unittest.TestCase):
    def test_it(self):
        self.assertEqual("''", dlb_contrib.sh.quote(''))
        self.assertEqual("'a\"b'", dlb_contrib.sh.quote('a"b'))
        self.assertEqual("'a'\\''b'", dlb_contrib.sh.quote("a'b"))


@unittest.skipIf(not testenv.has_executable_in_path('sh'),
                 'requires sh in $PATH')
class ShTest(testenv.TemporaryWorkingDirectoryTestCase):
    def test_line_output(self):
        with dlb.ex.Context():
            output = OutputTwoLines().start().processed_output
        self.assertEqual(b'first\nsecond\n', output)

    def test_incremental_line_output(self):
        with dlb.ex.Context():
            output = OutputThreeLinesIncrementally().start().processed_output
        self.assertEqual([b'first', b'third'], output)

    def test_read_files(self):
        with open('a', 'xb') as f:
            f.write(b'aah... ')
Exemplo n.º 5
0
        with open('doxypress.json', 'wb') as f:
            f.write(b'${{xyz}}')

        with self.assertRaises(ValueError) as cm:
            with dlb.ex.Context():
                DoxyPress(project_template_file='doxypress.json',
                          source_directories=['.'],
                          output_directory='d/').start()
        msg = (
            "unexpanded placeholder in project file template 'doxypress.json'\n"
            "  | file contains '${{xyz}}' but 'TEXTUAL_REPLACEMENTS' does not define a replacement"
        )
        self.assertEqual(msg, str(cm.exception))


@unittest.skipIf(not testenv.has_executable_in_path('doxypress'),
                 'requires doxypress in $PATH')
class DoxyPressExecutionTest(testenv.TemporaryWorkingDirectoryTestCase):
    def test_fails_for_empty_doxyfile(self):
        open('doxypress.json', 'xb').close()

        with self.assertRaises(dlb.ex.HelperExecutionError):
            with dlb.ex.Context():
                dlb_contrib.doxypress.DoxyPress(
                    project_template_file='doxypress.json',
                    source_directories=['.'],
                    output_directory='d/').start()


@unittest.skipIf(not testenv.has_executable_in_path('doxypress'),
                 'requires doxypress in $PATH')
Exemplo n.º 6
0
        self.assertEqual(({}, ()), r)

        line = '-pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0'
        r = dlb_contrib.pkgconfig.parse_from_output(line, )
        self.assertEqual(({}, ('-pthread', '-I/usr/include/gtk-3.0',
                               '-I/usr/include/at-spi2-atk/2.0')), r)
        r = dlb_contrib.pkgconfig.parse_from_output(line, '-I')
        self.assertEqual(({
            '-I': ('/usr/include/gtk-3.0', '/usr/include/at-spi2-atk/2.0')
        }, ('-pthread', )), r)

        r = dlb_contrib.pkgconfig.parse_from_output('--libs abc')
        self.assertEqual(({}, ('--libs', 'abc')), r)


@unittest.skipIf(not testenv.has_executable_in_path('pkg-config'),
                 'requires pkg-config in $PATH')
@unittest.skipIf(not os.path.isdir('/usr/include/gtk-3.0/'),
                 'requires GTK+ 3.0')
class PkgConfigTest(testenv.TemporaryWorkingDirectoryTestCase):
    def test_gtk(self):
        class PkgConfig(dlb_contrib.pkgconfig.PkgConfig):
            LIBRARY_NAMES = ('gtk+-3.0', 'orte')
            VERSION_CONSTRAINTS_BY_LIBRARY_NAME = {
                'gtk+-3.0': ['> 3.0.1', '< 4.0']
            }

        with dlb.ex.Context():
            dlb.di.set_threshold_level(dlb.di.DEBUG)
            result = PkgConfig().start()
Exemplo n.º 7
0
            dlb_contrib.strace.syscall_from_line(b'123() = 0')

    def test_fails_without_opening_parenthesis(self):
        with self.assertRaises(ValueError):
            dlb_contrib.strace.syscall_from_line(b'read')

    def test_fails_without_return_value(self):
        with self.assertRaises(ValueError):
            dlb_contrib.strace.syscall_from_line(b'read(1, 3)')

    def test_fails_for_empty(self):
        with self.assertRaises(ValueError):
            dlb_contrib.strace.syscall_from_line(b'')


@unittest.skipIf(not testenv.has_executable_in_path('strace'),
                 'requires strace in $PATH')
@unittest.skipIf(not testenv.has_executable_in_path('bash'),
                 'requires bash in $PATH')
@unittest.skipIf(not testenv.has_executable_in_path('cat'),
                 'requires cat in $PATH')
@unittest.skipIf(not testenv.has_executable_in_path('cp'),
                 'requires cat in $PATH')
class RunStracedTest(testenv.TemporaryWorkingDirectoryTestCase):
    def test_discovers_read_files(self):
        with open('x', 'xb') as f:
            f.write(b'abc')
        with open('y', 'xb') as f:
            f.write(b'')

        class ShowContent(dlb_contrib.strace.RunStraced):
Exemplo n.º 8
0
                "invalid line in 'recorded.fls': b'GUGUSELI dada'",
                str(cm.exception))

    def test_fails_for_relative_cwd(self):
        with dlb.ex.Context() as context:
            with open('recorded.fls', 'xb') as f:
                f.write(b'PWD he/he\n'
                        b'INPUT /var/lib/texmf/web2c/pdftex/pdflatex.fmt\n')
            with self.assertRaises(ValueError) as cm:
                dlb_contrib.tex.accessed_files_from_recorded(
                    context, dlb.fs.Path('recorded.fls'))
            self.assertEqual("invalid line in 'recorded.fls': b'PWD he/he'",
                             str(cm.exception))


@unittest.skipIf(not testenv.has_executable_in_path('tex'),
                 'requires latex in $PATH')
class TexTest(testenv.TemporaryWorkingDirectoryTestCase):
    def test_scenario1(self):
        os.mkdir('src')
        with open('report.tex', 'x', encoding='utf-8') as f:
            f.write('Hello world!\\end\n')

        output_path = dlb.fs.Path('build/out/')
        tex = dlb_contrib.tex.Tex(toplevel_file='report.tex',
                                  output_file=output_path / 'report.dvi',
                                  state_files=[])

        with dlb.ex.Context():
            r = tex.start()
            self.assertTrue(r)