mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-08-07 07:45:47 +00:00
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
import sys
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
from common import assembly_spec, catalog, generation, render
|
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
IGNORED_TEST_ROOT = REPO_ROOT / "tmp" / "cad-tests"
|
|
|
|
|
|
class IsolatedCadRoots:
|
|
def __init__(self, testcase: unittest.TestCase, *, prefix: str) -> None:
|
|
IGNORED_TEST_ROOT.mkdir(parents=True, exist_ok=True)
|
|
self._tempdir = tempfile.TemporaryDirectory(prefix=prefix, dir=IGNORED_TEST_ROOT)
|
|
testcase.addCleanup(self._tempdir.cleanup)
|
|
|
|
self.root = Path(self._tempdir.name)
|
|
self.cad_root = self.root / "models"
|
|
self.cad_root.mkdir(parents=True, exist_ok=True)
|
|
|
|
patches = [
|
|
mock.patch.object(assembly_spec, "CAD_ROOT", self.cad_root),
|
|
mock.patch.object(catalog, "CAD_ROOT", self.cad_root),
|
|
mock.patch.object(render, "CAD_ROOT", self.cad_root),
|
|
mock.patch.object(generation, "CAD_ROOT", self.cad_root),
|
|
]
|
|
snapshot_cli = sys.modules.get("snapshot.cli")
|
|
if snapshot_cli is not None:
|
|
patches.extend(
|
|
(
|
|
mock.patch.object(snapshot_cli, "CAD_ROOT", self.cad_root),
|
|
)
|
|
)
|
|
for patcher in patches:
|
|
patcher.start()
|
|
testcase.addCleanup(patcher.stop)
|
|
|
|
def temporary_cad_directory(self, *, prefix: str) -> tempfile.TemporaryDirectory[str]:
|
|
return tempfile.TemporaryDirectory(prefix=prefix, dir=self.cad_root)
|