]> source.dussan.org Git - nextcloud-server.git/commitdiff
make version check work on the lowest common version denominator
authorBernhard Posselt <dev@bernhard-posselt.com>
Thu, 26 Feb 2015 10:23:08 +0000 (11:23 +0100)
committerBernhard Posselt <dev@bernhard-posselt.com>
Thu, 26 Feb 2015 10:42:08 +0000 (11:42 +0100)
lib/private/app/dependencyanalyzer.php
tests/lib/app/dependencyanalyzer.php

index 068a8e51383c67faa9086eb0a9fd5fc66d5684ee..277303eac5502e03e3f4ff92c13def8ec052e42b 100644 (file)
@@ -61,17 +61,57 @@ class DependencyAnalyzer {
                        $this->analyzeOC($dependencies, $app));
        }
 
+       /**
+        * Truncates both verions to the lowest common version, e.g.
+        * 5.1.2.3 and 5.1 will be turned into 5.1 and 5.1,
+        * 5.2.6.5 and 5.1 will be turned into 5.2 and 5.1
+        * @param string $first
+        * @param string $second
+        * @return array first element is the first version, second element is the
+        * second version
+        */
+       private function normalizeVersions($first, $second) {
+               $first = explode('.', $first);
+               $second = explode('.', $second);
+
+               // get both arrays to the same minimum size
+               $length = min(count($second), count($first));
+               $first = array_slice($first, 0, $length);
+               $second = array_slice($second, 0, $length);
+
+               return [implode('.', $first), implode('.', $second)];
+       }
+
+       private function compare($first, $second, $operator) {
+               // we cant normalize versions if one of the given parameters is not a
+               // version string but null. In case one parameter is null normalization
+               // will therefore be skipped
+               if ($first !== null && $second !== null) {
+                       list($first, $second) = $this->normalizeVersions($first, $second);
+               }
+
+               return version_compare($first, $second, $operator);
+       }
+
+       private function compareBigger($first, $second) {
+               return $this->compare($first, $second, '>');
+       }
+
+       private function compareSmaller($first, $second) {
+               return $this->compare($first, $second, '<');
+       }
+
        private function analyzePhpVersion($dependencies) {
                $missing = [];
                if (isset($dependencies['php']['@attributes']['min-version'])) {
                        $minVersion = $dependencies['php']['@attributes']['min-version'];
-                       if (version_compare($this->platform->getPhpVersion(), $minVersion, '<')) {
+                       if ($this->compareSmaller($this->platform->getPhpVersion(), $minVersion)) {
                                $missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion);
                        }
                }
                if (isset($dependencies['php']['@attributes']['max-version'])) {
                        $maxVersion = $dependencies['php']['@attributes']['max-version'];
-                       if (version_compare($this->platform->getPhpVersion(), $maxVersion, '>')) {
+                       if ($this->compareBigger($this->platform->getPhpVersion(), $maxVersion)) {
                                $missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', $maxVersion);
                        }
                }
@@ -145,14 +185,14 @@ class DependencyAnalyzer {
                        if (is_array($lib)) {
                                if (isset($lib['@attributes']['min-version'])) {
                                        $minVersion = $lib['@attributes']['min-version'];
-                                       if (version_compare($libVersion, $minVersion, '<')) {
+                                       if ($this->compareSmaller($libVersion, $minVersion)) {
                                                $missing[] = (string)$this->l->t('Library %s with a version higher than %s is required - available version %s.',
                                                        array($libName, $minVersion, $libVersion));
                                        }
                                }
                                if (isset($lib['@attributes']['max-version'])) {
                                        $maxVersion = $lib['@attributes']['max-version'];
-                                       if (version_compare($libVersion, $maxVersion, '>')) {
+                                       if ($this->compareBigger($libVersion, $maxVersion)) {
                                                $missing[] = (string)$this->l->t('Library %s with a version lower than %s is required - available version %s.',
                                                        array($libName, $maxVersion, $libVersion));
                                        }
@@ -204,12 +244,12 @@ class DependencyAnalyzer {
                }
 
                if (!is_null($minVersion)) {
-                       if (version_compare($this->platform->getOcVersion(), $minVersion, '<')) {
+                       if ($this->compareSmaller($this->platform->getOcVersion(), $minVersion)) {
                                $missing[] = (string)$this->l->t('ownCloud %s or higher is required.', $minVersion);
                        }
                }
                if (!is_null($maxVersion)) {
-                       if (version_compare($this->platform->getOcVersion(), $maxVersion, '>')) {
+                       if ($this->compareBigger($this->platform->getOcVersion(), $maxVersion)) {
                                $missing[] = (string)$this->l->t('ownCloud with a version lower than %s is required.', $maxVersion);
                        }
                }
index a79995b78b3e80fbb005efae46e12b541a20682e..d73bac5a2a74c45921890808b2da9a489add2af6 100644 (file)
@@ -52,7 +52,7 @@ class DependencyAnalyzer extends \PHPUnit_Framework_TestCase {
                        }));
                $this->platformMock->expects($this->any())
                        ->method('getOcVersion')
-                       ->will( $this->returnValue('8.0.1'));
+                       ->will( $this->returnValue('8.0.2'));
 
                $this->l10nMock = $this->getMockBuilder('\OCP\IL10N')
                        ->disableOriginalConstructor()
@@ -183,8 +183,12 @@ class DependencyAnalyzer extends \PHPUnit_Framework_TestCase {
                return array(
                        // no version -> no missing dependency
                        array(array(), null),
+                       array(array(), array('@attributes' => array('min-version' => '8', 'max-version' => '8'))),
+                       array(array(), array('@attributes' => array('min-version' => '8.0', 'max-version' => '8.0'))),
+                       array(array(), array('@attributes' => array('min-version' => '8.0.2', 'max-version' => '8.0.2'))),
+                       array(array('ownCloud 8.0.3 or higher is required.'), array('@attributes' => array('min-version' => '8.0.3'))),
                        array(array('ownCloud 9 or higher is required.'), array('@attributes' => array('min-version' => '9'))),
-                       array(array('ownCloud with a version lower than 5.1.2 is required.'), array('@attributes' => array('max-version' => '5.1.2'))),
+                       array(array('ownCloud with a version lower than 8.0.1 is required.'), array('@attributes' => array('max-version' => '8.0.1'))),
                );
        }
 
@@ -208,7 +212,17 @@ class DependencyAnalyzer extends \PHPUnit_Framework_TestCase {
                                array(array('@attributes' => array('min-version' => '100.0'), '@value' => 'curl'))),
                        // curl in version 100.0 does not exist
                        array(array('Library curl with a version lower than 1.0.0 is required - available version 2.3.4.'),
-                               array(array('@attributes' => array('max-version' => '1.0.0'), '@value' => 'curl')))
+                               array(array('@attributes' => array('max-version' => '1.0.0'), '@value' => 'curl'))),
+                       array(array('Library curl with a version lower than 2.3.3 is required - available version 2.3.4.'),
+                               array(array('@attributes' => array('max-version' => '2.3.3'), '@value' => 'curl'))),
+                       array(array('Library curl with a version higher than 2.3.5 is required - available version 2.3.4.'),
+                               array(array('@attributes' => array('min-version' => '2.3.5'), '@value' => 'curl'))),
+                       array(array(),
+                               array(array('@attributes' => array('min-version' => '2.3.4', 'max-version' => '2.3.4'), '@value' => 'curl'))),
+                       array(array(),
+                               array(array('@attributes' => array('min-version' => '2.3', 'max-version' => '2.3'), '@value' => 'curl'))),
+                       array(array(),
+                               array(array('@attributes' => array('min-version' => '2', 'max-version' => '2'), '@value' => 'curl'))),
                );
        }
 
@@ -244,6 +258,7 @@ class DependencyAnalyzer extends \PHPUnit_Framework_TestCase {
                        array(array(), '5.4', '5.5'),
                        array(array('PHP 5.4.4 or higher is required.'), '5.4.4', null),
                        array(array('PHP with a version lower than 5.4.2 is required.'), null, '5.4.2'),
+                       array(array(), '5.4', '5.4'),
                );
        }
 }