stevehodgkiss / generator_spec

Test Rails generators with RSpec
MIT License
110 stars 22 forks source link

Nested generator are not picking up destination_root #46

Open 00dav00 opened 4 years ago

00dav00 commented 4 years ago

I'm trying to call another generator inside mine, the destination_root is set correctly for the tasks in my generator, but the tasks from other generators are trying to use the repo root instead of the path set using destination helper.

class InstallGenerator < ::Rails::Generators::Base
  def execute_devise_installer
    # This try to use the repo root as "destination_root"
    generate 'devise:install'
  end

  def create_my_file
    # This uses the path set with "destination" method in the spec
    create_file....
  end
end

Do you know how I can make the other generator use the testing destination_root?

Thanks!

stevehodgkiss commented 4 years ago

Hi @00dav00! I guess it depends how those generators are implemented. It's been many years since I've used this gem myself. If you can provide an example project that demonstrates the issue I might be able to help diagnose.

00dav00 commented 4 years ago

Hey @stevehodgkiss, thanks! So the problem appears on this spec which is why the spec is commented for now. The spec is using this generator.

The mount_resource_route method in the generator is using the path set by the destination helper, while the other 2 methods (execute_devise_installer and execute_dta_installer) are using the gem route.

This cause the test suite to fail because the files created by the other gems won't be deleted by prepare_destination. The build stops because the generator shows the prompt where the user should decide if the initializers should be overwritten, ignored, etc.

Do you have any clue about why the devise and DTA generators are not picking the set destination root?

mcelicalderon commented 4 years ago

I did some research on the issue and just wanted to post my findings. This issue only affects rails >= 6.0 After reading the source of Rails::Generators::TestCase I'm still unable to understand where the problem is, but I'm still not very familiar with all the generators code. In any case, I'm under the impression that it is Rails who is ignoring destination_root, I will continue to look into that, the change might be here

Oh, and it does work in rails 6 with methods like create_file. But this one comes from the Thor gem I believe. The problem might be around rails generator methods like generate which calls another rails generator like @00dav00 pointed out.

mcelicalderon commented 4 years ago

So it's definitively not a problem with this gem, and it makes sense as the public interface for TestCase remains the same. I didn't quite get the exact code that causes this to happen, but it is discussed here and implemented here I believe. If it is useful for anyone else as a reference, this PR fixes it for out project when running Rails >= 6.0. It basically consists on having a bin/rails file in the root of the destination_root directory. And this file references the dummy rails app we have in the project for engine tests. During testing I noticed that not even referencing the dummy project will also fix it, but will present an error that won't stop the test from passing.