Module: TestBelt::RakeTasks

Defined in:
lib/test_belt/rake_tasks.rb

Defined Under Namespace

Classes: TestTask

Constant Summary collapse

FILE_SUFFIX =
"_test.rb"

Class Method Summary collapse

Class Method Details

.for(test_namespace = :test) ⇒ Object



49
50
51
52
# File 'lib/test_belt/rake_tasks.rb', line 49

def for(test_namespace = :test)
  self.irb_task(test_namespace.to_s)
  self.to_tasks(test_namespace.to_s)
end

.irb_task(path) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/test_belt/rake_tasks.rb', line 54

def irb_task(path)
  irb_file = File.join(path, "irb.rb")
  if File.exist?(irb_file)
    desc "Open irb preloaded with #{irb_file}"
    task :irb do
      sh "irb -rubygems -r ./#{irb_file}"
    end
  end
end

.to_tasks(path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/test_belt/rake_tasks.rb', line 64

def to_tasks(path)
  suite_name = File.basename(path)

  # define a rake test task for all top-level test files at this path
  if !Dir.glob(File.join(path, "**/*#{FILE_SUFFIX}")).empty?
    TestTask.new(suite_name.to_sym) do |t|
      file_location = suite_name == path ? '' : " for #{File.join(path.split(File::SEPARATOR)[1..-1])}"
      t.description = "Run all tests#{file_location}"
      t.test_files = FileList["#{path}/**/*#{FILE_SUFFIX}"]
    end.to_task
  end

  namespace suite_name.to_s do
    # define rake test tasks for each top-level test file individually
    Dir.glob(File.join(path, "*#{FILE_SUFFIX}")).each do |test_file|
      test_name = File.basename(test_file, FILE_SUFFIX)
      TestTask.new(test_name.to_sym) do |t|
        t.description = "Run tests for #{[path.split(File::SEPARATOR), test_name].flatten[1..-1].join(':')}"
        t.test_files = FileList[test_file]
      end.to_task
    end

    # recursively define rake test tasks for each file
    # in each top-level directory
    Dir.glob(File.join(path, "*/")).each do |test_dir|
      self.to_tasks(test_dir)
    end
  end
end