delta_test 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +4 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +165 -0
  8. data/Rakefile +17 -0
  9. data/bin/delta_test +12 -0
  10. data/circle.yml +12 -0
  11. data/delta_test.gemspec +30 -0
  12. data/lib/delta_test/analyzer.rb +47 -0
  13. data/lib/delta_test/cli.rb +224 -0
  14. data/lib/delta_test/configuration.rb +173 -0
  15. data/lib/delta_test/dependencies_table.rb +83 -0
  16. data/lib/delta_test/errors.rb +55 -0
  17. data/lib/delta_test/generator.rb +101 -0
  18. data/lib/delta_test/git.rb +88 -0
  19. data/lib/delta_test/related_spec_list.rb +64 -0
  20. data/lib/delta_test/spec_helpers.rb +42 -0
  21. data/lib/delta_test/utils.rb +93 -0
  22. data/lib/delta_test/version.rb +9 -0
  23. data/lib/delta_test.rb +47 -0
  24. data/spec/fixtures/sample/alpha.rb +19 -0
  25. data/spec/fixtures/sample/beta.rb +15 -0
  26. data/spec/fixtures/sample/gamma.rb +9 -0
  27. data/spec/lib/delta_test/analyzer_spec.rb +126 -0
  28. data/spec/lib/delta_test/cli_spec.rb +422 -0
  29. data/spec/lib/delta_test/configuration_spec.rb +353 -0
  30. data/spec/lib/delta_test/dependencies_table_spec.rb +129 -0
  31. data/spec/lib/delta_test/generator_spec.rb +201 -0
  32. data/spec/lib/delta_test/git_spec.rb +178 -0
  33. data/spec/lib/delta_test/related_spec_list_spec.rb +182 -0
  34. data/spec/lib/delta_test/spec_helpers_spec.rb +72 -0
  35. data/spec/lib/delta_test/utils_spec.rb +244 -0
  36. data/spec/lib/delta_test_spec.rb +119 -0
  37. data/spec/rails/.gitignore +19 -0
  38. data/spec/rails/.rspec +3 -0
  39. data/spec/rails/Gemfile +15 -0
  40. data/spec/rails/Gemfile.lock +163 -0
  41. data/spec/rails/README.rdoc +28 -0
  42. data/spec/rails/Rakefile +6 -0
  43. data/spec/rails/app/controllers/application_controller.rb +5 -0
  44. data/spec/rails/app/controllers/concerns/.keep +0 -0
  45. data/spec/rails/app/helpers/application_helper.rb +2 -0
  46. data/spec/rails/app/mailers/.keep +0 -0
  47. data/spec/rails/app/models/.keep +0 -0
  48. data/spec/rails/app/models/concerns/.keep +0 -0
  49. data/spec/rails/app/views/layouts/application.html.haml +7 -0
  50. data/spec/rails/bin/bundle +3 -0
  51. data/spec/rails/bin/rails +4 -0
  52. data/spec/rails/bin/rake +4 -0
  53. data/spec/rails/bin/setup +29 -0
  54. data/spec/rails/config/application.rb +35 -0
  55. data/spec/rails/config/boot.rb +3 -0
  56. data/spec/rails/config/database.yml +25 -0
  57. data/spec/rails/config/environment.rb +5 -0
  58. data/spec/rails/config/environments/development.rb +41 -0
  59. data/spec/rails/config/environments/production.rb +79 -0
  60. data/spec/rails/config/environments/test.rb +42 -0
  61. data/spec/rails/config/initializers/assets.rb +11 -0
  62. data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
  63. data/spec/rails/config/initializers/cookies_serializer.rb +3 -0
  64. data/spec/rails/config/initializers/filter_parameter_logging.rb +4 -0
  65. data/spec/rails/config/initializers/inflections.rb +16 -0
  66. data/spec/rails/config/initializers/mime_types.rb +4 -0
  67. data/spec/rails/config/initializers/session_store.rb +3 -0
  68. data/spec/rails/config/initializers/wrap_parameters.rb +14 -0
  69. data/spec/rails/config/locales/en.yml +23 -0
  70. data/spec/rails/config/routes.rb +56 -0
  71. data/spec/rails/config/secrets.yml +22 -0
  72. data/spec/rails/config.ru +4 -0
  73. data/spec/rails/db/seeds.rb +7 -0
  74. data/spec/rails/delta_test.yml +5 -0
  75. data/spec/rails/lib/assets/.keep +0 -0
  76. data/spec/rails/lib/tasks/.keep +0 -0
  77. data/spec/rails/log/.keep +0 -0
  78. data/spec/rails/public/404.html +67 -0
  79. data/spec/rails/public/422.html +67 -0
  80. data/spec/rails/public/500.html +66 -0
  81. data/spec/rails/public/favicon.ico +0 -0
  82. data/spec/rails/public/robots.txt +5 -0
  83. data/spec/rails/spec/features/sample_spec.rb +7 -0
  84. data/spec/rails/spec/spec_helper.rb +16 -0
  85. data/spec/rails/vendor/assets/javascripts/.keep +0 -0
  86. data/spec/rails/vendor/assets/stylesheets/.keep +0 -0
  87. data/spec/spec_helper.rb +39 -0
  88. data/spec/supports/create_table_file.rb +21 -0
  89. metadata +283 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 44702b3609807ac74dcf042d0286c7bcfa454b84
