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 new Lint/ScriptPermission cop #4342

Merged
merged 1 commit into from
May 15, 2017

Conversation

yhirano55
Copy link
Contributor

@yhirano55 yhirano55 commented May 3, 2017

This cop checks if a file which has a shebang line as
its first line is granted execute permission.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@yhirano55 yhirano55 force-pushed the style-script-permission branch 3 times, most recently from 0de9b32 to 310b339 Compare May 3, 2017 06:30
@yhirano55 yhirano55 changed the title Add Style/ScriptPermission Add new Style/ScriptPermission cop May 3, 2017
@@ -868,6 +868,13 @@ Style/SafeNavigation:
safe navigation (`&.`).
Enabled: true

Style/ScriptPermission:
Description: 'Set file permission with `chmod +x`.'
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd use some more generic wording here - after all some people use Windows. ;-)

module RuboCop
module Cop
module Style
# This cop checks whether the file permission is executable.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be extended a bit - after all the cop applies just scripts. Frankly, from this description people looking the manual would be quite confused.

@bbatsov
Copy link
Collaborator

bbatsov commented May 5, 2017

This cop should certain check that the script files actually have shebang in them. Right now this is going to ignore every scripts that's not in bin/ and exe/ and that sucks.

Guess some logic can be used from other cops that we have which are dealing with shebangs.

@yhirano55 yhirano55 force-pushed the style-script-permission branch from 310b339 to 270ac02 Compare May 10, 2017 06:00
@yhirano55
Copy link
Contributor Author

@bbatsov Thanks for your code-review! I have addressed your feedbacks. Please review this codes, if you have a chance :)

# This cop checks if a file which has a shebang line as
# its first line is granted execute permission.
class ScriptPermission < Cop
MSG = 'Grant script file execute permission.'.freeze
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should mention the file name here, as this wording might convince some people. I'd also use working like Script file X doesn't have execute permission. (or something similar).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see. I'll fix it. Thanks!!

@bbatsov
Copy link
Collaborator

bbatsov commented May 12, 2017

Please, update the specs to use the new rspec helpers recently added by @backus.

@backus
Copy link
Contributor

backus commented May 12, 2017

This should probably go under the Lint department. Doesn't seem like style

@yhirano55 yhirano55 force-pushed the style-script-permission branch from 270ac02 to 46c05a1 Compare May 15, 2017 15:38
@yhirano55 yhirano55 changed the title Add new Style/ScriptPermission cop Add new Lint/ScriptPermission cop May 15, 2017
@yhirano55
Copy link
Contributor Author

@bbatsov @backus Thanks for your comments. I've done:

  • Used new rspec helpers.
  • Changed department from Style to Lint.

@bbatsov bbatsov merged commit cebc032 into rubocop:master May 15, 2017
@yhirano55 yhirano55 deleted the style-script-permission branch May 15, 2017 20:51
@zavan
Copy link

zavan commented May 30, 2017

I'm having a problem with this cop, it says my bin/update file doesn't have execute permissions, but it does.

I have tried chmod +x bin/update to no effect, here's my ls -laH output:

zavan@zavan-pc:~/projects/ruby/alm$ ls -laH bin/
total 92
drwxr-xr-x  2 zavan zavan 4096 mai 30 14:12 .
drwxr-xr-x 16 zavan zavan 4096 mai 30 13:54 ..
-rwxr-xr-x  1 zavan zavan  129 fev 24 20:41 bundle
-rwxr-xr-x  1 zavan zavan  180 jan 30 00:34 delayed_job
-rwxr-xr-x  1 zavan zavan  141 mai 22 23:46 rails
-rwxr-xr-x  1 zavan zavan   90 mai 22 23:46 rake
-rwxr-xr-x  1 zavan zavan  376 jan 30 00:34 rspec
-rwxr-xr-x  1 zavan zavan  920 fev 24 22:03 setup
-rwxr-xr-x  1 zavan zavan  782 mai 30 14:12 update

Notice that all files have the same permissions, but only this one is erroring out.

File::Stat#executable? returns true:

[13] pry(main)> >> File.new('/home/zavan/projects/ruby/alm/bin/update').stat.executable?
=> true

I have checked and the first line is #!/usr/bin/env ruby, here's the full source code:

#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end

chdir APP_ROOT do
  # This script is a way to update your development environment automatically.
  # Add necessary update steps to this file.

  puts '== Installing dependencies =='
  system! 'gem install bundler --conservative'
  system('bundle check') || system!('bundle install')

  puts "\n== Updating database =="
  system! 'bin/rails db:migrate'

  puts "\n== Removing old logs and tempfiles =="
  system! 'bin/rails log:clear tmp:clear'

  puts "\n== Restarting application server =="
  system! 'bin/rails restart'
end

I have also tried deleting and recreating the file and rewriting the shebang line.

Also, whatever happened to #3659? I agree that this is out of scope.

@sonalkr132
Copy link

@zavan I think the buffer/cache might be causing the problem. In my case, the cop complained correctly that a script file wasn't executable and it didn't shut up until I changed content of the file (add/delete an empty line)

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.

5 participants