2021-09-15 21:03:02 +02:00
|
|
|
import difflib
|
2020-12-29 13:58:51 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
from pathlib import Path
|
|
|
|
|
2022-10-03 18:28:55 +02:00
|
|
|
import requests
|
2022-09-18 19:42:30 +02:00
|
|
|
import tomli
|
2022-09-13 21:08:58 +02:00
|
|
|
from django_tools.serve_media_app.utils import clean_filename
|
|
|
|
from django_tools.unittest_utils.assertments import assert_is_dir, assert_is_file
|
2022-09-15 18:16:57 +02:00
|
|
|
from django_tools.unittest_utils.project_setup import check_editor_config
|
2022-10-03 18:28:55 +02:00
|
|
|
from packaging.version import Version
|
2021-09-15 21:03:02 +02:00
|
|
|
|
2020-12-29 13:58:51 +01:00
|
|
|
import inventory
|
|
|
|
|
|
|
|
|
|
|
|
PACKAGE_ROOT = Path(__file__).parent.parent
|
|
|
|
|
|
|
|
|
|
|
|
def assert_file_contains_string(file_path, string):
|
|
|
|
with file_path.open('r') as f:
|
|
|
|
for line in f:
|
|
|
|
if string in line:
|
|
|
|
return
|
|
|
|
raise AssertionError(f'File {file_path} does not contain {string!r} !')
|
|
|
|
|
|
|
|
|
2022-10-03 18:28:55 +02:00
|
|
|
def get_github_version_tag(github_project_url):
|
|
|
|
api_url = github_project_url.replace('github.com', 'api.github.com/repos')
|
|
|
|
tags = requests.get(f'{api_url}/tags').json()
|
|
|
|
for tag in tags:
|
|
|
|
version_str = tag['name']
|
|
|
|
ver_obj = Version(version_str)
|
|
|
|
if ver_obj.base_version and not ver_obj.is_prerelease:
|
|
|
|
return ver_obj
|
|
|
|
|
|
|
|
|
2020-12-29 13:58:51 +01:00
|
|
|
def test_version():
|
2022-09-18 19:42:30 +02:00
|
|
|
upstream_version = inventory.__version__
|
2022-10-03 18:28:55 +02:00
|
|
|
current_ver_obj = Version(upstream_version)
|
|
|
|
|
|
|
|
github_ver = get_github_version_tag(github_project_url='https://github.com/jedie/PyInventory')
|
|
|
|
assert (
|
|
|
|
github_ver == current_ver_obj
|
|
|
|
), f'Current version from github is: {github_ver} but current package version is: {current_ver_obj}'
|
2020-12-29 13:58:51 +01:00
|
|
|
|
2022-09-18 19:42:30 +02:00
|
|
|
pyproject_toml_path = Path(PACKAGE_ROOT, 'pyproject.toml')
|
|
|
|
pyproject_toml = tomli.loads(pyproject_toml_path.read_text(encoding='UTF-8'))
|
|
|
|
pyproject_version = pyproject_toml['tool']['poetry']['version']
|
|
|
|
assert pyproject_version.startswith(f'{upstream_version}+ynh')
|
|
|
|
|
|
|
|
# pyproject.toml needs a PEP 440 conform version and used "+ynh"
|
|
|
|
# the YunoHost syntax is: "~ynh", just "convert this:
|
|
|
|
manifest_version = pyproject_version.replace('+', '~')
|
|
|
|
|
|
|
|
assert_file_contains_string(
|
|
|
|
file_path=Path(PACKAGE_ROOT, 'manifest.json'),
|
|
|
|
string=f'"version": "{manifest_version}"',
|
|
|
|
)
|
2020-12-29 13:58:51 +01:00
|
|
|
|
|
|
|
|
2021-09-15 21:03:02 +02:00
|
|
|
def poetry_check_output(*args):
|
2020-12-29 13:58:51 +01:00
|
|
|
poerty_bin = shutil.which('poetry')
|
|
|
|
|
|
|
|
output = subprocess.check_output(
|
2021-09-15 21:03:02 +02:00
|
|
|
(poerty_bin,) + args,
|
2022-09-13 21:08:58 +02:00
|
|
|
text=True,
|
2020-12-29 13:58:51 +01:00
|
|
|
env=os.environ,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
cwd=str(PACKAGE_ROOT),
|
|
|
|
)
|
|
|
|
print(output)
|
2021-09-15 21:03:02 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def test_poetry_check():
|
|
|
|
output = poetry_check_output('check')
|
2020-12-29 13:58:51 +01:00
|
|
|
assert output == 'All set!\n'
|
2021-09-15 21:03:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_requirements_txt():
|
2022-09-13 21:26:25 +02:00
|
|
|
requirements_txt = PACKAGE_ROOT / 'conf' / 'requirements.txt'
|
2021-09-15 21:03:02 +02:00
|
|
|
assert_is_file(requirements_txt)
|
|
|
|
|
|
|
|
output = poetry_check_output('export', '-f', 'requirements.txt')
|
|
|
|
assert 'Warning' not in output
|
|
|
|
|
|
|
|
current_content = requirements_txt.read_text()
|
|
|
|
|
|
|
|
diff = '\n'.join(
|
|
|
|
difflib.unified_diff(
|
2022-09-13 21:08:58 +02:00
|
|
|
current_content.splitlines(),
|
|
|
|
output.splitlines(),
|
|
|
|
fromfile=str(requirements_txt),
|
|
|
|
tofile='FRESH EXPORT',
|
2021-09-15 21:03:02 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
print(diff)
|
|
|
|
assert diff == '', f'{requirements_txt} is not up-to-date! (Hint: call: "make update")'
|
2022-09-13 21:08:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_screenshot_filenames():
|
|
|
|
"""
|
|
|
|
https://forum.yunohost.org/t/yunohost-bot-cant-handle-spaces-in-screenshots/19483
|
|
|
|
"""
|
|
|
|
screenshot_path = PACKAGE_ROOT / 'doc' / 'screenshots'
|
|
|
|
assert_is_dir(screenshot_path)
|
|
|
|
renamed = []
|
|
|
|
for file_path in screenshot_path.iterdir():
|
|
|
|
file_name = file_path.name
|
|
|
|
cleaned_name = clean_filename(file_name)
|
|
|
|
if cleaned_name != file_name:
|
|
|
|
new_path = file_path.with_name(cleaned_name)
|
|
|
|
file_path.rename(new_path)
|
|
|
|
renamed.append(f'{file_name!r} renamed to {cleaned_name!r}')
|
|
|
|
assert not renamed, f'Bad screenshots file names found: {", ".join(renamed)}'
|
2022-09-15 18:16:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_editor_config():
|
|
|
|
check_editor_config(package_root=PACKAGE_ROOT)
|