Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generator spec generator #2217

Merged
merged 8 commits into from Jan 22, 2020
Merged

Conversation

joel
Copy link
Contributor

@joel joel commented Dec 3, 2019

Context

Rails offer the possibility to create our own set of generators [1]. While executing the following command

bin/rails generate generator my_awesome_generator

the test framework is called

See hook_for :test_framework

RSpec must give the expected generator

Problem

The given command get this error

bin/rails generate generator foo
      create  lib/generators/foo
      create  lib/generators/foo/foo_generator.rb
      create  lib/generators/foo/USAGE
      create  lib/generators/foo/templates
       error  rspec [not found]

History

@yhirano55 provided an implementation #1847 Jul 19, 2017, which I don't know why it wasn't integrated in the first place, @penelopezone?

@tragiclifestories opened an issue about it #2084 on Feb 26, 2019.

@ConSou implemented a version #2085 the same day and it was merged a couple of days after by @JonRowe on Mar 1, 2019.

However, as @kdiogenes commented a couple of days ago here #2085 (comment) this doesn't work.

Environment

  • Rails 6.0.1
  • Rspec Rails (3.9.0)

This PR fix the issue.

bin/rails generate generator --help
Running via Spring preloader in process 26116
Usage:
  rails generate generator NAME [options]

Options:
      [--skip-namespace], [--no-skip-namespace]  # Skip namespace (affects only isolated applications)
      [--namespace], [--no-namespace]            # Namespace generator under lib/generators/name
                                                 # Default: true
  -t, [--test-framework=NAME]                    # Test framework to be invoked
                                                 # Default: rspec

Rspec options:
  [--generator-specs], [--no-generator-specs]  # Generate generator specs

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

Description:
    Stubs out a new generator at lib/generators. Pass the generator name as an argument,
    either CamelCased or snake_cased.

Example:
    `rails generate generator Awesome`

    creates a standard awesome generator:
        lib/generators/awesome/
        lib/generators/awesome/awesome_generator.rb
        lib/generators/awesome/USAGE
        lib/generators/awesome/templates/
        test/lib/generators/awesome_generator_test.rb
Rspec options:
  [--generator-specs], [--no-generator-specs]  # Generate generator specs
bin/rails generate generator awesome
Running via Spring preloader in process 25980
       exist  lib/generators/awesome
      create  lib/generators/awesome/awesome_generator.rb
      create  lib/generators/awesome/USAGE
       exist  lib/generators/awesome/templates
      invoke  rspec
      create    spec/generator/awesomes_generator_spec.rb

Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a cucumber scenario demonstrating their use, thats one reason why this was merged broken in the first place :)

class GeneratorsGenerator < Base
class_option :generator_specs, :type => :boolean, :default => false, :desc => "Generate generator specs"
class GeneratorGenerator < Base
class_option :generator_specs, :type => :boolean, :default => true, :desc => "Generate generator specs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to flip the default state here, generally speaking generators are a rarely used feature so I think its fine to have to request them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not strongly against it, however, this may be quite confusing if we rely on the rails documentation [1]

The following command will silently ignore the spec

bin/rails generate generator my_awesome_generator

We need to explicitly ask for

bin/rails generate generator my_awesome_generator --generator-specs

Which it sounds a bit unnatural to me, but your call here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I freely admit I might be misunderstanding the hierarchy here. I agree that:

bin/rails generate generator my_generator

should work, but in no other circumstance should you end up with them without adding --generator-specs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bin/rails generate generator my_generator works.

It just a matter of default behavior here.

No Spec generated:

bin/rails generate generator foo
       exist  lib/generators/foo
      create  lib/generators/foo/foo_generator.rb
      create  lib/generators/foo/USAGE
       exist  lib/generators/foo/templates
      invoke  rspec

Spec generated:

bin/rails generate generator bar --generator-specs
       exist  lib/generators/bar
      create  lib/generators/bar/bar_generator.rb
      create  lib/generators/bar/USAGE
       exist  lib/generators/bar/templates
      invoke  rspec
      create    spec/generator/bars_generator_spec.rb

