"script/rails" being used during Generator call #8894

Closed
subhashb opened this Issue Jan 11, 2013 · 5 comments

Projects

None yet

3 participants

@subhashb

A script/rails file not found error is being thrown when trying to use Rails::Generators::TestCase for custom generations.

Error being thrown is:

/Users/subhash/.rvm/rubies/ruby-1.9.3-p327/bin/ruby: No such file or directory -- script/rails (LoadError)

Call from rspec to generate a file is:

describe "with valid ActiveRecord model" do
  describe "generates a document" do
    run_generator %w(Post)
    subject { file('app/models/post.rb') }
    it { should exist }
  end
end

Ammeter gem (file) has a call to run generators like this:

def generator(given_args=self.default_arguments, config={})
  @generator ||= begin
    args, opts = Thor::Options.split(given_args)
    self.test_unit_test_case_delegate.generator(args, opts, config)
  end
end

def run_generator(given_args=self.default_arguments, config={})
  capture(:stdout) { generator(given_args, config).invoke_all }
end

On investigation, I found that rails/railties/lib/rails/generators/actions.rb has the following snippet:

def generate(what, *args)
  log :generate, what
  argument = args.map {|arg| arg.to_s }.flatten.join(" ")

  in_root { run_ruby_script("script/rails generate #{what} #{argument}", verbose: false) }
end

Shouldn't this be changed to rails generate from 3.0?

Versions:

  • rails (3.2.10)
  • railties (3.2.10)
  • ammeter (0.2.8)
@goshakkk
Contributor

The thing is, rails generate is still translated to an invocation of script/rails generate (see this.)

Do you generate a test application to test your generator on? If so, it should already have a script/rails file.

@subhashb

Got that. Thanks.
My test application is working fine.

I am doing all this in a rubygem, and I am stuck writing a test. I am using ammeter to test the generator.
A major difference is that I am calling another generator within my generator.

Any clues?

@goshakkk
Contributor

I think it is that generator is called in a wrong directory (not inside a test app.)

Just glanced at ammeter README. Do you have destination set in your spec?

@subhashb

Yeap!

Here's the entire spec:

  destination File.expand_path("../../../../../tmp", __FILE__)
  before { prepare_destination }

  describe "with valid ActiveRecord model" do
    describe "generates a document" do
      run_generator %w(Post)
      subject { file('app/models/post.rb') }
      it { should exist }
    end
  end

And here's the code:

def run_mongoid_generator
  begin
    attributes = Object.const_get(model_name).columns_hash.values.collect { |c| c.name + ":" + type_class(c.sql_type) }.join(" ")
    generate "mongoid:model", "#{model_name.camelize}Doc #{attributes} --collection=#{model_name.pluralize}"
  rescue Exception => e
    Logger.new(STDOUT).error("ActiveRecord model \"#{model_name.to_s}\" was not found")
  end
end

Basically, I am chaining into Mongoid's model generator, from my gem.

@steveklabnik
Member

This seems like an issue with your code, not with Rails itself. I'm giving it a close.

@bensymonds bensymonds referenced this issue in withassociates/slices Sep 3, 2015
Merged

Update generator specs #119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment