Skip to content
Open
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
.metadata
.yardoc
.yardwarns
.rspec
.rspec_status
*.iml
/.bundle/
/.idea/
Expand Down
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ RSpec/DescribeClass:
- 'spec/acceptance/dsc/cim_instances.rb'
- 'spec/acceptance/dsc/class.rb'
- 'spec/acceptance/dsc/complex.rb'
- 'spec/unit/puppet/feature/pwshlib_spec.rb'
- 'spec/unit/pwsh_spec.rb'

# Offense count: 38
Expand Down
4 changes: 2 additions & 2 deletions lib/pwsh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ def exit
# @return [String] full path to the bootstrap template
def self.template_path
# A PowerShell -File compatible path to bootstrap the instance
path = File.expand_path('templates', __dir__)
path = File.join(path, 'init.ps1').tr('/', '\\')
path = File.join(File.expand_path('templates', __dir__), 'init.ps1')
path = path.tr('/', '\\') if Pwsh::Util.on_windows?
"\"#{path}\""
end

Expand Down
2 changes: 2 additions & 0 deletions lib/pwsh/windows_powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def self.compatible_version?
end
end

# :nocov:
if Pwsh::Util.on_windows?
require 'win32/registry'
module Pwsh
Expand Down Expand Up @@ -106,3 +107,4 @@ def self.powershell_three_version
end
end
end
# :nocov:
20 changes: 20 additions & 0 deletions spec/unit/puppet/feature/pwshlib_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'spec_helper'
require 'puppet'

RSpec.describe 'pwshlib puppet feature' do
before do
lib_path = File.expand_path('../../../../lib', __dir__)
$LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
load File.expand_path('../../../../lib/puppet/feature/pwshlib.rb', __dir__)
end

it 'registers the pwshlib feature with Puppet' do
expect(Puppet.features).to respond_to(:pwshlib?)
end

it 'reports the feature as available when ruby-pwsh is loaded' do
expect(Puppet.features.pwshlib?).to be(true)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,39 @@
expect(provider).to receive(:invoke_get_method).with(context, { name: 'foo', dsc_some_parameter: 'baz' }).and_return({ name: 'foo', property: 'bar' })
expect(provider.get(context, [{ name: 'foo' }])).to eq([{ name: 'foo', property: 'bar' }])
end

context 'when @cached_canonicalize_results contains a matching resource' do
before do
allow(context).to receive(:debug)
allow(provider).to receive(:namevar_attributes).and_return([:name])
provider.instance_variable_set(:@cached_canonicalize_results, [{ name: 'foo', property: 'bar' }])
end

it 'returns results from the canonicalize cache without calling invoke_get_method' do
expect(provider).not_to receive(:invoke_get_method)
result = provider.get(context, [{ name: 'foo' }])
expect(result).to eq([{ name: 'foo', property: 'bar' }])
end

it 'also populates @cached_query_results' do
provider.get(context, [{ name: 'foo' }])
expect(provider.instance_variable_get(:@cached_query_results)).not_to be_empty
end
end

context 'when @cached_canonicalized_resource is empty' do
before do
allow(context).to receive(:debug)
allow(provider).to receive(:namevar_attributes).and_return([:name])
allow(provider).to receive(:invoke_get_method).and_return({ name: 'foo', property: 'bar' })
provider.instance_variable_set(:@cached_canonicalized_resource, [])
end

it 'uses empty mandatory_properties when no canonicalized resource cached' do
expect(provider).to receive(:invoke_get_method).with(context, { name: 'foo' })
provider.get(context, [{ name: 'foo' }])
end
end
end

describe '.set' do
Expand Down Expand Up @@ -364,6 +397,17 @@
expect { result }.not_to raise_error
end
end

context 'when neither create, update, nor delete conditions match' do
let(:actual_state) { { name: 'foo', dsc_ensure: 'Absent' } }
let(:should_state) { { name: 'foo', dsc_ensure: 'Latest' } }

it 'falls back to context.updating and provider.update' do
expect(context).to receive(:updating).with(name_hash).and_yield
expect(provider).to receive(:update).with(context, name_hash, should_state)
provider.set(context, change_set)
end
end
end

context 'when `is` is nil' do
Expand Down Expand Up @@ -659,6 +703,55 @@
end
end

