Files
cibuildwheel/test/test_projects/base.py
T

40 lines
1.1 KiB
Python
Raw Normal View History

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]
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-05-02 16:54:19 +01:00
def __init__(self):
self.files = {}
self.template_context = {}
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():
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
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