Files
cibuildwheel/test/test_projects/base.py
T

43 lines
1.2 KiB
Python
Raw Normal View History

2020-06-18 02:00:30 +02:00
from pathlib import Path
2021-01-06 13:50:58 -05:00
from typing import Any, Dict, Union
2020-06-18 02:00:30 +02:00
2020-05-02 09:59:55 +01:00
import jinja2
2020-06-18 02:00:30 +02:00
2020-05-02 09:59:55 +01:00
FilesDict = Dict[str, Union[str, jinja2.Template]]
2020-05-02 16:54:19 +01:00
TemplateContext = Dict[str, Any]
class TestProject:
2021-05-03 11:45:43 -04:00
"""
An object that represents a project that can be built by cibuildwheel.
Can be manipulated in tests by changing `files` and `template_context`.
Write out to the filesystem using `generate`.
2021-05-03 11:45:43 -04:00
"""
2021-04-30 17:56:34 -04:00
__test__ = False # Have pytest ignore this class on `from .test_projects import TestProject`
2020-05-02 09:59:55 +01:00
files: FilesDict
2020-05-02 16:54:19 +01:00
template_context: TemplateContext
2020-05-02 16:54:19 +01:00
def __init__(self):
self.files = {}
self.template_context = {}
2020-06-18 02:00:30 +02:00
def generate(self, path: Path):
2020-05-02 09:59:55 +01:00
for filename, content in self.files.items():
2020-06-18 02:00:30 +02:00
file_path = path / filename
file_path.parent.mkdir(parents=True, exist_ok=True)
2021-05-03 11:45:43 -04:00
with file_path.open("w", encoding="utf8") as f:
2020-05-02 09:59:55 +01:00
if isinstance(content, jinja2.Template):
2020-05-02 16:54:19 +01:00
content = content.render(self.template_context)
2020-05-02 09:59:55 +01:00
f.write(content)
2020-05-03 10:30:44 +01:00
def copy(self):
other = TestProject()
2020-05-03 10:30:44 +01:00
other.files = self.files.copy()
other.template_context = self.template_context.copy()
return other