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

Add more file extensions #8

Merged
merged 11 commits into from
Aug 18, 2017
Merged

Add more file extensions #8

merged 11 commits into from
Aug 18, 2017

Conversation

robertodr
Copy link
Contributor

@robertodr robertodr commented Aug 10, 2017

I am not a Ruby expert, but I've managed to make the following changes:

  • The plugin now also checks files with extensions .hpp, .hh, .cxx, .cc, .cpp, hence addressing Support more file extensions (hpp, cpp, etc.) #2
  • The error message is more informative. It first lists the offending files, then suggests appropriate actions and eventually prints the patches to apply.
  • Generation of the more informative error message is now done differently.
  • Pass the list of extensions to check to the plugin, instead of hardcoding the valid extensions. Default behavior only checks Objective-C extensions (.h, .m, .mm)

You can see the plugin in action for this example repository: PCMSolver/danger-cpp-example#3

### Questions/Ideas

- [ ] I haven't added tests, because I don't know how to.
- [ ]Generating the more informative error message requires iterating twice over the changes array.

- The plugin now also checks files with extensions .hpp, .hh, .cxx, .cc, .cpp
- The error message is more informative. It first lists the offending files,
  then suggests appropriate actions and eventually prints the patches to
  apply.
@nikolaykasyanov nikolaykasyanov self-requested a review August 10, 2017 08:22
@nikolaykasyanov nikolaykasyanov self-assigned this Aug 10, 2017
@nikolaykasyanov
Copy link
Contributor

Looks great!

I believe it's a good start, a more generic approach (passing extensions as argument) can be implemented later.

Adding/editing tests is easy, most of them are here.

You might also want to run tests locally, to do that you need to install bundler gem and then do bundle install in the project directory. When it's done, run bundle exec rake spec to run tests.

When in doubt, please use to .travis.yml file as a reference.

Thank you for your contribution and feel free to ask anything.

@robertodr
Copy link
Contributor Author

robertodr commented Aug 10, 2017

Thank for the pointers, @nikolaykasyanov. I have now fixed the existing tests and refactored the generation of the error message. I will try to add a tests for:

  1. More than one failing file.
  2. The new file extensions.

If you have suggestions on how to pass file extensions to be checked to the plugin, I can try to implement that too.

@nikolaykasyanov
Copy link
Contributor

nikolaykasyanov commented Aug 10, 2017

@robertodr regarding file extension list as argument:

we can do that by introducing a new key into plugin's configuration hash. At the time only ignore_file_patterns exists.

A new key could be introduced and its value could be used instead of hardcoded list of extensions.

Also, if this key is not specified in the config, current list of extensions must be used for backward compatibility reasons.

Does it make sense?

Plugin invocation could look like this:

