Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_utils.py « modules « python « tests - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6aba3a7526372f72ff0da4fa896f2fb980351c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later

import functools
import shutil
import pathlib
import subprocess
import tempfile
import unittest


def with_tempdir(wrapped):
    """Creates a temporary directory for the function, cleaning up after it returns normally.

    When the wrapped function raises an exception, the contents of the temporary directory
    remain available for manual inspection.

    The wrapped function is called with an extra positional argument containing
    the pathlib.Path() of the temporary directory.
    """

    @functools.wraps(wrapped)
    def decorator(*args, **kwargs):
        dirname = tempfile.mkdtemp(prefix='blender-alembic-test')
        try:
            retval = wrapped(*args, pathlib.Path(dirname), **kwargs)
        except:
            print('Exception in %s, not cleaning up temporary directory %s' % (wrapped, dirname))
            raise
        else:
            shutil.rmtree(dirname)
        return retval

    return decorator


class AbstractBlenderRunnerTest(unittest.TestCase):
    """Base class for all test suites which needs to run Blender"""

    # Set in a subclass
    blender: pathlib.Path = None
    testdir: pathlib.Path = None

    def run_blender(self, filepath: str, python_script: str, timeout: int = 300) -> str:
        """Runs Blender by opening a blendfile and executing a script.

        Returns Blender's stdout + stderr combined into one string.

        :param filepath: taken relative to self.testdir.
        :param timeout: in seconds
        """

        assert self.blender, "Path to Blender binary is to be set in setUpClass()"
        assert self.testdir, "Path to tests binary is to be set in setUpClass()"

        blendfile = self.testdir / filepath if filepath else ""

        command = [
            self.blender,
            '--background',
            '-noaudio',
            '--factory-startup',
            '--enable-autoexec',
            '--debug-memory',
            '--debug-exit-on-error',
        ]

        if blendfile:
            command.append(str(blendfile))

        command.extend([
            '--python-exit-code', '47',
            '--python-expr', python_script,
        ]
        )

        proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                              timeout=timeout)
        output = proc.stdout.decode('utf8')
        if proc.returncode:
            self.fail('Error %d running Blender:\n%s' % (proc.returncode, output))

        return output