From 9b652ed5d50a40a1edde0f1b8c859f49ff8248c9 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Sun, 16 Aug 2015 17:49:45 +0200 Subject: [App Code Check] add check for version and mandatory fields * ref #17598 * including unit tests for mandatory fields/versions --- lib/private/app/codechecker/infochecker.php | 141 ++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 lib/private/app/codechecker/infochecker.php (limited to 'lib') diff --git a/lib/private/app/codechecker/infochecker.php b/lib/private/app/codechecker/infochecker.php new file mode 100644 index 00000000000..2af72ebedc3 --- /dev/null +++ b/lib/private/app/codechecker/infochecker.php @@ -0,0 +1,141 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OC\App\CodeChecker; + +use OC\App\InfoParser; +use OC\Hooks\BasicEmitter; + +class InfoChecker extends BasicEmitter { + + /** @var InfoParser */ + private $infoParser; + + private $mandatoryFields = [ + 'author', + 'description', + 'id', + 'licence', + 'name', + ]; + private $optionalFields = [ + 'bugs', + 'category', + 'documentation', + 'namespace', + 'ocsid', + 'repository', + 'require', + 'requiremin', + 'types', + 'version', + 'website', + ]; + private $deprecatedFields = [ + 'default_enable', + 'public', + 'remote', + 'shipped', + 'standalone', + ]; + + public function __construct(InfoParser $infoParser) { + $this->infoParser = $infoParser; + } + + /** + * @param string $appId + * @return array + */ + public function analyse($appId) { + $appPath = \OC_App::getAppPath($appId); + if ($appPath === false) { + throw new \RuntimeException("No app with given id <$appId> known."); + } + + $errors = []; + + $info = $this->infoParser->parse($appPath . '/appinfo/info.xml'); + + foreach ($info as $key => $value) { + if (in_array($key, $this->mandatoryFields)) { + $this->emit('InfoChecker', 'mandatoryFieldFound', [$key, $value]); + continue; + } + + if (in_array($key, $this->optionalFields)) { + $this->emit('InfoChecker', 'optionalFieldFound', [$key, $value]); + continue; + } + + if (in_array($key, $this->deprecatedFields)) { + // skip empty arrays - empty arrays for remote and public are always added + if($value === []) { + continue; + } + $this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]); + continue; + } + + $this->emit('InfoChecker', 'unusedFieldFound', [$key, $value]); + } + + foreach ($this->mandatoryFields as $key) { + if(!isset($info[$key])) { + $this->emit('InfoChecker', 'mandatoryFieldMissing', [$key]); + $errors[] = [ + 'type' => 'mandatoryFieldMissing', + 'field' => $key, + ]; + } + } + + $versionFile = $appPath . '/appinfo/version'; + if (is_file($versionFile)) { + $version = trim(file_get_contents($versionFile)); + if(isset($info['version'])) { + if($info['version'] !== $version) { + $this->emit('InfoChecker', 'differentVersions', + [$version, $info['version']]); + $errors[] = [ + 'type' => 'differentVersions', + 'message' => 'appinfo/version: ' . $version . + ' - appinfo/info.xml: ' . $info['version'], + ]; + } else { + $this->emit('InfoChecker', 'sameVersions', [$versionFile]); + } + } else { + $this->emit('InfoChecker', 'migrateVersion', [$version]); + } + } else { + if(!isset($info['version'])) { + $this->emit('InfoChecker', 'mandatoryFieldMissing', ['version']); + $errors[] = [ + 'type' => 'mandatoryFieldMissing', + 'field' => 'version', + ]; + } + } + + return $errors; + } +} -- cgit v1.2.3 From 5a3e57c2f5e558f88fbd83010ff64632bec46123 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 24 Sep 2015 12:48:44 +0200 Subject: encode arrays as string --- core/command/app/checkcode.php | 3 --- lib/private/app/codechecker/infochecker.php | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/core/command/app/checkcode.php b/core/command/app/checkcode.php index 0db9958f387..9f6e0729ce9 100644 --- a/core/command/app/checkcode.php +++ b/core/command/app/checkcode.php @@ -127,9 +127,6 @@ class CheckCode extends Command { if($value === [] || is_null($value) || $value === '') { $output->writeln("Deprecated field available: $key"); } else { - if(is_array($value)) { - $value = 'Array of ' . count($value) . ' element(s)'; - } $output->writeln("Deprecated field available: $key => $value"); } }); diff --git a/lib/private/app/codechecker/infochecker.php b/lib/private/app/codechecker/infochecker.php index 2af72ebedc3..1272707a339 100644 --- a/lib/private/app/codechecker/infochecker.php +++ b/lib/private/app/codechecker/infochecker.php @@ -39,6 +39,7 @@ class InfoChecker extends BasicEmitter { private $optionalFields = [ 'bugs', 'category', + 'dependencies', 'documentation', 'namespace', 'ocsid', @@ -51,6 +52,7 @@ class InfoChecker extends BasicEmitter { ]; private $deprecatedFields = [ 'default_enable', + 'info', 'public', 'remote', 'shipped', @@ -76,6 +78,9 @@ class InfoChecker extends BasicEmitter { $info = $this->infoParser->parse($appPath . '/appinfo/info.xml'); foreach ($info as $key => $value) { + if(is_array($value)) { + $value = json_encode($value); + } if (in_array($key, $this->mandatoryFields)) { $this->emit('InfoChecker', 'mandatoryFieldFound', [$key, $value]); continue; @@ -88,7 +93,7 @@ class InfoChecker extends BasicEmitter { if (in_array($key, $this->deprecatedFields)) { // skip empty arrays - empty arrays for remote and public are always added - if($value === []) { + if($value === '[]' && in_array($key, ['public', 'remote', 'info'])) { continue; } $this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]); -- cgit v1.2.3 From 03bf2f786ad84a2ecbe066a2c80097bf625a91d2 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 24 Sep 2015 16:47:00 +0200 Subject: public, remote and default_enable are not deprecated --- lib/private/app/codechecker/infochecker.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/private/app/codechecker/infochecker.php b/lib/private/app/codechecker/infochecker.php index 1272707a339..91580bde07d 100644 --- a/lib/private/app/codechecker/infochecker.php +++ b/lib/private/app/codechecker/infochecker.php @@ -39,10 +39,13 @@ class InfoChecker extends BasicEmitter { private $optionalFields = [ 'bugs', 'category', + 'default_enable', 'dependencies', 'documentation', 'namespace', 'ocsid', + 'public', + 'remote', 'repository', 'require', 'requiremin', @@ -51,10 +54,7 @@ class InfoChecker extends BasicEmitter { 'website', ]; private $deprecatedFields = [ - 'default_enable', 'info', - 'public', - 'remote', 'shipped', 'standalone', ]; -- cgit v1.2.3