Skip to content

Commit

Permalink
♻️ More idiomatic ruby
Browse files Browse the repository at this point in the history
- more tests
  • Loading branch information
pboling committed Sep 21, 2024
1 parent 8eef85e commit 2a8944e
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 16 deletions.
23 changes: 23 additions & 0 deletions .rubocop_gradual.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"spec/gem_bench/team_spec.rb:1266648289": [
[39, 7, 21, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 4057686576],
[39, 7, 463, "RSpec/ExampleLength: Example has too many lines. [12/5]", 717044346],
[238, 9, 22, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 837383149],
[247, 9, 39, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 3959041976],
[258, 11, 20, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 608832949],
[258, 11, 969, "RSpec/ExampleLength: Example has too many lines. [15/5]", 3322102119],
[276, 19, 17, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1188500192],
[281, 13, 20, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 608832949],
[281, 13, 1001, "RSpec/ExampleLength: Example has too many lines. [15/5]", 252278759],
[302, 15, 20, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 608832949],
[302, 15, 1174, "RSpec/ExampleLength: Example has too many lines. [16/5]", 1786252903],
[328, 9, 18, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 2487416927],
[342, 9, 22, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 837383149],
[351, 9, 39, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 3959041976],
[362, 11, 20, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 608832949],
[362, 11, 956, "RSpec/ExampleLength: Example has too many lines. [15/5]", 1208468539],
[383, 13, 20, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 608832949],
[383, 13, 1083, "RSpec/ExampleLength: Example has too many lines. [16/5]", 3055949499],
[408, 9, 18, "RSpec/MultipleExpectations: Example has too many expectations [2/1].", 2487416927]
]
}
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ and this project adheres to [Semantic Versioning v2](https://semver.org/spec/v2.
### Fixed
### Removed

## [2.0.5] SEP.21.2024
- COVERAGE: 99.80% -- 495/496 lines in 9 files
- BRANCH COVERAGE: 94.35% -- 167/177 branches in 9 files
- 58.87% documented
### Added
- More specs
- More documentation
- Even closer to 100% test coverage
### Fixed
- Documentation errors
- Minor improvements to logic and performance (a bit more idiomatic Ruby)

## [2.0.4] SEP.20.2024
- COVERAGE: 98.19% -- 488/497 lines in 9 files
- BRANCH COVERAGE: 88.95% -- 161/181 branches in 9 files
- 58.06% documented
### Added
- More Documentation
- More documentation
- Almost 100% test coverage
- Thread Safety (removed `GemBench.roster`, which was effectively never used internally)
- Thread safety (removed `GemBench.roster`, which was effectively never used internally)
- Performance improvements
### Fixed
- Can now handle more variations of Ruby syntax in the Gemfile analyzer
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ gem <<~GEM_NAME.chomp
pry-byebug
GEM_NAME

# Need this to be loaded by bundler, to exercise the "excluded" logic, since faker is excluded.
gem "faker"

# Specify your gem's dependencies in gem_bench.gemspec
gemspec
17 changes: 15 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ GEM
specs:
ansi (1.5.0)
ast (2.4.2)
awesome_print (1.9.2)
attr_extras (7.1.0)
backports (3.25.0)
byebug (11.1.3)
coderay (1.1.3)
concurrent-ruby (1.3.4)
diff-lcs (1.5.1)
diffy (3.4.2)
docile (1.4.1)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.7.2)
kettle-soup-cover (1.0.4)
simplecov (~> 0.22)
Expand All @@ -30,11 +35,14 @@ GEM
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
method_source (1.1.0)
optimist (3.1.0)
ostruct (0.6.0)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
patience_diff (1.2.0)
optimist (~> 3.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -141,6 +149,10 @@ GEM
standard-custom (>= 1.0.2, < 2)
standard-performance (>= 1.3.1, < 2)
version_gem (>= 1.1.4, < 3)
super_diff (0.12.1)
attr_extras (>= 6.2.4)
diff-lcs
patience_diff
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
unicode-display_width (2.6.0)
Expand All @@ -156,9 +168,9 @@ PLATFORMS
ruby

DEPENDENCIES
awesome_print (~> 1.9)
bundler
byebug (>= 2.0.3)
faker
gem_bench!
kettle-soup-cover (~> 1.0, >= 1.0.2)
method_source (>= 1.1.0)
Expand All @@ -170,6 +182,7 @@ DEPENDENCIES
rubocop-packaging (~> 0.5, >= 0.5.2)
rubocop-rspec (~> 3.0)
standard (~> 1.40)
super_diff (~> 0.12, >= 0.12.1)
yard (~> 0.9, >= 0.9.34)
yard-junk (~> 0.0.10)

Expand Down
3 changes: 2 additions & 1 deletion gem_bench.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ Gem::Specification.new do |spec|
spec.add_development_dependency("kettle-soup-cover", "~> 1.0", ">= 1.0.2")

# Unit tests
spec.add_development_dependency("awesome_print", "~> 1.9")
spec.add_development_dependency("faker", "~> 3.4", "3.4.2")
spec.add_development_dependency("method_source", ">= 1.1.0")
spec.add_development_dependency("rake", ">= 10")
spec.add_development_dependency("rspec", "~> 3.13")
spec.add_development_dependency("rspec-block_is_expected", "~> 1.0", ">= 1.0.6")
spec.add_development_dependency("super_diff", "~> 0.12", ">= 0.12.1")

# Linting
spec.add_development_dependency("rubocop-lts", "~> 10.1") # Lint & Style Support for Ruby 2.3+
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/ancient.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ source "https://rubygems.org"
gem "bundler", ">= 1.14"
gem "version_gem", "~> 1.1", ">= 1.1.4"

gem "faker", "~> 3.4", ">= 3.4.2"
gem "method_source", ">= 1.1.0"
gem "rake"
gem "rspec"
gem "rspec-block_is_expected"
gem "super_diff"

# For debugging, casecmp is only available in Ruby 2.4+
if RUBY_VERSION > "2.4" && ENV.fetch("DEBUG", "false").casecmp?("true")
Expand Down
31 changes: 20 additions & 11 deletions lib/gem_bench/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ module GemBench
# - if you are in a rails console, and want to evaluate the Gemfile of the Rails app, that's great!
# - if you are in a context with no Gemfile loaded, or a different Gemfile loaded than the one you want to evaluate,
# this class may not give sensible results. This is because it checks loaded gems via RubyGems and Bundler.
#
# Terminology:
#
# starter: a gem that needs to be loaded when bundler normally loads gems
#
# bencher: a gem that can, or should, have require: false to delay loading until after bootstrap
#
class Team
EXCLUDE = %w[
bundler
Expand Down Expand Up @@ -53,8 +60,8 @@ def initialize(**options)
)
@exclude_file_pattern_regex_proc = options[:exclude_file_pattern_regex_proc].respond_to?(:call) ? options[:exclude_file_pattern_regex_proc] : GemBench::EXCLUDE_FILE_PATTERN_REGEX_PROC
# Among the loaded gems there may be some that did not need to be.
@excluded, @all = @scout.loaded_gems.partition { |x| EXCLUDE.include?(x[0]) }
exclusions = " + #{excluded.length} loaded gems which GemBench is configured to ignore.\n" if @excluded.length > 0
exclude!
exclusions = " + #{excluded.length} loaded gems which GemBench is configured to ignore.\n" if excluded.any?
@starters = []
@benchers = []
@current_gemfile_suggestions = []
Expand All @@ -70,18 +77,19 @@ def initialize(**options)
false
end
puts "[GemBench] Will search for gems in #{gem_paths.inspect}\n#{if benching?
@scout.check_gemfile? ? "[GemBench] Will check Gemfile at #{gemfile_path}.\n" : "[GemBench] No Gemfile found.\n"
check_gemfile? ? "[GemBench] Will check Gemfile at #{gemfile_path}.\n" : "[GemBench] No Gemfile found.\n"
else
""
end}#{bad_ideas ? "[GemBench] Will show bad ideas. Be Careful.\n" : ""}[GemBench] Detected #{all.length} loaded gems#{exclusions}"
compare_gemfile if benching? && @scout.check_gemfile?
compare_gemfile if benching? && check_gemfile?
self.print if verbose
end

