Module: Dockerspec::Builder::Matchers::MatcherHelpers

Defined in:
lib/dockerspec/builder/matchers/helpers.rb

Overview

Some helpers methods for the Docker build RSpec matchers.

Instance Method Summary collapse

Instance Method Details

#maybe_json?(got, expected) ⇒ Boolean

A matcher to check JSON values like CMD and ENTRYPOINT.

The expected value can be in JSON (a Ruby array) or in String format just like in the Dockerfile.

The real (got) value will always be in array format.

Examples:

self.maybe_json?([0, 1, 3, 4], [0, 1, 3, 4]) #=> true
self.maybe_json?([0, 1, 3, 4], [0, 1, 3, 5]) #=> false
self.maybe_json?(%w(hello world), 'hello world') #=> true
self.maybe_json?(%w(hello world), 'bye') #=> false
self.maybe_json?(%w(hello world), /llo wor/) #=> true
self.maybe_json?(%w(hello world), /bye/) #=> false

Parameters:

  • got (Array)

    The received value.

  • expected (Array, String, Regexp)

    The expected value.

Returns:

  • (Boolean)

    Whether the expected value matches the real value.



81
82
83
84
# File 'lib/dockerspec/builder/matchers/helpers.rb', line 81

def maybe_json?(got, expected)
  return expected == got if expected.is_a?(Array)
  !expected.match(got.join(' ')).nil?
end

#sub_hash?(hash, sub_hash) ⇒ Boolean

Checks whether a hash is a subhash of another.

Examples:

self.sub_hash?({ a: 1, b: 2, c: 3 }, { a: 1 }) #=> true
self.sub_hash?({ a: 1, b: 2, c: 3 }, { b: 2, c: 3 }) #=> true
self.sub_hash?({ a: 1, b: 2, c: 3 }, { a: 2, b: 2 }) #=> false
self.sub_hash?({ a: 'Hello', b: 'World' }, { a: /H/, b: /Wor/ }
  #=> true
self.sub_hash?({ a: 'Hello', b: 'World' }, { a: /Bye/ } #=> false

Parameters:

  • hash (Hash)

    The hash in which to search.

  • sub_hash (Hash)

    The subhash.

Returns:

  • (Boolean)

    Whether it's a subhash.



47
48
49
50
51
52
53
54
55
56
# File 'lib/dockerspec/builder/matchers/helpers.rb', line 47

def sub_hash?(hash, sub_hash)
  sub_hash.all? do |sub_hash_key, sub_hash_value|
    next false unless hash.key?(sub_hash_key)
    if sub_hash_value.respond_to?(:match)
      !sub_hash_value.match(hash[sub_hash_key]).nil?
    else
      sub_hash_value == hash[sub_hash_key]
    end
  end
end