From 289efc52261ca385e6a4415cf325a42ba7570282 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 21 Nov 2025 23:03:03 +0200 Subject: [PATCH] test: support local tests --- Makefile | 39 +++++++++++++++++++++++++++++++++++---- tests/bootstrap.php | 37 +++++++++++++++++++++++++++++++++---- tests/phpunit.local.xml | 18 ++++++++++++++++++ tests/phpunit.xml | 14 ++++++++------ 4 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 tests/phpunit.local.xml diff --git a/Makefile b/Makefile index 13f83e7..405b770 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: Bernhard Posselt # SPDX-License-Identifier: AGPL-3.0-or-later # -# Nextcloud App Template — Makefile +# NextcloudAppTemplate — Makefile # --------------------------------- # A friendly, batteries-included Makefile for building and packaging a Nextcloud app # that uses pnpm (JS) and Composer (PHP). @@ -42,6 +42,10 @@ composer_bin := $(if $(composer),$(composer),php $(composer_phar)) pnpm_wrapper=$(build_tools_directory)/pnpm.sh pnpm_cmd=$(if $(pnpm),$(pnpm),$(pnpm_wrapper)) +# Optional: Set path to Nextcloud installation for local testing +# Can be overridden by environment variable: NEXTCLOUD_ROOT=/path make test +NEXTCLOUD_ROOT ?= + # Default target: install deps & build JS (and PHP if composer.json exists) all: build @@ -198,11 +202,38 @@ appstore: tar czf $(appstore_package_name).tar.gz $(app_name) # test: -# - Run PHP unit tests (standard + optional integration config) +# - Run PHP unit tests locally with a configured Nextcloud installation +# - Requires: A fully configured and installed Nextcloud instance with database +# - Auto-detects Nextcloud installation or uses NEXTCLOUD_ROOT (Makefile var or env var) +# - RECOMMENDED: Use 'make test-docker' instead (works in any environment) .PHONY: test test: composer - $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.xml - ( test ! -f tests/phpunit.integration.xml ) || $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.integration.xml + @NC_ROOT="$(NEXTCLOUD_ROOT)"; \ + if [ -n "$$NC_ROOT" ]; then \ + NC_ROOT=$$(echo "$$NC_ROOT" | sed "s|^\\\~|$$HOME|" | sed "s|^~|$$HOME|"); \ + fi; \ + if [ -z "$$NC_ROOT" ]; then \ + if [ -d "$(CURDIR)/../../../tests/bootstrap.php" ]; then \ + NC_ROOT="$(CURDIR)/../../.."; \ + fi; \ + fi; \ + if [ -z "$$NC_ROOT" ]; then \ + echo "\x1b[33mCould not find Nextcloud installation.\x1b[0m"; \ + echo ""; \ + echo "Local testing requires a fully configured Nextcloud instance."; \ + echo ""; \ + echo "Options:"; \ + echo " 1. Use Docker tests (recommended): \x1b[32mmake test-docker\x1b[0m"; \ + echo " 2. Set NEXTCLOUD_ROOT in Makefile (line 47) or as env var:"; \ + echo " \x1b[32mNEXTCLOUD_ROOT=/path/to/nextcloud make test\x1b[0m"; \ + echo ""; \ + exit 1; \ + fi; \ + echo "\x1b[32mUsing Nextcloud root: $$NC_ROOT\x1b[0m"; \ + NEXTCLOUD_ROOT="$$NC_ROOT" $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.local.xml; \ + if [ -f tests/phpunit.integration.xml ]; then \ + NEXTCLOUD_ROOT="$$NC_ROOT" $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.integration.xml; \ + fi # test-docker: # - Run PHP unit tests inside a Nextcloud Docker container diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 912f999..246ffa1 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,8 +2,37 @@ declare(strict_types=1); -require_once __DIR__ . '/../../../tests/bootstrap.php'; -require_once __DIR__ . '/../vendor/autoload.php'; +// Detect Nextcloud bootstrap location +// Priority: 1) NEXTCLOUD_ROOT env var, 2) Docker location, 3) Standard location +$nextcloudBootstrap = null; -\OC_App::loadApp(OCA\NextcloudAppTemplate\AppInfo\Application::APP_ID); -OC_Hook::clear(); +// Check both $_ENV and getenv() as they can differ depending on PHP configuration +$nextcloudRoot = $_ENV['NEXTCLOUD_ROOT'] ?? getenv('NEXTCLOUD_ROOT'); + +if (!empty($nextcloudRoot)) { + // Use NEXTCLOUD_ROOT environment variable (set by Makefile for local testing) + $nextcloudBootstrap = $nextcloudRoot . '/tests/bootstrap.php'; +} elseif (file_exists(__DIR__ . '/../../../tests/bootstrap.php')) { + // Standard location (Docker/installed in Nextcloud apps directory) + $nextcloudBootstrap = __DIR__ . '/../../../tests/bootstrap.php'; +} + +if ($nextcloudBootstrap && file_exists($nextcloudBootstrap)) { + // Running with full Nextcloud environment + // Define OC_CONSOLE to bypass installation check during tests + if (!defined('OC_CONSOLE')) { + define('OC_CONSOLE', 1); + } + require_once $nextcloudBootstrap; + require_once __DIR__ . '/../vendor/autoload.php'; + \OC_App::loadApp(OCA\NextcloudAppTemplate\AppInfo\Application::APP_ID); + OC_Hook::clear(); +} else { + // Cannot find Nextcloud bootstrap + echo "\n\033[31mError: Nextcloud bootstrap not found.\033[0m\n"; + echo "For local testing, set NEXTCLOUD_ROOT environment variable:\n"; + echo " NEXTCLOUD_ROOT=~/Dev/nextcloud-docker-dev/workspace/server make test\n"; + echo "\nOr run tests in Docker:\n"; + echo " make test-docker\n\n"; + exit(1); +} diff --git a/tests/phpunit.local.xml b/tests/phpunit.local.xml new file mode 100644 index 0000000..9ad6526 --- /dev/null +++ b/tests/phpunit.local.xml @@ -0,0 +1,18 @@ + + + + + . + + + + + ../appinfo + ../lib + + + diff --git a/tests/phpunit.xml b/tests/phpunit.xml index aec4f4c..52bbcd3 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,12 +1,14 @@ - - - . - - + + ../appinfo ../lib - + + + + . + +