Repository navigation
Expand file tree
/
Copy pathexamples_test.py
More file actions
61 lines (52 loc) · 2.47 KB
/
Copy pathexamples_test.py
File metadata and controls
61 lines (52 loc) · 2.47 KB
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
import os
import platform
import unittest
import shutil
import subprocess
import re
from pathlib import Path
class ExamplesTest(unittest.TestCase):
def test_examples(self):
def get_openscad_executable():
OPENSCAD_EXECUTABLES = {
'Darwin': "/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD",
'Linux': "openscad"
}
return OPENSCAD_EXECUTABLES[platform.system()]
root = Path(__file__).parent.parent
def copyWithRelativeIncludes(src, dest):
with open(src, 'r') as srcFile:
with open(dest, 'w') as destFile:
for l in srcFile:
if l.startswith("include <"):
incPath = l.replace("include <", "").replace(">;", "").replace("\n","")
relIncPath = os.path.relpath(incPath, Path(dest).parent)
l = f"include <{relIncPath}>;\n"
elif l.startswith("use <"):
incPath = l.replace("use <", "").replace(">;", "").replace("\n","")
relIncPath = os.path.relpath(incPath, Path(dest).parent)
l = f"use <{relIncPath}>;\n"
destFile.write(l)
for f in sorted(Path(root / "solid2" / "examples/").iterdir()):
if not re.match("[0-9][0-9]-.*\.py", f.name):
continue
if f.stem.endswith(".x"):
continue
test_scad_file = root / "tests" / "examples_scad" \
/ f.with_suffix('.scad').name
print(f)
# call example (generate *.scad file)
subprocess.check_call(["python3", f.as_posix()])
# copy generated scad file to examples_scad/
copyWithRelativeIncludes(f.with_suffix(".scad"), test_scad_file)
# call git diff test/examples_scad/{f}.scad
diff = subprocess.check_output(["git", "diff",
test_scad_file.as_posix()])
# make sure there's no diff
self.assertEqual(diff.decode(), "")
# render with openscad
subprocess.check_call([get_openscad_executable(), "-o",
test_scad_file.with_suffix(".png"),
"--preview", "-",
test_scad_file],
stderr=subprocess.DEVNULL)