Your call.

@joel
Copy link
Contributor Author

joel commented Dec 3, 2019

Can you add a cucumber scenario demonstrating their use, thats one reason why this was merged broken in the first place :)

@JonRowe I have no clue how to write that, are there scenarios already for any generators I can use as inspirations?

@JonRowe
Copy link
Member

JonRowe commented Dec 3, 2019

@JonRowe I have no clue how to write that, are there scenarios already for any generators I can use as inspirations?

There's only a markdown file, but the cucumber scenarios pretty much have the affect of running a command line er command, and then introspecting the output, any of them should be enough to get going, you essentially want a when step that runs the command and some then steps that check the generated output.

The benefit is they are isolated from rspec so can use the rails commands and thus check the loading etc

@JonRowe
Copy link
Member

JonRowe commented Dec 5, 2019

Can you rebase this?

@joel joel force-pushed the fix_generator_spec_generator branch from 03f4516 to 12e94d8 Compare December 9, 2019 07:36
@joel
Copy link
Contributor Author

joel commented Dec 9, 2019

Can you rebase this?

Is that okay?

@pirj
Copy link
Member

pirj commented Dec 9, 2019

Absolutely.

@JonRowe
Copy link
Member

JonRowe commented Dec 9, 2019

@joel does this now work for you? How are you going with a cucumber scenario?

@pirj
Copy link
Member

pirj commented Jan 8, 2020

@joel Are you up to wrap this up? Does this work as expected now?

@joel
Copy link
Contributor Author

joel commented Jan 11, 2020

@pirj it works as expected since the beginning haha 😆 I just didn't find the time to add the cucumber scenario asked by @JonRowe, I am not a Cucumber guy and there are not cucumbers scenarios I can use as example for the generators.

I can rebase if needed

@joel joel force-pushed the fix_generator_spec_generator branch from 12e94d8 to 32a6b7e Compare January 12, 2020 17:09
@joel joel force-pushed the fix_generator_spec_generator branch from 32a6b7e to 2ae4d21 Compare January 12, 2020 20:32
@benoittgt
Copy link
Member

I can help if needed for Cucumber @joel. 🙇🏻‍♂️

@joel
Copy link
Contributor Author

joel commented Jan 14, 2020

@benoittgt it would be awesome! Can you open a PR on top of mine?

@benoittgt
Copy link
Member

@JonRowe Where do you see the feature test?

I wrote something like:

