Skip to content
This repository was archived by the owner on Feb 9, 2025. It is now read-only.

Commit d86fa68

Browse files
authored
Merge pull request #15 from vinnyglennon/add_rubocop_standard_linting
Add Rubocop/StandardRB linting
2 parents b474881 + bf8a561 commit d86fa68

File tree

11 files changed

+355
-35
lines changed

11 files changed

+355
-35
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ coverage.data
1616

1717
.DS_Store
1818
.env
19-
*.gem
19+
*.gem
20+
.vscode/*

.rubocop.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

Gemfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ source 'https://rubygems.org'
66
gemspec
77

88
gem 'rake', '~> 13.0'
9-
109
gem 'rspec', '~> 3.0'
11-
1210
gem 'simplecov', '~> 0.22'
13-
1411
gem 'pry'
12+
gem "standard"

Gemfile.lock

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,94 @@ PATH
99
GEM
1010
remote: https://rubygems.org/
1111
specs:
12+
ast (2.4.2)
1213
coderay (1.1.3)
1314
diff-lcs (1.5.1)
14-
docile (1.4.0)
15-
dotenv (2.8.1)
15+
docile (1.4.1)
16+
dotenv (3.1.2)
1617
event_stream_parser (1.0.0)
17-
faraday (2.9.0)
18-
faraday-net_http (>= 2.0, < 3.2)
18+
faraday (2.11.0)
19+
faraday-net_http (>= 2.0, < 3.4)
20+
logger
1921
faraday-multipart (1.0.4)
2022
multipart-post (~> 2)
21-
faraday-net_http (3.1.0)
23+
faraday-net_http (3.3.0)
2224
net-http
2325
front_matter_parser (1.0.1)
24-
method_source (1.0.0)
25-
multipart-post (2.4.0)
26+
json (2.7.2)
27+
language_server-protocol (3.17.0.3)
28+
lint_roller (1.1.0)
29+
logger (1.6.1)
30+
method_source (1.1.0)
31+
multipart-post (2.4.1)
2632
net-http (0.4.1)
2733
uri
34+
parallel (1.26.3)
35+
parser (3.3.5.0)
36+
ast (~> 2.4.1)
37+
racc
2838
pry (0.14.2)
2939
coderay (~> 1.1)
3040
method_source (~> 1.0)
31-
rake (13.1.0)
41+
racc (1.8.1)
42+
rainbow (3.1.1)
43+
rake (13.2.1)
44+
regexp_parser (2.9.2)
45+
rexml (3.3.7)
3246
rspec (3.13.0)
3347
rspec-core (~> 3.13.0)
3448
rspec-expectations (~> 3.13.0)
3549
rspec-mocks (~> 3.13.0)
36-
rspec-core (3.13.0)
50+
rspec-core (3.13.1)
3751
rspec-support (~> 3.13.0)
38-
rspec-expectations (3.13.0)
52+
rspec-expectations (3.13.2)
3953
diff-lcs (>= 1.2.0, < 2.0)
4054
rspec-support (~> 3.13.0)
41-
rspec-mocks (3.13.0)
55+
rspec-mocks (3.13.1)
4256
diff-lcs (>= 1.2.0, < 2.0)
4357
rspec-support (~> 3.13.0)
4458
rspec-support (3.13.1)
45-
ruby-openai (6.3.1)
59+
rubocop (1.65.1)
60+
json (~> 2.3)
61+
language_server-protocol (>= 3.17.0)
62+
parallel (~> 1.10)
63+
parser (>= 3.3.0.2)
64+
rainbow (>= 2.2.2, < 4.0)
65+
regexp_parser (>= 2.4, < 3.0)
66+
rexml (>= 3.2.5, < 4.0)
67+
rubocop-ast (>= 1.31.1, < 2.0)
68+
ruby-progressbar (~> 1.7)
69+
unicode-display_width (>= 2.4.0, < 3.0)
70+
rubocop-ast (1.32.2)
71+
parser (>= 3.3.1.0)
72+
rubocop-performance (1.21.1)
73+
rubocop (>= 1.48.1, < 2.0)
74+
rubocop-ast (>= 1.31.1, < 2.0)
75+
ruby-openai (7.1.0)
4676
event_stream_parser (>= 0.3.0, < 2.0.0)
4777
faraday (>= 1)
4878
faraday-multipart (>= 1)
79+
ruby-progressbar (1.13.0)
4980
simplecov (0.22.0)
5081
docile (~> 1.1)
5182
simplecov-html (~> 0.11)
5283
simplecov_json_formatter (~> 0.1)
5384
simplecov-html (0.12.3)
5485
simplecov_json_formatter (0.1.4)
55-
uri (0.13.0)
86+
standard (1.40.0)
87+
language_server-protocol (~> 3.17.0.2)
88+
lint_roller (~> 1.0)
89+
rubocop (~> 1.65.0)
90+
standard-custom (~> 1.0.0)
91+
standard-performance (~> 1.4)
92+
standard-custom (1.0.2)
93+
lint_roller (~> 1.0)
94+
rubocop (~> 1.50)
95+
standard-performance (1.4.0)
96+
lint_roller (~> 1.1)
97+
rubocop-performance (~> 1.21.0)
98+
unicode-display_width (2.5.0)
99+
uri (0.13.1)
56100

57101
PLATFORMS
58102
arm64-darwin-23
@@ -64,6 +108,7 @@ DEPENDENCIES
64108
rake (~> 13.0)
65109
rspec (~> 3.0)
66110
simplecov (~> 0.22)
111+
standard
67112

68113
BUNDLED WITH
69114
2.5.6

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ codegen path/to/the/file/you/want/to/test --openai_key <your_openai_key>
4444
```
4545

4646

47-
5. That's it! You're ready to start generating tests for your codebase.
47+
5. That's it! You're ready to start generating tests for your codebase.
4848

4949

5050
**Extra**:
@@ -80,6 +80,9 @@ CLI takes an optional openai_org_id parameter. Used to specify the organisation
8080
codegen path/to/the/file/you/want/to/test --openai_key <your_openai_key> --openai_org_id <your_openai_organisation_id>
8181
```
8282

83+
## Linting
84+
When testing .rb code, then Rubocop or StandardRB will automatically be applied to the generated test code, if these gems are in your GemFile. For Rubocop, a .rubocop.yml needs to be on the top-level also. If both Rubocop and StandardRB are correctly set up, only StandardRB runs against the generated test code.
85+
8386

8487
## How it works
8588

lib/dotcodegen/init.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ def self.run
77
source_dir = File.expand_path('../../config/default/.codegen', __dir__)
88
destination_dir = File.expand_path('.codegen', Dir.pwd)
99

10-
FileUtils.mkdir_p(destination_dir) unless Dir.exist?(destination_dir)
10+
FileUtils.mkdir_p(destination_dir)
1111
FileUtils.cp_r("#{source_dir}/.", destination_dir)
1212

1313
instructions_dir = File.expand_path('instructions', destination_dir)
14-
FileUtils.mkdir_p(instructions_dir) unless Dir.exist?(instructions_dir)
14+
FileUtils.mkdir_p(instructions_dir)
1515

1616
Dir.glob("#{source_dir}/instructions/*.md").each do |md_file|
1717
FileUtils.cp(md_file, instructions_dir)
1818
end
1919

20-
puts 'Codegen initialized.'
21-
exit
20+
Rails.logger.info 'Codegen initialized.'
2221
end
2322
# rubocop:enable Metrics/MethodLength
2423
end

lib/dotcodegen/lint_code.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module Dotcodegen
4+
class LintCode
5+
def initialize(file_path:)
6+
@file_path = file_path
7+
end
8+
9+
def ruby_supported?
10+
@file_path.end_with? ".rb"
11+
end
12+
13+
def run
14+
if ruby_supported?
15+
if gem_available?('standard')
16+
standardrb_code
17+
18+
# Attempt to lint with RuboCop if the gem is available and .rubocop.yml exists
19+
# StandardRB includes Rubocop by default. Hence the presence of .rubocop.yml is best indication that Rubocop is in active use
20+
elsif gem_available?('rubocop') && rubocop_config_exists?
21+
rubocop_code
22+
end
23+
end
24+
end
25+
26+
27+
def standardrb_code
28+
puts "Linting: StandardRB"
29+
system("standardrb --fix-unsafely #{@file_path}")
30+
end
31+
32+
def rubocop_code
33+
puts "Linting: Rubocop"
34+
system("rubocop --autocorrect-all --disable-pending-cops #{@file_path}")
35+
end
36+
37+
def gem_available?(name)
38+
Gem::Specification.find_by_name(name)
39+
rescue Gem::LoadError
40+
false
41+
end
42+
43+
# Function to check for .rubocop.yml configuration file
44+
def rubocop_config_exists?
45+
File.exist?('.rubocop.yml')
46+
end
47+
end
48+
end

lib/dotcodegen/test_file_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'fileutils'
44
require_relative 'test_code_generator'
5+
require_relative 'lint_code'
56

67
module Dotcodegen
78
class TestFileGenerator
@@ -35,12 +36,15 @@ def ensure_test_file_presence
3536
File.write(test_file_path, '')
3637
end
3738

39+
3840
def write_generated_code_to_test_file
3941
generated_code = Dotcodegen::TestCodeGenerator.new(config: matcher,
4042
file_to_test_path: file_path,
4143
openai_key:,
4244
openai_org_id:).generate_test_code
4345
File.write(test_file_path, generated_code)
46+
47+
Dotcodegen::LintCode.new(file_path: test_file_path).run
4448
end
4549

4650
def open_test_file_in_editor

0 commit comments

Comments
 (0)