Commits

Anonymous committed 2d34df9

[#275] Add requires_chiliproject. We now use the rubygems dependency syntax.

Comments (0)

Files changed (2)

lib/redmine/plugin.rb

       self.id.to_s <=> plugin.id.to_s
     end
 
-    # Sets a requirement on Redmine version
+    # Sets a requirement on the ChiliProject version.
+    # Raises a PluginRequirementError exception if the requirement is not met.
+    #
+    # It uses the same syntax as rubygems requirements.
+    # Examples
+    #   # Requires exactly ChiliProject 1.1.1
+    #   requires_chiliproject "1.1.1"
+    #   requires_chiliproject "= 1.1.1"
+
+    #   # Requires ChiliProject 1.1.x
+    #   requires_chiliproject "~> 1.1.0"
+
+    #   # Requires ChiliProject between 1.1.0 and 1.1.5 or higher
+    #   requires_chiliproject ">= 1.1.0", "<= 1.1.5"
+
+    def requires_chiliproject(*args)
+      required_version = Gem::Requirement.new(*args)
+      chili_version = Gem::Version.new(Redmine::VERSION.to_semver)
+
+      unless required_version.satisfied_by? chili_version
+        raise PluginRequirementError.new("#{id} plugin requires ChiliProject version #{required_version} but current version is #{chili_version}.")
+      end
+      true
+    end
+
+    # Sets a requirement on Redmine version.
     # Raises a PluginRequirementError exception if the requirement is not met
     #
+    # THIS IS A REDMINE COMPATIBILITY INTERFACE
+    #
     # Examples
     #   # Requires Redmine 0.7.3 or higher
     #   requires_redmine :version_or_higher => '0.7.3'

test/unit/lib/redmine/plugin_test.rb

     end
   end
 
+  def test_requires_chiliproject
+    test = self
+    version = Redmine::VERSION.to_semver
+
+    @klass.register :foo do
+      test.assert requires_chiliproject('>= 0.1')
+      test.assert requires_chiliproject(">= #{version}")
+      test.assert requires_chiliproject(version)
+      test.assert_raise Redmine::PluginRequirementError do
+        requires_chiliproject('>= 99.0.0')
+      end
+      test.assert_raise Redmine::PluginRequirementError do
+        requires_chiliproject('< 0.9')
+      end
+      requires_chiliproject('> 0.9', "<= 99.0.0")
+      test.assert_raise Redmine::PluginRequirementError do
+        requires_chiliproject('< 0.9', ">= 98.0.0")
+      end
+
+      test.assert requires_chiliproject("~> #{Redmine::VERSION.to_semver.gsub(/\d+$/, '0')}")
+    end
+  end
+
   def test_requires_redmine_plugin
     test = self
     other_version = '0.5.0'