selective-ruby-rspec 0.1.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: c3d785a9a11a8a066d3aed92bcdecda6f1cb52aa08eebc726c3697d194fb9205
4
+ data.tar.gz: 83ebdde2f7d64514c3465ed69981b576ab0bbe8cbeb0a9aea38b2dfa9d34b09e
5
+ SHA512:
6
+ metadata.gz: f7b08bb3a657876e3bff4699d555769eba60e2ebd144ed7b25f765aa235463bb17173eacf456f6422f97d0e082f5f4c6c692eaa5237c8cac1f15ce64e43135f6
7
+ data.tar.gz: 8e59c41dba74ba550001d117d717d04e0b20e7f02f57040e3bb176040c3a92c5db1fb843aea2baf0ebbd6c7a64ef94e31c64d2dd31238c87efa0797516c120aa
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Selective
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,130 @@
1
+ module Selective
2
+ module Ruby
3
+ module RSpec
4
+ module Monkeypatches
5
+ MAP = {
6
+ "BaseTextFormatter" => [:message, :dump_pending, :seed, :close],
7
+ "ProgressFormatter" => [:start_dump],
8
+ "DocumentationFormatter" => [:message]
9
+ }
10
+
11
+ MAP.each do |module_name, methods|
12
+ m = Selective::Ruby::RSpec::Monkeypatches.const_set(module_name, Module.new)
13
+ methods.each do |method|
14
+ m.define_method(method) do |*args|
15
+ end
16
+ end
17
+ end
18
+
19
+ module Reporter
20
+ def finish
21
+ return if Selective::Ruby::Core::Controller.suppress_reporting?
22
+ # This handles the scenario of no tests to run
23
+ start(nil) if @start.nil?
24
+ if ::RSpec.configuration.profile_examples
25
+ puts "\n\nExample group profiling is not supported with Selective and is now disabled.\n\n"
26
+ ::RSpec.configuration.profile_examples = nil
27
+ end
28
+
29
+ super
30
+ end
31
+
32
+ def start(*args)
33
+ return if Selective::Ruby::Core::Controller.suppress_reporting?
34
+
35
+ super
36
+ end
37
+
38
+ def register_listener(listener, *notifications)
39
+ # Prevent double registration of listeners with
40
+ # the same output path.
41
+ filtered_notifications = notifications.reject do |n|
42
+ @listeners[n.to_sym].any? do |l|
43
+ next false unless [l, listener].all? do |x|
44
+ x.respond_to?(:output) && x.output.respond_to?(:path)
45
+ end
46
+
47
+ # :nocov:
48
+ l.output.path == listener.output.path
49
+ # :nocov:
50
+ end
51
+ end
52
+
53
+ super(listener, *filtered_notifications)
54
+ end
55
+ end
56
+
57
+ module Configuration
58
+ attr_accessor :currently_loading_spec_file
59
+
60
+ def load_file_handling_errors(method, file)
61
+ self.currently_loading_spec_file = file
62
+ super
63
+ ensure
64
+ self.currently_loading_spec_file = nil
65
+ end
66
+
67
+ def get_files_to_run(*args)
68
+ super.reject { |f| loaded_spec_files.member?(f) }
69
+ end
70
+ end
71
+
72
+ module World
73
+ attr_accessor :example_map
74
+
75
+ def initialize(*args)
76
+ super
77
+ @example_map = {}
78
+ end
79
+ end
80
+
81
+ module Runner
82
+ attr_writer :options
83
+ end
84
+
85
+ module Example
86
+ def initialize(*args)
87
+ super
88
+ ::RSpec.world.example_map[id] = example_group
89
+ end
90
+
91
+ def run(*args)
92
+ @exception = nil
93
+ super
94
+ end
95
+ end
96
+
97
+ module MetaHashPopulator
98
+ def populate_location_attributes
99
+ user_metadata[:caller] ||= caller unless ::RSpec.configuration.currently_loading_spec_file.nil?
100
+ super
101
+ end
102
+
103
+ def file_path_and_line_number_from(backtrace)
104
+ return super if ::RSpec.configuration.currently_loading_spec_file.nil?
105
+
106
+ filtered = backtrace.find { |l| l.include? ::RSpec.configuration.currently_loading_spec_file }
107
+ filtered.nil? ? super : super([filtered])
108
+ end
109
+ end
110
+
111
+ def self.apply
112
+ ::RSpec::Support.require_rspec_core("formatters/base_text_formatter")
113
+
114
+ MAP.each do |module_name, _methods|
115
+ ::RSpec::Core::Formatters
116
+ .const_get(module_name)
117
+ .prepend(Selective::Ruby::RSpec::Monkeypatches.const_get(module_name))
118
+ end
119
+
120
+ ::RSpec::Core::Reporter.prepend(Selective::Ruby::RSpec::Monkeypatches::Reporter)
121
+ ::RSpec::Core::Configuration.prepend(Selective::Ruby::RSpec::Monkeypatches::Configuration)
122
+ ::RSpec::Core::World.prepend(Selective::Ruby::RSpec::Monkeypatches::World)
123
+ ::RSpec::Core::Runner.prepend(Selective::Ruby::RSpec::Monkeypatches::Runner)
124
+ ::RSpec::Core::Example.prepend(Selective::Ruby::RSpec::Monkeypatches::Example)
125
+ ::RSpec::Core::Metadata::HashPopulator.prepend(Selective::Ruby::RSpec::Monkeypatches::MetaHashPopulator)
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,140 @@
1
+ require "tempfile"
2
+ require "json"
3
+
4
+ module Selective
5
+ module Ruby
6
+ module RSpec
7
+ class RunnerWrapper
8
+ class TestManifestError < StandardError; end
9
+
10
+ attr_reader :args, :rspec_runner, :config
11
+
12
+ DEFAULT_SPEC_PATH = "./spec"
13
+
14
+ def initialize(args)
15
+ require "rspec/core"
16
+
17
+ Selective::Ruby::RSpec::Monkeypatches.apply
18
+ apply_rspec_configuration
19
+
20
+ args << "--format=progress" unless args.any? { |e| e.start_with?("--format") }
21
+ @args = args
22
+
23
+ @config = ::RSpec::Core::ConfigurationOptions.new(args)
24
+ if config.options[:files_or_directories_to_run].empty?
25
+ config.options[:files_or_directories_to_run] = [DEFAULT_SPEC_PATH]
26
+ end
27
+
28
+ @rspec_runner = ::RSpec::Core::Runner.new(@config)
29
+ @rspec_runner.setup($stderr, $stdout)
30
+ end
31
+
32
+ def manifest
33
+ output = nil
34
+ Tempfile.create("selective-rspec-dry-run") do |f|
35
+ quoted_paths = config.options[:files_or_directories_to_run].map { |path| "'#{path}'" }.join(" ")
36
+ output = `bundle exec selective exec rspec #{quoted_paths} --format=json --out=#{f.path} --dry-run`
37
+ JSON.parse(f.read).tap do |content|
38
+ if content["examples"].empty?
39
+ message = content["messages"]&.first
40
+ raise_test_manifest_error(message || "No examples found")
41
+ end
42
+ end
43
+ end
44
+ rescue JSON::ParserError => e
45
+ raise_test_manifest_error(e.message)
46
+ end
47
+
48
+ def run_test_cases(test_case_ids, callback)
49
+ ::RSpec.world.reporter.send(:start, nil)
50
+ Selective::Ruby::Core::Controller.suppress_reporting!
51
+ test_case_ids.flatten.each do |test_case_id|
52
+ run_test_case(test_case_id, callback)
53
+ end
54
+ end
55
+
56
+ def exec
57
+ rspec_runner.run($stderr, $stdout)
58
+ end
59
+
60
+ def remove_failed_test_case_result(test_case_id)
61
+ failure = ::RSpec.world.reporter.failed_examples.detect { |e| e.id == test_case_id }
62
+ if (failed_example_index = ::RSpec.world.reporter.failed_examples.index(failure))
63
+ ::RSpec.world.reporter.failed_examples.delete_at(failed_example_index)
64
+ end
65
+ example = get_example_from_reporter(test_case_id)
66
+ ::RSpec.world.reporter.examples.delete_at(::RSpec.world.reporter.examples.index(example))
67
+ end
68
+
69
+ def base_test_path
70
+ file = config.options[:files_or_directories_to_run].first
71
+ Pathname(normalize_path(file)).each_filename.first
72
+ end
73
+
74
+ def exit_status
75
+ ::RSpec.world.reporter.failed_examples.any? ? 1 : 0
76
+ end
77
+
78
+ def finish
79
+ ::RSpec.world.reporter.finish
80
+ end
81
+
82
+ private
83
+
84
+ def run_test_case(test_case_id, callback)
85
+ ::RSpec.configuration.reset_filters
86
+ ::RSpec.world.prepare_example_filtering
87
+ config.options[:files_or_directories_to_run] = test_case_id
88
+ rspec_runner.options = config
89
+ rspec_runner.setup($stderr, $stdout)
90
+
91
+ # $rspec_rerun_debug = true if test_case == './spec/selective_spec.rb[1:2]' && ::RSpec.world.reporter.examples.length == 5
92
+ rspec_runner.run_specs([::RSpec.world.example_map[test_case_id]])
93
+ callback.call(format_example(get_example_from_reporter(test_case_id)))
94
+ end
95
+
96
+ def get_example_from_reporter(test_case_id)
97
+ ::RSpec.world.reporter.examples.detect { |e| e.id == test_case_id }
98
+ end
99
+
100
+ def normalize_path(path)
101
+ Pathname.new(path).relative_path_from("./")
102
+ end
103
+
104
+ def format_example(example)
105
+ {
106
+ id: example.id,
107
+ description: example.description,
108
+ full_description: example.full_description,
109
+ status: example.execution_result.status.to_s,
110
+ file_path: example.metadata[:file_path],
111
+ line_number: example.metadata[:line_number],
112
+ run_time: example.execution_result.run_time,
113
+ pending_message: example.execution_result.pending_message
114
+ }.tap { |h| h.merge!(failure_formatter(example)) if h[:status] == "failed" }
115
+ end
116
+
117
+ def failure_formatter(example)
118
+ presenter = ::RSpec::Core::Formatters::ExceptionPresenter::Factory.new(example).build
119
+
120
+ {
121
+ failure_message_lines: presenter.message_lines,
122
+ failure_formatted_backtrace: presenter.formatted_backtrace
123
+ # failure_full_backtrace: example.exception.backtrace
124
+ }
125
+ end
126
+
127
+ def apply_rspec_configuration
128
+ ::RSpec.configure do |config|
129
+ config.backtrace_exclusion_patterns = config.backtrace_exclusion_patterns | [/lib\/selective/]
130
+ config.silence_filter_announcements = true
131
+ end
132
+ end
133
+
134
+ def raise_test_manifest_error(output)
135
+ raise TestManifestError.new("Selective could not generate a test manifest. The output was:\n#{output}")
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Selective
4
+ module Ruby
5
+ module RSpec
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
6
+ loader.inflector.inflect("rspec" => "RSpec")
7
+ loader.ignore("#{__dir__}/selective-ruby-rspec.rb")
8
+ loader.setup
9
+
10
+ require "selective-ruby-core"
11
+
12
+ module Selective
13
+ module Ruby
14
+ module RSpec
15
+ class Error < StandardError; end
16
+
17
+ def self.register
18
+ Selective::Ruby::Core.register_runner(
19
+ "rspec", Selective::Ruby::RSpec::RunnerWrapper
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Selective::Ruby::RSpec.register
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selective-ruby-rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Wood
8
+ - Nate Vick
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-11-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: zeitwerk
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 2.6.12
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 2.6.12
28
+ - !ruby/object:Gem::Dependency
29
+ name: selective-ruby-core
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Selective is an intelligent test runner for your current CI provider.
43
+ Get real-time test results, intelligent ordering based on code changes, shorter
44
+ run times, automatic flake detection, the ability to re-enqueue failed tests, and
45
+ more.
46
+ email:
47
+ - ben@hint.io
48
+ - nate@hint.io
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - LICENSE
54
+ - Rakefile
55
+ - lib/selective-ruby-rspec.rb
56
+ - lib/selective/ruby/rspec/monkeypatches.rb
57
+ - lib/selective/ruby/rspec/runner_wrapper.rb
58
+ - lib/selective/ruby/rspec/version.rb
59
+ homepage: https://www.selective.ci
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://www.selective.ci
64
+ source_code_uri: http://github.com/selectiveci/selective-ruby-rspec
65
+ changelog_uri: https://github.com/selectiveci/selective-ruby-rspec/blob/main/CHANGELOG.md
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.6.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.4.10
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Selective Ruby RSpec Client
85
+ test_files: []