Skip to content

Commit

Permalink
Merge pull request #172 from YunoHost/actions/black
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamandar authored Nov 18, 2024
2 parents 26c67a8 + cbe51ee commit 8202da7
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 40 deletions.
40 changes: 24 additions & 16 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
import tomllib
from typing import Generator

from lib.lib_package_linter import (Error, Info, Success, TestResult,
TestSuite, Warning, config_panel_v1_schema,
not_empty, test, tests_reports,
validate_schema)
from lib.lib_package_linter import (
Error,
Info,
Success,
TestResult,
TestSuite,
Warning,
config_panel_v1_schema,
not_empty,
test,
tests_reports,
validate_schema,
)
from lib.print import _print, is_json_output
from tests.test_catalog import AppCatalog
from tests.test_configurations import Configurations
Expand Down Expand Up @@ -412,7 +421,9 @@ def doc_dir(app) -> TestResult:
@test()
def doc_dir_v2(app) -> TestResult:

if (app.path / "doc").exists() and not (app.path / "doc" / "DESCRIPTION.md").exists():
if (app.path / "doc").exists() and not (
app.path / "doc" / "DESCRIPTION.md"
).exists():
yield Error(
"A DESCRIPTION.md is now mandatory in packaging v2 and is meant to contains an extensive description of what the app is and does. Consider also adding a '/doc/screenshots/' folder with a few screenshots of what the app looks like."
)
Expand Down Expand Up @@ -523,28 +534,25 @@ def config_panel(app) -> TestResult:
"Please do not commit config_panel.toml.example ... This is just a 'documentation' for the config panel syntax meant to be kept in example_ynh"
)

if not not_empty(app.path / "config_panel.toml") and not_empty(app.path / "scripts" / "config"):
if not not_empty(app.path / "config_panel.toml") and not_empty(
app.path / "scripts" / "config"
):
yield Warning(
"The script 'config' exists but there is no config_panel.toml ... Please remove the 'config' script if this is just the example from example_ynh, or add a proper config_panel.toml if the point is really to have a config panel"
)

if not_empty(app.path / "config_panel.toml"):
check_old_panel = os.system(
"grep -q 'version = \"0.1\"' '%s'"
% (app.path / "config_panel.toml")
"grep -q 'version = \"0.1\"' '%s'" % (app.path / "config_panel.toml")
)
if check_old_panel == 0:
yield Error(
"Config panels version 0.1 are not supported anymore, should be adapted for version 1.0"
)
elif (
(app.path / "scripts" / "config").exists()
and os.system(
"grep -q 'YNH_CONFIG_\\|yunohost app action' '%s'"
% (app.path / "scripts" / "config")
)
== 0
):
elif (app.path / "scripts" / "config").exists() and os.system(
"grep -q 'YNH_CONFIG_\\|yunohost app action' '%s'"
% (app.path / "scripts" / "config")
) == 0:
yield Error(
"The config panel is set to version 1.x, but the config script is apparently still using some old code from 0.1 such as '$YNH_CONFIG_STUFF' or 'yunohost app action'"
)
Expand Down
29 changes: 23 additions & 6 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
from types import ModuleType
from typing import Any, Generator

from lib.lib_package_linter import (Critical, Error, Info, Success, TestResult,
TestSuite, Warning, test, urlopen)
from lib.lib_package_linter import (
Critical,
Error,
Info,
Success,
TestResult,
TestSuite,
Warning,
test,
urlopen,
)
from lib.print import _print

PACKAGE_LINTER_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -48,8 +57,11 @@ def __init__(self, app_id: str) -> None:

def _fetch_app_repo(self) -> None:
flagfile = PACKAGE_LINTER_DIR / ".apps_git_clone_cache"
if APPS_CACHE.exists() and flagfile.exists() \
and time.time() - flagfile.stat().st_mtime < 3600:
if (
APPS_CACHE.exists()
and flagfile.exists()
and time.time() - flagfile.stat().st_mtime < 3600
):
return

if not APPS_CACHE.exists():
Expand Down Expand Up @@ -169,7 +181,9 @@ def _time_points_until_today() -> Generator[datetime, None, None]:

date = datetime(year, month, day)

def get_history(N: int) -> Generator[tuple[datetime, dict[str, Any]], None, None]:
def get_history(
N: int,
) -> Generator[tuple[datetime, dict[str, Any]], None, None]:

