Have generators handle multi-word names (#316)#330
Conversation
| File.delete("./src/views/#{snake_case}/edit.slang") | ||
| File.delete("./src/views/#{snake_case}/index.slang") | ||
| File.delete("./src/views/#{snake_case}/new.slang") | ||
| File.delete("./src/views/#{snake_case}/show.slang") |
There was a problem hiding this comment.
If one expectation above fails these files will not be deleted. Or absence of this files is guaranteed by Amber::CLI::Spec.cleanup ? If not, you may need to use begin ... ensure block for this:
begin
# expectations
ensure
# delete files
endBut if it is guaranteed, you don't need to delete them manually.
There was a problem hiding this comment.
The calls to Amber::CLI::Spec.cleanup should ensure cleanup, but admittedly, I didn't explicitly test for or look for it.
I did run a bunch of failing test suites though, and there were never any leftovers (according to git).
The entire spec in this file is wrapped in a begin ... ensure ... end that executes Amber::CLI::Spec.cleanup which is more comprehensive than my file deletion.
I can rerun some failing tests and explicitly look for leftover files (as they may be gitignored).
There was a problem hiding this comment.
I made it fail one of the should tests on generating a scaffold, then did an ls tmp and it was empty. It looks like we are good to count on Amber::CLI::Spec.cleanup
There was a problem hiding this comment.
So you don't need to delete it manually, right ?
There was a problem hiding this comment.
When the test suite is complete, Amber::CLI::Spec.cleanup makes sure everything is cleaned up.
The test itself tests two cases of possible CLI input (in a loop) - PostComment and post_comment
The reason for the manual delete is to avoid false green tests with the second iteration of the loop.
There was a problem hiding this comment.
Ah, sorry, i haven't noticed the loop above. Thanks for the explanation!
|
What's the preferred course of action here? Resolve the conflicts here, or rebase (off master) my changes locally and push with the new commits? |
|
Rebase off master |
45ca56a to
2e43481
Compare
|
Rebased, refactored, and add a new concept of |
30ab05d to
3ca2585
Compare
|
Hold off on this, I found a bug while manually testing - I didn't touch the routes generator. |
8e01848 to
cb4a02e
Compare
|
Manually tested with both Crecto and Granite, and added spec tests for both as well. LMK if anything else is needed for this PR. |
drujensen
left a comment
There was a problem hiding this comment.
lgtm. Nice work. Love all the test coverage. 💯 🥇
|
@marksiemers I found a couple issues testing the branch:
produces: The
|
|
@drujensen - The issue with the app name having dashes is a pre-existing bug |
|
@drujensen @veelenga |
elorest
left a comment
There was a problem hiding this comment.
Overall I I like this. Don't like all the seemingly repetitive code in generator_spec though.
| File.read("./config/routes.cr").should contain "#{camel_case}Controller" | ||
| File.read("./config/routes.cr").should_not contain "#{incorrect_case}Controller" | ||
|
|
||
| File.delete("./spec/models/#{snake_case}_spec.cr") |
There was a problem hiding this comment.
Why are we deleting all of these one by one? We already delete he folder after the test. At least we should be.
There was a problem hiding this comment.
Looks like you sort of answered my question below. Even if you do have a reason for deleting these we shouldn't be deleting them one by one.
There was a problem hiding this comment.
I can clean it up, the two (not mutually exclusive) ideas that come to mind first are:
- Use
rm -rf ./spec/*rm -rf ./src/* - Refactor to put the file handling in a module
There was a problem hiding this comment.
I can clean this up, two thoughts come to mind (not mutually exclusive), LMKWYT:
- Use
rm -rf ./src/*rm -rf ./spec/* - Have a module take care of the deletion of files (and potentially other file handling operations)
There was a problem hiding this comment.
The first option seems clean and easy to read.
There was a problem hiding this comment.
For the scaffold, I changed it so that a new app is created for each iteration of the loop, and the spec cleanup is also run. This is because the routes file is touched and that can't be deleted safely without generating a new app.
When applicable, I switched to rm -rf ./[dir]/*
veelenga
left a comment
There was a problem hiding this comment.
I haven't tried to run generators and test it locally, but the change looks good to me 👍
* Add tests for generating file names and class names correctly (#316) * No longer downcase name when instantiating a Template (#316) * Switch all cases of `@name.capitalize` to `@name.camelcase` (#316) * Switch from `@name.camelcase` everywhere to `class_name` instance method (#316) * Add `display_name` method and use it in generators * Incorporate class naming convention in route generators (#316) * Incorporate table naming convention in crecto and granite generators (#316) * Delete files in a better way in generator_spec
* Add tests for generating file names and class names correctly (#316) * No longer downcase name when instantiating a Template (#316) * Switch all cases of `@name.capitalize` to `@name.camelcase` (#316) * Switch from `@name.camelcase` everywhere to `class_name` instance method (#316) * Add `display_name` method and use it in generators * Incorporate class naming convention in route generators (#316) * Incorporate table naming convention in crecto and granite generators (#316) * Delete files in a better way in generator_spec Former-commit-id: 7e422db
Description of the Change
This changes how the generators handle file names and class names. It is related to #316 .
That issue was originally about controllers, but it applies to all the generators, so this fix addresses all of them.
I ended up added a lot of LOC to the test suite. Part of this is due to the
before_eachandafter_eachhooks in crystal being global (so I couldn't use them here), another reason is that the generators create a lot of files, checking each one takes a bit of code.That said, I'm opening this PR for initial feedback to see if this approach is acceptable - both to testing and coding this fix. If there is a better way to structure the test suite, I'm all ears.
Alternate Designs
I thought about DRY'ing the code up a bit so that
camelcasewasn't used in so many places, but it would have required adding at least an instance variable and a getter to theTemplate- in the end that may be the best solution, in case the generators need to deal with other naming considerations (singular and plural).I went with this solution to get something working, and provided a (hopefully) comprehensive set of tests if a refactor happens in the future.
Benefits
It fixes issue #316
Possible Drawbacks
This change touches a lot of files and has an effect on every generator. Based on the test suite, everything is still generating as normal, but it is possible something breaks in the actual generation of files.
On my machine, one of the pre-existing tests is either not passing, or hanging indefinitely. I checked out the master branch and got the same results.
It hangs here: https://github.com/amberframework/amber/blob/master/spec/amber/cli/commands/generator_spec.cr#L14
Or fails here: https://github.com/amberframework/amber/blob/master/spec/amber/cli/commands/generator_spec.cr#L16