Skip to content

Commit

Permalink
(RK-378) Add environment_name to Puppetfile DSL
Browse files Browse the repository at this point in the history
Prior to this commit, there was not an easy way to access the
environment name from the Puppetfile. This commit adds
'environment_name' to the Puppetfile DSL to allow users to access the
environment name if it is provided. The common way to access that is
'@librarian.environment_name' which will be a string containing the name
of the environment or `nil` if there was no environment provided.
  • Loading branch information
Jarret Lavallee committed Nov 5, 2021
1 parent 5caed0c commit 7c9b582
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Unreleased
----------

- (CODEMGMT-1294) Resync repos with unresolvable refs [#1239](https://github.com/puppetlabs/r10k/pull/1239)
- (RK-378) Restore access to the environment name from the Puppetfile [#1241](https://github.com/puppetlabs/r10k/pull/1241)

3.13.0
------
Expand Down
6 changes: 4 additions & 2 deletions lib/r10k/module_loader/puppetfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Puppetfile

attr_accessor :default_branch_override, :environment
attr_reader :modules, :moduledir, :puppetfile_path,
:managed_directories, :desired_contents, :purge_exclusions
:managed_directories, :desired_contents, :purge_exclusions,
:environment_name

# @param basedir [String] The path that contains the moduledir &
# Puppetfile by default. May be an environment, project, or
Expand All @@ -40,6 +41,7 @@ def initialize(basedir:,
@puppetfile_path = resolve_path(@basedir, puppetfile)
@overrides = overrides
@environment = environment
@environment_name = @environment&.name
@default_branch_override = @overrides.dig(:environments, :default_branch_override)
@allow_puppetfile_forge = @overrides.dig(:forge, :allow_puppetfile_override)

Expand Down Expand Up @@ -168,7 +170,7 @@ def add_module(name, info)
# @deprecated
# @return [String] The base directory that contains the Puppetfile
def basedir
logger.warn _('"basedir" has been deprecated and will be removed in a future release')
logger.warn _('"basedir" is deprecated. Please use "environment_name" instead. "basedir" will be removed in a future version.')
@basedir
end

Expand Down
5 changes: 4 additions & 1 deletion spec/unit/module_loader/puppetfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
it 'should disable and not add modules that conflict with the environment' do
env = instance_double('R10K::Environment::Base')
mod = instance_double('R10K::Module::Base', name: 'conflict', origin: :puppetfile, 'origin=': nil)
allow(env).to receive(:name).and_return('conflict')
loader = R10K::ModuleLoader::Puppetfile.new(basedir: basedir, environment: env)
allow(env).to receive(:'module_conflicts?').with(mod).and_return(true)
allow(mod).to receive(:spec_deletable=)
Expand All @@ -187,7 +188,9 @@
context 'when belonging to an environment' do
let(:env_contents) { ['env1', 'env2' ] }
let(:env) { double(:environment, desired_contents: env_contents) }

before {
allow(env).to receive(:name).and_return('env1')
}
subject { R10K::ModuleLoader::Puppetfile.new(basedir: '/test/basedir', environment: env) }

it "includes environment's desired_contents" do
Expand Down

0 comments on commit 7c9b582

Please sign in to comment.