--- /dev/null
+++ b/features/generator_specs/generator_specs.feature
@@ -0,0 +1,21 @@
+Feature: Generator spec
+
+    RSpec spec are normally generated alongside other application components.
+    For instance, `rails generate model` will also generate an RSpec spec file
+    for the model but you can also use your own generator. See
+    [customizing your workflow](https://guides.rubyonrails.org/generators.html#customizing-your-workflow)
+
+    Scenario: Use custom generator
+        When I run `rails generate generator my_generator`
+        Then the features should pass
+        And the output should contain:
+        """
+          create  lib/generators/my_generator
+          create  lib/generators/my_generator/my_generator_generator.rb
+          create  lib/generators/my_generator/USAGE
+          create  lib/generators/my_generator/templates
+          invoke  rspec
+          create    spec/generator/my_generator_generator_spec.rb
+        """

But this feature spec needs to verify the output from an existing Rails app. We have the example_app in rspec-rails, do you think I should move this test in an example_app test?

@JonRowe
Copy link
Member

JonRowe commented Jan 17, 2020

There is an app generated in the cukes, is that not enough?

@benoittgt
Copy link
Member

benoittgt commented Jan 20, 2020

I added a proposal in d7ff45d

I think we should add specs by default. See this question #2217 (comment)

When I was writing test I didn't understand why we had invoke rspec but nothing happen.

@joel, feel free to edit my commit. This is a draft but it should work. Especially the feature description.

If you have better idea for the before hook feel free to share your tips.

The before hook help us to redo the test and clean the files
Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @benoittgt, just a bit of feedback

features/generator_specs/generator_specs.feature Outdated Show resolved Hide resolved
features/generator_specs/generator_specs.feature Outdated Show resolved Hide resolved
features/generator_specs/generator_specs.feature Outdated Show resolved Hide resolved
features/support/hooks.rb Outdated Show resolved Hide resolved
@benoittgt
Copy link
Member

@JonRowe what do you think about:

I think we should add specs by default. See this question #2217 (comment)

I think we should always generated specs.

@joel
Copy link
Contributor Author

joel commented Jan 21, 2020

I think we should always generated specs.

@benoittgt I would say I agree and it was what I did in the first place, even if is not a big deal for generators though.

@joel
Copy link
Contributor Author

joel commented Jan 21, 2020

Anyway thanks @benoittgt 👍

@benoittgt
Copy link
Member

@benoittgt I would say I agree and it was what I did in the first place, even if is not a big deal for generators though.

I just read this answer from Jon:

I'm not sure we want to flip the default state here, generally speaking generators are a rarely used feature so I think its fine to have to request them.

Sorry I missed it. The PR is ready for review and even merge if it's ok for you.

@JonRowe
Copy link
Member

JonRowe commented Jan 21, 2020

I'm not sure we want to flip the default state here, generally speaking generators are a rarely used feature so I think its fine to have to request them.

Sorry I missed it. The PR is ready for review and even merge if it's ok for you.

No you can flip it, I mis understood (I rarely use generators). I'm ok with the default being to generate generator specs.

@joel
Copy link
Contributor Author

joel commented Jan 21, 2020

I rarely use generators

I guess really few people use them 😅

@benoittgt
Copy link
Member

I switch to default generated spec. I think we are good. :) I can squash if needed.

@benoittgt benoittgt merged commit ca988b0 into rspec:master Jan 22, 2020
benoittgt added a commit that referenced this pull request Jan 22, 2020
JonRowe pushed a commit that referenced this pull request Mar 13, 2020
@geekodour
Copy link

geekodour commented Jul 13, 2020

EDIT: updating to rspec-rails 4.0.0 resolved this.


I am facing this issue yet again with Rails 6.0.3.2 and RSpec 3.9. Is there anything else that needs to be done when using rspec?

λ rails -v
Rails 6.0.3.2

λ rspec -v
RSpec 3.9                                                                                                                                                                                                             - rspec-core 3.9.1                                                                                                                                                                                                  - rspec-expectations 3.9.0                                                                                                                                                                                        
  - rspec-mocks 3.9.1                                                                                                                                                                                               
  - rspec-rails 3.9.0                                                                                                                                                                                               
  - rspec-support 3.9.2

λ rails generate generator --help 
Usage:
  rails generate generator NAME [options]

Options:
      [--skip-namespace], [--no-skip-namespace]  # Skip namespace (affects only isolated applications)
      [--namespace], [--no-namespace]            # Namespace generator under lib/generators/name
                                                 # Default: true
  -t, [--test-framework=NAME]                    # Test framework to be invoked
                                                 # Default: rspec

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

Description:
    Stubs out a new generator at lib/generators. Pass the generator name as an argument,
    either CamelCased or snake_cased.

Example:
    `rails generate generator Awesome`

    creates a standard awesome generator:
        lib/generators/awesome/
        lib/generators/awesome/awesome_generator.rb
        lib/generators/awesome/USAGE
        lib/generators/awesome/templates/
        test/lib/generators/awesome_generator_test.rb

I don't have the

Rspec options:
  [--generator-specs], [--no-generator-specs]  # Generate generator specs

in the helptext, plus the same [not found] issue from the original description is printed.

@JonRowe
Copy link
Member

JonRowe commented Jul 14, 2020

For Rails 6.0 support you need rspec-rails > 4.0.0.

(Repeated despite edit to make it clear to future travellers).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants