Class: DRG::Tasks::SpecRunner

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/drg/tasks/spec_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

log

Constructor Details

#initialize(file) ⇒ SpecRunner

Returns a new instance of SpecRunner.

Parameters:

  • file (Pathname)


11
12
13
# File 'lib/drg/tasks/spec_runner.rb', line 11

def initialize(file)
  @file = Pathname.new(file)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/drg/tasks/spec_runner.rb', line 8

def file
  @file
end

Instance Method Details

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/drg/tasks/spec_runner.rb', line 15

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|
    file_path = Pathname.new(File.expand_path(ruby_file))
    spec = DRG::Spec.generate(file_path)
    next unless spec
    rspec_file = Pathname.new(spec_file(ruby_file))
    log "Generating #{rspec_file}"
    FileUtils.mkdir_p(rspec_file.parent)
    File.open(spec_file(ruby_file), 'wb') do |f|
      f << spec.join("\n")
    end
  end
end

#ruby_filesObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/drg/tasks/spec_runner.rb', line 30

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

#spec_file(ruby_file) ⇒ Object

Note:

subbing out /app/ is Rails specific



43
44
45
# File 'lib/drg/tasks/spec_runner.rb', line 43

def spec_file(ruby_file)
  File.join(spec_path, "#{ruby_file.sub('.rb', '_spec.rb')}").sub '/app/', '/'
end

#spec_pathObject



47
48
49
50
51
52
53
# File 'lib/drg/tasks/spec_runner.rb', line 47

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