describe '.log_change_detail' do
let(:name_hash) { { name: 'foo' } }
let(:is_hash) { { name: 'foo', dsc_setting: 'old_value' } }
let(:should_hash) { { name: 'foo', dsc_setting: 'new_value' } }
let(:type_def) do
{
name: 'dsc_foo',
dscmeta_module_name: 'FooModule',
attributes: { dsc_setting: { mof_type: 'String' } }
}
end

before do
allow(context).to receive(:type).and_return(type)
allow(type).to receive(:definition).and_return(type_def)
allow(provider).to receive(:namevar_attributes).and_return([:name])
allow(provider).to receive(:enum_attributes).and_return([])
allow(provider).to receive(:recursively_downcase) { |v| v }
allow(provider).to receive(:same?).and_return(false)
end

it 'emits a notice for each changed dsc_ property' do
expect(context).to receive(:notice).with(/dsc_setting/)
provider.log_change_detail(context, name_hash, is_hash, should_hash)
end

it 'includes type name and module name in the notice' do
expect(context).to receive(:notice).with(/dsc_foo.*FooModule/)
provider.log_change_detail(context, name_hash, is_hash, should_hash)
end

it 'returns immediately when is_value is nil' do
expect(context).not_to receive(:notice)
provider.log_change_detail(context, name_hash, nil, should_hash)
end

it 'includes the declaring class when tags contain a namespaced class' do
should_with_tags = should_hash.merge(tags: ['dsc_foo', 'my_module::my_class'])
expect(context).to receive(:notice).with(/my_module::my_class/)
provider.log_change_detail(context, name_hash, is_hash, should_with_tags)
end

it 'handles type definition errors gracefully' do
allow(type).to receive(:definition).and_raise(StandardError)
allow(context).to receive(:notice)
expect { provider.log_change_detail(context, name_hash, is_hash, should_hash) }.not_to raise_error
end
end

describe '.invoke_get_method' do
subject(:result) { provider.invoke_get_method(context, name_hash) }

Expand Down Expand Up @@ -944,6 +1037,18 @@
end
end

context 'when JSON.parse raises a StandardError on the invocation output' do
before do
allow(ps_manager).to receive(:execute).with(script, nil).and_return({ stdout: 'invalid json!' })
allow(JSON).to receive(:parse).and_raise(JSON::ParserError, 'unexpected token')
end

it 'writes the error to context and returns nil' do
expect(context).to receive(:err).with(instance_of(JSON::ParserError))
expect(result).to be_nil
end
end

context 'when handling DateTimes' do
before do
allow(ps_manager).to receive(:execute).with(script, nil).and_return({ stdout: 'DSC Data' })
Expand Down Expand Up @@ -2114,6 +2219,25 @@
it 'includes the ModuleName in the output hash as a hashtable of name and version' do
expect(result).to match(/#{expected_module_name}/)
end

context "when dscmeta_resource_implementation is 'Class'" do
let(:test_resource) do
{
parameters: test_parameter,
dscmeta_resource_friendly_name: 'Foo',
dscmeta_module_name: 'PuppetDsc',
dscmeta_module_version: '1.2.3.4',
dscmeta_resource_implementation: 'Class',
dsc_invoke_method: 'Get',
vendored_modules_path: 'C:/path/to/resources'
}
end

it 'uses the bare module name for Class resources instead of a psd1 path' do
expect(result).to match(/ModuleName = 'PuppetDsc'/)
expect(result).not_to match(/\.psd1/)
end
end
end
end

Expand Down Expand Up @@ -2453,6 +2577,29 @@
end
end

describe '.unwrap_string' do
let(:sensitive_string) { Puppet::Pops::Types::PSensitiveType::Sensitive.new('secret') }

it 'unwraps a Sensitive value directly' do
expect(provider.unwrap_string(sensitive_string)).to eq('secret')
end

it 'recursively unwraps Sensitive values inside a Hash' do
result = provider.unwrap_string({ key: sensitive_string, other: 'plain' })
expect(result[:key]).to eq('secret')
expect(result[:other]).to eq('plain')
end

it 'recursively unwraps Sensitive values inside an Array' do
expect(provider.unwrap_string(['plain', sensitive_string])).to eq(%w[plain secret])
end

it 'returns non-sensitive values unchanged' do
expect(provider.unwrap_string('just a string')).to eq('just a string')
expect(provider.unwrap_string(42)).to eq(42)
end
end

describe '.ps_manager' do
describe '.ps_manager on non-Windows' do
before do
Expand Down
Loading
Loading