code_style_validation.check ignore_file_patterns: [/^Pods\//] file_extensions: ['.h', '.m', ...]

@robertodr
Copy link
Contributor Author

How does it look now? 😄 I modified one line in the tests, so that the new parameter is tested:

@my_plugin.check file_extensions: ['.h', '.m', '.mm', '.C', '.cpp']

Adding a new proper test for a .hpp and/or .cpp file is beyond my capabilities, however.

README.md Outdated
This plugin uses 'clang-format' to look for code style violations in added
lines on the current MR / PR, and offers inline patches.
By default only Objective-C files, with extensions `.h`, `.m`, `.mm` and
`.C`, are checked.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does .C have to be capitalized?

Copy link
Contributor Author

@robertodr robertodr Aug 10, 2017

Choose a reason for hiding this comment

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

According to Wikipedia Objective-C files usually have those extensions. I only kept Objective-C source file extensions as defaults for the file_extensions input parameter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good find, I've never seen that extension in the wild, though. Also, it's effectively equal to .c extension on case-insensitive filesystems (i.e. on most Macs).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, of course! I'll remove it and leave .m, .mm and .h as defaults.

@nikolaykasyanov
Copy link
Contributor

@robertodr 👍 looks great.

It seems possible to make a test like existing "Reports code style violation as error", but such that does not specify .m as extension, thus making Danger to not emit an error. It could be called "Does not report errors in files with unwanted extensions" (not the best wording, though).

Another crucial test I'd expect is to verify that ignore_file_patterns are indeed respected when some of the ignored files have extensions matching file_extensions (for example, we ignore generated/ directory that contains .m file and we pass file_extensions: [ '.m' ], in this case generated/file.m should indeed be ignored). This can be achieved by adding explicit file_extensions argument to the invocation.

@nikolaykasyanov
Copy link
Contributor

I could assist you with mentioned tests tomorrow if needed.

@robertodr
Copy link
Contributor Author

That would be great. I might not have time to look at it tomorrow, though.

@nikolaykasyanov
Copy link
Contributor

@robertodr could you please provide me a push access to this Pull Request so I can play with tests?

@nikolaykasyanov
Copy link
Contributor

@robertodr nevermind, it seems that checkbox was already enabled.

message = ''
unless offending_files.empty?
message = 'Code style violations detected in the following files:' + "\n"
offending_files.each do |file_name|
Copy link
Contributor

Choose a reason for hiding this comment

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

You seem to be using tabs here. Please use spaces.

Copy link
Contributor

Choose a reason for hiding this comment

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

It could be helpful to configure Rubocop support in your editor. This repo doesn't have Rubocop's config yet, so it'll report some false positives, but it will also detect things like inconsistent indentation and trailing spaces.


message = ''
unless offending_files.empty?
message = 'Code style violations detected in the following files:' + "\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

Trailing space.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops... Sorry. I haven't yet reconfigured my Neovim properly after reinstalling the OS.

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries!

@nikolaykasyanov
Copy link
Contributor

My test fails on Travis, looking into it.

@robertodr robertodr changed the title Add more file file extensions Add more file extensions Aug 12, 2017
@robertodr
Copy link
Contributor Author

robertodr commented Aug 14, 2017

My neovim configuration is back on track! I have fixed indentation inconsistencies and fixed #9
I also had a crazy idea, that I don't know how to implement. Usually, one PR might touch more than one or two files in many places. The patches might become quite long and the comment from the bot might easily take up a large portion of the screen and obfuscate comments from reviewers. Here comes the crazy idea, would it be possible to have the bot write a Gist with the patch? The comment from the bot would then:

  1. List the offending files.
  2. Suggest actions to correct the errors.
  3. Point users to the Gist with the patch.

What do you think?

@nikolaykasyanov
Copy link
Contributor

@robertodr thanks! I'll try this out in our internal environment for a couple of days before merging.

Don't you mind if I squash commits upon merging?

@robertodr
Copy link
Contributor Author

Just test for as long as needed. No problem with squashing, as long as it preserves authorship assignment. What do you think about the crazy idea? Does it make sense? Is it doable?

@nikolaykasyanov
Copy link
Contributor

nikolaykasyanov commented Aug 15, 2017

@robertodr sorry, I didn't notice your big comment.

The idea about Gist sounds reasonable. The only concern is that it introduces a separate code path for open source projects (because posting diffs to Gist most certainly won't work for proprietary/closed source projects).

I wonder if GitHub (GitLab) supports some kind of folding, that might solve the problem of big patches cluttering the review.

Alternatively, patches could be attached as files (if GitHub API & Danger support that) and linked in comment text.

@robertodr
Copy link
Contributor Author

Oh! Yes, of course. I had not thought about that. Well, this PR is complete. Anything else can be part of subsequent PRs. Would you agree to tag the code and mint a release when the PR is merged?

@nikolaykasyanov nikolaykasyanov merged commit 5c250d9 into flix-tech:master Aug 18, 2017
@nikolaykasyanov
Copy link
Contributor

@robertodr here you go: https://github.com/flix-tech/danger-code_style_validation/releases/tag/0.1.0

Thanks for your contribution!

@robertodr
Copy link
Contributor Author

Thanks! It's been a pleasure collaborating with you on this.

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.

2 participants