Skip to content

Commit

Permalink
Use testpath for filesystem assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Oct 3, 2018
1 parent 8e1a937 commit 07702cb
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 38 deletions.
11 changes: 3 additions & 8 deletions nsist/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import io

try:
from pathlib import Path
except ImportError:
from pathlib2 import Path # Backport
from testpath import assert_isfile

from nsist import commands, _rewrite_shebangs
from .utils import assert_is_file

cmds = {'acommand': {'entry_point': 'somemod:somefunc',
'extra_preamble': io.StringIO(u'import extra')}}

def test_prepare_bin_dir(tmpdir):
commands.prepare_bin_directory(tmpdir, cmds)
assert_is_file(str(tmpdir / 'acommand.exe'))
assert_isfile(str(tmpdir / 'acommand.exe'))
script_file = tmpdir / 'acommand-script.py'
assert_is_file(str(script_file))
assert_isfile(str(script_file))

with script_file.open() as f:
script_contents = f.read()
Expand Down
19 changes: 10 additions & 9 deletions nsist/tests/test_copymodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import sys

import pytest
from testpath import assert_isfile, assert_isdir

from nsist.copymodules import copy_modules, ExtensionModuleMismatch
from .utils import assert_is_file, assert_is_dir, test_dir, skip_on_windows, only_on_windows
from .utils import test_dir, skip_on_windows, only_on_windows

pjoin = os.path.join
running_python = '.'.join(str(x) for x in sys.version_info[:3])
Expand All @@ -18,8 +19,8 @@
def test_copy_plain(tmpdir):
tmpdir = str(tmpdir)
copy_modules(['plainmod', 'plainpkg'], tmpdir, '3.3.5', sample_path)
assert_is_file(pjoin(tmpdir, 'plainmod.py'))
assert_is_dir(pjoin(tmpdir, 'plainpkg'))
assert_isfile(pjoin(tmpdir, 'plainmod.py'))
assert_isdir(pjoin(tmpdir, 'plainpkg'))

@skip_on_windows
def test_copy_wrong_platform(tmpdir):
Expand All @@ -34,8 +35,8 @@ def test_copy_wrong_platform(tmpdir):
def test_copy_windows(tmpdir):
tmpdir = str(tmpdir)
copy_modules(['win_extmod', 'win_extpkg'], tmpdir, running_python, sample_path)
assert_is_file(pjoin(tmpdir, 'win_extmod.pyd'))
assert_is_dir(pjoin(tmpdir, 'win_extpkg'))
assert_isfile(pjoin(tmpdir, 'win_extmod.pyd'))
assert_isdir(pjoin(tmpdir, 'win_extpkg'))

@only_on_windows
def test_copy_wrong_pyversion(tmpdir):
Expand All @@ -50,10 +51,10 @@ def test_copy_from_zipfile(tmpdir):
tmpdir = str(tmpdir)
copy_modules(['zippedmod2', 'zippedpkg2'],
tmpdir, running_python, sample_path)
# assert_is_file(pjoin(tmpdir, 'zippedmod.py'))
# assert_is_dir(pjoin(tmpdir, 'zippedpkg'))
assert_is_file(pjoin(tmpdir, 'zippedmod2.py'))
assert_is_dir(pjoin(tmpdir, 'zippedpkg2'))
# assert_isfile(pjoin(tmpdir, 'zippedmod.py'))
# assert_isdir(pjoin(tmpdir, 'zippedpkg'))
assert_isfile(pjoin(tmpdir, 'zippedmod2.py'))
assert_isdir(pjoin(tmpdir, 'zippedpkg2'))

def test_module_not_found(tmpdir):
tmpdir = str(tmpdir)
Expand Down
6 changes: 3 additions & 3 deletions nsist/tests/test_installerbuilder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import io

from os.path import join as pjoin
from testpath import assert_isfile

from nsist import InstallerBuilder, DEFAULT_ICON
from .utils import assert_is_file, test_dir
from .utils import test_dir


sample_preamble = pjoin(test_dir, u'sample_preamble.py')
Expand All @@ -18,7 +18,7 @@ def test_prepare_shortcuts(tmpdir):
ib.prepare_shortcuts()

scfile = pjoin(tmpdir, 'sc1.launch.pyw')
assert_is_file(scfile)
assert_isfile(scfile)

with io.open(scfile, 'r', encoding='utf-8') as f:
contents = f.read()
Expand Down
8 changes: 4 additions & 4 deletions nsist/tests/test_local_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import subprocess

import pytest
from testpath import assert_isfile, assert_isdir

from nsist.pypi import fetch_pypi_wheels
from .utils import assert_is_dir, assert_is_file

# To exclude tests requiring network on an unplugged machine, use: pytest -m "not network"

Expand All @@ -19,10 +19,10 @@ def test_matching_one_pattern(tmpdir):

fetch_pypi_wheels([], [os.path.join(td1, '*.whl')], td2, platform.python_version(), 64)

assert_is_dir(os.path.join(td2, 'requests'))
assert_is_file(os.path.join(td2, 'requests-2.19.1.dist-info', 'METADATA'))
assert_isdir(os.path.join(td2, 'requests'))
assert_isfile(os.path.join(td2, 'requests-2.19.1.dist-info', 'METADATA'))

assert_is_dir(os.path.join(td2, 'urllib3'))
assert_isdir(os.path.join(td2, 'urllib3'))
assert glob.glob(os.path.join(td2, 'urllib3*.dist-info'))

@pytest.mark.network
Expand Down
12 changes: 6 additions & 6 deletions nsist/tests/test_pypi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from os.path import join as pjoin
from pathlib import Path
import pytest
from testpath import assert_isfile

from nsist.pypi import (
WheelLocator, extract_wheel, CachedRelease, merge_dir_to, NoWheelError,
)
from .utils import assert_is_file

# To exclude tests requiring network on an unplugged machine, use: pytest -m "not network"

Expand All @@ -14,11 +14,11 @@ def test_download(tmpdir):
tmpdir = str(tmpdir)
wd = WheelLocator("astsearch==0.1.2", "3.5.1", 64)
wheel = wd.fetch()
assert_is_file(str(wheel))
assert_isfile(wheel)

extract_wheel(wheel, target_dir=tmpdir)
assert_is_file(pjoin(tmpdir, 'astsearch.py'))
assert_is_file(pjoin(tmpdir, 'astsearch-0.1.2.dist-info', 'METADATA'))
assert_isfile(pjoin(tmpdir, 'astsearch.py'))
assert_isfile(pjoin(tmpdir, 'astsearch-0.1.2.dist-info', 'METADATA'))

@pytest.mark.network
def test_bad_name():
Expand Down Expand Up @@ -127,8 +127,8 @@ def test_merge_dir_to(tmpdir):

merge_dir_to(td2, td1)

assert_is_file(str(td1 / 'subdir' / 'foo'))
assert_is_file(str(td1 / 'subdir' / 'bar'))
assert_isfile(td1 / 'subdir' / 'foo')
assert_isfile(td1 / 'subdir' / 'bar')
with (td1 / 'ab').open() as f:
assert f.read() == u"alternate"

Expand Down
7 changes: 0 additions & 7 deletions nsist/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

test_dir = dirname(__file__)

def assert_is_file(path):
assert exists(path), "%s does not exist"
assert isfile(path), "%s exists but is not a directory."

def assert_is_dir(path):
assert exists(path), "%s does not exist"
assert isdir(path), "%s exists but is not a directory."

def skip_on_windows(function):
"""Decorator to skip a test on Windows."""
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ requires = [
"yarg",
"win_cli_launchers"
]
dev-requires = ["testpath"]
classifiers = [
"License :: OSI Approved :: MIT License",
"Intended Audience :: Developers",
Expand All @@ -28,5 +27,8 @@ classifiers = [
"Topic :: System :: Software Distribution",
]

[tool.flit.metadata.requires-extra]
test = ["testpath"]

[tool.flit.scripts]
pynsist = "nsist:main"
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ deps = pytest
win_cli_launchers
jinja2
yarg
testpath
commands = pytest nsist/tests

[testenv:notnetwork]
Expand Down

0 comments on commit 07702cb

Please sign in to comment.