pdk icon indicating copy to clipboard operation
pdk copied to clipboard

Misleading diagnostic from unit tests when an attribute expected to be an array is missing

Open jcbollinger opened this issue 4 years ago • 7 comments

Describe the bug When a spec test asserts that a class or resource is declared with an attribute having an array value, but instead that class or resource is declared without specifying any value for that attribute, the resulting diagnostic from pdk test unit is misleading. For example:

  1) example on redhat-8-x86_64 is expected to contain Class[example] with arr => []
     Failure/Error:
       is_expected.to contain_class('example')
         .with(arr: [])
     
       expected that the catalogue would contain Class[example] with arr set to [] but it is set to [nil]
     # ./spec/classes/example_spec.rb:12:in `block (4 levels) in <top (required)>'

Specifically, the diagnostic says that the attribute is set to '[nil]', which appears to correspond to an array with an element having an undef value, as opposed to no value at all or undef.

In contrast, if the test instead asserts that the attribute has a scalar value then the resulting diagnostic reports that "it is set to nil". To the extent that the attribute can be considered to have any value when none has been declared, surely that value does not depend on the one that was asserted.

To Reproduce

The overall module structure was generated with pdk new module example.

Any class will do. The diagnostic quoted above was generated with this one:

class example { }

The above diagnostic was generated with this spec test:

# frozen_string_literal: true

require 'spec_helper'

describe 'example' do
  on_supported_os.each do |os, os_facts|
    context "on #{os}" do
      let(:facts) { os_facts }

      it do
        is_expected.to compile
        is_expected.to contain_class('example')
          .with(arr: [])
      end
    end
  end
end

The diagnostic generated does not seem to depend on whether the class or resource in question actually has an attribute of the specified name. All that matters is that

  1. an attribute is expected to have an array value, but instead is not declared at all or is explicitly declared undef; and
  2. if the class or resource type actually does have an attribute of the given name, then its type must permit the value to go unspecified and / or be explicitly undef, else a different diagnostic is generated.

Expected behavior If a test asserts a value for a class or resource attribute, but no value is in fact expressly declared, then the diagnostic output should say "but it is set to nil", regardless of the data type of the asserted value.

Additional context

  • PDK installed via yum, from the repo yum.puppetlabs.com/puppet6/el/7/x86_64
  • PDK version 1.17.0
  • RedHat Enterprise Linux 7 / x86_64

jcbollinger avatar Feb 02 '21 21:02 jcbollinger

This issue has been marked stale because it is open with no recent activity. The PDK Team is actively prioritizing existing bugs and new features, if this issue is still important to you please comment and we will add this to our backlog to complete.

github-actions[bot] avatar Aug 26 '21 01:08 github-actions[bot]

Pursuant to the no-issue-activity label: I would rate this a low-priority issue, but I do account it a bug, and the fact that there has been no activity on it does not mean that it should not be fixed.

jcbollinger avatar Aug 26 '21 12:08 jcbollinger

This issue has been marked stale because it is open with no recent activity. The PDK Team is actively prioritizing existing bugs and new features, if this issue is still important to you please comment and we will add this to our backlog to complete.

github-actions[bot] avatar Feb 23 '22 01:02 github-actions[bot]

Again, I acknowledge that this is probably a low-priority issue, but it still should be fixed.

jcbollinger avatar Feb 23 '22 14:02 jcbollinger

This issue has been marked stale because it is open with no recent activity. The PDK Team is actively prioritizing existing bugs and new features, if this issue is still important to you please comment and we will add this to our backlog to complete.

github-actions[bot] avatar Aug 23 '22 02:08 github-actions[bot]

I would still like this issue to be fixed.

jcbollinger avatar Aug 23 '22 12:08 jcbollinger

@jcbollinger I'm not sure if this is the right place to raise this error, this seems to be an issue with the rspec, not PDK itself?

david22swan avatar Sep 06 '23 10:09 david22swan