Sha256: 2912176e0e7d38a1e4f1cd1c849c28075cbb9be184900f36ab3f61b315787cc0

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

require 'fileutils'

module TestsDoc

  class File < Struct.new(:path, :output, :options)

    def write_if_changed
      ensure_folder

      if write_output?
        ::File.open(path, 'w+') do |f|
          f.write(output)
        end
      end
    end

    private

      def write_output?
        return true unless ::File.file?(path)

        response_changed?
      end

      def response_changed?
        if TestsDoc.configuration.debug
          diff = filtered_file_content.split("\n") - filtered_output.split("\n")

          TestsDoc.configuration.logger.puts("DIFF: #{diff}")

          diff.size > 0
        else
          filtered_file_content != filtered_output
        end
      end

      def filtered_file_content
        @filtered_file_content ||= ::File.read(path).gsub(changes_whitelist, "")
      end

      def filtered_output
        @filtered_output       ||= output.gsub(changes_whitelist, "")
      end

      def ensure_folder
        dirname = ::File.dirname(path)
        unless ::File.directory?(dirname)
          ::FileUtils.mkdir_p(dirname)
        end
      end

      def changes_whitelist
        whitelist_option = if TestsDoc.configuration.changes_whitelist_regexes.any?
                             TestsDoc.configuration.changes_whitelist_regexes
                           else
                             TestsDoc.configuration.changes_whitelist_regex
                           end

        Regexp.union([whitelist_option, options[:whitelist]].flatten.compact)
      end

    # /private

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tests_doc-0.0.7 lib/tests_doc/file.rb
tests_doc-0.0.6 lib/tests_doc/file.rb
tests_doc-0.0.5 lib/tests_doc/file.rb
tests_doc-0.0.4 lib/tests_doc/file.rb
tests_doc-0.0.3 lib/tests_doc/file.rb
tests_doc-0.0.2 lib/tests_doc/file.rb
tests_doc-0.0.1 lib/tests_doc/file.rb