Module: FileSpec::Matchers

Extended by:
RSpec::Matchers::DSL
Defined in:
lib/file_spec.rb

Overview

A collection of RSpec matchers for making file assertions

Examples:

RSpec.configure do |config|
  config.include FileSpec::Matchers
end

Instance Method Summary collapse

Instance Method Details

#be_a_directoryObject

Determine if a directory exists

Examples:

expect("foo").to be_a_directory
expect("bar").not_to be_a_directory


146
147
148
# File 'lib/file_spec.rb', line 146

matcher :be_a_directory do
  match { |actual| File.directory?(actual) }
end

#be_a_fileObject

Determine if a file exists

Examples:

expect("foo.txt").to be_a_file
expect("bar.txt").not_to be_a_file


136
137
138
# File 'lib/file_spec.rb', line 136

matcher :be_a_file do
  match { |actual| File.file?(actual) }
end

#be_executableObject

Test if a file is executable

Examples:

expect("bin/rails").to be_executable
expect("Gemfile").not_to be_executable


156
157
158
# File 'lib/file_spec.rb', line 156

matcher :be_executable do
  match { |actual| File.executable?(actual) }
end

#have_contentObject

Determine if a file has matching content

Examples:

expect("foo.txt").to have_content("bar")
expect("foo.txt").to have_content(/ba/)


120
121
122
123
124
125
126
127
128
# File 'lib/file_spec.rb', line 120

matcher :have_content do |expected|
  match do |actual|
    @actual = File.read(actual)
    values_match?(expected, @actual)
  end

  diffable if expected.is_a?(String)
  description { "have content: #{description_of(expected)}" }
end

#have_entriesObject

Find the files in a directory

Examples:

expect("foo").to have_entries(%w[foo.txt bar.txt])


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/file_spec.rb', line 165

matcher :have_entries do |expected|
  match do |actual|
    root = Pathname.new(actual)

    @expected = expected.sort
    @actual = root.glob("**/*", File::FNM_DOTMATCH)
      .select(&:file?)
      .map { |path| path.relative_path_from(root).to_s }
      .sort

    values_match?(@expected, @actual)
  end

  description { "contain files: #{description_of(@expected)}" }
end