| 1 |
# Copyright 2017-2024 Facundo Batista, Nicolás Demarchi |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify it |
| 4 |
# under the terms of the GNU General Public License version 3, as published |
| 5 |
# by the Free Software Foundation. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, but |
| 8 |
# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 9 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 10 |
# PURPOSE. See the GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License along |
| 13 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
# |
| 15 |
# For further info, check https://github.com/PyAr/fades |
| 16 |
|
| 17 |
"""Common code for the tests.""" |
| 18 |
|
| 19 |
import os |
| 20 |
from tempfile import mkstemp |
| 21 |
|
| 22 |
from packaging.requirements import Requirement |
| 23 |
|
| 24 |
|
| 25 |
def get_tempfile(testcase): |
| 26 |
"""Return the name of a temp file that will be removed when the test finishes.""" |
| 27 |
# create the file and close its descriptor |
| 28 |
descriptor, tempfile = mkstemp(prefix="test-temp-file") |
| 29 |
os.close(descriptor) |
| 30 |
|
| 31 |
def clean(): |
| 32 |
"""Clean the file from disk, if still there.""" |
| 33 |
if os.path.exists(tempfile): |
| 34 |
os.remove(tempfile) |
| 35 |
testcase.addCleanup(clean) |
| 36 |
|
| 37 |
return tempfile |
| 38 |
|
| 39 |
|
| 40 |
def create_tempfile(testcase, lines): |
| 41 |
tempfile = get_tempfile(testcase) |
| 42 |
|
| 43 |
with open(tempfile, 'w', encoding='utf-8') as f: |
| 44 |
for line in lines: |
| 45 |
f.write(line + '\n') |
| 46 |
|
| 47 |
return tempfile |
| 48 |
|
| 49 |
|
| 50 |
def get_python_filepaths(roots): |
| 51 |
"""Helper to retrieve paths of Python files.""" |
| 52 |
python_paths = [] |
| 53 |
for root in roots: |
| 54 |
for dirpath, dirnames, filenames in os.walk(root): |
| 55 |
for filename in filenames: |
| 56 |
if filename.endswith(".py"): |
| 57 |
python_paths.append(os.path.join(dirpath, filename)) |
| 58 |
return python_paths |
| 59 |
|
| 60 |
|
| 61 |
def get_reqs(*items): |
| 62 |
"""Transform text requirements into Requirement objects.""" |
| 63 |
return [Requirement(item) for item in items] |