Skip to content

Commit

Permalink
Use patlib.Path in tests and tools
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickJadoul committed Jun 18, 2020
1 parent f012fa1 commit 363afed
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 53 deletions.
3 changes: 2 additions & 1 deletion bin/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import cgi
import io
import os
import re
from pathlib import Path

import mkdocs

Expand Down Expand Up @@ -41,13 +40,12 @@ def on_page_markdown(self, markdown, page, **kwargs):
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'):
Expand All @@ -60,13 +58,12 @@ def found_includemarkdown_tag(match):
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)
Expand Down
8 changes: 3 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
7 changes: 2 additions & 5 deletions test/test_dependency_versions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re
import pytest
import textwrap
Expand Down Expand Up @@ -39,11 +38,9 @@


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

Expand Down Expand Up @@ -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']:
Expand Down
13 changes: 7 additions & 6 deletions test/test_projects/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from pathlib import Path

import jinja2

from typing import Union, Dict, Any


Expand All @@ -21,12 +23,11 @@ def __init__(self):
self.files = {}
self.template_context = {}

def generate(self, path: str):
def generate(self, path: Path):
path.mkdir(parents=True, exist_ok=True)
for filename, content in self.files.items():
file_path = os.path.join(path, filename)
os.makedirs(os.path.dirname(file_path), exist_ok=True)

with open(file_path, 'w', encoding='utf8') as f:
file_path = path / filename
with file_path.open('w', encoding='utf8') as f:
if isinstance(content, jinja2.Template):
content = content.render(self.template_context)

Expand Down
4 changes: 2 additions & 2 deletions test/test_subdir_package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path

import jinja2

Expand Down Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
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'


Expand Down
32 changes: 9 additions & 23 deletions unit_test/dependency_constraints_test.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 363afed

Please sign in to comment.