ruby_minitest_analyzer 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ed1a131727d516076a07651d4f2a949e91e712488f17aa470507e0f120aed8c
4
+ data.tar.gz: c276fe4754bdca68d8941f1b15924cacc40d4b0dae1028a3beceb42e71f1bc18
5
+ SHA512:
6
+ metadata.gz: 520fefaada9536cb8abd3ae938362c5bce37b62a137b813c2c33e5d35d17f31befadfb1780379edd41c4c5e39d77f13514b6ff033579388447ecf17fb1e1edb6
7
+ data.tar.gz: '0998fe3f1e28c1663a7729ad312db187bb9daa234ddd4b529a02f99999f3bb97de9c732072d3a5218cf773536bd86fcd9676b67cf8d47f2b98d0a35c98ccc3bf'
@@ -0,0 +1,22 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@1.0.4
4
+ jobs:
5
+ test:
6
+ docker:
7
+ - image: cimg/ruby:2.7
8
+ steps:
9
+ - checkout
10
+ - ruby/install-deps
11
+ - run:
12
+ name: Run tests
13
+ command: bundle exec rake
14
+ - run:
15
+ name: Run Styling
16
+ command: bundle exec rubocop
17
+
18
+ workflows:
19
+ version: 2
20
+ deploy:
21
+ jobs:
22
+ - test
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,157 @@
1
+ require: rubocop-minitest
2
+
3
+ Style/Documentation:
4
+ Enabled: false
5
+
6
+ Layout/LineLength:
7
+ Max: 120
8
+ Exclude:
9
+ - 'test/ruby_minitest_analyzer/test_summary_presenter_test.rb'
10
+ - 'test/ruby_minitest_analyzer_test.rb'
11
+
12
+ Metrics/MethodLength:
13
+ Max: 35
14
+ Exclude:
15
+ - 'test/ruby_minitest_analyzer.rb'
16
+
17
+ Metrics/AbcSize:
18
+ Enabled: false
19
+
20
+ Metrics/ParameterLists:
21
+ Max: 7
22
+
23
+ RequiredRubyVersion:
24
+ Enabled: false
25
+
26
+ Gemspec/DateAssignment: # new in 1.10
27
+ Enabled: true
28
+ Gemspec/RequireMFA: # new in 1.23
29
+ Enabled: true
30
+ Layout/LineEndStringConcatenationIndentation: # new in 1.18
31
+ Enabled: true
32
+ Layout/SpaceBeforeBrackets: # new in 1.7
33
+ Enabled: true
34
+ Lint/AmbiguousAssignment: # new in 1.7
35
+ Enabled: true
36
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
37
+ Enabled: true
38
+ Lint/AmbiguousRange: # new in 1.19
39
+ Enabled: true
40
+ Lint/DeprecatedConstants: # new in 1.8
41
+ Enabled: true
42
+ Lint/DuplicateBranch: # new in 1.3
43
+ Enabled: true
44
+ Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
45
+ Enabled: true
46
+ Lint/EmptyBlock: # new in 1.1
47
+ Enabled: true
48
+ Lint/EmptyClass: # new in 1.3
49
+ Enabled: true
50
+ Lint/EmptyInPattern: # new in 1.16
51
+ Enabled: true
52
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
53
+ Enabled: true
54
+ Lint/LambdaWithoutLiteralBlock: # new in 1.8
55
+ Enabled: true
56
+ Lint/NoReturnInBeginEndBlocks: # new in 1.2
57
+ Enabled: true
58
+ Lint/NumberedParameterAssignment: # new in 1.9
59
+ Enabled: true
60
+ Lint/OrAssignmentToConstant: # new in 1.9
61
+ Enabled: true
62
+ Lint/RedundantDirGlobSort: # new in 1.8
63
+ Enabled: true
64
+ Lint/RequireRelativeSelfPath: # new in 1.22
65
+ Enabled: true
66
+ Lint/SymbolConversion: # new in 1.9
67
+ Enabled: true
68
+ Lint/ToEnumArguments: # new in 1.1
69
+ Enabled: true
70
+ Lint/TripleQuotes: # new in 1.9
71
+ Enabled: true
72
+ Lint/UnexpectedBlockArity: # new in 1.5
73
+ Enabled: true
74
+ Lint/UnmodifiedReduceAccumulator: # new in 1.1
75
+ Enabled: true
76
+ Lint/UselessRuby2Keywords: # new in 1.23
77
+ Enabled: true
78
+ Naming/BlockForwarding: # new in 1.24
79
+ Enabled: true
80
+ Security/IoMethods: # new in 1.22
81
+ Enabled: true
82
+ Style/ArgumentsForwarding: # new in 1.1
83
+ Enabled: true
84
+ Style/CollectionCompact: # new in 1.2
85
+ Enabled: true
86
+ Style/DocumentDynamicEvalDefinition: # new in 1.1
87
+ Enabled: true
88
+ Style/EndlessMethod: # new in 1.8
89
+ Enabled: true
90
+ Style/FileRead: # new in 1.24
91
+ Enabled: true
92
+ Style/FileWrite: # new in 1.24
93
+ Enabled: true
94
+ Style/HashConversion: # new in 1.10
95
+ Enabled: true
96
+ Style/HashExcept: # new in 1.7
97
+ Enabled: true
98
+ Style/IfWithBooleanLiteralBranches: # new in 1.9
99
+ Enabled: true
100
+ Style/InPatternThen: # new in 1.16
101
+ Enabled: true
102
+ Style/MapToHash: # new in 1.24
103
+ Enabled: true
104
+ Style/MultilineInPatternThen: # new in 1.16
105
+ Enabled: true
106
+ Style/NegatedIfElseCondition: # new in 1.2
107
+ Enabled: true
108
+ Style/NilLambda: # new in 1.3
109
+ Enabled: true
110
+ Style/NumberedParameters: # new in 1.22
111
+ Enabled: true
112
+ Style/NumberedParametersLimit: # new in 1.22
113
+ Enabled: true
114
+ Style/OpenStructUse: # new in 1.23
115
+ Enabled: true
116
+ Style/QuotedSymbols: # new in 1.16
117
+ Enabled: true
118
+ Style/RedundantArgument: # new in 1.4
119
+ Enabled: true
120
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
121
+ Enabled: true
122
+ Style/SelectByRegexp: # new in 1.22
123
+ Enabled: true
124
+ Style/StringChars: # new in 1.12
125
+ Enabled: true
126
+ Style/SwapValues: # new in 1.1
127
+ Enabled: true
128
+ Minitest/AssertInDelta: # new in 0.10
129
+ Enabled: true
130
+ Minitest/AssertionInLifecycleHook: # new in 0.10
131
+ Enabled: true
132
+ Minitest/AssertKindOf: # new in 0.10
133
+ Enabled: true
134
+ Minitest/AssertOutput: # new in 0.10
135
+ Enabled: true
136
+ Minitest/AssertPathExists: # new in 0.10
137
+ Enabled: true
138
+ Minitest/AssertSilent: # new in 0.10
139
+ Enabled: true
140
+ Minitest/AssertWithExpectedArgument: # new in 0.11
141
+ Enabled: true
142
+ Minitest/LiteralAsActualArgument: # new in 0.10
143
+ Enabled: true
144
+ Minitest/MultipleAssertions: # new in 0.10
145
+ Enabled: false
146
+ Minitest/RefuteInDelta: # new in 0.10
147
+ Enabled: true
148
+ Minitest/RefuteKindOf: # new in 0.10
149
+ Enabled: true
150
+ Minitest/RefutePathExists: # new in 0.10
151
+ Enabled: true
152
+ Minitest/TestMethodName: # new in 0.10
153
+ Enabled: true
154
+ Minitest/UnreachableAssertion: # new in 0.14
155
+ Enabled: true
156
+ Minitest/UnspecifiedException: # new in 0.10
157
+ Enabled: true
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.8
7
+ before_install: gem install bundler -v 1.17.2
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ group(:test) do
8
+ gem('mocha')
9
+ gem('pry', '~> 0.13.1')
10
+ gem('pry-byebug', '~> 3.9.0')
11
+ gem('rubocop', '~> 1.25.1')
12
+ gem('rubocop-minitest', '~> 0.17.2')
13
+ end
14
+
15
+ gem('table_print')
16
+
17
+ # Specify your gem's dependencies in ruby_minitest_analyzer.gemspec
18
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_minitest_analyzer (0.1.1)
5
+ table_print (~> 1.5.7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ byebug (11.1.3)
12
+ coderay (1.1.3)
13
+ method_source (1.0.0)
14
+ minitest (5.15.0)
15
+ mocha (1.13.0)
16
+ parallel (1.21.0)
17
+ parser (3.1.1.0)
18
+ ast (~> 2.4.1)
19
+ pry (0.13.1)
20
+ coderay (~> 1.1)
21
+ method_source (~> 1.0)
22
+ pry-byebug (3.9.0)
23
+ byebug (~> 11.0)
24
+ pry (~> 0.13.0)
25
+ rainbow (3.1.1)
26
+ rake (10.5.0)
27
+ regexp_parser (2.2.1)
28
+ rexml (3.2.5)
29
+ rubocop (1.25.1)
30
+ parallel (~> 1.10)
31
+ parser (>= 3.1.0.0)
32
+ rainbow (>= 2.2.2, < 4.0)
33
+ regexp_parser (>= 1.8, < 3.0)
34
+ rexml
35
+ rubocop-ast (>= 1.15.1, < 2.0)
36
+ ruby-progressbar (~> 1.7)
37
+ unicode-display_width (>= 1.4.0, < 3.0)
38
+ rubocop-ast (1.16.0)
39
+ parser (>= 3.1.1.0)
40
+ rubocop-minitest (0.17.2)
41
+ rubocop (>= 0.90, < 2.0)
42
+ ruby-progressbar (1.11.0)
43
+ table_print (1.5.7)
44
+ unicode-display_width (2.1.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.17)
51
+ minitest (~> 5.0)
52
+ mocha
53
+ pry (~> 0.13.1)
54
+ pry-byebug (~> 3.9.0)
55
+ rake (~> 10.0)
56
+ rubocop (~> 1.25.1)
57
+ rubocop-minitest (~> 0.17.2)
58
+ ruby_minitest_analyzer!
59
+ table_print
60
+
61
+ BUNDLED WITH
62
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # Ruby Minitest Analyzer
2
+
3
+ Minitest uses Ruby classes, **if a Minitest class inherits from another class, it will also inherit its methods causing Minitest to run the parent's tests twice.**
4
+ In some cases, we want them to run them twice, but most of the time, we don't.
5
+
6
+ Ruby Minitest Analyzer will analyze your Minitest classes and detect any duplicate test run.
7
+
8
+ ![Untitled-2022-03-13-1106](https://user-images.githubusercontent.com/11672878/158284753-08ea962f-9ac6-46a6-acec-e2464a258a41.png)
9
+
10
+ This Library is explained in detail in [this Post](https://ignaciochiazzo.medium.com/dont-run-ruby-minitest-classess-twice-988645662cdb?source=friends_link&sk=4fafa2404be622156fd50cab519d5fd0)
11
+
12
+ ### Example
13
+ See the following case.
14
+
15
+ ```ruby
16
+ require "minitest"
17
+ require "minitest/autorun"
18
+
19
+ class ProductParentTest < Minitest::Test
20
+ def test_parent
21
+ puts __method__
22
+ end
23
+ end
24
+
25
+ class ProductTest < ProductParentTest
26
+ def test_1
27
+ puts __method__
28
+ end
29
+
30
+ def test_2
31
+ puts __method__
32
+ end
33
+ end
34
+ ```
35
+ How many tests will we run if we run the `ProductTest` tests? 4! (yes, four). Try it!
36
+
37
+ ```console
38
+ # Running:
39
+
40
+ test_2
41
+ test_parent
42
+ test_1
43
+ test_parent
44
+
45
+ Finished in 0.001218s, 3284.0724 runs/s, 0.0000 assertions/s.
46
+ 4 runs, 0 assertions, 0 failures, 0 errors, 0 skips
47
+ ```
48
+
49
+ The reason is that `ProductTest` is a subclass of `ProductParentTest`.
50
+
51
+ More details in [this Post](https://ignaciochiazzo.medium.com/dont-run-ruby-minitest-classess-twice-988645662cdb?source=friends_link&sk=4fafa2404be622156fd50cab519d5fd0)
52
+
53
+ ## Installation
54
+
55
+ Add this line to your Application's Gemfile:
56
+
57
+ ```ruby
58
+ gem 'ruby_minitest_analyzer'
59
+ ```
60
+
61
+ And then execute:
62
+
63
+ $ bundle
64
+
65
+ Or install it yourself as:
66
+
67
+ $ gem install ruby_minitest_analyzer
68
+
69
+ ### How can I run the analyzer on my Application?
70
+
71
+ 1) Create a new test `minitest_analyzer.rb` file.
72
+ 2) Create a method `require_all_test_files` requiring all the tests files, including
73
+ the files needed to run the tests e.g. `test_helper.rb`. See the example below.
74
+ 3) Call `::RubyMinitestAnalyzer.run!(nil)`.
75
+ 4) Run the file.
76
+
77
+ <details>
78
+ <summary> minitest_analyzer.rb example:</summary>
79
+
80
+ ```ruby
81
+ # I placed this file within /test
82
+
83
+ require_relative 'test_helper.rb'
84
+ require 'ruby_minitest_analyzer'
85
+
86
+ def require_all_files
87
+ # require test_helpers
88
+ require_relative("test_helper")
89
+
90
+ # require tests classes
91
+ Dir[File.expand_path('**/*.rb', __dir__)].each do |f|
92
+ require_relative(f)
93
+ end
94
+ end
95
+
96
+ require_all_files
97
+ ::RubyMinitestAnalyzer.run!(nil)
98
+ ```
99
+ </details>
100
+
101
+ Running the file example:
102
+
103
+ ```console
104
+ ➜ rails-minitest-analyzer git:(main) ✗ ruby minitest.rb
105
+ ---------------Setting up---------------
106
+ Requiring files...
107
+ Total of 6 test classes to analyze.
108
+ ---------------Setup finished! Ready to analyze the tests---------------
109
+ Analyzing!
110
+
111
+ Analyzed a total of 6 classes.
112
+
113
+ * Total duplicated tests that can be removed: 12
114
+ * Total classes with duplicated tests: 3
115
+
116
+ Classes that run the tests multiple times:
117
+
118
+ CLASS NAME | CLASS_TEST_METHODS_COUNT | CLASS_DESCENDANT_COUNT | EXTRA_TESTS_EXECUTIONS_COUNT
119
+ ----------------|--------------------------|------------------------|------------------------------
120
+ GrandParentTest | 1 | 5 | 5
121
+ Parent1Test | 2 | 2 | 4
122
+ Parent2Test | 3 | 1 | 3
123
+
124
+ Finished!
125
+ ```
126
+
127
+ ## Development
128
+
129
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
130
+
131
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132
+
133
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_minitest_analyzer.
136
+
137
+ ## License
138
+
139
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop'
5
+ require 'rake/testtask'
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.libs << 'lib'
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ end
12
+
13
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ruby_minitest_analyzer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest'
4
+ require 'set'
5
+ require_relative 'single_test_class_summary'
6
+
7
+ class MinitestAnalyzer < Minitest::Test
8
+ class << self
9
+ def analyze
10
+ minitest_classes = minitest_classes_set
11
+ duplicated_suites = {}
12
+
13
+ minitest_classes.each do |klass|
14
+ analyze_class(klass, duplicated_suites, minitest_classes)
15
+ end
16
+
17
+ print_analyzed_class(minitest_classes)
18
+
19
+ duplicated_suites
20
+ end
21
+
22
+ private
23
+
24
+ def print_analyzed_class(minitest_classes)
25
+ puts "Analyzed a total of #{minitest_classes.count} classes.\n\n"
26
+ end
27
+
28
+ def analyze_class(klass, duplicated_suites_acc, minitest_classes)
29
+ klass_runnable_tests = tests_methods(klass)
30
+
31
+ # If the parent doesn't have any runnable tests then it does not have duplicated.
32
+ return unless klass_runnable_tests.count.positive?
33
+
34
+ klass.ancestors.each do |klass_parent|
35
+ # klass_parent
36
+ # klass
37
+
38
+ next if klass == klass_parent
39
+
40
+ # skip if the ancestor is not a minitest class
41
+ next unless minitest_classes.include?(klass_parent)
42
+
43
+ klass_parent_runnable_tests = tests_methods(klass_parent)
44
+
45
+ # If the parent doesn't have any runnable test then it does not have duplicated.
46
+ next unless klass_parent_runnable_tests.count.positive?
47
+
48
+ # klass_parent and klass have runnable tests, there are duplicated tests.
49
+ add_duplicated_test_to_hash(klass, klass_parent, klass_parent_runnable_tests, duplicated_suites_acc)
50
+ end
51
+ end
52
+
53
+ def add_duplicated_test_to_hash(klass, klass_parent, klass_parent_runnable_tests, hash)
54
+ if hash[klass_parent.name]
55
+ info = hash[klass_parent.name]
56
+ info.class_descendant_count += 1
57
+ info.extra_tests_executions_count += info.runnable_tests_count
58
+ info.add_subclass(klass)
59
+ info
60
+ else
61
+ tests_count = klass_parent_runnable_tests.count
62
+ hash_info = SingleTestClassSummary.new(
63
+ klass: klass_parent,
64
+ runnable_tests_count: tests_count,
65
+ class_descendant_count: 1,
66
+ extra_tests_executions_count: tests_count,
67
+ subclasses: [klass]
68
+ )
69
+
70
+ hash[klass_parent.name] = hash_info
71
+ end
72
+ end
73
+
74
+ def minitest_classes_set
75
+ @minitest_classes_set ||= Minitest::Runnable.runnables.reject { |s| s.runnable_methods.empty? }
76
+ end
77
+
78
+ def tests_methods(klass)
79
+ re = /test_/
80
+ klass.public_instance_methods(false).grep(re).map(&:to_s)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MinitestAnalyzerConfig
4
+ def initialize(required_classes_paths: [], test_files_locations_paths: [], exempted_test_file_locations_paths: [])
5
+ @required_classes_paths = required_classes_paths
6
+ @test_files_locations_paths = test_files_locations_paths
7
+ @exempted_test_file_locations_paths = exempted_test_file_locations_paths
8
+ end
9
+
10
+ # An array of String containing the paths of all the required classes to run the tests.
11
+ # E.g. ["../../../test/test_helper"]
12
+ attr_reader :required_classes_paths
13
+
14
+ # An array of String containing the paths of all the tests classes.
15
+ # E.g: Dir["test/**/*.rb"]
16
+ attr_reader :test_files_locations_paths
17
+
18
+ # An array of String containing the paths for all the files within test_file_locations
19
+ # that are exempted from being required.
20
+ attr_reader :exempted_test_file_locations_paths
21
+
22
+ def setup
23
+ print_tests_stats do
24
+ require_all_test_files
25
+ end
26
+ end
27
+
28
+ protected
29
+
30
+ def require_all_test_files
31
+ # require required_classes
32
+ required_classes_paths.each do |f|
33
+ require_relative_file(f)
34
+ end
35
+
36
+ # require test classes
37
+ test_files_locations_paths.each do |f|
38
+ next if f == current_location_source
39
+ next if exempted_test_file_locations_paths.include?(f)
40
+
41
+ require_relative_file(f)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def require_relative_file(file_path)
48
+ require_relative(file_path)
49
+ rescue LoadError => e
50
+ puts "There was an error requiring a file: #{file_path}"
51
+ raise e
52
+ end
53
+
54
+ def print_tests_stats
55
+ puts "#{delimiter}Setting up#{delimiter}"
56
+ puts 'Requiring files...'
57
+ minitest_classes = yield
58
+ print_analyzer_stats(minitest_classes)
59
+ puts "#{delimiter}Setup finished! Ready to analyze the tests#{delimiter}\n"
60
+ end
61
+
62
+ def delimiter
63
+ '-' * 15
64
+ end
65
+
66
+ def print_analyzer_stats(minitest_classes)
67
+ puts "Total of #{minitest_classes.count} test classes to analyze. \n"
68
+ end
69
+
70
+ def current_location_source
71
+ # Returns the current file location
72
+ self.class.instance_method(:setup).source_location&.first
73
+ end
74
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SingleTestClassSummary
4
+ # klass -> The test class analyzed
5
+ # runnable_tests_count -> The number of tests methods within the class.
6
+ # class_descendant_count -> The number of extra runs of the whole test class (AKA as the number of descendants).
7
+ # extra_tests_execution_run -> The number of tests that run extra. E.G.
8
+
9
+ def initialize(klass:, runnable_tests_count:, class_descendant_count:, extra_tests_executions_count:, subclasses:)
10
+ @klass = klass
11
+ @runnable_tests_count = runnable_tests_count
12
+ @class_descendant_count = class_descendant_count
13
+ @extra_tests_executions_count = extra_tests_executions_count
14
+ @subclasses = subclasses
15
+ end
16
+
17
+ attr_accessor :klass, :runnable_tests_count, :class_descendant_count, :extra_tests_executions_count
18
+ attr_reader :subclasses
19
+
20
+ def add_subclass(subc)
21
+ @subclasses << subc
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'table_print'
4
+
5
+ class TestSummaryPresenter
6
+ def initialize(duplicated_suites)
7
+ @duplicated_suites = duplicated_suites
8
+ @total_extra_classes = @duplicated_suites.count
9
+ @total_extra_tests = total_extra_tests_across_suites
10
+ end
11
+
12
+ def present
13
+ puts "* Total duplicated tests that can be removed: #{@total_extra_tests}"
14
+ puts "* Total classes with duplicated tests: #{@total_extra_classes} "
15
+ print_table_stats
16
+ puts "\n\n"
17
+ end
18
+
19
+ private
20
+
21
+ def print_table_stats
22
+ if @duplicated_suites.empty?
23
+ puts 'Nice Job! No duplicated tests found!!!'
24
+ return
25
+ end
26
+ puts "\nClasses that run the tests multiple times: \n\n"
27
+
28
+ list = @duplicated_suites.map { |_klass_name, summary| summary }
29
+ tp.set :max_width, 40
30
+ tp(
31
+ list,
32
+ { 'Class Name': ->(s) { demodulize_class(s.klass) } },
33
+ { class_test_methods_count: ->(s) { s.runnable_tests_count } },
34
+ 'class_descendant_count',
35
+ 'extra_tests_executions_count',
36
+ Class: ->(s) { s.klass }
37
+ )
38
+ end
39
+
40
+ def demodulize_class(klass)
41
+ klass_name = klass.name
42
+ if klass_name.respond_to?(:demodulize)
43
+ klass_name.demodulize
44
+ else
45
+ klass_name
46
+ end
47
+ end
48
+
49
+ # Calculates all the extra tests that might be removed.
50
+ def total_extra_tests_across_suites
51
+ @duplicated_suites.sum { |_klass, info| info.extra_tests_executions_count }
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyMinitestAnalyzer
4
+ VERSION = '0.1.1'
5
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_minitest_analyzer/version'
4
+ require_relative 'ruby_minitest_analyzer/minitest_analyzer_config'
5
+ require_relative 'ruby_minitest_analyzer/minitest_analyzer'
6
+ require_relative 'ruby_minitest_analyzer/test_summary_presenter'
7
+
8
+ module RubyMinitestAnalyzer
9
+ def self.run!(minitest_analyzer_config = nil, should_exit_process: false)
10
+ load_test_files(minitest_analyzer_config)
11
+
12
+ # Analyze the data and print the results
13
+ puts "Analyzing!\n\n"
14
+ duplicated_suites_data = MinitestAnalyzer.analyze
15
+ presenter = TestSummaryPresenter.new(duplicated_suites_data)
16
+ presenter.present
17
+ Process.exit! if should_exit_process
18
+ puts 'Finished'
19
+ end
20
+
21
+ def self.load_test_files(minitest_analyzer_config)
22
+ # Load all the tests and required files such as test_helper.rb
23
+ if minitest_analyzer_config.nil?
24
+ puts('Not requiring files within the gem since tests files are loaded outside')
25
+ else
26
+ minitest_analyzer_config.setup
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ruby_minitest_analyzer/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ruby_minitest_analyzer'
9
+ spec.version = RubyMinitestAnalyzer::VERSION
10
+ spec.authors = ['Ignacio Chiazzo Cardarello']
11
+ spec.email = ['ignaciochiazzo@gmail.com']
12
+ spec.summary = ' Minitest Ruby Tests Analyzer .'
13
+ spec.description = ' Analyzes duplicated tests in Ruby Minitest applications'
14
+ spec.homepage = 'https://github.com/ignacio-chiazzo/ruby-minitest-analyzer'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ # spec.metadata["homepage_uri"] = spec.homepage
23
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
24
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ else
26
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
27
+ 'public gem pushes.'
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = 'exe'
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.add_development_dependency 'bundler', '~> 1.17'
40
+ spec.add_development_dependency 'minitest', '~> 5.0'
41
+ spec.add_development_dependency 'rake', '~> 10.0'
42
+ spec.add_dependency 'table_print', '~> 1.5.7'
43
+ spec.metadata['rubygems_mfa_required'] = 'true'
44
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_minitest_analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ignacio Chiazzo Cardarello
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: table_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.7
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.7
69
+ description: " Analyzes duplicated tests in Ruby Minitest applications"
70
+ email:
71
+ - ignaciochiazzo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".circleci/config.yml"
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/ruby_minitest_analyzer.rb
89
+ - lib/ruby_minitest_analyzer/minitest_analyzer.rb
90
+ - lib/ruby_minitest_analyzer/minitest_analyzer_config.rb
91
+ - lib/ruby_minitest_analyzer/single_test_class_summary.rb
92
+ - lib/ruby_minitest_analyzer/test_summary_presenter.rb
93
+ - lib/ruby_minitest_analyzer/version.rb
94
+ - ruby_minitest_analyzer.gemspec
95
+ homepage: https://github.com/ignacio-chiazzo/ruby-minitest-analyzer
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ rubygems_mfa_required: 'true'
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.2.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Minitest Ruby Tests Analyzer .
119
+ test_files: []