rspec-ordering-mttf 0.1.0

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: ab6f52747514ec2196e536585a6eddc495659ca1df08fbc1f4f36841dcbc7b27
4
+ data.tar.gz: 34603849812cd2b060df5bc3b637e5342382c9e5de6361ee2733b2492c9bd1a3
5
+ SHA512:
6
+ metadata.gz: f000b0d0cacb15544e10900924340b8d3c235ca97a6b7faf3229a007d0db5d4db1370dfa03b0b2a3f9aff12a08fb9e6c04f4a2f39408c9c00d36ea49b88d7f44
7
+ data.tar.gz: 60ff68191448b04924a22f2a71288b111f889916ed36b8b930670ea245f69345bdb8030e449c2da58a12a38203ab042b3095464f7334260e1edd676fe9d54aca
data/.dir-locals.el ADDED
@@ -0,0 +1,4 @@
1
+ ;;; Directory Local Variables
2
+ ;;; For more information see (info "(emacs) Directory Variables")
3
+
4
+ ((ruby-mode . ((flycheck-checker . ruby-standard))))
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ -I lib/rspec/ordering/mttf
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-06-21
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rspec-ordering-mttf.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "standard", "~> 1.3"
13
+
14
+ gem "guard-rspec", require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,100 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-ordering-mttf (0.1.0)
5
+ rspec
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ ffi (1.15.5)
14
+ formatador (1.1.0)
15
+ guard (2.18.0)
16
+ formatador (>= 0.2.4)
17
+ listen (>= 2.7, < 4.0)
18
+ lumberjack (>= 1.0.12, < 2.0)
19
+ nenv (~> 0.1)
20
+ notiffany (~> 0.0)
21
+ pry (>= 0.13.0)
22
+ shellany (~> 0.0)
23
+ thor (>= 0.18.1)
24
+ guard-compat (1.2.1)
25
+ guard-rspec (4.7.3)
26
+ guard (~> 2.1)
27
+ guard-compat (~> 1.1)
28
+ rspec (>= 2.99.0, < 4.0)
29
+ listen (3.7.1)
30
+ rb-fsevent (~> 0.10, >= 0.10.3)
31
+ rb-inotify (~> 0.9, >= 0.9.10)
32
+ lumberjack (1.2.8)
33
+ method_source (1.0.0)
34
+ minitest (5.16.1)
35
+ nenv (0.3.0)
36
+ notiffany (0.1.3)
37
+ nenv (~> 0.1)
38
+ shellany (~> 0.0)
39
+ parallel (1.22.1)
40
+ parser (3.1.2.0)
41
+ ast (~> 2.4.1)
42
+ pry (0.14.1)
43
+ coderay (~> 1.1)
44
+ method_source (~> 1.0)
45
+ rainbow (3.1.1)
46
+ rake (13.0.6)
47
+ rb-fsevent (0.11.1)
48
+ rb-inotify (0.10.1)
49
+ ffi (~> 1.0)
50
+ regexp_parser (2.5.0)
51
+ rexml (3.2.5)
52
+ rspec (3.11.0)
53
+ rspec-core (~> 3.11.0)
54
+ rspec-expectations (~> 3.11.0)
55
+ rspec-mocks (~> 3.11.0)
56
+ rspec-core (3.11.0)
57
+ rspec-support (~> 3.11.0)
58
+ rspec-expectations (3.11.0)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.11.0)
61
+ rspec-mocks (3.11.1)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.11.0)
64
+ rspec-support (3.11.0)
65
+ rubocop (1.29.1)
66
+ parallel (~> 1.10)
67
+ parser (>= 3.1.0.0)
68
+ rainbow (>= 2.2.2, < 4.0)
69
+ regexp_parser (>= 1.8, < 3.0)
70
+ rexml (>= 3.2.5, < 4.0)
71
+ rubocop-ast (>= 1.17.0, < 2.0)
72
+ ruby-progressbar (~> 1.7)
73
+ unicode-display_width (>= 1.4.0, < 3.0)
74
+ rubocop-ast (1.18.0)
75
+ parser (>= 3.1.1.0)
76
+ rubocop-performance (1.13.3)
77
+ rubocop (>= 1.7.0, < 2.0)
78
+ rubocop-ast (>= 0.4.0)
79
+ ruby-progressbar (1.11.0)
80
+ shellany (0.0.1)
81
+ standard (1.12.1)
82
+ rubocop (= 1.29.1)
83
+ rubocop-performance (= 1.13.3)
84
+ thor (1.2.1)
85
+ unicode-display_width (2.1.0)
86
+
87
+ PLATFORMS
88
+ arm64-darwin-21
89
+ x86_64-darwin-19
90
+ x86_64-linux
91
+
92
+ DEPENDENCIES
93
+ guard-rspec
94
+ minitest (~> 5.0)
95
+ rake (~> 13.0)
96
+ rspec-ordering-mttf!
97
+ standard (~> 1.3)
98
+
99
+ BUNDLED WITH
100
+ 2.3.15
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+ notification :emacs
5
+
6
+ # RSpec files
7
+ rspec = dsl.rspec
8
+ watch(rspec.spec_helper) { rspec.spec_dir }
9
+ watch(rspec.spec_support) { rspec.spec_dir }
10
+ watch(rspec.spec_files)
11
+
12
+ # Ruby files
13
+ ruby = dsl.ruby
14
+ watch(ruby.lib_files) { rspec.spec_dir }
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Brandon Duff
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,44 @@
1
+ # RSpec::Ordering::Mttf (Mean Time to Failure) [![Ruby](https://github.com/brandonduff/rspec-ordering-mttf/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/brandonduff/rspec-ordering-mttf/actions/workflows/main.yml)
2
+
3
+ A custom orderer for RSpec that optimizes for test latency (mean time to failure) over test throughput.
4
+
5
+ ## Why would I want this?
6
+
7
+ If you do Test-Driven Development, you likely implement some version of this algorithm manually all the time. It's likely you have too many tests to run them all on every change, so you decide which tests to run based on those that are likely to fail and how quickly they run. This orderer uses a basic set of heuristics to automate that work:
8
+
9
+ 1. Never-run tests first, in arbitrary order
10
+ 1. Group remaining tests by the date at which they most recently failed.
11
+ 1. Sort groups such that the most recent failure date is first, and never-failing tests are at the end.
12
+ 1. Within a group, run the fastest tests first.
13
+
14
+ These heuristics are taken from [JUnitMax](https://junit.org/junit4/javadoc/latest/org/junit/experimental/max/MaxCore.html).
15
+
16
+ ## Installation
17
+
18
+ Install the gem and add to the application's Gemfile by executing:
19
+
20
+ $ bundle add rspec-ordering-mttf
21
+
22
+ If bundler is not being used to manage dependencies, install the gem by executing:
23
+
24
+ $ gem install rspec-ordering-mttf
25
+
26
+ ## Usage
27
+
28
+ Call `RSpec::Ordering::Mttf.configure` with the RSpec configuration. This will configure a default global ordering of "mean time to failure." I expect to change these ergonomics soon, but I haven't yet decided how.
29
+
30
+ ## Development
31
+
32
+ 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.
33
+
34
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ This gem uses Standard for code styling.
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/brandonduff/rspec-ordering-mttf.
41
+
42
+ ## License
43
+
44
+ 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
+ begin
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ rescue LoadError
8
+ # Ignored
9
+ end
10
+
11
+ require "standard/rake"
12
+
13
+ task default: %i[spec standard]
data/TODO.md ADDED
@@ -0,0 +1,9 @@
1
+ - [x] Record metadata at end of run
2
+ - [x] Read metadata at start of run
3
+ - [x] Running in date groups, ordering by fastest
4
+ - [x] record and compare run time on examples
5
+ - [x] record run time on groups
6
+ - [x] merge on metadata, don't overwrite
7
+ - [x] sort the groups as well. our orderer will get called with groups and then with examples in the groups
8
+ - [x] parent groups will reflect the most relevant of their deepest children
9
+ - [x] refactoring: make RunResults a class instead of a hash. Should clean up a lot
@@ -0,0 +1,72 @@
1
+ module RSpec
2
+ module Ordering
3
+ module Mttf
4
+ class ExampleResultData
5
+ include Comparable
6
+
7
+ def self.from_example(example)
8
+ last_failed_date = if example.execution_result.status == :failed
9
+ RSpec.configuration.current_date
10
+ else
11
+ example.metadata[:last_failed_date]
12
+ end
13
+ new(status: example.execution_result.status,
14
+ last_failed_date: last_failed_date,
15
+ last_run_date: RSpec.configuration.current_date,
16
+ run_time: example.execution_result.run_time)
17
+ end
18
+
19
+ def self.from_example_metadata(example)
20
+ metadata = example.metadata
21
+ new(status: metadata[:status],
22
+ last_failed_date: metadata[:last_failed_date],
23
+ last_run_date: metadata[:last_run_date],
24
+ run_time: metadata[:last_run_time])
25
+ end
26
+
27
+ def self.add_examples_to_group_results(examples, group_results)
28
+ examples = Array(examples)
29
+ summed_run_time = examples.map(&:run_time).compact.sum
30
+ smallest_result = [group_results, examples.min].compact.min
31
+
32
+ new(
33
+ status: smallest_result.status,
34
+ last_failed_date: smallest_result.last_failed_date,
35
+ last_run_date: smallest_result.last_run_date,
36
+ run_time: (group_results&.run_time || 0) + summed_run_time
37
+ )
38
+ end
39
+
40
+ def initialize(status:, last_failed_date:, last_run_date:, run_time:)
41
+ @status = status
42
+ @last_failed_date = last_failed_date
43
+ @last_run_date = last_run_date
44
+ @run_time = run_time
45
+ end
46
+
47
+ attr_reader :status, :last_failed_date, :last_run_date, :run_time
48
+
49
+ def <=>(other)
50
+ if last_run_date.nil?
51
+ -1
52
+ elsif other.last_run_date.nil?
53
+ 1
54
+ elsif last_failed_date.nil? && !other.last_failed_date.nil?
55
+ 1
56
+ elsif other.last_failed_date.nil? && !last_failed_date.nil?
57
+ -1
58
+ elsif other.last_failed_date && last_failed_date &&
59
+ other.last_failed_date != last_failed_date
60
+ other.last_failed_date <=> last_failed_date
61
+ elsif run_time && !other.run_time
62
+ -1
63
+ elsif !run_time && other.run_time
64
+ 1
65
+ else
66
+ run_time <=> other.run_time
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,25 @@
1
+ module RSpec
2
+ module Ordering
3
+ module Mttf
4
+ class Orderer
5
+ def initialize(memory)
6
+ @run_results = memory.read
7
+ end
8
+
9
+ def order(items)
10
+ items.each do |example|
11
+ run_results.annotate_example(example)
12
+ end
13
+
14
+ items.sort_by do |example|
15
+ ExampleResultData.from_example_metadata(example)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :run_results
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module RSpec
2
+ module Ordering
3
+ module Mttf
4
+ class RunMemory
5
+ def initialize(filename)
6
+ @store = YAML::Store.new(filename)
7
+ @run_results = RunResults.new
8
+ end
9
+
10
+ def example_group_finished(summary_notification)
11
+ collect_result(summary_notification.group)
12
+ end
13
+
14
+ def stop(_reporter)
15
+ write
16
+ end
17
+
18
+ def read
19
+ store.transaction(true) do
20
+ RunResults.new(store["results"] || {})
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def write
27
+ existing = read
28
+ store.transaction do
29
+ store["results"] = existing.merge(run_results)
30
+ end
31
+ end
32
+
33
+ def collect_result(group)
34
+ run_results.record_group(group)
35
+ end
36
+
37
+ attr_reader :store, :run_results
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,54 @@
1
+ module RSpec
2
+ module Ordering
3
+ module Mttf
4
+ class RunResults
5
+ def initialize(hash = {})
6
+ @results = hash
7
+ end
8
+
9
+ def merge(other)
10
+ results.merge(other.results)
11
+ end
12
+
13
+ def record_group(group)
14
+ new_examples = record_examples(group)
15
+ updated_group_results = update_group(group, new_examples)
16
+ update_group(group.superclass, updated_group_results) unless group.top_level?
17
+ end
18
+
19
+ def annotate_example(example)
20
+ if (example_run_result = self[example])
21
+ example.metadata[:last_run_date] = example_run_result.last_run_date
22
+ example.metadata[:last_failed_date] = example_run_result.last_failed_date
23
+ example.metadata[:last_run_time] = example_run_result.run_time
24
+ end
25
+ end
26
+
27
+ def [](example)
28
+ results[example.id]
29
+ end
30
+
31
+ protected
32
+
33
+ attr_reader :results
34
+
35
+ private
36
+
37
+ def []=(example, value)
38
+ results[example.id] = value
39
+ end
40
+
41
+ def update_group(group, new_examples)
42
+ self[group] = ExampleResultData
43
+ .add_examples_to_group_results(new_examples, self[group])
44
+ end
45
+
46
+ def record_examples(group)
47
+ group.examples.map do |example|
48
+ self[example] = ExampleResultData.from_example(example)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module Ordering
5
+ module Mttf
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mttf/version"
4
+ require_relative "mttf/run_results"
5
+ require_relative "mttf/run_memory"
6
+ require_relative "mttf/orderer"
7
+ require_relative "mttf/example_result_data"
8
+ require "yaml/store"
9
+
10
+ module RSpec
11
+ module Ordering
12
+ module Mttf
13
+ def self.configure(config, current_date: Date.today, previous_run_data: ".rspec-run-data.store")
14
+ run_memory = RunMemory.new(previous_run_data)
15
+ config.register_ordering(:global, Orderer.new(run_memory))
16
+
17
+ config.reporter.register_listener(run_memory, :example_group_finished)
18
+ config.reporter.register_listener(run_memory, :stop)
19
+
20
+ config.add_setting :current_date
21
+ config.current_date = current_date
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rspec/ordering/mttf/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rspec-ordering-mttf"
7
+ spec.version = RSpec::Ordering::Mttf::VERSION
8
+ spec.authors = ["Brandon Duff"]
9
+ spec.email = ["bduff@hey.com"]
10
+
11
+ spec.summary = "Order tests optimizing for Mean Time to Failure"
12
+ spec.homepage = "https://github.com/brandonduff/rspec-ordering-mttf"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/brandonduff/rspec-ordering-mttf"
18
+ spec.metadata["changelog_uri"] = "https://github.com/brandonduff/rspec-ordering-mttf/blob/main/changelog.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency "rspec"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,8 @@
1
+ module Rspec
2
+ module Ordering
3
+ module Mttf
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-ordering-mttf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Duff
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - bduff@hey.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".dir-locals.el"
35
+ - ".rspec"
36
+ - ".standard.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - Guardfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - TODO.md
45
+ - lib/rspec/ordering/mttf.rb
46
+ - lib/rspec/ordering/mttf/example_result_data.rb
47
+ - lib/rspec/ordering/mttf/orderer.rb
48
+ - lib/rspec/ordering/mttf/run_memory.rb
49
+ - lib/rspec/ordering/mttf/run_results.rb
50
+ - lib/rspec/ordering/mttf/version.rb
51
+ - rspec-ordering-mttf.gemspec
52
+ - sig/rspec/ordering/mttf.rbs
53
+ homepage: https://github.com/brandonduff/rspec-ordering-mttf
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://github.com/brandonduff/rspec-ordering-mttf
58
+ source_code_uri: https://github.com/brandonduff/rspec-ordering-mttf
59
+ changelog_uri: https://github.com/brandonduff/rspec-ordering-mttf/blob/main/changelog.md
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.6.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.1.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Order tests optimizing for Mean Time to Failure
79
+ test_files: []