Rake namespace not recognizing a local task

151 views Asked by At

I am learning Ruby and trying to experiment with parsing arguments and having trouble with this example I made. The Rake namespace is not recognizing a local task. The :example task works fine. The :exampleAlt task is not working.

The error is:

$ rake myapp:exampleAlt
rake aborted!
NameError: undefined local variable or method `runTests' for main:Object
C:/CTemp/rubyTest/Rakefile:19:in `block (2 levels) in <top (required)>'
Tasks: TOP => myapp:exampleAlt

And here is my code:

require 'rake'
require 'rake/testtask'

# uses '--' args format because the 'optparse' lib we use wants it that way
# runs myapp tests with args client, env, and application
namespace :myapp do |args|

  desc "Runs all tests."
  task :runTests, [:client, :env, :app] do |t, args|    
    puts "Args: #{args}"
  end

  desc "Runs example."
  task :example do    
    Rake.application.invoke_task("myapp:runTests[--client=EXAMPLE, --env=Staging, --app=myapp]")
  end

  desc "Runs example alternate."
  task :exampleAlt do    
    Rake::Task[myapp:runTests].invoke('--client=EXAMPLE', '--env=Staging', '--app=myappalt')
  end

  desc "Runs tests with default values."
  task :defaults, :client, :env, :app do |t, args|    
    args.with_defaults(:default_client => '--client=EXAMPLE', :default_env => '--env=Staging', :default_app => '--app=myapp')
    puts "Args: #{args}"
  end

end
1

There are 1 answers

1
knut On BEST ANSWER

You have a problem in line 20:

Rake::Task[myapp:runTests].invoke('--client=EXAMPLE', '--env=Staging', '--app=myappalt')

The myapp:runTests is no symbol and no string. If you make it a string, then your task will run:

      Rake::Task['myapp:runTests'].invoke('--client=EXAMPLE', '--env=Staging', '--app=myappalt')

With Rake::Task[...].invoke you don't work inside you namespace, you call a task on a global level.