Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_read_helpers.rb « helpers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c30a9e6466f924bb0e21bccf32e636cdcc7d3a28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module FileReadHelpers
  def stub_file_read(file, content: nil, error: nil)
    allow_original_file_read

    expectation = allow(File).to receive(:read).with(file)

    if error
      expectation.and_raise(error)
    elsif content
      expectation.and_return(content)
    else
      expectation
    end
  end

  def expect_file_read(file, content: nil, error: nil)
    allow_original_file_read

    expectation = expect(File).to receive(:read).with(file)

    if error
      expectation.and_raise(error)
    elsif content
      expectation.and_return(content)
    else
      expectation
    end
  end

  def expect_file_not_to_read(file)
    allow_original_file_read

    expect(File).not_to receive(:read).with(file)
  end

  private

  def allow_original_file_read
    # Don't set this mock twice, otherwise subsequent calls will clobber
    # previous mocks
    return if @allow_original_file_read

    @allow_original_file_read = true
    allow(File).to receive(:read).and_call_original
  end
end