Use patlib.Path in tests and tools

This commit is contained in:
Yannick Jadoul
2020-06-18 15:29:48 +02:00
parent f012fa1af0
commit acf5c24881
8 changed files with 35 additions and 53 deletions
+2 -1
View File
@@ -3,10 +3,11 @@
import os import os
import subprocess import subprocess
import sys import sys
from pathlib import Path
if __name__ == '__main__': if __name__ == '__main__':
# move cwd to the project root # move cwd to the project root
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) os.chdir(Path(__file__).resolve().parents[1])
# run the unit tests # run the unit tests
subprocess.check_call([sys.executable, '-m', 'pytest', 'unit_test']) subprocess.check_call([sys.executable, '-m', 'pytest', 'unit_test'])
@@ -1,7 +1,6 @@
import cgi import cgi
import io
import os
import re import re
from pathlib import Path
import mkdocs import mkdocs
@@ -41,13 +40,12 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin):
def found_include_tag(match): def found_include_tag(match):
filename = match.group('filename') filename = match.group('filename')
file_path_abs = os.path.join(os.path.dirname(page_src_path), filename) file_path_abs = Path(page_src_path).parent / filename
if not os.path.exists(file_path_abs): if not file_path_abs.exists():
raise ValueError('file not found', filename) raise ValueError('file not found', filename)
with io.open(file_path_abs, encoding='utf8') as f: text_to_include = file_path_abs.read_text(encoding='utf8')
text_to_include = f.read()
# Allow good practice of having a final newline in the file # Allow good practice of having a final newline in the file
if text_to_include.endswith('\n'): if text_to_include.endswith('\n'):
@@ -60,13 +58,12 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin):
start = match.group('start') start = match.group('start')
end = match.group('end') end = match.group('end')
file_path_abs = os.path.join(os.path.dirname(page_src_path), filename) file_path_abs = Path(page_src_path).parent / filename
if not os.path.exists(file_path_abs): if not file_path_abs.exists():
raise ValueError('file not found', filename) raise ValueError('file not found', filename)
with io.open(file_path_abs, encoding='utf8') as f: text_to_include = file_path_abs.read_text(encoding='utf8')
text_to_include = f.read()
if start: if start:
_, _, text_to_include = text_to_include.partition(start) _, _, text_to_include = text_to_include.partition(start)
+3 -5
View File
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import io from pathlib import Path
import os
try: try:
from setuptools import setup from setuptools import setup
except ImportError: except ImportError:
from distutils.core import setup from distutils.core import setup
this_directory = os.path.dirname(__file__) this_directory = Path(__file__).parent
with io.open(os.path.join(this_directory, 'README.md'), encoding='utf-8') as f: long_description = this_directory.joinpath('README.md').read_text(encoding='utf-8')
long_description = f.read()
setup( setup(
name='cibuildwheel', name='cibuildwheel',
+2 -5
View File
@@ -1,4 +1,3 @@
import os
import re import re
import pytest import pytest
import textwrap import textwrap
@@ -39,11 +38,9 @@ VERSION_REGEX = r'([\w-]+)==([^\s]+)'
def get_versions_from_constraint_file(constraint_file): def get_versions_from_constraint_file(constraint_file):
with open(constraint_file, encoding='utf8') as f: constraint_file_text = constraint_file.read_text(encoding='utf8')
constraint_file_text = f.read()
versions = {} versions = {}
for package, version in re.findall(VERSION_REGEX, constraint_file_text): for package, version in re.findall(VERSION_REGEX, constraint_file_text):
versions[package] = version versions[package] = version
@@ -73,7 +70,7 @@ def test_pinned_versions(tmp_path, python_version):
constraint_filename = 'constraints.txt' constraint_filename = 'constraints.txt'
build_pattern = '[cp]p38-*' build_pattern = '[cp]p38-*'
constraint_file = os.path.join(cibuildwheel.util.resources_dir, constraint_filename) constraint_file = cibuildwheel.util.resources_dir / constraint_filename
constraint_versions = get_versions_from_constraint_file(constraint_file) constraint_versions = get_versions_from_constraint_file(constraint_file)
for package in ['pip', 'setuptools', 'wheel', 'virtualenv']: for package in ['pip', 'setuptools', 'wheel', 'virtualenv']:
+7 -5
View File
@@ -1,5 +1,7 @@
import os from pathlib import Path
import jinja2 import jinja2
from typing import Union, Dict, Any from typing import Union, Dict, Any
@@ -21,12 +23,12 @@ class TestProject:
self.files = {} self.files = {}
self.template_context = {} self.template_context = {}
def generate(self, path: str): def generate(self, path: Path):
for filename, content in self.files.items(): for filename, content in self.files.items():
file_path = os.path.join(path, filename) file_path = path / filename
os.makedirs(os.path.dirname(file_path), exist_ok=True) file_path.parent.mkdir(parents=True, exist_ok=True)
with open(file_path, 'w', encoding='utf8') as f: with file_path.open('w', encoding='utf8') as f:
if isinstance(content, jinja2.Template): if isinstance(content, jinja2.Template):
content = content.render(self.template_context) content = content.render(self.template_context)
+2 -2
View File
@@ -1,4 +1,4 @@
import os from pathlib import Path
import jinja2 import jinja2
@@ -35,7 +35,7 @@ def test(capfd, tmp_path):
project_dir = tmp_path / 'project' project_dir = tmp_path / 'project'
subdir_package_project.generate(project_dir) subdir_package_project.generate(project_dir)
package_dir = os.path.join('src', 'spam') package_dir = Path('src', 'spam')
# build the wheels # build the wheels
actual_wheels = utils.cibuildwheel_run(project_dir, package_dir=package_dir, add_env={ actual_wheels = utils.cibuildwheel_run(project_dir, package_dir=package_dir, add_env={
'CIBW_BEFORE_BUILD': 'python {project}/bin/before_build.py', 'CIBW_BEFORE_BUILD': 'python {project}/bin/before_build.py',
+3 -2
View File
@@ -10,9 +10,10 @@ import shutil
import subprocess import subprocess
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path
from tempfile import mkdtemp from tempfile import mkdtemp
IS_WINDOWS_RUNNING_ON_AZURE = os.path.exists('C:\\hostedtoolcache') IS_WINDOWS_RUNNING_ON_AZURE = Path('C:\\hostedtoolcache').exists()
IS_WINDOWS_RUNNING_ON_TRAVIS = os.environ.get('TRAVIS_OS_NAME') == 'windows' IS_WINDOWS_RUNNING_ON_TRAVIS = os.environ.get('TRAVIS_OS_NAME') == 'windows'
@@ -66,7 +67,7 @@ def cibuildwheel_run(project_path, package_dir='.', env=None, add_env=None, outp
with TemporaryDirectoryIfNone(output_dir) as _output_dir: with TemporaryDirectoryIfNone(output_dir) as _output_dir:
subprocess.check_call( subprocess.check_call(
[sys.executable, '-m', 'cibuildwheel', '--output-dir', str(_output_dir), package_dir], [sys.executable, '-m', 'cibuildwheel', '--output-dir', str(_output_dir), str(package_dir)],
env=env, env=env,
cwd=project_path, cwd=project_path,
) )
+9 -23
View File
@@ -1,30 +1,16 @@
from cibuildwheel.util import DependencyConstraints from cibuildwheel.util import DependencyConstraints
import os
from pathlib import Path
def test_defaults(): def test_defaults():
dependency_constraints = DependencyConstraints.with_defaults() dependency_constraints = DependencyConstraints.with_defaults()
project_root = os.path.dirname(os.path.dirname(__file__)) project_root = Path(__file__).parents[1]
resources_dir = os.path.join(project_root, 'cibuildwheel', 'resources') resources_dir = project_root / 'cibuildwheel' / 'resources'
assert os.path.samefile( assert dependency_constraints.base_file_path.samefile(resources_dir / 'constraints.txt')
dependency_constraints.base_file_path, assert dependency_constraints.get_for_python_version('3.8').samefile(resources_dir / 'constraints.txt')
os.path.join(resources_dir, 'constraints.txt') assert dependency_constraints.get_for_python_version('3.6').samefile(resources_dir / 'constraints-python36.txt')
) assert dependency_constraints.get_for_python_version('3.5').samefile(resources_dir / 'constraints-python35.txt')
assert os.path.samefile( assert dependency_constraints.get_for_python_version('2.7').samefile(resources_dir / 'constraints-python27.txt')
dependency_constraints.get_for_python_version('3.8'),
os.path.join(resources_dir, 'constraints.txt')
)
assert os.path.samefile(
dependency_constraints.get_for_python_version('3.6'),
os.path.join(resources_dir, 'constraints-python36.txt')
)
assert os.path.samefile(
dependency_constraints.get_for_python_version('3.5'),
os.path.join(resources_dir, 'constraints-python35.txt')
)
assert os.path.samefile(
dependency_constraints.get_for_python_version('2.7'),
os.path.join(resources_dir, 'constraints-python27.txt')
)