Class: RSpec::FileMatchers::HaveFileItems

Inherits:
Object
  • Object
show all
Defined in:
lib/file_spec/matchers/abstract/have_file_items.rb

Direct Known Subclasses

HaveDirs, HaveFiles, HaveSymlinks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*file_items) ⇒ HaveFileItems

Returns a new instance of HaveFileItems.



7
8
9
10
11
12
13
14
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 7

def initialize(*file_items)
  items = file_items.flatten
  case items.last
  when Hash
    self.symlink_type = items.last[:type]
  end
  self.file_items = items.select{|item| !item.kind_of? Hash}.map {|f| f.any_kind_of?(File, Dir) ? f.path : f }
end

Instance Attribute Details

#file_itemsObject

Returns the value of attribute file_items.



5
6
7
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 5

def file_items
  @file_items
end

#locationObject

Returns the value of attribute location.



5
6
7
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 5

def location
  @location
end

Returns the value of attribute symlink_type.



5
6
7
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 5

def symlink_type
  @symlink_type
end

Instance Method Details

#artifactObject



59
60
61
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 59

def artifact
  raise "artifact method must be overridden by subclass"
end

#failure_messageObject



63
64
65
66
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 63

def failure_message                                                               
  return "Expected #{artifact} #{file_item_names.first} to exist, but it did not" if file_items.size == 1
  "Expected #{artifact.to_s.pluralize} #{file_item_names} to exist, but they did not"
end

#file_item_namesObject



55
56
57
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 55

def file_item_names
  @file_item_names ||= file_items.map(&:to_s)
end

#matches?(relative_path = nil, &block) ⇒ Boolean

Returns:

  • (Boolean)


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
49
50
51
52
53
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 16

def matches? relative_path=nil, &block
  case relative_path
  when File             
    full_path = File.expand_path(relative_path.path)
    @location = File.dirname(full_path) if !@location
  when Dir
    full_path = File.expand_path(relative_path.path)
    @location = full_path if !@location
  when String                        
    begin
      full_path = File.expand_path(relative_path)
      if File.directory?(full_path)
        @location = full_path if !@location
      end
    rescue
      raise ArgumentError, "The path string #{relative_path} could not be resolved to an existing directory on this filesystem."
    end
  end
  
  file_item_names.each do |loc|                  
    path = location ? File.join(location, loc) : loc
    bad = !File.send(:"#{artifact}?", path)
    
    if artifact == :symlink && !bad    
      sym_path = File.readlink(path)
      case symlink_type
      when :dir, :directory
        bad = !File.directory?(sym_path)
      when :file
        bad = !File.file?(sym_path)
      else
        raise ArgumentError, "Bad symlink type #{symlink_type}, must be either :file or :dir" if symlink_type
      end
    end
    return false if bad
  end
  true
end

#negative_failure_messageObject



68
69
70
71
# File 'lib/file_spec/matchers/abstract/have_file_items.rb', line 68

def negative_failure_message
  return "Did not expect #{artifact} #{file_item_names.first} to exist, but it did" if file_items.size == 1
  "Did not expect #{artifact.to_s.pluralize} #{file_item_names} to exist, but they did"
end