rspec-watcher 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: 6d56c09fc797e502a6ea38113634a79e2ccdff63d397fa0387b0d6e119daeb3f
4
+ data.tar.gz: 2ac174f52116b68c12e78d1aa96697d32deca1dd0137406a22b5cc70398f326a
5
+ SHA512:
6
+ metadata.gz: 21149fae2370cecdb6292e49b2dbc6bce4a6342d48cca8ab58f6f36fe70a58027b0081ca41ad811abc13112195580681c38393ec073e3eb9d409776573b567f5
7
+ data.tar.gz: f1b7acc4119b35d3bdb39bc9ac73664d1a838e1dbf036a07fbdddd221c6a85d1c150556a35b94ed3fd48ffa76d32cd0c7552c43f9ae4a1304e54d78ab59b498b
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rspec_watcher.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Matous Vokal
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,59 @@
1
+ # RspecWatcher
2
+
3
+ Automatically runs specs in reaction to changes in files. Loads the project once and uses code reloading to get changes instead of starting a new process for every test run.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add rspec-watcher
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install rspec-watcher
14
+
15
+ ## Usage
16
+
17
+ Example configuration for a Rails project in `config/rspec_watcher.rb`:
18
+
19
+ ```ruby
20
+ RSpecWatcher.configure do
21
+ watch 'spec', only: /_spec\.rb\z/ do |modified, added, _removed|
22
+ modified + added
23
+ end
24
+
25
+ watch 'spec', ignore: /_spec\.rb\z/
26
+
27
+ watch 'app', only: /\.rb\z/, ignore: %r{controllers/} do |modified, added, removed|
28
+ (modified + added + removed).map do |path|
29
+ path.sub('app/', 'spec/').sub('.rb', '_spec.rb')
30
+ end
31
+ end
32
+
33
+ watch 'app/controllers', only: /\.rb\z/ do |modified, added, removed|
34
+ (modified + added + removed).map do |path|
35
+ path.sub('app/', 'spec/').sub('controllers/', 'requests/').sub('_controller.rb', '_spec.rb')
36
+ end
37
+ end
38
+
39
+ watch 'config', only: /routes\.rb\z/
40
+ end
41
+ ```
42
+
43
+ Start the watcher with `RAILS_ENV=test bundle exec rake rspec_watcher:watch`
44
+
45
+ In order to use trhe watcher without Rails, `path_inferrer` and `reloader` need to be configured. Check `lib/rspec_watcher.rb`
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Sorc96/rspec-watcher.
56
+
57
+ ## License
58
+
59
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rspec_watcher/version'
4
+ require_relative 'rspec_watcher/railtie' if defined?(Rails)
5
+
6
+ module RSpecWatcher
7
+ SPEC_INFERRER = ->(_modified, _added, _removed) { [] }
8
+ PATH_INFERRER = ->(path) { Rails.root.join(path) }
9
+ RELOADER = -> { Rails.application.reloader.reload! }
10
+
11
+ @path_inferrer = PATH_INFERRER
12
+ @reloader = RELOADER
13
+
14
+ class << self
15
+ attr_accessor :path_inferrer, :reloader
16
+ attr_reader :rules
17
+
18
+ def configure(&block)
19
+ @rules = []
20
+ @failed_specs = []
21
+ instance_exec(&block)
22
+ end
23
+
24
+ def watch(path, **options, &inferrer)
25
+ inferrer ||= SPEC_INFERRER
26
+ rules << [path, options, inferrer]
27
+ end
28
+
29
+ def start
30
+ listeners.each(&:start)
31
+ end
32
+
33
+ private
34
+
35
+ def listeners
36
+ rules.map do |path, options, inferrer|
37
+ Listen.to(path_inferrer.call(path), **options) do |modified, added, removed|
38
+ run_specs(inferrer.call(modified, added, removed))
39
+ end
40
+ end
41
+ end
42
+
43
+ def run_specs(paths)
44
+ clear_screen
45
+ reloader.call
46
+ RSpec.clear_examples
47
+ RSpec.world.wants_to_quit = false
48
+ RSpec::Core::Runner.run(specs_to_run(paths))
49
+ prepare_failed_specs_for_rerun
50
+ end
51
+
52
+ def clear_screen
53
+ puts "\e[H\e[2J"
54
+ end
55
+
56
+ # Filter out nonexistent files and paths to specific lines if the whole file will be rerun
57
+ def specs_to_run(paths)
58
+ (paths + @failed_specs).reject do |path|
59
+ file_path = path.split(':').first
60
+ next true unless File.exist?(file_path)
61
+
62
+ path.include?(':') && paths.include?(file_path)
63
+ end
64
+ end
65
+
66
+ def prepare_failed_specs_for_rerun
67
+ @failed_specs = RSpec
68
+ .world
69
+ .all_examples
70
+ .select(&:exception)
71
+ .map(&:location_rerun_argument)
72
+ .map { |path| File.absolute_path(path) }
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecWatcher
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :rspec_watcher
6
+
7
+ rake_tasks do
8
+ path = File.expand_path(__dir__)
9
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :rspec_watcher do
4
+ desc 'Start the watcher with configuration from config/rspec_watcher.rb'
5
+ task :watch do
6
+ abort('Not running in test environment') unless Rails.env.test?
7
+
8
+ require 'rspec/core'
9
+ require Rails.root.join('config/rspec_watcher')
10
+
11
+ RSpecWatcher.start
12
+
13
+ puts 'Watching specs...'
14
+ sleep
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecWatcher
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rspec_watcher/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rspec-watcher"
7
+ spec.version = RSpecWatcher::VERSION
8
+ spec.authors = ["Matous Vokal"]
9
+ spec.email = ["matous.vokal@gmail.com"]
10
+
11
+ spec.summary = "Instant feedback for awesome TDD experience with RSpec."
12
+ spec.description = "Automatically runs specs in reaction to changes in files. Loads the project once and uses code reloading to get changes instead of starting a new process for every test run."
13
+ spec.homepage = "https://github.com/Sorc96/rspec-watcher"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/Sorc96/rspec-watcher"
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
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
25
+ end
26
+ end
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "rspec", "~> 3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-watcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matous Vokal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-30 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: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Automatically runs specs in reaction to changes in files. Loads the project
28
+ once and uses code reloading to get changes instead of starting a new process for
29
+ every test run.
30
+ email:
31
+ - matous.vokal@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/rspec-watcher.rb
41
+ - lib/rspec_watcher/railtie.rb
42
+ - lib/rspec_watcher/tasks/watch.rake
43
+ - lib/rspec_watcher/version.rb
44
+ - rspec_watcher.gemspec
45
+ homepage: https://github.com/Sorc96/rspec-watcher
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/Sorc96/rspec-watcher
50
+ source_code_uri: https://github.com/Sorc96/rspec-watcher
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.6.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.2.32
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Instant feedback for awesome TDD experience with RSpec.
70
+ test_files: []