Class: RSpec::FileMatchers::HaveFileItem

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

Direct Known Subclasses

HaveDir, HaveFile, HaveSymlink

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ HaveFileItem

Returns a new instance of HaveFileItem.



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

def initialize(*args)
  self.location = get_location(args).to_s
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



3
4
5
# File 'lib/file_spec/matchers/abstract/have_file_item.rb', line 3

def location
  @location
end

Returns the value of attribute symlink_type.



3
4
5
# File 'lib/file_spec/matchers/abstract/have_file_item.rb', line 3

def symlink_type
  @symlink_type
end

Instance Method Details

#artifactObject



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

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

#failure_messageObject



72
73
74
# File 'lib/file_spec/matchers/abstract/have_file_item.rb', line 72

def failure_message
  "Expected #{artifact} at: #{location} to exist, but it did not"
end

#get_location(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/file_spec/matchers/abstract/have_file_item.rb', line 9

def get_location(*args)
  args = args.flatten

  case args.last
  when Hash
    self.symlink_type = args.last[:type]
  end
  
  loc = if args.size > 1
    dir, name = *args
    dir = dir.kind_of?(Dir) ? dir.path : dir
    name = name.kind_of?(File) ? name.path : name        
    File.join(dir.to_s, name.to_s)
  else
    args[0].to_s
  end
end

#matches?(relative_path, &block) ⇒ Boolean

Returns:

  • (Boolean)


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
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/file_spec/matchers/abstract/have_file_item.rb', line 27

def matches? relative_path, &block
  case relative_path
  when File
    @location = relative_path if !@location
    Dir.chdir File.dirname(location)
  when Dir  
    @location = relative_path if !@location
    Dir.chdir location
  when String                        
    if File.exist?(relative_path)      
      @location = relative_path if !@location
      Dir.chdir File.dirname(location) 
    end
  end      

  match = File.send :"#{artifact}?", location
  if artifact == :symlink
    case symlink_type
    when :dir, :directory
      match = File.readlink(location).directory?
    when :file
      match = File.readlink(location).file?
    else
      raise ArgumentError, "Bad symlink type #{symlink_type}, must be either :file or :dir" if symlink_type
    end
  end
        
  if block && match   
    case artifact
    when :directory
      dir = location # Dir.new(location).path
      Dir.chdir location do
        yield dir
      end
    when :file        
      yield File.new(location)          
    end      
  end
  match
end

#negative_failure_messageObject



76
77
78
# File 'lib/file_spec/matchers/abstract/have_file_item.rb', line 76

def negative_failure_message
  "Did not expected #{artifact} at: #{location} to exist, but it did"
end