Skip to content

Commit bede28d

Browse files
justin808claude
andcommitted
Add RuboCop linting and newline enforcement
Set up automated linting and code quality tools: 1. Added RuboCop with extensions: - rubocop-rake for rake task linting - rubocop-rspec for RSpec linting - Created .rubocop.yml with sensible defaults 2. Created rake tasks for linting (rakelib/lint.rake): - rake lint - Run all linters - rake lint:fix - Auto-fix issues - rake check_newlines - Verify files end with newlines - rake fix_newlines - Fix missing newlines 3. Added pre-commit hook installer (bin/install-hooks): - Checks for missing newlines - Runs RuboCop on staged Ruby files - Prevents commits with linting issues 4. Added GitHub Actions workflow (.github/workflows/lint.yml): - Runs on all PRs and pushes to master - Enforces linting in CI - Checks for missing newlines 5. Updated CI workflow: - Added :ci task to run specs, lint, and newline checks - Ensures all checks pass before merge To install pre-commit hook: ./bin/install-hooks 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent d1662ca commit bede28d

File tree

6 files changed

+222
-1
lines changed

6 files changed

+222
-1
lines changed

.github/workflows/lint.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
pull_request:
7+
branches: [master, main]
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Set up Ruby
17+
uses: ruby/setup-ruby@v1
18+
with:
19+
ruby-version: '3.2'
20+
bundler-cache: true
21+
22+
- name: Run RuboCop
23+
run: bundle exec rubocop
24+
25+
- name: Check for files missing newlines
26+
run: bundle exec rake check_newlines

.rubocop.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
require:
2+
- rubocop-rake
3+
- rubocop-rspec
4+
5+
AllCops:
6+
TargetRubyVersion: 2.6
7+
NewCops: enable
8+
Exclude:
9+
- 'vendor/**/*'
10+
- 'spec/fixtures/**/*'
11+
- 'tmp/**/*'
12+
- 'pkg/**/*'
13+
- 'node_modules/**/*'
14+
- 'specs_e2e/**/*'
15+
- 'e2e/**/*'
16+
17+
# Ensure all files end with a newline
18+
Layout/TrailingEmptyLines:
19+
Enabled: true
20+
EnforcedStyle: final_newline
21+
22+
# Ensure no trailing whitespace
23+
Layout/TrailingWhitespace:
24+
Enabled: true
25+
26+
# Line length - be reasonable but not too strict
27+
Layout/LineLength:
28+
Max: 120
29+
Exclude:
30+
- 'spec/**/*'
31+
- 'lib/generators/**/*'
32+
33+
# Allow longer blocks in specs and rake tasks
34+
Metrics/BlockLength:
35+
Exclude:
36+
- 'spec/**/*'
37+
- '**/*.rake'
38+
- 'Rakefile'
39+
- '*.gemspec'
40+
41+
# Allow longer methods in rake tasks
42+
Metrics/MethodLength:
43+
Exclude:
44+
- '**/*.rake'
45+
- 'Rakefile'
46+
47+
# String literals
48+
Style/StringLiterals:
49+
Enabled: true
50+
EnforcedStyle: single_quotes
51+
ConsistentQuotesInMultiline: true
52+
53+
# Frozen string literal pragma
54+
Style/FrozenStringLiteralComment:
55+
Enabled: true
56+
EnforcedStyle: always
57+
Exclude:
58+
- 'spec/**/*'
59+
60+
# Documentation
61+
Style/Documentation:
62+
Enabled: false
63+
64+
# Allow compact module/class definitions
65+
Style/ClassAndModuleChildren:
66+
Enabled: false
67+
68+
# RSpec specific
69+
RSpec/ExampleLength:
70+
Max: 20
71+
72+
RSpec/MultipleExpectations:
73+
Max: 5
74+
75+
RSpec/NestedGroups:
76+
Max: 4

Rakefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ RSpec::Core::RakeTask.new(:spec) do |t|
33
t.pattern = 'spec/cypress_on_rails/*_spec.rb'
44
end
55

6-
task default: :spec
6+
desc 'Run all CI checks (specs, linting, newlines)'
7+
task ci: %i[spec lint check_newlines]
8+
9+
task default: :spec

