From 01e064c733ac134a77b7ef59d0f7cc2da1794e61 Mon Sep 17 00:00:00 2001 From: Minecrell Date: Sat, 14 Mar 2020 19:51:22 +0100 Subject: [PATCH] CI: add tests to ensure directory structure stays clean (!1070) pmbootstrap now allows APKBUILDs in arbitrarily nested directories within pmaports. This means that we really need some checks to ensure APKBUILDs don't end up all over the place. Add a test case with a whitelist of allowed directories. Having this list in pmaports allows changing it without a new pmbootstrap release. --- .../testcases/test_directory_structure.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .gitlab-ci/testcases/test_directory_structure.py diff --git a/.gitlab-ci/testcases/test_directory_structure.py b/.gitlab-ci/testcases/test_directory_structure.py new file mode 100644 index 000000000..35eaef6a2 --- /dev/null +++ b/.gitlab-ci/testcases/test_directory_structure.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-3.0-or-later +import glob +import os + +expected_directories = [ + "console", + "cross", + "device/testing", + "firmware", + "hybris", + "kde", + "maemo", + "main", + "modem", + "temp", +] + +# pmbootstrap allows placing APKBUILDs in arbitrarily nested directories. +# This test makes sure all of them are in one of the expected locations. +def test_directories(): + apkbuilds = set(glob.iglob("**/APKBUILD", recursive=True)) + expected = set(f for d in expected_directories for f in glob.iglob(d + "/*/APKBUILD")) + assert apkbuilds == expected, "Found APKBUILD in unexpected directory. " \ + "Consider adding it to test_directory_structure.py!" + +# Make sure files are either: +# - in root directory (README.md) +# - hidden (.gitlab-ci/, device/.shared-patches/) +# - or belong to a package (below a directory with APKBUILD) +def test_files_belong_to_package(): + # Walk directories and set package_dir when we find an APKBUILD + # This allows matching files in subdirectories to the package directory. + package_dir = None + for dirpath, dirs, files in os.walk("."): + # Skip "hidden" directories + dirs[:] = [d for d in dirs if not d.startswith(".")] + # Ignore files in root directory + if dirpath == '.': + continue + + # Switched to another directory? + if package_dir and not dirpath.startswith(package_dir + os.sep): + package_dir = None + + if 'APKBUILD' in files: + assert not package_dir, f"Nested packages: {package_dir} and {dirpath} " \ + "both contain an APKBUILD" + package_dir = dirpath + + assert not files or package_dir, "Found files that do not belong to any package: " \ + f"{dirpath}/{files}"