Commit f5df4976 authored by Robert Speicher's avatar Robert Speicher

Make the frozen_string Danger check more resilient

It's possible for a Ruby file to be empty and thus `gets` returns `nil`.

Also indents the Markdown since we're using `<<~` and removes an
unneeded variable.
parent 7b6d3974
...@@ -6,22 +6,21 @@ MAGIC_COMMENT = "# frozen_string_literal: true" ...@@ -6,22 +6,21 @@ MAGIC_COMMENT = "# frozen_string_literal: true"
def get_files_with_no_magic_comment(files) def get_files_with_no_magic_comment(files)
files.select do |file| files.select do |file|
file.end_with?(FILE_EXTENSION) && file.end_with?(FILE_EXTENSION) &&
!File.open(file, &:gets).start_with?(MAGIC_COMMENT) !File.open(file, &:gets)&.start_with?(MAGIC_COMMENT)
end end
end end
files_to_check = git.added_files files_to_fix = get_files_with_no_magic_comment(git.added_files)
files_to_fix = get_files_with_no_magic_comment(files_to_check)
if files_to_fix.any? if files_to_fix.any?
warn 'This merge request adds files that do not enforce frozen string literal. ' \ warn 'This merge request adds files that do not enforce frozen string literal. ' \
'See https://gitlab.com/gitlab-org/gitlab-ce/issues/47424 for more information.' 'See https://gitlab.com/gitlab-org/gitlab-ce/issues/47424 for more information.'
markdown(<<~MARKDOWN) markdown(<<~MARKDOWN)
## Enable Frozen String Literal ## Enable Frozen String Literal
The following files should have `#{MAGIC_COMMENT}` in the first line: The following files should have `#{MAGIC_COMMENT}` on the first line:
* #{files_to_fix.map { |path| "`#{path}`" }.join("\n* ")} * #{files_to_fix.map { |path| "`#{path}`" }.join("\n* ")}
MARKDOWN MARKDOWN
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment