Module: RSpec::PathMatchers

Defined in:
lib/rspec/path_matchers.rb,
lib/rspec/path_matchers/options.rb,
lib/rspec/path_matchers/version.rb,
lib/rspec/path_matchers/refinements.rb,
lib/rspec/path_matchers/options/base.rb,
lib/rspec/path_matchers/options/mode.rb,
lib/rspec/path_matchers/options/size.rb,
lib/rspec/path_matchers/matchers/base.rb,
lib/rspec/path_matchers/options/atime.rb,
lib/rspec/path_matchers/options/ctime.rb,
lib/rspec/path_matchers/options/group.rb,
lib/rspec/path_matchers/options/mtime.rb,
lib/rspec/path_matchers/options/owner.rb,
lib/rspec/path_matchers/options/content.rb,
lib/rspec/path_matchers/options/etc_base.rb,
lib/rspec/path_matchers/options/birthtime.rb,
lib/rspec/path_matchers/options/json_content.rb,
lib/rspec/path_matchers/options/yaml_content.rb,
lib/rspec/path_matchers/matchers/file_matcher.rb,
lib/rspec/path_matchers/options/symlink_atime.rb,
lib/rspec/path_matchers/options/symlink_ctime.rb,
lib/rspec/path_matchers/options/symlink_group.rb,
lib/rspec/path_matchers/options/symlink_mtime.rb,
lib/rspec/path_matchers/options/symlink_owner.rb,
lib/rspec/path_matchers/options/file_stat_base.rb,
lib/rspec/path_matchers/options/symlink_target.rb,
lib/rspec/path_matchers/matchers/symlink_matcher.rb,
lib/rspec/path_matchers/matchers/no_entry_matcher.rb,
lib/rspec/path_matchers/options/symlink_birthtime.rb,
lib/rspec/path_matchers/matchers/directory_matcher.rb,
lib/rspec/path_matchers/options/parsed_content_base.rb,
lib/rspec/path_matchers/options/symlink_target_type.rb,
lib/rspec/path_matchers/options/symlink_target_exist.rb

Overview

A collection of matchers for testing directory entries

This module provides the main DSL methods for use in RSpec tests.

Examples:

require 'rspec/path_matchers'

RSpec.configure do |config|
  config.include RSpec::PathMatchers
end

RSpec.describe '/var/log' do
  it { is_expected.to be_dir.containing(file('syslog')) }
end

Defined Under Namespace

Modules: Matchers, Options, Refinements

Constant Summary collapse

Failure =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A simple, immutable structure to hold failure data internally.

Data.define(:relative_path, :message)
VERSION =
'0.2.2'

Top-Level Matchers collapse

Top-Level Child Matchers collapse

Nested Entry Declarations collapse

Class Method Summary collapse

Class Method Details

.matcher?(object) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if object is a matcher

Returns:

  • (Boolean)


24
25
26
# File 'lib/rspec/path_matchers.rb', line 24

def self.matcher?(object)
  object.respond_to?(:matches?) && object.respond_to?(:description)
end

Instance Method Details

#be_dir(**options_hash) ⇒ RSpec::PathMatchers::Matchers::DirectoryMatcher

Creates a matcher that tests if the subject path is a directory

Examples:

Basic existence check

expect('/var/log').to be_dir

Checking attributes

expect('/tmp').to be_dir(mode: '1777')

Parameters:

  • options_hash (Hash)

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

Returns:



46
47
48
# File 'lib/rspec/path_matchers.rb', line 46

def be_dir(**options_hash)
  RSpec::PathMatchers::Matchers::DirectoryMatcher.new('', matcher_name: __method__, **options_hash)
end

#be_file(**options_hash) ⇒ RSpec::PathMatchers::Matchers::FileMatcher

Creates a matcher that tests if the subject path is a file

Examples:

expect('/etc/hosts').to be_file(content: include('localhost'))

Parameters:

  • options_hash (Hash)

    A hash of attribute matchers (e.g., content:, size:).

Returns:



58
59
60
# File 'lib/rspec/path_matchers.rb', line 58

def be_file(**options_hash)
  RSpec::PathMatchers::Matchers::FileMatcher.new('', matcher_name: __method__, **options_hash)
end

Creates a matcher that tests if the subject path is a symbolic link

Examples:

expect('/usr/bin/ruby').to be_symlink(target: a_string_ending_with('ruby3.2'))

Parameters:

  • options_hash (Hash)

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

Returns:



70
71
72
# File 'lib/rspec/path_matchers.rb', line 70

def be_symlink(**options_hash)
  RSpec::PathMatchers::Matchers::SymlinkMatcher.new('', matcher_name: __method__, **options_hash)
end

#dir(name, **options_hash) ⇒ RSpec::PathMatchers::Matchers::DirectoryMatcher

Declares an expectation for a directory within a directory

Intended for use as an argument to #containing or #containing_exactly.

