2020-04-25 16:09:11 +01:00
|
|
|
import os
|
2020-05-02 09:59:55 +01:00
|
|
|
import jinja2
|
2020-05-02 22:50:52 +01:00
|
|
|
from typing import Union, Dict, Any
|
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]
|
2020-04-25 16:09:11 +01:00
|
|
|
|
|
|
|
|
|
2020-05-17 16:54:33 +01:00
|
|
|
class TestProject:
|
|
|
|
|
'''
|
|
|
|
|
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`.
|
|
|
|
|
'''
|
2020-05-02 09:59:55 +01:00
|
|
|
files: FilesDict
|
2020-05-02 16:54:19 +01:00
|
|
|
template_context: TemplateContext
|
2020-04-25 16:09:11 +01:00
|
|
|
|
2020-05-02 16:54:19 +01:00
|
|
|
def __init__(self):
|
|
|
|
|
self.files = {}
|
|
|
|
|
self.template_context = {}
|
2020-04-25 16:09:11 +01:00
|
|
|
|
2020-05-02 16:54:19 +01:00
|
|
|
def generate(self, path: str):
|
2020-05-02 09:59:55 +01:00
|
|
|
for filename, content in self.files.items():
|
2020-04-25 16:09:11 +01:00
|
|
|
file_path = os.path.join(path, filename)
|
|
|
|
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
|
|
|
|
|
2020-05-15 12:19:38 +01:00
|
|
|
with open(file_path, '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
|
|
|
|
2020-04-25 16:09:11 +01:00
|
|
|
f.write(content)
|
2020-05-03 10:30:44 +01:00
|
|
|
|
|
|
|
|
def copy(self):
|
2020-05-17 16:54:33 +01:00
|
|
|
other = TestProject()
|
2020-05-03 10:30:44 +01:00
|
|
|
other.files = self.files.copy()
|
|
|
|
|
other.template_context = self.template_context.copy()
|
|
|
|
|
return other
|