Class: RSpec::PathMatchers::Matchers::DirectoryMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/rspec/path_matchers/matchers/directory_matcher.rb

Overview

An RSpec matcher that checks for the existence and properties of a directory

Constant Summary collapse

OPTIONS =
[
  RSpec::PathMatchers::Options::Atime,
  RSpec::PathMatchers::Options::Birthtime,
  RSpec::PathMatchers::Options::Ctime,
  RSpec::PathMatchers::Options::Group,
  RSpec::PathMatchers::Options::Mode,
  RSpec::PathMatchers::Options::Mtime,
  RSpec::PathMatchers::Options::Owner
].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#base_path, #entry_name, #failures, #matcher_name, #options, #path

Instance Method Summary collapse

Methods inherited from Base

#does_not_match?, #failure_message, #failure_message_when_negated, #matches?

Constructor Details

#initialize(entry_name, matcher_name:, **options_hash) ⇒ DirectoryMatcher

Initializes the matcher with the directory name and options

Parameters:

  • entry_name (String)

    The name of the directory relative to the subject or empty

  • matcher_name (Symbol)

    The name of the DSL method used to create this matcher

  • options_hash (Hash)

    A hash of attribute matchers (e.g., mode:, owner:)



40
41
42
43
44
45
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 40

def initialize(entry_name, matcher_name:, **options_hash)
  super

  @exact = false
  @nested_matchers = []
end

Instance Attribute Details

#exactBoolean (readonly)

If true, the dir must contain only entries given in containing_exactly

The default is false, meaning the directory can contain additional entries.

Returns:

  • (Boolean)


30
31
32
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 30

def exact
  @exact
end

Instance Method Details

#collect_negative_validation_errors(errors) ⇒ Object



76
77
78
79
80
81
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 76

def collect_negative_validation_errors(errors)
  super
  return unless nested_matchers.any?

  errors << "The matcher `not_to #{matcher_name}(...)` cannot have expectations on its contents"
end

#collect_validation_errors(errors) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 83

def collect_validation_errors(errors)
  super

  if @nested_matchers.size > 1
    errors << 'Collectively, `#containing` and `#containing_exactly` may be called only once'
  end

  # Recursively validate nested matchers.
  nested_matchers.each { |matcher| matcher.collect_validation_errors(errors) }
end

#containing(*matchers) ⇒ Object



47
48
49
50
51
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 47

def containing(*matchers)
  @nested_matchers << matchers
  @exact = false
  self
end

#containing_exactly(*matchers) ⇒ Object



53
54
55
56
57
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 53

def containing_exactly(*matchers)
  @nested_matchers << matchers
  @exact = true
  self
end

#correct_type?Boolean

Returns:

  • (Boolean)


74
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 74

def correct_type? = File.directory?(path)

#descriptionObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 59

def description
  desc = super
  return desc if nested_matchers.empty?

  nested_descriptions = nested_matchers.map do |matcher|
    matcher.description.lines.map.with_index do |line, i|
      i.zero? ? "- #{line.chomp}" : "  #{line.chomp}"
    end.join("\n")
  end

  "#{desc} containing#{' exactly' if exact}:\n  #{nested_descriptions.join("\n  ")}"
end

#entry_typeObject



72
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 72

def entry_type = :directory

#option_definitionsObject



73
# File 'lib/rspec/path_matchers/matchers/directory_matcher.rb', line 73

def option_definitions = OPTIONS