4
+ data.tar.gz: 81066922a6b90393bf1e18ff17c23e5c715b482e
5
+ SHA512:
6
+ metadata.gz: 55201af622d4ffcb874180409b7a5ba73019646b979cb87e6feeb673adccc23b0dd81c22dce4e47fdaf72ec1642b1f2eae70cdcc09e7b489cb22e3c3bde0b1d0
7
+ data.tar.gz: b8e12d40f4ddafe2cc61701bca6da224d13936ee8e8bf3f2839f62bada795d608543f96e1742d5dbb018d351044e408317f2aa8f04d4c01bcac2a154c918acea
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format doc
3
+ --require spec_helper
4
+ --exclude-pattern 'spec/rails/**/*'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delta_test.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yuki Iwanaga
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,165 @@
1
+ delta_test [![Circle CI](https://circleci.com/gh/creasty/delta_test.svg?style=svg)](https://circleci.com/gh/creasty/delta_test)
2
+ ==========
3
+
4
+ **delta_test** analyzes your tests and runs only related tests for your file changes.
5
+
6
+
7
+ Setup
8
+ -----
9
+
10
+ ### Installation
11
+
12
+ Add this line to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'delta_test', group: :test
16
+ ```
17
+
18
+ ### Configuration
19
+
20
+ Create configuration file at your project root directory.
21
+
22
+ ```bash
23
+ $ vi delta_test.yml
24
+ ```
25
+
26
+ ```yaml
27
+ patterns:
28
+ - lib/**/*.rb
29
+ - app/**/*.rb
30
+ ```
31
+
32
+ ### Rspec
33
+
34
+ Include delta_test in your `spec/spec_helper.rb`.
35
+
36
+ ```ruby
37
+ require 'delta_test'
38
+ require 'delta_test/spec_helpers'
39
+
40
+ RSpec.configure do |config|
41
+
42
+ config.include DeltaTest::SpecHelpers
43
+
44
+ end
45
+ ```
46
+
47
+
48
+ Usage
49
+ -----
50
+
51
+ First you'll need to run full tests to create **dependencies table**:
52
+
53
+ ```bash
54
+ $ git checkout master
55
+ $ delta_test exec bundle exec rspec
56
+ ```
57
+
58
+ Then, on other branch:
59
+
60
+ ```bash
61
+ $ git checkout -b feature/something_awesome
62
+ $ # Make changes & create commits...
63
+ $ delta_test exec bundle exec rspec # runs only related tests for changes from master
64
+ ```
65
+
66
+
67
+ Advanced usage
68
+ --------------
69
+
70
+ ### Command
71
+
72
+ ```
73
+ usage: delta_test <command> [--base=<base>] [--head=<head>] [--verbose] [<args>]
74
+ [-v]
75
+
76
+ options:
77
+ --base=<base> A branch or a commit id to diff from.
78
+ <head> is default to master.
79
+
80
+ --head=<head> A branch or a commit id to diff to.
81
+ <head> is default to HEAD. (current branch you're on)
82
+
83
+ --verbose Print more output.
84
+
85
+ -v, --version Show version.
86
+
87
+ commands:
88
+ list List related spec files for changes between base and head.
89
+ head is default to master; base is to the current branch.
90
+
91
+ table Show dependencies table.
92
+
93
+ exec <script> Rxecute test script using delta_test.
94
+ Run command something like `delta_test list | xargs script'.
95
+ ```
96
+
97
+ ### Configurations
98
+
99
+ ```yaml
100
+ table_file: tmp/.delta_test_dt
101
+
102
+ patterns:
103
+ - lib/**/*.rb
104
+ - app/**/*.rb
105
+
106
+ exclude_patterns:
107
+ - lib/batch/*.rb
108
+
109
+ custom_mappings:
110
+ spec/features/i18n_spec.rb:
111
+ - config/locales/**/*.yml
112
+ ```
113
+
114
+
115
+ Testing
116
+ -------
117
+
118
+ Run units tests:
119
+
120
+ ```bash
121
+ $ rspec
122
+
123
+ # or
124
+
125
+ $ rake test
126
+ ```
127
+
128
+ Run integration tests:
129
+
130
+ ```bash
131
+ $ (cd spec/rails && bundle install)
132
+ $ rake rails:test
133
+ ```
134
+
135
+
136
+ Contributing
137
+ ------------
138
+
139
+ Contributions are always welcome!
140
+
141
+ ### Bug reports
142
+
143
+ 1. Ensure the bug can be reproduced on the latest master
144
+ 1. Check it's not a duplicate
145
+ 1. Raise an issue
146
+
147
+ ### Pull requests
148
+
149
+ 1. Fork the repository
150
+ 1. Create a branch
151
+ 1. Write test-driven code
152
+ 1. Update the documentation if necessary
153
+ 1. Create a new pull request
154
+
155
+
156
+ License
157
+ -------
158
+
159
+ This project is released under the MIT license. See `LICENSE.txt` file for details.
160
+
161
+
162
+ Maintainer
163
+ ----------
164
+
165
+ [@creasty](http://github.com/creasty)
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Run unit tests'
4
+ task :test do
5
+ s = system('bundle exec rspec')
6
+ exit unless s
7
+ end
8
+
9
+ namespace :rails do
10
+ desc 'Run rails tests'
11
+ task :test do
12
+ s = system('cd spec/rails && DELTA_TEST_ACTIVE=true bundle exec rspec')
13
+ exit unless s
14
+ end
15
+ end
16
+
17
+ task default: ['test', 'rails:test']
data/bin/delta_test ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # enable local usage from cloned repo
4
+ root = File.expand_path('../..', __FILE__)
5
+ $LOAD_PATH << "#{root}/lib" if File.exist?("#{root}/Gemfile")
6
+
7
+ require 'delta_test'
8
+ require 'delta_test/cli'
9
+
10
+ DeltaTest.config.auto_configure!
11
+
12
+ DeltaTest::CLI.new.run(ARGV)
data/circle.yml ADDED
@@ -0,0 +1,12 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.0
4
+
5
+ dependencies:
6
+ post:
7
+ - cd spec/rails && bundle install
8
+
9
+ test:
10
+ override:
11
+ - bundle exec rake test
12
+ - bundle exec rake rails:test
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'delta_test/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'delta_test'
8
+ spec.version = DeltaTest::VERSION
9
+ spec.authors = ['Yuki Iwanaga']
10
+ spec.email = ['yuki@creasty.com']
11
+ spec.summary = %q{delta_test analyzes your tests and runs only related tests for your file changes}
12
+ spec.description = %q{delta_test analyzes your tests and runs only related tests for your file changes}
13
+ spec.homepage = 'http://github.com/creasty/delta_test'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_dependency 'ruby-prof'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.7'
26
+ spec.add_development_dependency 'fakefs'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '>= 3.0'
30
+ end
@@ -0,0 +1,47 @@
1
+ require 'ruby-prof'
2
+
3
+ module DeltaTest
4
+ class Analyzer
5
+
6
+ attr_reader :result
7
+
8
+ ###
9
+ # Start analyzer
10
+ ###
11
+ def start
12
+ @result = nil
13
+ @files = Set.new
14
+
15
+ RubyProf.stop if RubyProf.running?
16
+ RubyProf.start
17
+ end
18
+
19
+ ###
20
+ # Stop analyzer
21
+ ###
22
+ def stop
23
+ @result = nil
24
+ @result = RubyProf.stop if RubyProf.running?
25
+ end
26
+
27
+ ###
28
+ # Gather source files in the call stack
29
+ #
30
+ # @return {Set<String>}
31
+ ###
32
+ def related_source_files
33
+ return @files unless @result
34
+
35
+ @result.threads.each do |thread|
36
+ thread.methods.each do |method|
37
+ @files << method.source_file
38
+ end
39
+ end
40
+
41
+ @result = nil
42
+
43
+ @files
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,224 @@
1
+ require 'open3'
2
+ require 'shellwords'
3
+
4
+ require_relative 'related_spec_list'
5
+
6
+ module DeltaTest
7
+ class CLI
8
+
9
+ DEFAULTS = {
10
+ 'base' => 'master',
11
+ 'head' => 'HEAD',
12
+ 'verbose' => false,
13
+ }.freeze
14
+
15
+ attr_reader *%i[
16
+ args
17
+ command
18
+ options
19
+ ]
20
+
21
+ def initialize
22
+ @args = []
23
+ @command = nil
24
+ @options = {}
25
+ end
26
+
27
+ ###
28
+ # Run cli
29
+ #
30
+ # @params {Array} args
31
+ ###
32
+ def run(args)
33
+ @args = args.dup
34
+
35
+ @command = @args.shift
36
+ @options = parse_options!
37
+
38
+ @list = RelatedSpecList.new
39
+
40
+ DeltaTest.verbose = !!@options['verbose']
41
+
42
+ invoke
43
+ end
44
+
45
+ ###
46
+ # Invoke action method
47
+ ###
48
+ def invoke
49
+ begin
50
+ case @command
51
+ when 'list'
52
+ do_list
53
+ when 'table'
54
+ do_table
55
+ when 'exec'
56
+ do_exec
57
+ when '-v', '--version'
58
+ do_version
59
+ else
60
+ do_help
61
+ end
62
+ rescue => e
63
+ if DeltaTest.verbose?
64
+ raise e
65
+ else
66
+ exit_with_message(1, '[%s] %s' % [e.class.name, e.message])
67
+ end
68
+ end
69
+ end
70
+
71
+ ###
72
+ # Parse option arguments
73
+ #
74
+ # @return {Hash<String, Boolean|String>}
75
+ ###
76
+ def parse_options!
77
+ options = {}
78
+
79
+ @args.reject! do |arg|
80
+ case arg
81
+ when /^-([a-z0-9])$/i, /^--([a-z0-9][a-z0-9-]*)$/i
82
+ options[$1] = true
83
+ when /^--([a-z0-9][a-z0-9-]*)=(.+)$/i
84
+ options[$1] = $2
85
+ else
86
+ break
87
+ end
88
+ end
89
+
90
+ DEFAULTS.merge(options)
91
+ end
92
+
93
+ ###
94
+ # Print message and exit with a status
95
+ #
96
+ # @params {Integer} status - exit code
97
+ # @params {Object} *args
98
+ ###
99
+ def exit_with_message(status, *args)
100
+ if status.zero?
101
+ puts(*args)
102
+ else
103
+ $stderr.puts(*args)
104
+ end
105
+
106
+ exit status
107
+ end
108
+
109
+ ###
110
+ # Whether run full test or not
111
+ #
112
+ # @return {Boolean}
113
+ ###
114
+ def run_full_tests?
115
+ Git.same_commit?(@options['base'], @options['head'])
116
+ end
117
+
118
+ ###
119
+ # Show table contents
120
+ ###
121
+ def do_table
122
+ @list.load_table!
123
+
124
+ @list.table.each do |spec_file, dependencies|
125
+ puts spec_file
126
+ puts
127
+ dependencies.each do |dependency|
128
+ puts "\t#{dependency}"
129
+ end
130
+ puts
131
+ end
132
+ end
133
+
134
+ ###
135
+ # Show related spec files
136
+ ###
137
+ def do_list
138
+ @list.load_table!
139
+ @list.retrive_changed_files!(@options['base'], @options['head'])
140
+
141
+ puts @list.related_spec_files
142
+ end
143
+
144
+ ###
145
+ # Execute test script with delta_test
146
+ ###
147
+ def do_exec
148
+ spec_files = nil
149
+ args = []
150
+
151
+ if run_full_tests?
152
+ args << ('%s=%s' % [VERBOSE_FLAG, true]) if DeltaTest.verbose?
153
+ args << ('%s=%s' % [ACTIVE_FLAG, true])
154
+ else
155
+ args << 'cat'
156
+ args << '|'
157
+ args << 'xargs'
158
+
159
+ @list.load_table!
160
+ @list.retrive_changed_files!(@options['base'], @options['head'])
161
+
162
+ spec_files = @list.related_spec_files.to_a
163
+
164
+ if spec_files.empty?
165
+ exit_with_message(0, 'Nothing to test')
166
+ end
167
+ end
168
+
169
+ args += @args
170
+ args = args.join(' ')
171
+
172
+ $stdout.sync = true
173
+
174
+ Open3.popen3(args) do |i, o, e, w|
175
+ i.write(spec_files.join("\n")) if spec_files
176
+ i.close
177
+ o.each { |l| puts l }
178
+ e.each { |l| $stderr.puts l }
179
+ end
180
+ end
181
+
182
+ ###
183
+ # Show version
184
+ ###
185
+ def do_version
186
+ puts 'DeltaTest v%s' % VERSION
187
+ end
188
+
189
+ ###
190
+ # Show help
191
+ ###
192
+ def do_help
193
+ if !@command.nil? && '-' != @command[0]
194
+ puts "Command not found: #{@command}"
195
+ end
196
+
197
+ puts <<-HELP
198
+ usage: delta_test <command> [--base=<base>] [--head=<head>] [--verbose] [<args>]
199
+ [-v]
200
+
201
+ options:
202
+ --base=<base> A branch or a commit id to diff from.
203
+ <head> is default to master.
204
+
205
+ --head=<head> A branch or a commit id to diff to.
206
+ <head> is default to HEAD. (current branch you're on)
207
+
208
+ --verbose Print more output.
209
+
210
+ -v, --version Show version.
211
+
212
+ commands:
213
+ list List related spec files for changes between base and head.
214
+ head is default to master; base is to the current branch.
215
+
216
+ table Show dependencies table.
217
+
218
+ exec <script> Rxecute test script using delta_test.
219
+ Run command something like `delta_test list | xargs script'.
220
+ HELP
221
+ end
222
+
223
+ end
224
+ end