Module: Assert::RakeTasks

Includes:
Rake::DSL
Defined in:
lib/assert/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



17
18
19
20
# File 'lib/assert/rake_tasks.rb', line 17

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

.included(receiver) ⇒ Object

Setup the rake tasks for testing

  • add ‘include Assert::RakeTasks’ to your Rakefile



12
13
14
15
# File 'lib/assert/rake_tasks.rb', line 12

def self.included(receiver)
  # auto-build rake tasks for the ./test files (if defined in ./test)
  self.for(:test) if File.exists?(File.expand_path('./test', Dir.pwd))
end

.irb_task(path) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/assert/rake_tasks.rb', line 65

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/assert/rake_tasks.rb', line 75

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

  # define a rake test task for all test files that have addional sub-folder tests
  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 = (File.exists?(p = (path+FILE_SUFFIX)) ? FileList[p] : []) + FileList["#{path}/**/*#{FILE_SUFFIX}"]
    end.to_task
  end

  namespace suite_name.to_s do
    Dir.glob(File.join(path, "*#{FILE_SUFFIX}")).each do |test_file|
      test_name = File.basename(test_file, FILE_SUFFIX)

      # define rake test task for all test files without sub-folder tests
      if Dir.glob(File.join(path, test_name, "*#{FILE_SUFFIX}")).empty?
        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
    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