def list_starters(format: :name)
starters.map { |starter| starter.to_s(format) }
end

# @return void
def print
string = ""
if all.empty?
Expand All @@ -92,7 +100,7 @@ def print
else
"[GemBench] Found no gems containing #{look_for_regex} in Ruby code.\n"
end
elsif starters.length > 0
else
string << "\n#{GemBench::USAGE}" unless check_gemfile?
string << if benching?
"[GemBench] We found a Rails::Railtie or Rails::Engine in the following files. However, it is possible that there are false positives, so you may want to verify that this is the case.\n\n"
Expand All @@ -116,18 +124,15 @@ def print
starters.each_with_index do |starter, index|
string << "#{starter.info(index + 1)}\n"
end
if extra_verbose? && !benching? && benchers.length > 0
if extra_verbose? && !benching? && benchers.any?
string << "[GemBench] #{benchers.length} out of #{all.length} evaluated gems did not contain #{look_for_regex}. They are:\n"
benchers.each_with_index do |bencher, index|
string << "#{bencher.info(index + 1)}\n"
end
end
else
string << "[GemBench] Congrats! All gems appear clean.\n"
string << "\n#{GemBench::USAGE}" unless check_gemfile?
end
if check_gemfile? && benching?
if current_gemfile_suggestions.length > 0
if current_gemfile_suggestions.any?
string << "[GemBench] Evaluated #{all.length} gems and Gemfile at #{gemfile_path}.\n[GemBench] Here are #{current_gemfile_suggestions.length} suggestions for improvement:\n"
current_gemfile_suggestions.each_with_index do |player, index|
string << "#{player.suggest(index + 1)}\n"
Expand Down Expand Up @@ -159,7 +164,7 @@ def nothing

