Class: Foodtaster::RSpec::Matchers::FileMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/foodtaster/rspec/matchers/file_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileMatcher

Returns a new instance of FileMatcher.



5
6
7
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 5

def initialize(path)
  @path = path
end

Instance Method Details

#descriptionObject



69
70
71
72
73
74
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 69

def description
  ["have file '#{@path}'",
    @content && "with content #{@content.inspect}",
    @owner && "with owner #{@owner}",
    @mode && "with mode #{@mode}"].delete_if { |a| !a }.join(" ")
end

#failure_message_for_shouldObject



58
59
60
61
62
63
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 58

def failure_message_for_should
  ["expected that #{@vm.name} should have file '#{@path}'",
    @content && !@results[:content] && "with content #{@content.inspect}, but actual content is:\n#{@actual_content.inspect}\n",
    @owner && !@results[:owner] && "with owner #{@owner}, but actual owner is #{@actual_owner}",
    @mode && !@results[:mode] && "with mode #{@mode.to_s(8)}(octal), but actual mode is #{@actual_mode}(octal)"].delete_if { |a| !a }.join(" ")
end

#failure_message_for_should_notObject



65
66
67
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 65

def failure_message_for_should_not
  "expected that #{@vm.name} should not have file '#{@path}'"
end

#matches?(vm) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 9

def matches?(vm)
  @vm = vm
  @results = {}
  return false unless vm.execute("sudo test -e #{@path}").successful?


  if @content
    @actual_content = vm.execute("sudo cat #{@path}").stdout

    if @content.is_a?(Regexp)
      @results[:content] = !!@actual_content.match(@content)
    else
      @results[:content] = (@actual_content.to_s == @content.to_s)
    end
  end

  if @owner
    @actual_owner = vm.execute("sudo stat #{@path} -c \"%U\"").stdout.chomp

    @results[:owner] = (@actual_owner.to_s == @owner.to_s)
  end

  if @mode
    @actual_mode = vm.execute("sudo stat #{@path} -c \"%a\"").stdout.chomp

    @results[:mode] = (@actual_mode == @mode.to_s(8))
  end

  @results.values.all?
end

#with_content(content) ⇒ Object



40
41
42
43
44
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 40

def with_content(content)
  @content = content

  self
end

#with_mode(mode) ⇒ Object



52
53
54
55
56
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 52

def with_mode(mode)
  @mode = mode

  self
end

#with_owner(owner) ⇒ Object



46
47
48
49
50
# File 'lib/foodtaster/rspec/matchers/file_matcher.rb', line 46

def with_owner(owner)
  @owner = owner

  self
end