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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
issues: write

integration:
needs: "lint-unit"
# needs: "lint-unit"

Check warning on line 23 in .github/workflows/ci.yml

View workflow job for this annotation

GitHub Actions / lint-unit / yamllint

23:1 [comments-indentation] comment not indented like content
runs-on: macos-latest
strategy:
matrix:
Expand Down
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

## 5.4.9 - *2024-11-18*

Standardise files with files in sous-chefs/repo-management
Expand Down
68 changes: 35 additions & 33 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#

class HomebrewUserWrapper
require 'chef/mixin/homebrew_user'
include Chef::Mixin::HomebrewUser
require 'chef/mixin/homebrew'
include Chef::Mixin::Homebrew
# require Chef::VERSION >= Chef::Version.new('18.6.2') ? 'chef/mixin/homebrew' : 'chef/mixin/homebrew_user'
# include Chef::VERSION >= Chef::Version.new('18.6.2') ? Chef::Mixin::Homebrew : Chef::Mixin::HomebrewUser
include Chef::Mixin::Which
end

Expand Down Expand Up @@ -60,41 +62,41 @@ 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
HomebrewUserWrapper.new.find_homebrew_username
rescue
Chef::Exceptions::CannotDetermineHomebrewPath
end.tap do |owner|
Chef::Log.debug("Homebrew owner is #{owner}")
end
end

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

def sudo_user
ENV['SUDO_USER']
end

def current_user
ENV['USER']
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']
# end
end unless defined?(Homebrew)

class HomebrewWrapper
include Homebrew
end

Chef::Mixin::Homebrew.include(Homebrew)
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
2 changes: 1 addition & 1 deletion spec/recipes/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

before(:each) do
allow(Homebrew).to receive(:owner).and_return('vagrant')
allow_any_instance_of(HomebrewUserWrapper).to receive(:find_homebrew_username).and_return('vagrant')
end

it 'manages the Cask Cache directory' do
Expand Down
4 changes: 2 additions & 2 deletions spec/recipes/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end

allow(Homebrew).to receive(:exist?).and_return(true)
allow(Homebrew).to receive(:owner).and_return('vagrant')
allow_any_instance_of(HomebrewUserWrapper).to receive(:find_homebrew_username).and_return('vagrant')
stub_command('which git').and_return(true)
end

Expand Down Expand Up @@ -40,7 +40,7 @@
end

before do
allow_any_instance_of(Chef::Mixin::HomebrewUser).to receive(:find_homebrew_uid).and_return(Process.uid)
allow_any_instance_of(Chef::Mixin::Homebrew).to receive(:find_homebrew_uid).and_return(Process.uid)
end

it 'does not run homebrew installation' do
Expand Down
2 changes: 1 addition & 1 deletion spec/recipes/install_formulas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
allow(resource).to receive_shell_out('/usr/local/bin/brew analytics state', user: 'vagrant')
end
stub_command('which git').and_return('/usr/local/bin/git')
allow(Homebrew).to receive(:owner).and_return('vagrant')
allow_any_instance_of(HomebrewUserWrapper).to receive(:find_homebrew_username).and_return('vagrant')
allow(Homebrew).to receive(:exist?).and_return(true)
end

Expand Down
Loading