Skip to content

Honor the globally configured platform in example groups - #53

Open
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/global-platform-config
Open

Honor the globally configured platform in example groups#53
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/global-platform-config

Conversation

@tas50

@tas50 tas50 commented Aug 22, 2026

Copy link
Copy Markdown

Fixes chefspec#953

Problem

The platform can be set globally instead of per example group, which is how a lot of existing cookbooks configure it from spec_helper.rb:

RSpec.configure do |config|
  config.platform = 'ubuntu'
  config.version = '20.04'
end

SoloRunner#with_default_options already falls back to that setting, but the example group helpers did not:

let(:chefspec_platform) { nil }
let(:chefspec_platform_version) { nil }

Both the preload and the Chef.node stub are gated on chefspec_platform:

chef_runner_instance.preload! if chefspec_platform
...
allow(::Chef).to receive(:node).and_return(chef_node) if chefspec_platform

So with only the global setting, custom resources are never registered and referencing one from a recipe block raises:

NoMethodError:
  undefined method 'mycookbook_greet' for an instance of Chef::Recipe

Built in resources keep working, which is what makes this hard to diagnose. Adding platform 'ubuntu' to the describe block fixes it, and several people in the issue report losing hours to it.

Fix

Default the platform and version lets to the global configuration, so the example group helpers agree with the runner.

Testing

Adds a spec_global_platform acceptance example: a custom resource, a global RSpec.configure platform, and no per example group platform.

  • With the fix: 2 examples, 0 failures
  • With the fix reverted: 2 examples, 2 failures (NoMethodError)
  • Unit suite: 197 examples, 0 failures
  • All acceptance examples pass, including spec_platform, which covers the no-platform warning path

This does not address the related wrinkle in the issue thread where a global version combines with a per example group platform (ubuntu/7.8). That is a separate bug.

@tas50
tas50 requested review from a team and jaymzh as code owners August 22, 2026 00:07
The platform can be set globally with RSpec.configure rather than per
example group, which is how many existing cookbooks configure it from
spec_helper.rb.

The runner already fell back to the global setting, but the example group
helpers did not: chefspec_platform defaulted to nil, so the preload and
the Chef.node stub were both skipped. Custom resources were then never
registered and referencing one from a recipe block raised NoMethodError,
while built in resources kept working, which made the failure look like a
problem with the resource.

Default the platform and version lets to the global configuration.

Fixes chefspec#953

Signed-off-by: Tim Smith <tsmith84@proton.me>
@tas50
tas50 force-pushed the fix/global-platform-config branch from 4db50e1 to 978a36d Compare August 22, 2026 00:09
@tas50

tas50 commented Aug 22, 2026

Copy link
Copy Markdown
Author

CI on this PR is red for an unrelated reason: main's bundle cannot be installed on Linux, which breaks every job before any test runs. That is fixed separately in #56, which is green across Ruby 3.1 to 3.4. This PR should go green once #56 lands and this branch is rebased.

Locally, against a working bundle, this branch passes rake unit and the acceptance examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChefSpec mysteriously fails on custom resource when configuring the platform using RSpec.configure

1 participant