Sha256: e03752d361a7fe880e1f2165657c94411ae95f1106eb1e9e5f039a58764445c0

Contents?: true

Size: 1.58 KB

Versions: 105

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8

# Add an helper method named `let_tmp_file` to spec example, this
# helper will create a temporary file with the content from the block,
# it behave like a normal `let` statement. Also to make things easier temporary
# debug it will create another `let` statement with the actual content of the block.
#
# Example:
# ```
# let_tmp_file(:hello_world_file) { "Hello world" } # return a path to a tmp file containing "Hello World"
# and will create this debug `let`, the value of the file will be the same.
# let(:hello_world_file_content) # return "Hello world"
#
module FileHelpers
  AUTO_CLEAN = true

  def self.included(base)
    base.extend(ClassMethods)
  end

  def write_to_tmp_file(content)
    file = Stud::Temporary.file
    file.write(content.to_s)
    file.close
    file.path
  end

  module ClassMethods
    def let_empty_tmp_file(name, &block)
      let(name) do
        path = nil
        f = Stud::Temporary.file
        f.close
        path = f.path
        @__let_tmp_files = [] unless @__let_tmp_files
        @__let_tmp_files << path
        path
      end
    end

    def let_tmp_file(name, &block)
      after :each do
        if @__let_tmp_files && FileHelpers::AUTO_CLEAN
          @__let_tmp_files.each do |f|
            FileUtils.rm_f(f)
          end
        end
      end

      name_content = "#{name}_content"
      let(name_content, &block)
      let(name) do
        content = __send__(name_content)
        path = write_to_tmp_file(content)
        @__let_tmp_files = [] unless @__let_tmp_files
        @__let_tmp_files << path
        path
      end
    end
  end
end

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
logstash-input-beats-6.2.6-java spec/support/file_helpers.rb
logstash-input-beats-5.1.11-java spec/support/file_helpers.rb
logstash-input-beats-6.2.5-java spec/support/file_helpers.rb
logstash-input-beats-5.1.10-java spec/support/file_helpers.rb
logstash-input-beats-6.2.4-java spec/support/file_helpers.rb
logstash-input-beats-6.2.3-java spec/support/file_helpers.rb
logstash-input-beats-6.2.2-java spec/support/file_helpers.rb
logstash-input-beats-6.2.1-java spec/support/file_helpers.rb
logstash-input-beats-6.2.0-java spec/support/file_helpers.rb
logstash-input-beats-6.1.6-java spec/support/file_helpers.rb
logstash-input-beats-6.1.5-java spec/support/file_helpers.rb
logstash-input-beats-6.1.4-java spec/support/file_helpers.rb
logstash-input-beats-6.1.3-java spec/support/file_helpers.rb
logstash-input-beats-6.1.2-java spec/support/file_helpers.rb
logstash-input-beats-6.1.1-java spec/support/file_helpers.rb
logstash-input-beats-6.1.0-java spec/support/file_helpers.rb
logstash-input-beats-6.0.14-java spec/support/file_helpers.rb
logstash-input-beats-6.0.13-java spec/support/file_helpers.rb
logstash-input-beats-6.0.12-java spec/support/file_helpers.rb
logstash-input-beats-6.0.11-java spec/support/file_helpers.rb