diff options
author | Tim Smith <tsmith@chef.io> | 2020-02-26 12:59:20 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-26 12:59:20 -0800 |
commit | 0a9a6070eb17936ff13c0114ed620b9efddd1d5e (patch) | |
tree | 3b98db73217c86fce3c2e8a3d5e622ed210d2e71 | |
parent | 005d5fc87bba0ac8ea9eba87edbe2ac22f4389d3 (diff) | |
parent | cf453de40ea9e8f70069b11890d52a09d96d441a (diff) | |
download | chef-0a9a6070eb17936ff13c0114ed620b9efddd1d5e.tar.gz |
Merge pull request #9400 from chef/lcg/platform_version_sugar
Convert the node[:platform_version] to a Chef::VersionString
-rw-r--r-- | chef-utils/lib/chef-utils/version_string.rb | 16 | ||||
-rw-r--r-- | lib/chef/node.rb | 4 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 9 |
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 |