Parameters:

  • name (String)

    The name of the expected directory.

  • options_hash (Hash)

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

Returns:



141
142
143
# File 'lib/rspec/path_matchers.rb', line 141

def dir(name, **options_hash)
  RSpec::PathMatchers::Matchers::DirectoryMatcher.new(name, matcher_name: __method__, **options_hash)
end

#file(name, **options_hash) ⇒ RSpec::PathMatchers::Matchers::FileMatcher

Declares an expectation for a file within a directory

Intended for use as an argument to #containing or #containing_exactly.

Parameters:

  • name (String)

    The name of the expected file.

  • options_hash (Hash)

    A hash of attribute matchers (e.g., content:, size:).

Returns:



129
130
131
# File 'lib/rspec/path_matchers.rb', line 129

def file(name, **options_hash)
  RSpec::PathMatchers::Matchers::FileMatcher.new(name, matcher_name: __method__, **options_hash)
end

#have_dir(entry_name, **options_hash) ⇒ RSpec::PathMatchers::Matchers::DirectoryMatcher

A matcher to test if the subject path has a child directory with the given name

Examples:

expect('/tmp').to have_dir('new_project', owner: 'root')

Parameters:

  • entry_name (String)

    The name of the expected child directory.

  • options_hash (Hash)

    A hash of attribute matchers.

Returns:



87
88
89
# File 'lib/rspec/path_matchers.rb', line 87

def have_dir(entry_name, **options_hash) # rubocop:disable Naming/PredicatePrefix
  RSpec::PathMatchers::Matchers::DirectoryMatcher.new(entry_name, matcher_name: __method__, **options_hash)
end

#have_file(entry_name, **options_hash) ⇒ RSpec::PathMatchers::Matchers::FileMatcher

A matcher to test if the subject path has a child file with the given name

Examples:

expect('/etc').to have_file('hosts', content: /localhost/)

Parameters:

  • entry_name (String)

    The name of the expected child file.

  • options_hash (Hash)

    A hash of attribute matchers.

Returns:



100
101
102
# File 'lib/rspec/path_matchers.rb', line 100

def have_file(entry_name, **options_hash) # rubocop:disable Naming/PredicatePrefix
  RSpec::PathMatchers::Matchers::FileMatcher.new(entry_name, matcher_name: __method__, **options_hash)
end

A matcher to test if the subject path has a child symlink with the given name

Examples:

expect('/usr/local/bin').to have_symlink('ruby')

Parameters:

  • entry_name (String)

    The name of the expected child symlink.

  • options_hash (Hash)

    A hash of attribute matchers.

Returns:



113
114
115
# File 'lib/rspec/path_matchers.rb', line 113

def have_symlink(entry_name, **options_hash) # rubocop:disable Naming/PredicatePrefix
  RSpec::PathMatchers::Matchers::SymlinkMatcher.new(entry_name, matcher_name: __method__, **options_hash)
end

#no_dir_named(name) ⇒ RSpec::PathMatchers::Matchers::NoEntryMatcher

Declares an expectation that a directory with the given name does NOT exist

Intended for use as an argument to #containing or #containing_exactly.

Parameters:

  • name (String)

    The name of the directory that should be absent.

Returns:



164
165
166
# File 'lib/rspec/path_matchers.rb', line 164

def no_dir_named(name)
  RSpec::PathMatchers::Matchers::NoEntryMatcher.new(name, matcher_name: __method__, entry_type: :directory)
end

#no_file_named(name) ⇒ RSpec::PathMatchers::Matchers::NoEntryMatcher

Declares an expectation that a file with the given name does NOT exist

Intended for use as an argument to #containing or #containing_exactly.

Parameters:

  • name (String)

    The name of the file that should be absent.

Returns:



175
176
177
# File 'lib/rspec/path_matchers.rb', line 175

def no_file_named(name)
  RSpec::PathMatchers::Matchers::NoEntryMatcher.new(name, matcher_name: __method__, entry_type: :file)
end

Declares an expectation that a symlink with the given name does NOT exist

Intended for use as an argument to #containing or #containing_exactly.

Parameters:

  • name (String)

    The name of the symlink that should be absent.

Returns:



186
187
188
# File 'lib/rspec/path_matchers.rb', line 186

def no_symlink_named(name)
  RSpec::PathMatchers::Matchers::NoEntryMatcher.new(name, matcher_name: __method__, entry_type: :symlink)
end

Declares an expectation for a symbolic link within a directory

Intended for use as an argument to #containing or #containing_exactly.

Parameters:

  • name (String)

    The name of the expected symlink.

  • options_hash (Hash)

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

Returns:



153
154
155
# File 'lib/rspec/path_matchers.rb', line 153

def symlink(name, **options_hash)
  RSpec::PathMatchers::Matchers::SymlinkMatcher.new(name, matcher_name: __method__, **options_hash)
end