Skip to content

Stub exist? in FileReadHelpers

Malcolm Locke requested to merge mallocke/add-exist-to-stub-file-read into master

What does this MR do and why?

The FileReadHelpers allows us to stub fake filesystem calls in RSpec.

It's a reasonable expectation that if File.read should work for a stubbed filename then File.exist? should return true for the same filename.

# stub_file_read_spec.rb

# frozen_string_literal: true

require "spec_helper"

RSpec.describe "stub_file_read", feature_category: :shared do
  include FileReadHelpers

  before do
    stub_file_read("/foo.txt", content: "aaaaa")
  end

  specify "File.read" do
    expect(File.read("/foo.txt")).to eq("aaaaa")
  end

  specify "File.exist?" do
    expect(File.exist?("/foo.txt")).to be(true)
  end
end
$ bundle exec rspec -f d stub_file_read_spec.rb 
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Test environment set up in 0.928900729 seconds

stub file
  # order random
  File.exist? (FAILED - 1)
  File.read

Failures:

  1) stub file File.exist?
     Failure/Error: expect(File.exist?("/foo.txt")).to be(true)
     
       expected true
            got false
     # ./stub_file_read_spec.rb:17:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:440:in `block (3 levels) in <top (required)>'
     # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
     # ./spec/spec_helper.rb:431:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:427:in `block (3 levels) in <top (required)>'
     # ./lib/gitlab/application_context.rb:68:in `with_raw_context'
     # ./spec/spec_helper.rb:427:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:275:in `block (2 levels) in <top (required)>'
     # ./spec/support/system_exit_detected.rb:7:in `block (2 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:106:in `block (3 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:60:in `with_cross_joins_prevented'
     # ./spec/support/database/prevent_cross_joins.rb:106:in `block (2 levels) in <main>'

Finished in 4.41 seconds (files took 10.07 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./stub_file_read_spec.rb:16 # stub file File.exist?

Randomized with seed 61496

This MR stubs File.exist? => true in addition to File.read whenever stub_file_read or expect_file_read are called. If a caller wishes File.exist? to return false from either of these they can call the helper with exist: false.

Edited by Malcolm Locke

Merge request reports