Sha256: baac977112c1953f6c7707322f21b004aeb7d7f586bfa26ec93ab50bd2d88975

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

require 'fileutils'

module Tests
  class Filesystem
    PROJECT_NAME = 'test-project'
    ROOT_DIRECTORY = Pathname.new('../../../..').expand_path(__FILE__)
    TEMP_DIRECTORY = ROOT_DIRECTORY.join('tmp/acceptance')
    PROJECT_DIRECTORY = TEMP_DIRECTORY.join(PROJECT_NAME)

    def root_directory
      ROOT_DIRECTORY
    end

    def temp_directory
      TEMP_DIRECTORY
    end

    def project_directory
      PROJECT_DIRECTORY
    end

    def wrap(path)
      if path.is_a?(Pathname)
        path
      else
        find_in_project(path)
      end
    end

    def within_project(&block)
      Dir.chdir(project_directory, &block)
    end

    def clean
      if temp_directory.exist?
        temp_directory.rmtree
      end
    end

    def create
      project_directory.mkpath
    end

    def find_in_project(path)
      project_directory.join(path)
    end

    def open(path, *args, &block)
      find_in_project(path).open(*args, &block)
    end

    def read(path)
      find_in_project(path).read
    end

    def write(path, content)
      pathname = wrap(path)
      create_parents_of(pathname)
      pathname.open('w') { |f| f.write(content) }
    end

    def create_parents_of(path)
      wrap(path).dirname.mkpath
    end

    def append_to_file(path, content, options = {})
      create_parents_of(path)
      open(path, 'a') { |f| f.puts(content + "\n") }
    end

    def remove_from_file(path, pattern)
      content = read(path)
      content.sub!(/#{pattern}\n/, '')
      write(path, content)
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/shoulda-matchers-2.8.0/spec/support/tests/filesystem.rb
shoulda-matchers-2.8.0 spec/support/tests/filesystem.rb
shoulda-matchers-2.8.0.rc2 spec/support/tests/filesystem.rb
shoulda-matchers-2.8.0.rc1 spec/support/tests/filesystem.rb