Sha256: 7a04eb4ef94d6fe2a99ca5d4fd16cdf112a2f1d272cbe96800871c88bc2f28e5

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

require 'fileutils'

module DRG
  module Tasks
    class SpecRunner
      include Log

      attr_reader :file

      # @param [Pathname] file
      def initialize(file)
        @file = Pathname.new(file)
      end

      def perform
        fail ArgumentError, %Q(File or directory does not exist: "#{file}") if !File.exists?(file) && !File.exists?("#{file}.rb")
        ruby_files.each do |ruby_file|
          rspec_file = Pathname.new(spec_file(ruby_file))
          spec_file_path = rspec_file.to_s[%r|/(spec/.+)|, 1]
          next if rspec_file.exist?.tap { |result| log "- #{spec_file_path} - already exists", :gray if result }
          spec = generate_spec(ruby_file)
          next unless spec
          log "+ #{spec_file_path}"
          FileUtils.mkdir_p(rspec_file.parent)
          File.open(rspec_file, 'wb') do |f|
            f << spec.join("\n")
          end
        end
      end

      def generate_spec(ruby_file)
        spec = DRG::Spec.generate Pathname.new(File.expand_path(ruby_file))
        if spec
          spec
        else
          log "- #{ruby_file} - no methods", :gray
          nil
        end
      end

      def ruby_files
        if File.directory?(file)
          Dir[File.join(file, '**', '*.rb')]
        else
          if file.extname.empty?
            ["#{file}.rb"]
          else
            [file]
          end
        end
      end

      # @note subbing out /app/ is Rails specific
      def spec_file(ruby_file)
        File.join(spec_path, "#{specify(ruby_file)}").sub '/app/', '/'
      end

      def specify(file_name)
        file_name.sub('.rb', '_spec.rb')
      end

      def spec_path
        if File.directory?(File.expand_path('spec'))
          File.expand_path('spec')
        else
          fail "Couldn't find spec directory"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
drg-0.15.6 lib/drg/tasks/spec_runner.rb
drg-0.15.5 lib/drg/tasks/spec_runner.rb
drg-0.15.4 lib/drg/tasks/spec_runner.rb
drg-0.15.3 lib/drg/tasks/spec_runner.rb