summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2020-02-26 11:47:50 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2020-02-26 11:47:50 -0800
commitcf453de40ea9e8f70069b11890d52a09d96d441a (patch)
treea4f6bf578b6a78abe9ca673e3a87a2159381c7cd
parent63633cc1ddfbf79ff77e97376184e2d27b954678 (diff)
downloadchef-cf453de40ea9e8f70069b11890d52a09d96d441a.tar.gz
Convert the node[:platform_version] to a Chef::VersionStringlcg/platform_version_sugar
`node[:platform_version] =~ "~> 1.2"` will now just work. This might conceivably be breaking if people are doing really weird things by the class changing (Chef::VersionString still inherits from String though, so it'll have to be weirdness on par with the bad `thing.class.to_s == "Mash"` kind of comparisons that were in ohai). Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--chef-utils/lib/chef-utils/version_string.rb16
-rw-r--r--lib/chef/node.rb4
-rw-r--r--spec/unit/node_spec.rb9
3 files changed, 23 insertions, 6 deletions
diff --git a/chef-utils/lib/chef-utils/version_string.rb b/chef-utils/lib/chef-utils/version_string.rb
index 7a7096909f..e0888b346e 100644
--- a/chef-utils/lib/chef-utils/version_string.rb
+++ b/chef-utils/lib/chef-utils/version_string.rb
@@ -27,8 +27,13 @@ module ChefUtils
#
# @param val [String] Version string to parse.
def initialize(val)
- super
- @parsed_version = ::Gem::Version.create(self)
+ val = "" unless val
+ super(val)
+ begin
+ @parsed_version = ::Gem::Version.create(self)
+ rescue ArgumentError
+ @parsed_version = nil
+ end
end
# @!group Compat wrappers for String
@@ -135,7 +140,12 @@ module ChefUtils
when Regexp
super
else
- Gem::Requirement.create(other) =~ parsed_version
+ begin
+ Gem::Requirement.create(other) =~ parsed_version
+ rescue ArgumentError
+ # one side of the comparison wasn't parseable
+ super
+ end
end
end
diff --git a/lib/chef/node.rb b/lib/chef/node.rb
index 1e5d3a8d59..a5bf1d9b8a 100644
--- a/lib/chef/node.rb
+++ b/lib/chef/node.rb
@@ -2,7 +2,7 @@
# Author:: Christopher Brown (<cb@chef.io>)
# Author:: Christopher Walters (<cw@chef.io>)
# Author:: Tim Hinderliter (<tim@chef.io>)
-# Copyright:: Copyright 2008-2019, Chef Software Inc.
+# Copyright:: Copyright 2008-2020, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -363,7 +363,7 @@ class Chef
# FIXME(log): should be trace
logger.debug("Platform is #{platform} version #{version}")
automatic[:platform] = platform
- automatic[:platform_version] = version
+ automatic[:platform_version] = Chef::VersionString.new(version)
automatic[:chef_guid] = Chef::Config[:chef_guid] || ( Chef::Config[:chef_guid] = node_uuid )
automatic[:name] = name
automatic[:chef_environment] = chef_environment
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb
index 1c84278ad5..5b50f888f0 100644
--- a/spec/unit/node_spec.rb
+++ b/spec/unit/node_spec.rb
@@ -1,6 +1,6 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
-# Copyright:: Copyright 2008-2019, Chef Software Inc.
+# Copyright:: Copyright 2008-2020, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -966,6 +966,13 @@ describe Chef::Node do
expect(node.normal_attrs).to eq({ "foo" => "bar", "tags" => [] })
end
+ it "converts the platform_version to a Chef::VersionString" do
+ node.consume_external_attrs(@ohai_data, {})
+ expect(node.automatic_attrs[:platform_version]).to be_a_kind_of(Chef::VersionString)
+ expect(node[:platform_version]).to be_a_kind_of(Chef::VersionString)
+ expect(node[:platform_version] =~ "~> 23.6").to be true
+ end
+
end
describe "when expanding its run list and merging attributes" do