lib/minitest/filesystem/matcher.rb in minitest-filesystem-1.1.0 vs lib/minitest/filesystem/matcher.rb in minitest-filesystem-1.1.1
- old
+ new
@@ -8,21 +8,21 @@
@expected_tree = block
@is_matching = true
end
def file(file)
- entry(file, :file) && is_a?(file, :file)
+ exists?(file, :file)
end
def link(link, target=nil)
- entry(link, :symlink) && is_a?(link, :symlink) && is_target_correct?(link, target)
+ exists?(link, :symlink) && is_target_correct?(link, target)
end
def dir(dir, &block)
matcher = self.class.new(@actual_tree.expand_path(dir), &block) if block_given?
- entry(dir, :directory) && is_a?(dir, :directory) && subtree(matcher)
+ exists?(dir, :directory) && subtree(matcher)
end
def match_found?
instance_eval(&@expected_tree)
@is_matching
@@ -32,38 +32,44 @@
@failure_msg || ""
end
private
+ # Checks existance of specified entry.
+ # Existance is defined both in terms of presence and of being of the
+ # right type (e.g. a file being a file and not a directory)
+ def exists?(entry, kind=:entry)
+ entry(entry, kind) && is_a?(entry, kind)
+ end
+
+ # Checks if an entry with given name exists.
def entry(entry, kind=:entry)
update_matching_status(
@actual_tree.include?(entry),
not_found_msg_for(entry, kind))
end
+ # Checks if a specific entry (supposed to exist) is of a given kind.
+ def is_a?(entry, kind)
+ update_matching_status(
+ @actual_tree.is_a?(entry, kind),
+ mismatch_msg_for(entry, kind))
+ end
+
+ # Checks the target of a symbolic link.
def is_target_correct?(link, target)
return true unless target
update_matching_status(
- @actual_tree.expand_path(target) == follow_link(@actual_tree.expand_path(link)),
+ @actual_tree.has_target?(link, target),
link_target_mismatch_msg_for(link, target))
end
def subtree(matcher)
update_matching_status(matcher.match_found?, matcher.message) if matcher
end
- def follow_link(link)
- Pathname.new(File.readlink(link))
- end
-
- def is_a?(entry, kind)
- update_matching_status(
- @actual_tree.is_a?(entry, kind),
- mismatch_msg_for(entry, kind))
- end
-
def update_matching_status(check, msg)
@is_matching = @is_matching && check
set_failure_msg(msg) unless @is_matching
@is_matching
@@ -76,11 +82,11 @@
def mismatch_msg_for(entry, kind)
"Expected `#{entry}` to be a #{kind}, but it was not."
end
def link_target_mismatch_msg_for(link, target)
- "Expected `#{link}` to point to `#{target}`, but it pointed to #{File.readlink(@actual_tree.expand_path(link))}"
+ "Expected `#{link}` to point to `#{target}`, but it pointed to #{@actual_tree.follow_link(link)}"
end
def set_failure_msg(msg)
@failure_msg ||= msg
end
@@ -98,12 +104,20 @@
end
def is_a?(entry, kind)
(expand_path entry).send("#{kind}?")
end
+
+ def has_target?(entry, target)
+ expand_path(target) == follow_link(entry)
+ end
def expand_path(file)
@root + Pathname.new(file)
+ end
+
+ def follow_link(link)
+ Pathname.new(File.readlink(expand_path(link)))
end
private
def expand_tree_under(dir)