Commit 0393142f authored by Yorick Peterse's avatar Yorick Peterse

Merge branch '204753-add-rubocop-rule-to-limit-file-names-to-100-characters' into 'master'

Resolve "Add RuboCop rule to limit file names to 100 characters"

Closes #204753

See merge request gitlab-org/gitlab!24799
parents 50301e0e d9342714
# frozen_string_literal: true
module RuboCop
module Cop
class FilenameLength < Cop
include RangeHelp
FILEPATH_MAX_BYTES = 256
FILENAME_MAX_BYTES = 100
MSG_FILEPATH_LEN = "This file path is too long. It should be #{FILEPATH_MAX_BYTES} or less"
MSG_FILENAME_LEN = "This file name is too long. It should be #{FILENAME_MAX_BYTES} or less"
def investigate(processed_source)
file_path = processed_source.file_path
return if config.file_to_exclude?(file_path)
if file_path.bytesize > FILEPATH_MAX_BYTES
add_offense(nil, location: source_range(processed_source.buffer, 1, 0, 1), message: MSG_FILEPATH_LEN)
elsif File.basename(file_path).bytesize > FILENAME_MAX_BYTES
add_offense(nil, location: source_range(processed_source.buffer, 1, 0, 1), message: MSG_FILENAME_LEN)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/filename_length'
require_relative '../../support/helpers/expect_offense'
describe RuboCop::Cop::FilenameLength do
subject(:cop) { described_class.new }
it 'does not flag files with names 100 characters long' do
expect_no_offenses('puts "it does not matter"', 'a' * 100)
end
it 'tags files with names 101 characters long' do
filename = 'a' * 101
expect_offense(<<~SOURCE, filename)
source code
^ This file name is too long. It should be 100 or less
SOURCE
end
it 'tags files with names 256 characters long' do
filename = 'a' * 256
expect_offense(<<~SOURCE, filename)
source code
^ This file name is too long. It should be 100 or less
SOURCE
end
it 'tags files with filepath 256 characters long' do
filepath = File.join 'a', 'b' * 254
expect_offense(<<~SOURCE, filepath)
source code
^ This file name is too long. It should be 100 or less
SOURCE
end
it 'tags files with filepath 257 characters long' do
filepath = File.join 'a', 'b' * 255
expect_offense(<<~SOURCE, filepath)
source code
^ This file path is too long. It should be 256 or less
SOURCE
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