1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
import errno
import unittest
import os
import re
import shutil
import subprocess
import sys
import sysconfig
from contextlib import contextmanager
import pytest
sys.path = [os.path.join(os.path.dirname(os.path.realpath(__file__)),'..')]+sys.path
from argparse_testlib import skip_on_python_older_than, pushd
def _mandir(prefix, num=1):
data = sysconfig.get_path('data', vars={'base': prefix})
return os.path.join(data, 'share/man/man' + str(num))
@contextmanager
def change_argv(argv):
old_argv = sys.argv
sys.argv = argv
try:
yield
finally:
sys.argv = old_argv
def _rmtree(directory):
try:
shutil.rmtree(directory)
except OSError as err:
if err.errno != errno.ENOENT:
raise
def run_pip(args):
environ = os.environ.copy()
environ['PYTHONPATH'] = ':'.join(sys.path)
from pip import __version__
pip_version = tuple([int(x) for x in __version__.split('.')[:2]])
if pip_version < (21, 3):
subprocess.call(
[sys.executable, '-m', 'pip'] + args + ["--use-feature=in-tree-build", "."],
env=environ)
else:
subprocess.call([sys.executable, '-m', 'pip'] + args + ["."], env=environ)
def run_setup_py(args):
environ = os.environ.copy()
environ['PYTHONPATH'] = ':'.join(sys.path)
with change_argv(['setup.py'] + args):
return subprocess.call([sys.executable, 'setup.py'] + args,
env=environ)
def run_one_installer(installer, args):
"""
Run 'pip <args> .' or 'python setup.py <args>'
"""
skip_on_python_older_than(
"3.10",
"Too old Python version for testing PIP installation",
installer == "pip",
)
method = run_pip if installer == "pip" else run_setup_py
method(args)
def file_cmp(file1, file2, filter_string=None):
with open(file1, 'r') as f1:
with open(file2, 'r') as f2:
a1 = f1.readlines()
a2 = f2.readlines()
if len(a1) != len(a2):
# get the pretty diff
assert a1 == a2
first = True
for left, right in zip(a1, a2):
if first:
left = re.sub('[0-9]{4}\\\\-[0-9]{2}\\\\-[0-9]{2}', '!!DATE!!', left)
left = left.replace("-dev", ".dev0") # issue #50, setuptools < v60
right = re.sub('[0-9]{4}\\\\-[0-9]{2}\\\\-[0-9]{2}', '!!DATE!!', right)
first = False
if filter_string is not None:
left = filter_string(left)
right = filter_string(right)
assert left == right
class TestAllExamples:
def test_old_example(self):
with pushd('examples/old_format'):
try:
os.remove('example.1')
except OSError:
pass
run_setup_py(['build_manpage'])
file_cmp('example.1', 'expected-output.1')
def test_old_example_file_name(self):
with pushd('examples/old_format_file_name'):
try:
os.remove('example.1')
except OSError:
pass
run_setup_py(['build_manpage'])
file_cmp('example.1', 'expected-output.1')
@pytest.mark.parametrize("installer", ["pip", "setuppy"])
def test_copr(self, installer):
with pushd('examples/copr'):
name = 'copr-cli.1'
prefix = '/usr'
idir = os.path.join(os.getcwd(), installer + "_install_dir")
mandir = os.path.join(idir, _mandir("usr/"))
_rmtree(idir)
run_one_installer(installer, ['install', '--root', idir, '--prefix', prefix])
def version_version_filter(string):
return string.replace('[VERSION [VERSION ...]]',
'[VERSION ...]')
file_cmp(os.path.join(mandir, os.path.basename(name)),
'expected-output.1',
filter_string=version_version_filter)
file_cmp(name, 'expected-output.1',
filter_string=version_version_filter)
@pytest.mark.parametrize("installer", ["pip", "setuppy"])
def test_distgen(self, installer):
with pushd('examples/raw-description'):
name = 'man/dg.1'
prefix = "/usr"
idir = os.path.join(os.getcwd(), installer + "_install_dir")
_rmtree(idir)
mandir = os.path.join(idir, _mandir("usr/"))
run_one_installer(installer, ['install', '--root', idir, '--prefix', prefix])
file_cmp(os.path.join(mandir, os.path.basename(name)), 'expected-output.1')
file_cmp(name, 'expected-output.1')
@pytest.mark.parametrize("installer", ["pip", "setuppy"])
def test_resalloc(self, installer):
with pushd('examples/resalloc'):
prefix = "/usr"
idir = os.path.join(os.getcwd(), installer + "_install_dir")
_rmtree(idir)
mandir = os.path.join(idir, _mandir("usr/"))
run_one_installer(installer, ['install', '--root', idir, '--prefix', prefix])
for name in ['man/resalloc.1', 'man/resalloc-maint.1']:
file_cmp(os.path.join(mandir, os.path.basename(name)),
'expected/' + name)
file_cmp(name, 'expected/' + name)
@pytest.mark.parametrize("installer", ["pip", "setuppy"])
def test_argument_groups_example(self, installer):
with pushd('examples/argument_groups'):
prefix = "/usr"
idir = os.path.join(os.getcwd(), installer + "_install_dir")
_rmtree(idir)
mandir = os.path.join(idir, _mandir("usr/"))
run_one_installer(installer, ['install', '--root', idir, '--prefix', prefix])
compiled = os.path.join('man', 'test.1')
base = os.path.basename(compiled)
expected = os.path.join('expected', base)
installed = os.path.join(mandir, base)
file_cmp(installed, expected)
file_cmp(compiled, expected)
@pytest.mark.parametrize("installer", ["pip", "setuppy"])
def test_osc(self, installer):
with pushd('examples/osc'):
name = 'osc.1'
prefix = '/usr'
idir = os.path.join(os.getcwd(), installer + "_install_dir")
mandir = os.path.join(idir, _mandir("usr/"))
_rmtree(idir)
run_one_installer(installer, ['install', '--root', idir, '--prefix', prefix])
file_cmp(os.path.join(mandir, os.path.basename(name)), 'expected-output.1')
file_cmp(name, 'expected-output.1')
@pytest.mark.parametrize("installer", ["pip", "setuppy"])
def test_pre_written_man_page(self, installer):
with pushd('examples/pre-written-man-page'):
name = 'psutils.1'
prefix = '/usr'
idir = os.path.join(os.getcwd(), installer + "_install_dir")
mandir = os.path.join(idir, _mandir("usr/"))
_rmtree(idir)
run_one_installer(installer, ['install', '--root', idir, '--prefix', prefix])
file_cmp(os.path.join(mandir, name), name)
if __name__ == "__main__":
unittest.main()
|