Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix lib name for chef-client 18.6.2 and greater #191

Merged
merged 18 commits into from
Mar 17, 2025
7 changes: 0 additions & 7 deletions Berksfile

This file was deleted.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This file is used to list changes made in each version of the homebrew cookbook.

## Unreleased

- Updated library call for new homebrew class name found in chef-client 18.6.2+ releases

## 5.4.9 - *2024-11-18*

Standardise files with files in sous-chefs/repo-management
Expand Down
13 changes: 13 additions & 0 deletions Policyfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Policyfile.rb - Describe how you want Chef Infra Client to build your system.

name 'homebrew'

# Where to find external cookbooks:
default_source :supermarket

# run_list: chef-client will run these recipes in the order specified.
run_list 'test::default'

# Specify a custom source for a single cookbook:
cookbook 'homebrew', path: '.'
cookbook 'test', path: './test/cookbooks/test'
44 changes: 10 additions & 34 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#

class HomebrewUserWrapper
require 'chef/mixin/homebrew_user'
include Chef::Mixin::HomebrewUser
require 'chef/mixin/homebrew'
include Chef::Mixin::Homebrew
include Chef::Mixin::Which
end

Expand Down Expand Up @@ -60,41 +60,17 @@ def exist?

def owner
@owner ||= begin
# once we only support 14.0 we can switch this to find_homebrew_username
require 'etc'
::Etc.getpwuid(HomebrewUserWrapper.new.find_homebrew_uid).name
rescue Chef::Exceptions::CannotDetermineHomebrewOwner
calculate_owner
end.tap do |owner|
Chef::Log.debug("Homebrew owner is #{owner}")
end
end

private

def calculate_owner
owner = homebrew_owner_attr || sudo_user || current_user
if owner == 'root'
raise Chef::Exceptions::User,
"Homebrew owner is 'root' which is not supported. " \
"To set an explicit owner, please set node['homebrew']['owner']."
end
owner
end

def homebrew_owner_attr
Chef.node['homebrew']['owner']
end

def sudo_user
ENV['SUDO_USER']
end

def current_user
ENV['USER']
HomebrewUserWrapper.new.find_homebrew_username
rescue
Chef::Exceptions::CannotDetermineHomebrewPath
end.tap do |owner|
Chef::Log.debug("Homebrew owner is #{owner}")
end
end
end unless defined?(Homebrew)

class HomebrewWrapper
include Homebrew
end

Chef::Mixin::Homebrew.include(Homebrew)
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

source_url 'https://github.com/sous-chefs/homebrew'
issues_url 'https://github.com/sous-chefs/homebrew/issues'
chef_version '>= 15.3'
chef_version '>= 18.6.2'
1 change: 1 addition & 0 deletions resources/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# limitations under the License.
#

unified_mode true
chef_version_for_provides '< 14.0' if respond_to?(:chef_version_for_provides)

property :cask_name, String, regex: %r{^[\w/-]+$}, name_property: true
Expand Down
1 change: 1 addition & 0 deletions resources/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# limitations under the License.
#

unified_mode true
chef_version_for_provides '< 14.0' if respond_to?(:chef_version_for_provides)

property :tap_name, String, name_property: true, regex: %r{^[\w-]+(?:\/[\w-]+)+$}
Expand Down
21 changes: 0 additions & 21 deletions spec/recipes/cask_spec.rb

This file was deleted.

100 changes: 0 additions & 100 deletions spec/recipes/default_spec.rb

This file was deleted.

51 changes: 0 additions & 51 deletions spec/recipes/install_formulas_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'chefspec'
require 'chefspec/berkshelf'
require 'chefspec/policyfile'

require_relative '../libraries/helpers'

Expand Down
66 changes: 23 additions & 43 deletions spec/unit/libraries/homebrew_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,38 @@
require 'spec_helper'
require 'chef/mixin/shell_out'

describe Homebrew do
let(:opt_homebrew_path) { '/opt/homebrew' }
let(:usr_local_homebrew_path) { '/usr/local' }
let(:usr_local_repository_path) { '/usr/local/Homebrew' }
RSpec.describe Homebrew do
let(:homebrew) { HomebrewWrapper.new }

let(:shellout) do
double(command: 'sysctl -n hw.optional.arm64', run_command: nil, error!: nil, stdout: arm64_test_output,
stderr: stderr, exitstatus: exitstatus, live_stream: nil)
end

before(:each) do
allow(Mixlib::ShellOut).to receive(:new).and_return(shellout)
allow(shellout).to receive(:live_stream=).and_return(nil)
end

context 'when on Apple arm64 Silicon' do
let(:arm64_test_output) { "1\n" }
let(:exitstatus) { 0 }
let(:stderr) { double(empty?: true) }

describe '#install_path' do
let(:dummy_class) { Class.new { include Homebrew } }
it 'returns /opt/homebrew path' do
expect(dummy_class.new.install_path).to eq opt_homebrew_path
end
describe '#install_path' do
it 'returns /opt/homebrew for ARM-based systems' do
allow(homebrew).to receive(:shell_out).with('sysctl -n hw.optional.arm64').and_return(double(stdout: "1\n"))
expect(homebrew.install_path).to eq('/opt/homebrew')
end

describe '#repository_path' do
let(:dummy_class) { Class.new { include Homebrew } }
it 'returns /opt/homebrew path' do
expect(dummy_class.new.repository_path).to eq opt_homebrew_path
end
it 'returns /usr/local for non-ARM systems' do
allow(homebrew).to receive(:shell_out).with('sysctl -n hw.optional.arm64').and_return(double(stdout: "0\n"))
expect(homebrew.install_path).to eq('/usr/local')
end
end

context 'when on Apple Intel Silicon' do
let(:arm64_test_output) { '' }
let(:exitstatus) { 1 }
let(:stderr) { 'sysctl: unknown oid \'hw.optional.arm64\'' }
describe '#repository_path' do
it 'returns /opt/homebrew for ARM-based systems' do
allow(homebrew).to receive(:shell_out).with('sysctl -n hw.optional.arm64').and_return(double(stdout: "1\n"))
expect(homebrew.repository_path).to eq('/opt/homebrew')
end

describe '#install_path' do
let(:dummy_class) { Class.new { include Homebrew } }
it 'returns /usr/local path' do
expect(dummy_class.new.install_path).to eq usr_local_homebrew_path
end
it 'returns /usr/local/Homebrew for non-ARM systems' do
allow(homebrew).to receive(:shell_out).with('sysctl -n hw.optional.arm64').and_return(double(stdout: "0\n"))
expect(homebrew.repository_path).to eq('/usr/local/Homebrew')
end
end

describe '#install_path' do
let(:dummy_class) { Class.new { include Homebrew } }
it 'returns /usr/local/Homebrew path' do
expect(dummy_class.new.repository_path).to eq usr_local_repository_path
end
describe '#owner' do
it 'returns the homebrew owner' do
user_wrapper = double('HomebrewUserWrapper', find_homebrew_username: 'testuser')
allow(HomebrewUserWrapper).to receive(:new).and_return(user_wrapper)
expect(homebrew.owner).to eq('testuser')
end
end
end