for t in list(_time_points_until_today())[(-1 * N) :]:
loader: ModuleType
Expand All @@ -192,7 +206,10 @@ def get_history(N: int) -> Generator[tuple[datetime, dict[str, Any]], None, None
raw_catalog_at_this_date = git(["show", f"{commit}:apps.json"])
loader = json

elif os.system(f"git -C {APPS_CACHE} cat-file -e {commit}:apps.toml") == 0:
elif (
os.system(f"git -C {APPS_CACHE} cat-file -e {commit}:apps.toml")
== 0
):
raw_catalog_at_this_date = git(["show", f"{commit}:apps.toml"])
loader = tomllib
else:
Expand Down
34 changes: 25 additions & 9 deletions tests/test_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
import tomllib
from typing import Any, Generator

from lib.lib_package_linter import (Error, Info, TestReport, TestResult,
TestSuite, Warning, not_empty, test,
tests_v1_schema, validate_schema)
from lib.lib_package_linter import (
Error,
Info,
TestReport,
TestResult,
TestSuite,
Warning,
not_empty,
test,
tests_v1_schema,
validate_schema,
)
from lib.print import _print


Expand Down Expand Up @@ -62,7 +71,10 @@ def encourage_extra_php_conf(self) -> TestResult:
def misc_source_management(self) -> TestResult:
source_dir = self.app.path / "sources"

if source_dir.exists() and len(list(elt for elt in source_dir.iterdir() if elt.is_file())) > 5:
if (
source_dir.exists()
and len(list(elt for elt in source_dir.iterdir() if elt.is_file())) > 5
):
yield Error(
"Upstream app sources shouldn't be stored in this 'sources' folder of this git repository as a copy/paste\n"
"During installation, the package should download sources from upstream via 'ynh_setup_source'.\n"
Expand Down Expand Up @@ -300,7 +312,9 @@ def misc_nginx_path_traversal(self) -> TestResult:
#
# Path traversal issues
#
def find_location_with_alias(locationblock: Any) -> Generator[tuple[str, str], None, None]:
def find_location_with_alias(
locationblock: Any,
) -> Generator[tuple[str, str], None, None]:

if locationblock[0][0] != "location":
return
Expand All @@ -319,7 +333,9 @@ def find_location_with_alias(locationblock: Any) -> Generator[tuple[str, str], N
else:
continue

def find_path_traversal_issue(nginxconf: list[Any]) -> Generator[str, None, None]:
def find_path_traversal_issue(
nginxconf: list[Any],
) -> Generator[str, None, None]:

for block in nginxconf:
for location, alias in find_location_with_alias(block):
Expand Down Expand Up @@ -414,9 +430,9 @@ def bind_public_ip(self) -> TestResult:

for number, line in enumerate(content.split("\n"), 1):
comment = ("#", "//", ";", "/*", "*")
if (
"0.0.0.0" in line or "::" in line
) and not line.strip().startswith(comment):
if ("0.0.0.0" in line or "::" in line) and not line.strip().startswith(
comment
):
for ip in re.split(r"[ \t,='\"(){}\[\]]", line):
if ip == "::" or ip.startswith("0.0.0.0"):
yield Info(
Expand Down
24 changes: 19 additions & 5 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@
import tomllib
from typing import Any, Callable

from lib.lib_package_linter import (Critical, Error, Info, TestResult,
TestSuite, Warning, c, manifest_v2_schema,
spdx_licenses, test, validate_schema)
from lib.lib_package_linter import (
Critical,
Error,
Info,
TestResult,
TestSuite,
Warning,
c,
manifest_v2_schema,
spdx_licenses,
test,
validate_schema,
)

# Only packaging v2 is supported on the linter now ... But someday™ according The Prophecy™, packaging v3 will be a thing
app_packaging_format = 2
Expand Down Expand Up @@ -70,7 +80,9 @@ def __init__(self, path: Path) -> None:
manifest_path = path / "manifest.toml"

# Taken from https://stackoverflow.com/a/49518779
def check_for_duplicate_keys(ordered_pairs: list[tuple[str, Any]]) -> dict[str, Any]:
def check_for_duplicate_keys(
ordered_pairs: list[tuple[str, Any]]
) -> dict[str, Any]:
dict_out = {}
for key, val in ordered_pairs:
if key in dict_out:
Expand All @@ -83,7 +95,9 @@ def check_for_duplicate_keys(ordered_pairs: list[tuple[str, Any]]) -> dict[str,
try:
self.manifest = tomllib.loads(self.raw_manifest)
except Exception as e:
print(f"{c.FAIL}✘ Looks like there's a syntax issue in your manifest?\n ---> {e}")
print(
f"{c.FAIL}✘ Looks like there's a syntax issue in your manifest?\n ---> {e}"
)
sys.exit(1)

@test()
Expand Down
17 changes: 13 additions & 4 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
import subprocess
from typing import Generator

from lib.lib_package_linter import (Critical, Error, Info, TestResult,
TestSuite, Warning, not_empty,
report_warning_not_reliable, test)
from lib.lib_package_linter import (
Critical,
Error,
Info,
TestResult,
TestSuite,
Warning,
not_empty,
report_warning_not_reliable,
test,
)
from lib.print import _print


Expand Down Expand Up @@ -371,7 +379,8 @@ def argument_fetching(self) -> TestResult:
def sources_list_tweaking(self) -> TestResult:
common_sh = self.app / "scripts" / "_common.sh"
if self.contains("/etc/apt/sources.list") or (
common_sh.exists() and "/etc/apt/sources.list" in common_sh.read_text()
common_sh.exists()
and "/etc/apt/sources.list" in common_sh.read_text()
and "ynh_add_repo" not in common_sh.read_text()
):
yield Error(
Expand Down

0 comments on commit 8202da7

Please sign in to comment.