def prepare_bad_ideas
string = ""
if benchers.length > 0
if benchers.any?
gemfile_instruction = check_gemfile? ? "" : "To safely evaluate a Gemfile:\n\t1. Make sure you are in the root of a project with a Gemfile\n\t2. Make sure the gem is actually a dependency in the Gemfile\n"
string << "[GemBench] Evaluated #{all.length} loaded gems and found #{benchers.length} which may be able to skip boot loading (require: false).\n*** => WARNING <= ***: Be careful adding non-primary dependencies to your Gemfile as it is generally a bad idea.\n#{gemfile_instruction}"
benchers.each_with_index do |player, index|
Expand Down Expand Up @@ -223,6 +228,10 @@ def add_to_roster(player)

private

def exclude!
self.excluded, self.all = loaded_gems.partition { |x| EXCLUDE.include?(x[0]) }
end

def extra_verbose?
verbose == "extra"
end
Expand Down
3 changes: 3 additions & 0 deletions spec/gem_bench/scout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
[
"# For complexity!\n",
"# (this syntax is not supported by gem_bench, but also shouldn't make it blow up)\n",
%(# Need this to be loaded by bundler, to exercise the "excluded" logic, since faker is excluded.\n),
"# Specify your gem's dependencies in gem_bench.gemspec\n",
],
)
Expand Down Expand Up @@ -204,6 +205,7 @@
[
"# For complexity!\n",
"# (this syntax is not supported by gem_bench, but also shouldn't make it blow up)\n",
%(# Need this to be loaded by bundler, to exercise the "excluded" logic, since faker is excluded.\n),
"# Specify your gem's dependencies in gem_bench.gemspec\n",
],
)
Expand All @@ -226,6 +228,7 @@
[
"# For complexity!\n",
"# (this syntax is not supported by gem_bench, but also shouldn't make it blow up)\n",
%(# Need this to be loaded by bundler, to exercise the "excluded" logic, since faker is excluded.\n),
"# Specify your gem's dependencies in gem_bench.gemspec\n",
],
)
Expand Down
Loading

0 comments on commit 2a8944e

Please sign in to comment.