bin/install-hooks

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'fileutils'
5+
6+
hooks_dir = File.expand_path('../.git/hooks', __dir__)
7+
pre_commit_hook = File.join(hooks_dir, 'pre-commit')
8+
9+
# Create pre-commit hook content
10+
hook_content = <<~HOOK
11+
#!/bin/sh
12+
# Pre-commit hook to run linters and check for newlines
13+
14+
# Check for files missing newlines
15+
echo "Checking for files missing newlines..."
16+
bundle exec rake check_newlines
17+
if [ $? -ne 0 ]; then
18+
echo "❌ Some files are missing final newlines. Run 'bundle exec rake fix_newlines' to fix."
19+
exit 1
20+
fi
21+
22+
# Run RuboCop on staged Ruby files
23+
echo "Running RuboCop on staged files..."
24+
files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\\.(rb|rake)$')
25+
if [ -n "$files" ]; then
26+
bundle exec rubocop $files
27+
if [ $? -ne 0 ]; then
28+
echo "❌ RuboCop failed. Fix issues or run 'bundle exec rake lint:fix'"
29+
exit 1
30+
fi
31+
fi
32+
33+
echo "✅ All checks passed!"
34+
HOOK
35+
36+
# Create hooks directory if it doesn't exist
37+
FileUtils.mkdir_p(hooks_dir)
38+
39+
# Write the pre-commit hook
40+
File.write(pre_commit_hook, hook_content)
41+
42+
# Make it executable
43+
File.chmod(0o755, pre_commit_hook)
44+
45+
puts '✅ Pre-commit hook installed successfully!'
46+
puts 'The hook will:'
47+
puts ' - Check that all files end with a newline'
48+
puts ' - Run RuboCop on staged Ruby files'
49+
puts ''
50+
puts 'To skip the hook temporarily, use: git commit --no-verify'

cypress-on-rails.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Gem::Specification.new do |s|
2222
s.add_development_dependency 'factory_bot', '!= 6.4.5'
2323
s.add_development_dependency 'vcr'
2424
s.add_development_dependency 'gem-release'
25+
s.add_development_dependency 'rubocop'
26+
s.add_development_dependency 'rubocop-rake'
27+
s.add_development_dependency 'rubocop-rspec'
2528
s.metadata = {
2629
"bug_tracker_uri" => "https://github.com/shakacode/cypress-on-rails/issues",
2730
"changelog_uri" => "https://github.com/shakacode/cypress-on-rails/blob/master/CHANGELOG.md",

rakelib/lint.rake

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
desc 'Run RuboCop'
4+
task :rubocop do
5+
sh 'bundle exec rubocop'
6+
end
7+
8+
desc 'Run RuboCop with auto-correct'
9+
task 'rubocop:auto_correct' do
10+
sh 'bundle exec rubocop -A'
11+
end
12+
13+
desc 'Run all linters'
14+
task lint: :rubocop
15+
16+
desc 'Auto-fix all linting issues'
17+
task 'lint:fix' => 'rubocop:auto_correct'
18+
19+
desc 'Ensure all files end with newline'
20+
task :check_newlines do
21+
files_without_newline = []
22+
23+
Dir.glob('**/*.{rb,rake,yml,yaml,md,gemspec,ru,erb,js,json}').each do |file|
24+
next if file.include?('vendor/') || file.include?('node_modules/') || file.include?('.git/')
25+
next if file.include?('pkg/') || file.include?('tmp/') || file.include?('coverage/')
26+
next unless File.file?(file)
27+
28+
content = File.read(file)
29+
files_without_newline << file unless content.empty? || content.end_with?("\n")
30+
end
31+
32+
if files_without_newline.any?
33+
puts 'Files missing final newline:'
34+
files_without_newline.each { |f| puts " #{f}" }
35+
exit 1
36+
else
37+
puts '✓ All files end with newline'
38+
end
39+
end
40+
41+
desc 'Fix files missing final newline'
42+
task :fix_newlines do
43+
fixed_files = []
44+
45+
Dir.glob('**/*.{rb,rake,yml,yaml,md,gemspec,ru,erb,js,json}').each do |file|
46+
next if file.include?('vendor/') || file.include?('node_modules/') || file.include?('.git/')
47+
next if file.include?('pkg/') || file.include?('tmp/') || file.include?('coverage/')
48+
next unless File.file?(file)
49+
50+
content = File.read(file)
51+
unless content.empty? || content.end_with?("\n")
52+
File.write(file, content + "\n")
53+
fixed_files << file
54+
end
55+
end
56+
57+
if fixed_files.any?
58+
puts "Fixed #{fixed_files.length} files:"
59+
fixed_files.each { |f| puts " #{f}" }
60+
else
61+
puts '✓ All files already end with newline'
62+
end
63+
end

0 commit comments

Comments
 (0)