diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2021-06-10 17:12:53 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2021-06-10 17:12:53 -0700 |
commit | 0505c6f1dc4d1ee2ec8ba8a755367c1c853dc2a3 (patch) | |
tree | ae1cfb115842318987b18c1b757a109b7eb7452f | |
parent | 0bc0a13cbc5775a34fbc2984f3abf5d13897f60d (diff) | |
download | chef-0505c6f1dc4d1ee2ec8ba8a755367c1c853dc2a3.tar.gz |
Add macos_ruby? helper and wire to the macos? helper
This is useful for internal use when we are in a class context or
whenever we are before load_node/build_node.
It might also be useful for users to gate requires that are
macos-specific for macos only gems, or something like that.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | chef-utils/README.md | 1 | ||||
-rw-r--r-- | chef-utils/lib/chef-utils/dsl/platform_family.rb | 13 | ||||
-rw-r--r-- | chef-utils/spec/unit/dsl/platform_family_spec.rb | 16 |
3 files changed, 27 insertions, 3 deletions
diff --git a/chef-utils/README.md b/chef-utils/README.md index 6b0052e073..9dcbd9b9ea 100644 --- a/chef-utils/README.md +++ b/chef-utils/README.md @@ -27,6 +27,7 @@ The Platform Family helpers provide an alternative to comparing values from `nod * `freebsd?` * `gentoo?` * `macos?` +* `macos_ruby?` - this is always true if the ruby VM is running on a mac host and is not stubbed by ChefSpec * `netbsd?` * `openbsd?` * `rhel?` - includes redhat, centos, scientific, oracle, and clearos platforms diff --git a/chef-utils/lib/chef-utils/dsl/platform_family.rb b/chef-utils/lib/chef-utils/dsl/platform_family.rb index 10d7b35901..fa460573fe 100644 --- a/chef-utils/lib/chef-utils/dsl/platform_family.rb +++ b/chef-utils/lib/chef-utils/dsl/platform_family.rb @@ -77,7 +77,7 @@ module ChefUtils # @return [Boolean] # def macos?(node = __getnode) - node["platform_family"] == "mac_os_x" + node ? node["platform_family"] == "mac_os_x" : macos_ruby? end # chef-sugar backcompat method alias_method :osx?, :macos? @@ -86,6 +86,17 @@ module ChefUtils # chef-sugar backcompat method alias_method :mac_os_x?, :macos? + # Determine if the Ruby VM is currently running on a Mac node (This is useful primarily for internal use + # by chef client before the node object exists). + # + # @since 17.3 + # + # @return [Boolean] + # + def macos_ruby? + !!(RUBY_PLATFORM =~ /darwin/) + end + # Determine if the current node is a member of the 'rhel' platform family (Red Hat, CentOS, Oracle or Scientific Linux, but NOT Amazon Linux or Fedora). # # @param [Chef::Node] node the node to check diff --git a/chef-utils/spec/unit/dsl/platform_family_spec.rb b/chef-utils/spec/unit/dsl/platform_family_spec.rb index 714a9f0a45..c4363c8e8d 100644 --- a/chef-utils/spec/unit/dsl/platform_family_spec.rb +++ b/chef-utils/spec/unit/dsl/platform_family_spec.rb @@ -25,7 +25,7 @@ def pf_reports_true_for(*args) expect(described_class.send(method, node)).to be true end end - (PLATFORM_FAMILY_HELPERS - [ :windows_ruby? ] - args).each do |method| + (PLATFORM_FAMILY_HELPERS - %i{windows_ruby? macos_ruby?} - args).each do |method| it "reports false for #{method}" do expect(described_class.send(method, node)).to be false end @@ -41,7 +41,7 @@ RSpec.describe ChefUtils::DSL::PlatformFamily do end end - ( PLATFORM_FAMILY_HELPERS - [ :windows_ruby? ]).each do |helper| + ( PLATFORM_FAMILY_HELPERS - %i{windows_ruby? macos_ruby?}).each do |helper| it "has the #{helper} in the ChefUtils module" do expect(ChefUtils).to respond_to(helper) end @@ -220,4 +220,16 @@ RSpec.describe ChefUtils::DSL::PlatformFamily do end end end + + context "node-independent mac APIs" do + if RUBY_PLATFORM.match?(/darwin/) + it "reports true for :macos_ruby?" do + expect(described_class.macos_ruby?).to be true + end + else + it "reports false for :macos_ruby?" do + expect(described_class.macos_ruby?).to be false + end + end + end end |