File: empty_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (77 lines) | stat: -rw-r--r-- 2,806 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'spec_helper'

require 'puppet_spec/compiler'
require 'matchers/resource'

describe 'the empty function' do
  include PuppetSpec::Compiler
  include Matchers::Resource

  let(:logs) { [] }
  let(:warnings) { logs.select { |log| log.level == :warning }.map { |log| log.message } }

  context 'for an array it' do
    it 'returns true when empty' do
      expect(compile_to_catalog("notify { String(empty([])): }")).to have_resource('Notify[true]')
    end

    it 'returns false when not empty' do
      expect(compile_to_catalog("notify { String(empty([1])): }")).to have_resource('Notify[false]')
    end
  end

  context 'for a hash it' do
    it 'returns true when empty' do
      expect(compile_to_catalog("notify { String(empty({})): }")).to have_resource('Notify[true]')
    end

    it 'returns false when not empty' do
      expect(compile_to_catalog("notify { String(empty({1=>1})): }")).to have_resource('Notify[false]')
    end
  end

  context 'for numeric values it' do
    it 'always returns false for integer values (including 0)' do
      Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
        expect(compile_to_catalog("notify { String(empty(0)): }")).to have_resource('Notify[false]')
      end
      expect(warnings).to include(/Calling function empty\(\) with Numeric value is deprecated/)
    end

    it 'always returns false for float values (including 0.0)' do
      Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
        expect(compile_to_catalog("notify { String(empty(0.0)): }")).to have_resource('Notify[false]')
      end
      expect(warnings).to include(/Calling function empty\(\) with Numeric value is deprecated/)
    end
  end

  context 'for a string it' do
    it 'returns true when empty' do
      expect(compile_to_catalog("notify { String(empty('')): }")).to have_resource('Notify[true]')
    end

    it 'returns false when not empty' do
      expect(compile_to_catalog("notify { String(empty(' ')): }")).to have_resource('Notify[false]')
    end
  end

  context 'for a binary it' do
    it 'returns true when empty' do
      expect(compile_to_catalog("notify { String(empty(Binary(''))): }")).to have_resource('Notify[true]')
    end

    it 'returns false when not empty' do
      expect(compile_to_catalog("notify { String(empty(Binary('b25l'))): }")).to have_resource('Notify[false]')
    end
  end

  context 'for undef it' do
    it 'returns true without deprecation warning' do
      Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
        expect(compile_to_catalog("notify { String(empty(undef)): }")).to have_resource('Notify[true]')
      end
      expect(warnings).to_not include(/Calling function empty\(\) with Undef value is deprecated/)
    end
  end
end