factory_sloth 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 93e7cdb1f418a30bf140f3b28ce646bcbf9d46302b45bb105948c32ceee042ff
4
+ data.tar.gz: b019faddee1c76fa58feb3129df86b6af87087c12f94ce8daddfaa5cfe14408a
5
+ SHA512:
6
+ metadata.gz: eab4ae49740dc1c9ded502ea5af2558e3edc441bf7a5527f8d382ae7bb71da9dd0bac816beb75572b1c3afdc18d89c7c402e567d90c1302b95b5232f293a43a9
7
+ data.tar.gz: 6c184366974c8428d81f800d063c10a243c8c7989154f9c7b5dfcb27651af85723fcd04d2b24d1b84819f62ab5150eb3ee3d52c5bb8bea950c56b5ff744ba484
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2023-05-14
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in factory_sloth.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "simplecov-cobertura", "~> 2.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ factory_sloth (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ docile (1.4.0)
11
+ rake (13.0.6)
12
+ rexml (3.2.5)
13
+ rspec (3.12.0)
14
+ rspec-core (~> 3.12.0)
15
+ rspec-expectations (~> 3.12.0)
16
+ rspec-mocks (~> 3.12.0)
17
+ rspec-core (3.12.2)
18
+ rspec-support (~> 3.12.0)
19
+ rspec-expectations (3.12.3)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.12.0)
22
+ rspec-mocks (3.12.5)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-support (3.12.0)
26
+ simplecov (0.22.0)
27
+ docile (~> 1.1)
28
+ simplecov-html (~> 0.11)
29
+ simplecov_json_formatter (~> 0.1)
30
+ simplecov-cobertura (2.1.0)
31
+ rexml
32
+ simplecov (~> 0.19)
33
+ simplecov-html (0.12.3)
34
+ simplecov_json_formatter (0.1.4)
35
+
36
+ PLATFORMS
37
+ arm64-darwin-21
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ factory_sloth!
42
+ rake (~> 13.0)
43
+ rspec (~> 3.0)
44
+ simplecov-cobertura (~> 2.1)
45
+
46
+ BUNDLED WITH
47
+ 2.4.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Janosch Müller
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,72 @@
1
+ # FactorySloth 🦥
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/factory_sloth.svg)](http://badge.fury.io/rb/factory_sloth)
4
+ [![Build Status](https://github.com/jaynetics/factory_sloth/actions/workflows/tests.yml/badge.svg)](https://github.com/jaynetics/factory_sloth/actions)
5
+
6
+ `factory_sloth` is too lazy to write to the database.
7
+
8
+ It finds unnecessary [factory_bot](https://github.com/thoughtbot/factory_bot) `create` calls and replaces them with less laborious `build` or `build_stubbed` calls to speed up your test suite.
9
+
10
+ ## Installation
11
+
12
+ `bundle add factory_sloth` or `gem install factory_sloth`.
13
+
14
+ ## Usage
15
+
16
+ Output of `factory_sloth --help`:
17
+
18
+ ```
19
+ Usage: factory_sloth [path1, path2, ...] [options]
20
+
21
+ Examples:
22
+ factory_sloth # run for all specs
23
+ factory_sloth spec/models
24
+ factory_sloth spec/foo_spec.rb spec/bar_spec.rb
25
+
26
+ Options:
27
+ -f, --force Ignore ./.factory_sloth_done
28
+ -l, --lint Dont fix, just list bad create calls
29
+ -v, --version Show gem version
30
+ -h, --help Show this help
31
+ ```
32
+
33
+ `factory_sloth` runs the changed specs to see if they still work. This takes a while for all possible changes - usually multiple times as long as the specs would normally take to run! For this reason, the gem creates a `.factory_sloth_done` file. This is a list of specs that have already been processed. It makes it possible to interrupt the program and continue later. You can delete this file to start over or ignore it with `-f`.
34
+
35
+ While running, `factory_sloth` produces output like this:
36
+
37
+ ```
38
+ Processing spec/features/sign_up_spec.rb ...
39
+ 🟡 2 create calls found, 0 replaced
40
+
41
+ Processing spec/lib/string_ext_spec.rb ...
42
+ ⚪️ 0 create calls found, 0 replaced
43
+
44
+ Processing spec/models/user_spec.rb ...
45
+ - create in line 3 can be replaced with build
46
+ - create in line 4 can be replaced with build
47
+ 🟢 3 create calls found, 2 replaced
48
+
49
+ Processing spec/weird_dir/crazy_spec.rb ...
50
+ - create in line 8 can be replaced with build_stubbed
51
+ 🔴 33 create calls found, 0 replaced (conflict)
52
+ ```
53
+
54
+ The `conflict` case is rare. It only happens if individual examples were green after changing them, but at least one example failed when evaluating the whole file after all changes. This probably means that some other example was red even before making changes, or that something else is wrong with this spec file, e.g. some examples depend on other examples' side effects.
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jaynetics/factory_sloth.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
67
+
68
+ ## Similar projects
69
+
70
+ - [factory_trace](https://github.com/djezzzl/factory_trace) finds unused factories
71
+ - [rspectre](https://github.com/dgollahon/rspectre) finds unused test setup
72
+ - [rubocop-factory_bot](https://github.com/rubocop/rubocop-factory_bot) provides rubocop linters for factories
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/exe/factory_sloth ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative File.join(__dir__, '..', 'lib', 'factory_sloth')
4
+
5
+ FactorySloth::CLI.call
@@ -0,0 +1,55 @@
1
+ require 'optparse'
2
+
3
+ module FactorySloth
4
+ module CLI
5
+ extend self
6
+
7
+ def call(argv = ARGV)
8
+ args = option_parser.parse!(argv)
9
+ specs = SpecPicker.call(paths: args)
10
+ forced_files = @force ? specs : args
11
+ bad_specs = FileProcessor.call(files: specs, forced_files: forced_files, dry_run: @lint)
12
+
13
+ if @lint && bad_specs.any?
14
+ warn "Found unnecessary create calls in:\n#{bad_specs.join("\n")}"
15
+ exit 1
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def option_parser
22
+ OptionParser.new do |opts|
23
+ opts.banner = <<~SH
24
+ Usage: factory_sloth [path1, path2, ...] [options]
25
+
26
+ Examples:
27
+ factory_sloth # run for all specs
28
+ factory_sloth ./spec/models
29
+ factory_sloth ./spec/foo_spec.rb ./spec/bar_spec.rb
30
+
31
+ SH
32
+
33
+ opts.separator 'Options:'
34
+
35
+ opts.on('-f', '--force', "Ignore #{DoneTracker.file}") do
36
+ @force = true
37
+ end
38
+
39
+ opts.on('-l', '--lint', 'Dont fix, just list bad create calls') do
40
+ @lint = true
41
+ end
42
+
43
+ opts.on('-v', '--version', 'Show gem version') do
44
+ puts "factory_sloth #{FactorySloth::VERSION}"
45
+ exit
46
+ end
47
+
48
+ opts.on('-h', '--help', 'Show this help') do
49
+ puts opts
50
+ exit
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,62 @@
1
+ require 'tempfile'
2
+
3
+ class FactorySloth::CodeMod
4
+ attr_reader :change_count, :create_count, :ok, :original_code, :patched_code
5
+ alias_method :ok?, :ok
6
+
7
+ def self.call(code)
8
+ new(code).tap(&:call)
9
+ end
10
+
11
+ def initialize(code)
12
+ self.change_count = 0
13
+ self.original_code = code
14
+ self.patched_code = code
15
+ end
16
+
17
+ def call
18
+ create_calls = FactorySloth::CreateCallFinder.call(code: original_code)
19
+
20
+ # Performance note: it might be faster to write ALL possible patches for a
21
+ # given spec file to tempfiles first, and then run them all in a single
22
+ # rspec call. However, this would make it impossible to use `--fail-fast`,
23
+ # and might make examples fail that are not as idempotent as they should be.
24
+ create_calls.sort_by { |line, col| [-line, -col] }.each do |line, col|
25
+ try_patch(line, col, 'build') || try_patch(line, col, 'build_stubbed')
26
+ end
27
+
28
+ # validate whole spec after changes, e.g. to detect side-effects
29
+ self.ok = spec_code_passes?(patched_code)
30
+ self.change_count = 0 unless ok?
31
+ self.patched_code = original_code unless ok?
32
+ self.create_count = create_calls.count
33
+ end
34
+
35
+ def changed?
36
+ change_count > 0
37
+ end
38
+
39
+ private
40
+
41
+ attr_writer :change_count, :create_count, :ok, :original_code, :patched_code
42
+
43
+ def try_patch(line, col, variant)
44
+ new_patched_code =
45
+ patched_code.sub(/\A(?:.*\n){#{line - 1}}.{#{col}}\Kcreate/, variant)
46
+ if spec_code_passes?(new_patched_code, line: line)
47
+ puts "- create in line #{line} can be replaced with #{variant}"
48
+ self.patched_code = new_patched_code
49
+ self.change_count += 1
50
+ end
51
+ end
52
+
53
+ def spec_code_passes?(spec_code, line: nil)
54
+ tempfile = Tempfile.new
55
+ tempfile.write(spec_code)
56
+ tempfile.close
57
+ path = [tempfile.path, line].compact.map(&:to_s).join(':')
58
+ result = !!system("bundle exec rspec #{path} --fail-fast &> /dev/null")
59
+ tempfile.unlink
60
+ result
61
+ end
62
+ end
@@ -0,0 +1,35 @@
1
+ require 'ripper'
2
+
3
+ class FactorySloth::CreateCallFinder < Ripper
4
+ attr_reader :locations
5
+
6
+ def self.call(code:)
7
+ new(code).tap(&:parse).locations
8
+ end
9
+
10
+ def initialize(...)
11
+ super
12
+ @locations = []
13
+ end
14
+ private_class_method :new
15
+
16
+ def on_ident(name, *)
17
+ [lineno, column] if %w[create create_list create_pair].include?(name)
18
+ end
19
+
20
+ def on_call(mod, _, loc, *)
21
+ @locations << loc if loc.instance_of?(Array) && mod == 'FactoryBot'
22
+ end
23
+
24
+ def on_command_call(mod, _, loc, *)
25
+ @locations << loc if loc.instance_of?(Array) && mod == 'FactoryBot'
26
+ end
27
+
28
+ def on_fcall(loc, *)
29
+ @locations << loc if loc.instance_of?(Array)
30
+ end
31
+
32
+ def on_vcall(loc, *)
33
+ @locations << loc if loc.instance_of?(Array)
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module FactorySloth::DoneTracker
2
+ extend self
3
+
4
+ def done?(path)
5
+ done.include?(normalize(path))
6
+ end
7
+
8
+ def mark_as_done(path)
9
+ normalized_path = normalize(path)
10
+ done << normalized_path
11
+ File.open(file, 'a') { |f| f.puts(normalized_path) }
12
+ end
13
+
14
+ def reset
15
+ File.unlink(file) if File.exist?(file)
16
+ done.clear
17
+ end
18
+
19
+ def file
20
+ './.factory_sloth_done'
21
+ end
22
+
23
+ private
24
+
25
+ def normalize(path)
26
+ path.start_with?('./') || path.start_with?('/') ? path : "./#{path}"
27
+ end
28
+
29
+ def done
30
+ @done ||= File.exist?(file) ? File.readlines(file, chomp: true) : []
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ module FactorySloth
2
+ module FileProcessor
3
+ extend self
4
+
5
+ def call(files:, forced_files: [], dry_run: false)
6
+ files.select do |path|
7
+ puts "Processing #{path} ..."
8
+
9
+ if DoneTracker.done?(path) && !forced_files.include?(path)
10
+ puts "🔵 Skipped (marked as done in #{DoneTracker.file})", ''
11
+ next
12
+ end
13
+
14
+ bad_creates_found = process(path, dry_run: dry_run)
15
+ DoneTracker.mark_as_done(path)
16
+ bad_creates_found
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def process(path, dry_run:)
23
+ code = File.read(path)
24
+ result = CodeMod.call(code)
25
+ unless dry_run
26
+ File.write(path, result.patched_code) if result.changed?
27
+ end
28
+ puts result_message(result, dry_run), ''
29
+ result.changed?
30
+ end
31
+
32
+ def result_message(result, dry_run)
33
+ stats = "#{result.create_count} create calls found, "\
34
+ "#{result.change_count} #{dry_run ? 'replaceable' : 'replaced'}"
35
+
36
+ return "🔴 #{stats} (conflict)" unless result.ok?
37
+
38
+ if result.create_count == 0
39
+ "⚪️ #{stats}"
40
+ elsif result.change_count == 0
41
+ "🟡 #{stats}"
42
+ else
43
+ "🟢 #{stats}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module FactorySloth::SpecPicker
2
+ extend self
3
+
4
+ def call(paths:)
5
+ paths = ['.'] if paths.empty?
6
+
7
+ paths.each_with_object([]) do |path, acc|
8
+ if File.directory?(path)
9
+ acc.concat(Dir["#{path.chomp('/')}/**/*_spec.rb"])
10
+ elsif File.exist?(path)
11
+ acc << path
12
+ else
13
+ raise ArgumentError, "no such file: #{path}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module FactorySloth
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,9 @@
1
+ module FactorySloth; end
2
+
3
+ require_relative 'factory_sloth/cli'
4
+ require_relative 'factory_sloth/code_mod'
5
+ require_relative 'factory_sloth/create_call_finder'
6
+ require_relative 'factory_sloth/done_tracker'
7
+ require_relative 'factory_sloth/file_processor'
8
+ require_relative 'factory_sloth/spec_picker'
9
+ require_relative 'factory_sloth/version'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_sloth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Janosch Müller
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - janosch84@gmail.com
16
+ executables:
17
+ - factory_sloth
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - exe/factory_sloth
29
+ - lib/factory_sloth.rb
30
+ - lib/factory_sloth/cli.rb
31
+ - lib/factory_sloth/code_mod.rb
32
+ - lib/factory_sloth/create_call_finder.rb
33
+ - lib/factory_sloth/done_tracker.rb
34
+ - lib/factory_sloth/file_processor.rb
35
+ - lib/factory_sloth/spec_picker.rb
36
+ - lib/factory_sloth/version.rb
37
+ homepage: https://github.com/jaynetics/factory_sloth
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/jaynetics/factory_sloth
42
+ source_code_uri: https://github.com/jaynetics/factory_sloth
43
+ changelog_uri: https://github.com/jaynetics/factory_sloth/blob/main/CHANGELOG.md
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.7.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.13
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Find and replace unnecessary factory_bot create calls.
63
+ test_files: []