File: test_crash.py

package info (click to toggle)
trimesh 4.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 33,416 kB
  • sloc: python: 35,596; makefile: 96; javascript: 85; sh: 38
file content (119 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
try:
    from . import generic as g
except BaseException:
    import generic as g


def not_open(file_name, proc):
    """
    Assert that a file name is not open
    """
    # expand input path
    file_name = g.os.path.abspath(g.os.path.expanduser(file_name))
    # assert none of the open files are the one specified
    assert all(i.path != file_name for i in proc.open_files())


class FileTests(g.unittest.TestCase):
    def test_close(self):
        """
        Even when loaders crash, we should close files
        """
        try:
            import psutil
        except ImportError:
            g.log.warning("no psutil, exiting")
            return

        # a reference to current process
        proc = psutil.Process()

        # create a blank empty PLY file
        f = g.tempfile.NamedTemporaryFile(suffix=".ply", delete=False)
        # close file
        f.close()

        # file shouldn't be open
        not_open(f.name, proc)
        try:
            # should crash
            g.trimesh.load(f.name)
            # shouldn't make it to here
            raise AssertionError()
        except ValueError:
            # should be raised
            pass
        # file shouldn't be open
        not_open(f.name, proc)
        try:
            # should crash
            g.trimesh.load_mesh(f.name)
            # shouldn't make it to here
            raise AssertionError()
        except ValueError:
            # should be raised
            pass
        not_open(f.name, proc)
        # clean up after test
        g.os.remove(f.name)

        # create a blank empty unsupported file
        f = g.tempfile.NamedTemporaryFile(suffix=".blorb", delete=False)
        # close file
        f.close()
        # file shouldn't be open
        not_open(f.name, proc)
        try:
            # should crash
            g.trimesh.load(f.name)
            # shouldn't make it to here
            raise AssertionError()
        except ValueError:
            # should be raised
            pass
        # file shouldn't be open
        not_open(f.name, proc)
        try:
            # should crash
            g.trimesh.load_mesh(f.name)
            # shouldn't make it to here
            raise AssertionError()
        except KeyError:
            # should be raised
            pass
        not_open(f.name, proc)
        # clean up after test
        g.os.remove(f.name)

        # create a blank empty DXF file
        f = g.tempfile.NamedTemporaryFile(suffix=".dxf", delete=False)
        # close file
        f.close()
        # file shouldn't be open
        not_open(f.name, proc)
        try:
            # should crash
            g.trimesh.load(f.name)
            # shouldn't make it to here
            raise AssertionError()
        except IndexError:
            # should be raised
            pass
        # file shouldn't be open
        not_open(f.name, proc)
        try:
            # should crash
            g.trimesh.load_path(f.name)
            # shouldn't make it to here
            raise AssertionError()
        except IndexError:
            # should be raised
            pass
        not_open(f.name, proc)
        # clean up after test
        g.os.remove(f.name)


if __name__ == "__main__":
    g.trimesh.util.attach_to_log()
    g.unittest.main()