guard-jest_runner 1.0.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: c1847de557f71cf8d145bdf4f0b19054c01481070f0e8c1ecca65d0cdb8191fc
4
+ data.tar.gz: a580e164831f24e9a8c865aa9e5aa2bcc8902d78cab70ae2e56e39f3761b8dc0
5
+ SHA512:
6
+ metadata.gz: 1829c01972e0bda7e10bfb1813b85dd0b355942723a026cf146d9755f79c7c5df7423a198c782f9bf0ec02b505fe5610c01688755a3b3002cb4f851fe32e677e
7
+ data.tar.gz: 8d5dd3199ec048465e64c5d837d139f223ff05726f383f52f72d42c681d9d86fdc771c83bfe3330c533d1567157f6ea223d1c273b79893507d3f18845c99ca45
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ require: rubocop-rspec
2
+ AllCops:
3
+ DisplayCopNames: true
4
+ DisplayStyleGuide: true
5
+ Exclude:
6
+ - bin/**/*
7
+ - db/**/*
8
+ - vendor/**/*
9
+ - Guardfile
10
+ Rails:
11
+ Enabled: true
12
+ Metrics/LineLength:
13
+ Max: 100
14
+ Style/Documentation:
15
+ Enabled: false
16
+ Style/NumericLiterals:
17
+ Enabled: false
18
+ Style/ClassAndModuleChildren:
19
+ Enabled: false
20
+ MethodLength:
21
+ Max: 15
22
+ Enabled: false
23
+ Enabled: false
24
+ RegexpLiteral:
25
+ Exclude:
26
+ - '**/*.gemspec'
27
+ - '**/Guardfile'
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in guard-jest_runner.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Guard::JestRunner
2
+
3
+ Guard::JestRunner allows you to automatically run jest when you change a Javascript/ES6 file.
4
+
5
+ Unlike [guard-jest](https://rubygems.org/gems/guard-jest), this guard runs just as directed by your Guardfile, which allows you to use it in a group and use a Red-Green-Refactor process.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'guard-jest_runner'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install guard-jest_runner
22
+
23
+ ## Usage
24
+
25
+ Please read [Guard usage doc](https://github.com/guard/guard#readme).
26
+
27
+ ## Guardfile
28
+
29
+ For a typical Rails app with webpack:
30
+
31
+ ``` ruby
32
+ guard :jest do
33
+ watch(%r{app/javascript/(.+)\.js$}) { |m| "spec/javascript/#{m[1]}.test.js" }
34
+ watch(%r{spec/javascript/.+\.js$})
35
+ end
36
+ ```
37
+
38
+ (**Recommended**) pair up with guard-eslint to get a Red-Green-Refactor process:
39
+
40
+ ```ruby
41
+ group :red_green_refactor_js, halt_on_fail: true do
42
+ guard :jest_runner do
43
+ watch(%r{app/javascript/(.+)\.js$}) { |m| "spec/javascript/#{m[1]}.test.js" }
44
+ watch(%r{spec/javascript/.+\.js$})
45
+ end
46
+
47
+ guard :eslint, formatter: 'codeframe' do
48
+ watch(%r{app/javascript/(.+)\.js$}) { |m| ""}
49
+ watch(%r{spec/javascript/(.+)\.js$})
50
+ end
51
+ end
52
+ ```
53
+
54
+ ### List of available options:
55
+
56
+ ``` ruby
57
+ all_on_start: false # Run all specs after changed specs pass.
58
+ keep_failed: true # Keep failed files until they pass (add them to new ones)
59
+ notification: true # Display notification always when jest completes.
60
+ # If you want to notify only on failure, set to :failure.
61
+ cli: nil # Additional command-line options to pass to jest.
62
+ # Don't use the '-f' or '--format' option here.
63
+ command: 'jest' # Specify a custom path to the jest command.
64
+ default_paths: ['**/*.js', '**/*.es6'] # The default paths that will be used for "all_on_start".
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RobinDaugherty/guard-jest_runner.
70
+
71
+ * Please create a topic branch for every separate change you make.
72
+ * Make sure your patches are well-tested.
73
+ * Update the README to reflect your changes.
74
+ * Please **do not change** the version number.
75
+ * Open a pull request.
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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "guard/jest"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
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,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'guard/jest_runner/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "guard-jest_runner"
8
+ s.version = Guard::JestRunnerVersion.to_s
9
+ s.authors = %w[RobinDaugherty]
10
+ s.email = %w[robin@robindaugherty.net]
11
+
12
+ s.description = %q{Allows you to add jest to your Guard toolchain, so that jest is run.}
13
+ s.summary = %q{Guard to run jest.}
14
+ s.homepage = "https://github.com/RobinDaugherty/guard-jest_runner"
15
+ s.license = "MIT"
16
+
17
+ if s.respond_to?(:metadata)
18
+ s.metadata['changelog_uri'] = 'https://github.com/RobinDaugherty/guard-jest_runner/releases'
19
+ s.metadata['source_code_uri'] = 'https://github.com/RobinDaugherty/guard-jest_runner'
20
+ s.metadata['bug_tracker_uri'] = 'https://github.com/RobinDaugherty/guard-jest_runner/issues'
21
+ else
22
+ puts "Your RubyGems does not support metadata. Update if you'd like to make a release."
23
+ end
24
+
25
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ s.require_paths = ["lib"]
27
+
28
+ s.required_ruby_version = ">= 2.0.0"
29
+
30
+ s.add_dependency 'guard', "~> 2.1"
31
+ s.add_dependency 'guard-compat', "~> 1.1"
32
+
33
+ s.add_development_dependency "rake", "~> 10.0"
34
+ s.add_development_dependency "rspec", "~> 3.0"
35
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Guard
6
+ class JestRunner
7
+ # This class runs `jest` command, retrieves result and notifies.
8
+ # An instance of this class is intended to invoke `jest` only once in its lifetime.
9
+ class Runner
10
+ def initialize(options)
11
+ @options = options
12
+ end
13
+
14
+ attr_reader :options
15
+
16
+ def run(paths)
17
+ paths = options[:default_paths] unless paths
18
+
19
+ command = command_for_check(paths)
20
+ passed = system(*command)
21
+ case options[:notification]
22
+ when :failed
23
+ notify(passed) unless passed
24
+ when true
25
+ notify(passed)
26
+ end
27
+
28
+ passed
29
+ end
30
+
31
+ def command_for_check(paths)
32
+ command = [options[:command]]
33
+
34
+ command.concat(args_specified_by_user)
35
+ command.concat(['--json', "--outputFile=#{json_file_path}"])
36
+ command.concat(paths)
37
+ end
38
+
39
+ def args_specified_by_user
40
+ @args_specified_by_user ||= begin
41
+ args = options[:cli]
42
+ case args
43
+ when Array then args
44
+ when String then args.shellsplit
45
+ when NilClass then []
46
+ else fail ':cli option must be either an array or string'
47
+ end
48
+ end
49
+ end
50
+
51
+ def json_file_path
52
+ @json_file_path ||= begin
53
+ json_file.close
54
+ json_file.path
55
+ end
56
+ end
57
+
58
+ ##
59
+ # Keep the Tempfile instance around so it isn't garbage-collected and therefore deleted.
60
+ def json_file
61
+ @json_file ||= begin
62
+ # Just generate random tempfile path.
63
+ basename = self.class.name.downcase.gsub('::', '_')
64
+ Tempfile.new(basename)
65
+ end
66
+ end
67
+
68
+ def result
69
+ @result ||= begin
70
+ File.open(json_file_path) do |file|
71
+ # Rubinius 2.0.0.rc1 does not support `JSON.load` with 3 args.
72
+ JSON.parse(file.read, symbolize_names: true)
73
+ end
74
+ end
75
+ end
76
+
77
+ def notify(passed)
78
+ image = passed ? :success : :failed
79
+ Notifier.notify(summary_text, title: 'Jest results', image: image)
80
+ end
81
+
82
+ def summary_text
83
+ summary = {
84
+ tests_run: result[:numTotalTests],
85
+ passed: result[:numPassedTests],
86
+ pending: result[:numPendingTests],
87
+ failed: result[:numFailedTests],
88
+ }
89
+
90
+ String.new.tap do |text|
91
+ if summary[:failed] > 0
92
+ text << pluralize(summary[:failed], 'example')
93
+ text << " failed"
94
+ text << " (#{summary[:passed]} passed"
95
+ text << ", #{summary[:pending]} pending" if summary[:pending] > 0
96
+ text << ")."
97
+ else
98
+ text << "#{summary[:passed]} passed"
99
+ text << " (#{summary[:pending]} pending)" if summary[:pending] > 0
100
+ text << "."
101
+ end
102
+ end
103
+ end
104
+
105
+ def failed_paths
106
+ result[:testResults].select { |f| f[:status] == "failed" }.map { |f| f[:name] }.uniq
107
+ end
108
+
109
+ def pluralize(number, thing, options = {})
110
+ text = String.new
111
+
112
+ if number == 0 && options[:no_for_zero]
113
+ text = 'no'
114
+ else
115
+ text << number.to_s
116
+ end
117
+
118
+ text << " #{thing}"
119
+ text << 's' unless number == 1
120
+
121
+ text
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Guard
4
+ # A workaround for some superclass BS
5
+ # where Jest < Guard has to exist?
6
+ module JestRunnerVersion
7
+ # http://semver.org/
8
+ MAJOR = 1
9
+ MINOR = 0
10
+ PATCH = 0
11
+
12
+ def self.to_s
13
+ [MAJOR, MINOR, PATCH].join('.')
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'guard/compat/plugin'
4
+
5
+ module Guard
6
+ class JestRunner < Plugin
7
+ autoload :Runner, 'guard/jest_runner/runner'
8
+
9
+ # Initializes a Guard plugin.
10
+ # Don't do any work here, especially as Guard plugins get initialized
11
+ # even if they are not in an active group!
12
+ #
13
+ # @param [Hash] options the custom Guard plugin options
14
+ # @option options [Array<Guard::Watcher>] watchers the Guard plugin file watchers
15
+ # @option options [Symbol] group the group this Guard plugin belongs to
16
+ # @option options [Boolean] any_return allow any object to be returned from a watcher
17
+ #
18
+ def initialize(options = {})
19
+ super
20
+
21
+ @options = {
22
+ all_on_start: false,
23
+ keep_failed: true,
24
+ notification: true,
25
+ cli: nil,
26
+ command: 'jest',
27
+ default_paths: ['**/*.js', '**/*.es6'],
28
+ }.merge(options)
29
+
30
+ @failed_paths = []
31
+ end
32
+
33
+ # Called once when Guard starts. Please override initialize method to init stuff.
34
+ #
35
+ # @raise [:task_has_failed] when start has failed
36
+ # @return [Object] the task result
37
+ #
38
+ def start
39
+ Compat::UI.info 'Guard::JestRunner is running'
40
+ run_all if options[:all_on_start]
41
+ end
42
+
43
+ # Called when `reload|r|z + enter` is pressed.
44
+ # This method should be mainly used for "reload" (really!) actions like reloading
45
+ # passenger/spork/bundler/...
46
+ #
47
+ # @raise [:task_has_failed] when reload has failed
48
+ # @return [Object] the task result
49
+ #
50
+ def reload
51
+ runner.reload
52
+ end
53
+
54
+ # Called when just `enter` is pressed
55
+ # This method should be principally used for long action like running all specs/tests/...
56
+ #
57
+ # @raise [:task_has_failed] when run_all has failed
58
+ # @return [Object] the task result
59
+ #
60
+ def run_all
61
+ Compat::UI.info 'Running jest for all Javascript files'
62
+ inspect_with_jest
63
+ end
64
+
65
+ # Called on file(s) additions that the Guard plugin watches.
66
+ #
67
+ # @param [Array<String>] paths the changes files or paths
68
+ # @raise [:task_has_failed] when run_on_additions has failed
69
+ # @return [Object] the task result
70
+ #
71
+ def run_on_additions(paths)
72
+ run_partially(paths)
73
+ end
74
+
75
+ # Called on file(s) modifications that the Guard plugin watches.
76
+ #
77
+ # @param [Array<String>] paths the changes files or paths
78
+ # @raise [:task_has_failed] when run_on_modifications has failed
79
+ # @return [Object] the task result
80
+ #
81
+ def run_on_modifications(paths)
82
+ run_partially(paths)
83
+ end
84
+
85
+ private
86
+
87
+ def inspect_with_jest(paths = [])
88
+ runner = Runner.new(@options)
89
+ passed = runner.run(paths)
90
+ @failed_paths = runner.failed_paths
91
+ throw :task_has_failed unless passed
92
+ rescue => error
93
+ Compat::UI.error 'The following exception occurred while running guard-jest_runner: ' \
94
+ "#{error.backtrace.first} #{error.message} (#{error.class.name})"
95
+ end
96
+
97
+ def run_partially(paths)
98
+ paths += @failed_paths if @options[:keep_failed]
99
+ paths = clean_paths(paths)
100
+
101
+ return if paths.empty?
102
+
103
+ displayed_paths = paths.map { |path| smart_path(path) }
104
+ Compat::UI.info "Running jest: #{displayed_paths.join(' ')}"
105
+
106
+ inspect_with_jest(paths)
107
+ end
108
+
109
+ def clean_paths(paths)
110
+ paths = paths.dup
111
+ paths.map! { |path| File.expand_path(path) }
112
+ paths.uniq!
113
+ paths.reject! do |path|
114
+ next true unless File.exist?(path)
115
+ included_in_other_path?(path, paths)
116
+ end
117
+ paths
118
+ end
119
+
120
+ def included_in_other_path?(target_path, other_paths)
121
+ dir_paths = other_paths.select { |path| File.directory?(path) }
122
+ dir_paths.delete(target_path)
123
+ dir_paths.any? do |dir_path|
124
+ target_path.start_with?(dir_path)
125
+ end
126
+ end
127
+
128
+ def smart_path(path)
129
+ if path.start_with?(Dir.pwd)
130
+ Pathname.new(path).relative_path_from(Pathname.getwd).to_s
131
+ else
132
+ path
133
+ end
134
+ end
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-jest_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - RobinDaugherty
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: guard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-compat
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Allows you to add jest to your Guard toolchain, so that jest is run.
70
+ email:
71
+ - robin@robindaugherty.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - guard-jest_runner.gemspec
86
+ - lib/guard/jest_runner.rb
87
+ - lib/guard/jest_runner/runner.rb
88
+ - lib/guard/jest_runner/version.rb
89
+ homepage: https://github.com/RobinDaugherty/guard-jest_runner
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ changelog_uri: https://github.com/RobinDaugherty/guard-jest_runner/releases
94
+ source_code_uri: https://github.com/RobinDaugherty/guard-jest_runner
95
+ bug_tracker_uri: https://github.com/RobinDaugherty/guard-jest_runner/issues
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.0.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.7.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Guard to run jest.
116
+ test_files: []