# module FileTest

### Constants

SEPARATOR_PATTERN

### Public Instance Methods

absolute?(path) click to toggle source

Predicate method for testing whether a path is absolute. It returns `true` if the pathname begins with a slash.

    # File lib/facets/filetest/relative.rb, line 9
    def absolute?(path)
      !relative?(path)
    end

chop_basename(path) click to toggle source

List File.split, but preserves the file separators.

    FileTest.chop_basename('/usr/lib') #=> ['/usr/', 'lib']
    FileTest.chop_basename('/') #=> nil

Returns Array of `[pre-basename, basename]` or `nil`.

This method is here simply to support the relative? and absolute? methods.

    # File lib/facets/filetest/relative.rb, line 29
    def chop_basename(path)
      base = File.basename(path)
      if /\A#{SEPARATOR_PATTERN}?\z/ =~ base
        return nil
      else
        return path[0, path.rindex(base)], base
      end
    end

contains?(child, parent=Dir.pwd) click to toggle source

Does the `parent` contain the `child`?

    # File lib/facets/filetest/contains.rb, line 6
    def contains?(child, parent=Dir.pwd)
      parent = File.expand_path(parent)
      child = File.expand_path(child)
      child.sub(parent,'') != child
    end

relative?(path) click to toggle source

The opposite of absolute?

    # File lib/facets/filetest/relative.rb, line 14
    def relative?(path)
      while r = chop_basename(path.to_s)
        path, basename = r
      end
      path == ''
    end

root?(dir=nil) click to toggle source

Is the specified directory the root directory?

CREDIT: Jeffrey Schwab

    # File lib/facets/filetest/root.rb, line 9
    def root?(dir=nil)
      pth = File.expand_path(dir||Dir.pwd)
      return true if pth == '/'
      return true if pth =~ /^(\w:)?\/$/
      false
    end

safe?(path) click to toggle source

Is a path considered reasonably “safe”?

Do not mistake this for a perfect solution!

    # File lib/facets/filetest/safe.rb, line 10
    def safe?(path)
      case path
      when /\A(#{SEPARATOR_PATTERN}|\~)(#{SEPARATOR_PATTERN}|\*)+/
        false
      else
        true
      end
    end