Permalink
Browse files

Handle rvm install flags with branch=stable & version=x.y.z.

The readme explains that to use a tagged version that you must set the
branch value to "none" which is overly surprising.

The ultimate solution is cover the method responsible
(#build_script_flags) with unit tests to catch all possible
combinations. Unfortunetly the branch and version flags can interfere
with each other making any solution here subpar.

Closes #152
  • Loading branch information...
1 parent 485e042 commit e899c038d470b54647f9f541f35e4bf528dd5218 @fnichol fnichol committed Jan 25, 2013
View
@@ -2,6 +2,7 @@ source 'https://rubygems.org'
gem 'rake'
gem 'foodcritic'
+gem 'minitest'
# allow CI to override the version of Chef for matrix testing
gem 'chef', (ENV['CHEF_VERSION'] || '>= 0.10.10')
@@ -22,18 +22,12 @@
class Chef
module RVM
module RecipeHelpers
- def build_script_flags(version, branch)
- script_flags = ""
- if version || (branch && branch != "none")
- script_flags += " -s --"
- end
- if version
- script_flags += " --version #{version}"
- end
- if branch && branch != "none"
- script_flags += " --branch #{branch}"
+ def build_script_flags(branch, version = "head")
+ if version =~ /\A\d+\.\d+\.\d+/ && %w{stable master none}.include?(branch)
+ " -s -- --version #{version}"
+ else
+ " -s -- --branch #{branch} --version #{version}"
end
- script_flags
end
def build_upgrade_strategy(strategy)
@@ -19,7 +19,7 @@
include_recipe 'rvm'
-script_flags = build_script_flags(node['rvm']['version'], node['rvm']['branch'])
+script_flags = build_script_flags(node['rvm']['branch'], node['rvm']['version'])
upgrade_strategy = build_upgrade_strategy(node['rvm']['upgrade'])
installer_url = node['rvm']['installer_url']
rvm_prefix = ::File.dirname(node['rvm']['root_path'])
View
@@ -25,7 +25,7 @@
version = rvm_user['version'] || node['rvm']['version']
branch = rvm_user['branch'] || node['rvm']['branch']
- script_flags = build_script_flags(version, branch)
+ script_flags = build_script_flags(branch, version)
upgrade_strategy = build_upgrade_strategy(rvm_user['upgrade'])
installer_url = rvm_user['installer_url'] || node['rvm']['installer_url']
rvm_prefix = rvm_user['home'] ||
@@ -0,0 +1,73 @@
+#
+# Cookbook Name:: rvm
+# Library:: Chef::RVM::RecipeHelpers
+#
+# Author:: Fletcher Nichol <fnichol@nichol.ca>
+#
+# Copyright 2013, Fletcher Nichol
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+def require_library(name)
+ require File.expand_path(File.join(
+ File.dirname(__FILE__), '../../../../libraries', "#{name}.rb"
+ ))
+end
+
+require 'minitest/autorun'
+require_library 'chef_rvm_recipe_helpers'
+
+class Dummy
+ include Chef::RVM::RecipeHelpers
+end
+
+describe 'Chef::RVM::RecipeHelpers' do
+
+ subject do
+ Dummy.new
+ end
+
+ describe '.build_script_flags' do
+
+ it 'sets branch and version flags' do
+ subject.build_script_flags("stable", "head").
+ must_equal " -s -- --branch stable --version head"
+ end
+
+ it 'sets a missing branch to "head"' do
+ subject.build_script_flags("cool").
+ must_equal " -s -- --branch cool --version head"
+ end
+
+ it 'only emits version with branch=stable and version=x.y.z' do
+ subject.build_script_flags("stable", "1.2.3").
+ must_equal " -s -- --version 1.2.3"
+ end
+
+ it 'only emits version with branch=master and version=x.y.z' do
+ subject.build_script_flags("master", "4.5.6").
+ must_equal " -s -- --version 4.5.6"
+ end
+
+ it 'only emits version with branch=none and version=x.y.z' do
+ subject.build_script_flags("none", "7.3.5").
+ must_equal " -s -- --version 7.3.5"
+ end
+
+ it 'emits version and branch with branch not stable|master and version=x.y.z' do
+ subject.build_script_flags("foo/bar", "0.9.8").
+ must_equal " -s -- --branch foo/bar --version 0.9.8"
+ end
+ end
+end

0 comments on commit e899c03

Please sign in to comment.