From acf5c248816dbfb5bc9f0aa4735825b8a411b222 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Thu, 18 Jun 2020 02:00:30 +0200 Subject: [PATCH] Use patlib.Path in tests and tools --- bin/run_tests.py | 3 +- .../mkdocs_include_markdown_plugin/plugin.py | 17 ++++------ setup.py | 8 ++--- test/test_dependency_versions.py | 7 ++-- test/test_projects/base.py | 12 ++++--- test/test_subdir_package.py | 4 +-- test/utils.py | 5 +-- unit_test/dependency_constraints_test.py | 32 ++++++------------- 8 files changed, 35 insertions(+), 53 deletions(-) diff --git a/bin/run_tests.py b/bin/run_tests.py index 93c23e88..117f8b9f 100755 --- a/bin/run_tests.py +++ b/bin/run_tests.py @@ -3,10 +3,11 @@ import os import subprocess import sys +from pathlib import Path if __name__ == '__main__': # 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 subprocess.check_call([sys.executable, '-m', 'pytest', 'unit_test']) diff --git a/docs/mkdocs_include_markdown_plugin/mkdocs_include_markdown_plugin/plugin.py b/docs/mkdocs_include_markdown_plugin/mkdocs_include_markdown_plugin/plugin.py index b6b7a723..f12ea469 100644 --- a/docs/mkdocs_include_markdown_plugin/mkdocs_include_markdown_plugin/plugin.py +++ b/docs/mkdocs_include_markdown_plugin/mkdocs_include_markdown_plugin/plugin.py @@ -1,7 +1,6 @@ import cgi -import io -import os import re +from pathlib import Path import mkdocs @@ -41,13 +40,12 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin): def found_include_tag(match): 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) - with io.open(file_path_abs, encoding='utf8') as f: - text_to_include = f.read() + text_to_include = file_path_abs.read_text(encoding='utf8') # Allow good practice of having a final newline in the file if text_to_include.endswith('\n'): @@ -60,13 +58,12 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin): start = match.group('start') 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) - with io.open(file_path_abs, encoding='utf8') as f: - text_to_include = f.read() + text_to_include = file_path_abs.read_text(encoding='utf8') if start: _, _, text_to_include = text_to_include.partition(start) diff --git a/setup.py b/setup.py index 3e4c90cb..d05407e5 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,13 @@ # -*- coding: utf-8 -*- -import io -import os +from pathlib import Path try: from setuptools import setup except ImportError: from distutils.core import setup -this_directory = os.path.dirname(__file__) -with io.open(os.path.join(this_directory, 'README.md'), encoding='utf-8') as f: - long_description = f.read() +this_directory = Path(__file__).parent +long_description = this_directory.joinpath('README.md').read_text(encoding='utf-8') setup( name='cibuildwheel', diff --git a/test/test_dependency_versions.py b/test/test_dependency_versions.py index 611350f1..2f79bd2a 100644 --- a/test/test_dependency_versions.py +++ b/test/test_dependency_versions.py @@ -1,4 +1,3 @@ -import os import re import pytest import textwrap @@ -39,11 +38,9 @@ VERSION_REGEX = r'([\w-]+)==([^\s]+)' def get_versions_from_constraint_file(constraint_file): - with open(constraint_file, encoding='utf8') as f: - constraint_file_text = f.read() + constraint_file_text = constraint_file.read_text(encoding='utf8') versions = {} - for package, version in re.findall(VERSION_REGEX, constraint_file_text): versions[package] = version @@ -73,7 +70,7 @@ def test_pinned_versions(tmp_path, python_version): constraint_filename = 'constraints.txt' 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) for package in ['pip', 'setuptools', 'wheel', 'virtualenv']: diff --git a/test/test_projects/base.py b/test/test_projects/base.py index 5cac80ad..b869f4f2 100644 --- a/test/test_projects/base.py +++ b/test/test_projects/base.py @@ -1,5 +1,7 @@ -import os +from pathlib import Path + import jinja2 + from typing import Union, Dict, Any @@ -21,12 +23,12 @@ class TestProject: self.files = {} self.template_context = {} - def generate(self, path: str): + def generate(self, path: Path): for filename, content in self.files.items(): - file_path = os.path.join(path, filename) - os.makedirs(os.path.dirname(file_path), exist_ok=True) + file_path = path / filename + 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): content = content.render(self.template_context) diff --git a/test/test_subdir_package.py b/test/test_subdir_package.py index 98b1b8cd..14a7a58b 100644 --- a/test/test_subdir_package.py +++ b/test/test_subdir_package.py @@ -1,4 +1,4 @@ -import os +from pathlib import Path import jinja2 @@ -35,7 +35,7 @@ def test(capfd, tmp_path): project_dir = tmp_path / 'project' subdir_package_project.generate(project_dir) - package_dir = os.path.join('src', 'spam') + package_dir = Path('src', 'spam') # build the wheels actual_wheels = utils.cibuildwheel_run(project_dir, package_dir=package_dir, add_env={ 'CIBW_BEFORE_BUILD': 'python {project}/bin/before_build.py', diff --git a/test/utils.py b/test/utils.py index 15ce402a..217a616f 100644 --- a/test/utils.py +++ b/test/utils.py @@ -10,9 +10,10 @@ import shutil import subprocess import sys from contextlib import contextmanager +from pathlib import Path 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' @@ -66,7 +67,7 @@ def cibuildwheel_run(project_path, package_dir='.', env=None, add_env=None, outp with TemporaryDirectoryIfNone(output_dir) as _output_dir: 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, cwd=project_path, ) diff --git a/unit_test/dependency_constraints_test.py b/unit_test/dependency_constraints_test.py index b3fb0cb3..5347011c 100644 --- a/unit_test/dependency_constraints_test.py +++ b/unit_test/dependency_constraints_test.py @@ -1,30 +1,16 @@ from cibuildwheel.util import DependencyConstraints -import os + +from pathlib import Path def test_defaults(): dependency_constraints = DependencyConstraints.with_defaults() - project_root = os.path.dirname(os.path.dirname(__file__)) - resources_dir = os.path.join(project_root, 'cibuildwheel', 'resources') + project_root = Path(__file__).parents[1] + resources_dir = project_root / 'cibuildwheel' / 'resources' - assert os.path.samefile( - dependency_constraints.base_file_path, - os.path.join(resources_dir, 'constraints.txt') - ) - assert os.path.samefile( - 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') - ) + assert dependency_constraints.base_file_path.samefile(resources_dir / 'constraints.txt') + assert dependency_constraints.get_for_python_version('3.8').samefile(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 dependency_constraints.get_for_python_version('2.7').samefile(resources_dir / 'constraints-python27.txt')