Exemplo n.º 1
0
    def test_app_cleans_up_images(self):
        testdir = os.path.dirname(__file__)
        diagpath = os.path.join(testdir, 'diagrams',
                                'background_url_image.diag')
        urlopen_cache = {}

        def cleanup():
            from blockdiag.utils import images
            urlopen_cache.update(images.urlopen_cache)

        try:
            tmpdir = TemporaryDirectory()
            fd, tmpfile = tmpdir.mkstemp()
            os.close(fd)

            args = ['-T', 'SVG', '-o', tmpfile, diagpath]
            app = BlockdiagApp()
            app.register_cleanup_handler(cleanup)  # to get internal state
            app.run(args)

            self.assertTrue(urlopen_cache)  # check images were cached
            for path in urlopen_cache.values():
                self.assertFalse(os.path.exists(path))  # and removed finally
        finally:
            tmpdir.clean()
Exemplo n.º 2
0
def svg_includes_source_code_tag_test():
    from xml.etree import ElementTree

    testdir = os.path.dirname(__file__)
    diagpath = os.path.join(testdir, 'diagrams', 'single_edge.diag')

    try:
        tmpdir = TemporaryDirectory()
        fd, tmpfile = tmpdir.mkstemp()
        os.close(fd)

        args = ['-T', 'SVG', '-o', tmpfile, diagpath]
        blockdiag.command.main(args)

        # compare embeded source code
        source_code = open(diagpath).read()
        tree = ElementTree.parse(tmpfile)
        desc = tree.find('{http://www.w3.org/2000/svg}desc')

        # strip spaces
        source_code = re.sub('\s+', ' ', source_code)
        embeded = re.sub('\s+', ' ', desc.text)
        assert source_code == embeded
    finally:
        tmpdir.clean()
Exemplo n.º 3
0
def generate(mainfunc, filetype, source, options):
    try:
        tmpdir = TemporaryDirectory()
        fd, tmpfile = tmpdir.mkstemp()
        os.close(fd)

        mainfunc(['--debug', '-T', filetype, '-o', tmpfile, source] +
                 list(options))
    finally:
        tmpdir.clean()
Exemplo n.º 4
0
def ghostscript_not_found_test():
    testdir = os.path.dirname(__file__)
    diagpath = os.path.join(testdir, 'diagrams', 'background_url_image.diag')

    try:
        old_path = os.environ['PATH']
        os.environ['PATH'] = ''
        tmpdir = TemporaryDirectory()
        fd, tmpfile = tmpdir.mkstemp()
        os.close(fd)

        args = ['-T', 'SVG', '-o', tmpfile, diagpath]
        ret = blockdiag.command.main(args)
        assert 'Could not convert image:' in sys.stderr.getvalue()
        assert ret == 0
    finally:
        tmpdir.clean()
        os.environ['PATH'] = old_path
Exemplo n.º 5
0
def stdin_test():
    testdir = os.path.dirname(__file__)
    diagpath = os.path.join(testdir, 'diagrams', 'single_edge.diag')

    try:
        stdin = sys.stdin
        sys.stdin = open(diagpath, 'r')

        tmpdir = TemporaryDirectory()
        fd, tmpfile = tmpdir.mkstemp()
        os.close(fd)

        args = ['-T', 'SVG', '-o', tmpfile, '-']
        ret = blockdiag.command.main(args)
        assert ret == 0
    finally:
        sys.stdin = stdin
        tmpdir.clean()
Exemplo n.º 6
0
def svg_sanitizes_url_on_error_test():
    testdir = os.path.dirname(__file__)
    diagpath = os.path.join(testdir, 'diagrams', 'background_url_local.diag')

    try:
        tmpdir = TemporaryDirectory()
        fd, tmpfile = tmpdir.mkstemp()
        os.close(fd)

        args = ['-T', 'SVG', '-o', tmpfile, diagpath]
        ret = blockdiag.command.main(args)
        tree = ElementTree.parse(tmpfile)
        images = tree.findall('{http://www.w3.org/2000/svg}image')
        valid_url, invalid_url = [
            image.attrib.get('{http://www.w3.org/1999/xlink}href')
            for image in images
        ]

        assert valid_url
        assert not invalid_url
        assert 'unknown image type:' in sys.stderr.getvalue()
        assert ret == 0
    finally:
        tmpdir.clean()
Exemplo n.º 7
0
    def test_app_cleans_up_plugins(self):
        testdir = os.path.dirname(__file__)
        diagpath = os.path.join(testdir, 'diagrams', 'plugin_autoclass.diag')
        loaded_plugins = []

        def cleanup():
            from blockdiag import plugins
            loaded_plugins.extend(plugins.loaded_plugins)

        try:
            tmpdir = TemporaryDirectory()
            fd, tmpfile = tmpdir.mkstemp()
            os.close(fd)

            args = ['-T', 'SVG', '-o', tmpfile, diagpath]
            app = BlockdiagApp()
            app.register_cleanup_handler(cleanup)  # to get internal state
            app.run(args)

            from blockdiag import plugins
            self.assertTrue(loaded_plugins)  # check plugins were loaded
            self.assertFalse(plugins.loaded_plugins)  # and unloaded finally
        finally:
            tmpdir.clean()
 def setUp(self):
     self._tmpdir = TemporaryDirectory()
Exemplo n.º 9
0
 def setUp(self):
     docutils.register_directive('blockdiag',
                                 directives.BlockdiagDirectiveBase)
     self._tmpdir = TemporaryDirectory()