Sha256: 1e94bf798ae30e6786d48673d578f95ca3fb6fe9d9c6f54545bcd5a19127036c

Contents?: true

Size: 1.43 KB

Versions: 19

Compression:

Stored size: 1.43 KB

Contents

module RSpec
  module Support
    module Files
      private

      def write(path, *content)
        Pathname.new(path).dirname.mkpath
        open(path, ::File::CREAT | ::File::WRONLY, *content)
      end

      def rewrite(path, *content)
        open(path, ::File::TRUNC | ::File::WRONLY, *content)
      end

      def replace(path, target, replacement)
        content = ::File.readlines(path)
        content[index(content, path, target)] = "#{replacement}\n"

        rewrite(path, content)
      end

      def replace_last(path, target, replacement)
        content = ::File.readlines(path)
        content[-index(content.reverse, path, target) - 1] = "#{replacement}\n"

        rewrite(path, content)
      end

      def unshift(path, line)
        content = ::File.readlines(path)
        content.unshift("#{line}\n")

        rewrite(path, content)
      end

      def append(path, contents)
        content = ::File.readlines(path)
        content << "#{contents}\n"

        rewrite(path, content)
      end

      def open(path, mode, *content)
        ::File.open(path, mode) do |file|
          file.write(Array(content).flatten.join)
        end
      end

      def index(content, path, target)
        content.index { |l| l.include?(target) } or
          raise ArgumentError.new("Cannot find `#{target}' inside `#{path}'.")
      end
    end
  end
end

RSpec.configure do |config|
  config.include RSpec::Support::Files, type: :cli
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
dry-web-web_pipe-0.2.0 spec/support/files.rb
dry-web-roda-0.13.1 spec/support/files.rb
dry-web-roda-0.13.0 spec/support/files.rb
dry-web-web_pipe-0.1.0 spec/support/files.rb
dry-web-roda-0.12.0 spec/support/files.rb
dry-web-roda-0.11.0 spec/support/files.rb
dry-web-roda-0.10.0 spec/support/files.rb
dry-web-roda-0.9.1 spec/support/files.rb
dry-web-roda-0.9.0 spec/support/files.rb
dry-web-roda-0.8.0 spec/support/files.rb
dry-web-roda-0.7.5 spec/support/files.rb
dry-web-roda-0.7.4 spec/support/files.rb
dry-web-roda-0.7.3 spec/support/files.rb
dry-web-roda-0.7.2 spec/support/files.rb
dry-web-roda-0.7.1 spec/support/files.rb
dry-web-roda-0.7.0 spec/support/files.rb
dry-web-roda-0.6.3 spec/support/files.rb
dry-web-roda-0.6.2 spec/support/files.rb
dry-web-roda-0.6.1 spec/support/files.rb