spec_producer 0.11.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -0
  3. data/CHANGELOG.md +15 -0
  4. data/README.md +73 -24
  5. data/lib/configuration.rb +41 -0
  6. data/lib/generators/spec_producer/install/install_generator.rb +13 -0
  7. data/lib/generators/spec_producer/install/templates/spec_producer +1 -0
  8. data/lib/spec_producer.rb +140 -47
  9. data/lib/spec_producer/factories_production_module.rb +17 -11
  10. data/lib/spec_producer/missing_files_module.rb +104 -41
  11. data/lib/spec_producer/missing_gems_module.rb +77 -0
  12. data/lib/spec_producer/producers.rb +10 -0
  13. data/lib/spec_producer/producers/base.rb +129 -0
  14. data/lib/spec_producer/producers/controllers_producer.rb +31 -0
  15. data/lib/spec_producer/producers/helpers_producer.rb +26 -0
  16. data/lib/spec_producer/producers/jobs_producer.rb +34 -0
  17. data/lib/spec_producer/producers/mailers_producer.rb +24 -0
  18. data/lib/spec_producer/producers/models_producer.rb +89 -0
  19. data/lib/spec_producer/producers/registry.rb +66 -0
  20. data/lib/spec_producer/producers/routes_producer.rb +44 -0
  21. data/lib/spec_producer/producers/serializers_producer.rb +46 -0
  22. data/lib/spec_producer/producers/views_producer.rb +25 -0
  23. data/lib/spec_producer/railtie.rb +13 -0
  24. data/lib/spec_producer/rspec_builders.rb +1 -0
  25. data/lib/spec_producer/rspec_builders/base.rb +148 -0
  26. data/lib/spec_producer/rspec_builders/builder.rb +220 -0
  27. data/lib/spec_producer/rspec_builders/matchers.rb +256 -0
  28. data/lib/spec_producer/spec_production_module.rb +80 -331
  29. data/lib/spec_producer/spec_runner.rb +14 -0
  30. data/lib/spec_producer/utils.rb +1 -0
  31. data/lib/spec_producer/utils/file_utils.rb +69 -0
  32. data/lib/spec_producer/version.rb +1 -1
  33. data/lib/tasks/spec_producer_tasks.rake +103 -0
  34. data/spec_producer.gemspec +6 -0
  35. metadata +111 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9dc0df225c68f60eaba14c10ce0df5d1e034a700
4
- data.tar.gz: 154ac15712e0627720352253248b53f55e788544
3
+ metadata.gz: 99eb0473031f263db6636d226b5fccf77a457642
4
+ data.tar.gz: 5659f755c10d008827383066296556a862abb626
5
5
  SHA512:
6
- metadata.gz: c22e5ee62ae1a302e6db6d206e0fbb96df5c34900ddc3ff4761505587c3ea992f85dddebe117d1d28188f8deac07767e3b0fd8a79a8a5301997e34dc70a11c51
7
- data.tar.gz: 2db9ee13638bf2f18ce909e14cb587bfda6c050447e12c5fef095a1fb07abb2a0a936c82424f6732fe645907a42b0cd6e6e47bdcb5e44dcf9aa2443de204f74d
6
+ metadata.gz: db4f51442225f9e5939e6391abfe21abc8622406264781e3058beb44c1b4f488d3aedac6715f4ce202b1545167314d4159f7d135567ead401021f3becd97f9c1
7
+ data.tar.gz: c5619012c248e4c808545bf54ab223be438ae45e7bbbc3eefbb666fc53752ce29660b2cb49f1e713ae7ccc3902ecaceda24b4e0f80e79964ec9014c788029d77
@@ -0,0 +1 @@
1
+ 2.2.5
@@ -1,3 +1,18 @@
1
+ ## 0.13.0 (2017-01-05)
2
+
3
+ Features:
4
+ * Major refactoring on how specs are produced (some previous functionality may not be supported here)
5
+ * Add rake tasks for each functionality supported
6
+
7
+ ## 0.12.0 (2016-11-8)
8
+
9
+ Features:
10
+ * Cover more characteristics through static analysis
11
+ * Update serializer specs
12
+ * Colorized responses
13
+ * Run specs after production
14
+ * Initial attempt for command line runs
15
+
1
16
  ## 0.11.0 (2016-10-18)
2
17
 
3
18
  Features:
data/README.md CHANGED
@@ -3,15 +3,17 @@
3
3
  [![Build Status](https://travis-ci.org/arcanoid/spec_producer.svg?branch=master)](https://travis-ci.org/arcanoid/spec_producer)
4
4
  [![Gem Version](https://badge.fury.io/rb/spec_producer.svg)](https://badge.fury.io/rb/spec_producer)
5
5
 
6
- SpecProducer is a gem that is meant to assist users in skipping the tedious work of creating spec tests for basic
7
- functionality. It reads through the files of the project and prepares some of the basic spec tests for you.
6
+ SpecProducer is a library that is meant to assist users in skipping the tedious work of creating spec tests for basic
7
+ Rails applications. It reads through Active Record subclasses of the project and prepares some of the basic spec tests for you.
8
8
 
9
9
  ## Installation
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'spec_producer'
14
+ group :development do
15
+ gem 'spec_producer'
16
+ end
15
17
  ```
16
18
 
17
19
  And then execute:
@@ -24,83 +26,130 @@ Or install it yourself as:
24
26
 
25
27
  ## Usage
26
28
 
27
- Currently this gem supports the production of spec tests for activemodel Models and routing specs.
28
- If the spec file already exists then it prints what could be its contents.
29
+ Currently this gem supports the production of spec tests for active record models.
30
+ If the spec file already exists then it prints out the contents it would generated
31
+ for a model.
29
32
 
30
- To produce all possible tests, run:
33
+ To produce all possible tests you can run the public API methods directly from a Rails console
34
+ or use the rake tasks provided by the gem (run rake -T | grep spec_producer for all available rake tasks)
35
+ on a rails project.
31
36
 
32
37
  ```ruby
33
- SpecProducer.produce_specs_for_all_types
34
- ```
38
+ # Using a rake task (currently supports producing only model specs)
39
+ bundle exec rake spec_producer:all
35
40
 
36
- To produce all tests for models, run:
41
+ #or produce all tests for models:
37
42
 
38
- ```ruby
39
- SpecProducer.produce_specs_for_models
43
+ bundle exec rake spec_producer:models
40
44
  ```
41
45
 
42
46
  To produce all tests for routes, run:
43
47
 
44
48
  ```ruby
45
- SpecProducer.produce_specs_for_routes
49
+ bundle exec rake spec_producer:routes
46
50
  ```
47
51
 
48
52
  To produce all spec files for views, run:
49
53
 
50
54
  ```ruby
51
- SpecProducer.produce_specs_for_views
55
+ bundle exec rake spec_producer:views
52
56
  ```
53
57
 
54
58
  To produce all spec files for helpers, run:
55
59
 
56
60
  ```ruby
57
- SpecProducer.produce_specs_for_helpers
61
+ bundle exec rake spec_producer:helpers
58
62
  ```
59
63
 
60
64
  To produce all spec files for controllers, run:
61
65
 
62
66
  ```ruby
63
- SpecProducer.produce_specs_for_controllers
67
+ bundle exec rake spec_producer:controllers
68
+ ```
69
+
70
+ To produce all spec files for jobs, run:
71
+
72
+ ```ruby
73
+ bundle exec rake spec_producer:jobs
74
+ ```
75
+
76
+ To produce all spec files for mailers, run:
77
+
78
+ ```ruby
79
+ bundle exec rake spec_producer:mailers
80
+ ```
81
+
82
+ To produce all spec files for serializers, run:
83
+
84
+ ```ruby
85
+ bundle exec rake spec_producer:serializers
86
+ ```
87
+
88
+ In case you have factory_girl gem installed, to produce a list of sample factory files for your models, run:
89
+
90
+ ```ruby
91
+ bundle exec rake spec_producer:factories
64
92
  ```
65
93
 
66
94
  Additionally this gem (from version 0.2.0) allows users to print all their missing spec files by reading all
67
- directories for Views, Models, Controllers and Helpers.
95
+ directories for Views, Models, Controllers, Helpers etc.
68
96
 
69
97
  To print all types of missing tests, run:
70
98
 
71
99
  ```ruby
72
- SpecProducer.print_all_missing_spec_files
100
+ bundle exec rake missing_specs_printer:all
73
101
  ```
74
102
 
75
103
  To print all missing model tests, run:
76
104
 
77
105
  ```ruby
78
- SpecProducer.print_missing_model_specs
106
+ bundle exec rake missing_specs_printer:models
79
107
  ```
80
108
 
81
109
  To print all missing controller tests, run:
82
110
 
83
111
  ```ruby
84
- SpecProducer.print_missing_controller_specs
112
+ bundle exec rake missing_specs_printer:controllers
85
113
  ```
86
114
 
87
115
  To print all missing helper tests, run:
88
116
 
89
117
  ```ruby
90
- SpecProducer.print_missing_helper_specs
118
+ bundle exec rake missing_specs_printer:helpers
91
119
  ```
92
120
 
93
121
  To print all missing view tests, run:
94
122
 
95
123
  ```ruby
96
- SpecProducer.print_missing_view_specs
124
+ bundle exec rake missing_specs_printer:views
97
125
  ```
98
126
 
99
- ## Development
127
+ To print all missing job tests, run:
128
+
129
+ ```ruby
130
+ bundle exec rake missing_specs_printer:jobs
131
+ ```
132
+
133
+ To print all missing mailer tests, run:
134
+
135
+ ```ruby
136
+ bundle exec rake missing_specs_printer:mailers
137
+ ```
100
138
 
101
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
139
+ To print all missing route tests, run:
102
140
 
103
- 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
141
+ ```ruby
142
+ bundle exec rake missing_specs_printer:routes
143
+ ```
144
+
145
+ To print all missing serializer tests, run:
146
+
147
+ ```ruby
148
+ bundle exec rake missing_specs_printer:serializers
149
+ ```
150
+
151
+ ## Development
152
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
104
153
 
105
154
  ## Contributing
106
155
 
@@ -0,0 +1,41 @@
1
+ module SpecProducer
2
+ module Configuration
3
+ CURRENT_ATTRS = [:raise_errors].freeze
4
+ DEPRECATED_ATTRS = [].freeze
5
+ CONFIG_ATTRS = (CURRENT_ATTRS + DEPRECATED_ATTRS).freeze
6
+
7
+ def configure
8
+ return unless block_given?
9
+ yield config
10
+ end
11
+
12
+ def configuration
13
+ config
14
+ end
15
+
16
+ def config
17
+ @config ||= Configuration.new
18
+ end
19
+ private :config
20
+
21
+ class Configuration < Struct.new(*CONFIG_ATTRS)
22
+ def initialize
23
+ super
24
+
25
+ set_default_values
26
+ end
27
+
28
+ def options
29
+ Hash[ * CONFIG_ATTRS.map { |key| [key, send(key)] }]
30
+ end
31
+
32
+ #######
33
+ private
34
+ #######
35
+
36
+ def set_default_values
37
+ self.raise_errors = true
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module SpecProducer
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc "Adds spec_generator in bin/ folder"
5
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
6
+
7
+ def copy_executable
8
+ template "spec_producer", "bin/spec_producer.rb"
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ # Basic bin/generator to add
@@ -1,82 +1,147 @@
1
1
  require "spec_producer/version"
2
+
3
+ require 'spec_producer/railtie'
4
+
2
5
  require "spec_producer/missing_files_module"
6
+ require "spec_producer/missing_gems_module"
3
7
  require "spec_producer/spec_production_module"
4
8
  require "spec_producer/factories_production_module"
5
9
 
10
+ require 'active_support/core_ext/module/delegation'
11
+
12
+ require 'spec_producer/producers'
13
+ require 'spec_producer/rspec_builders'
14
+ require 'spec_producer/spec_runner'
15
+
16
+ require 'configuration'
17
+
6
18
  module SpecProducer
19
+ extend Configuration
20
+ # == Spec \Producer
21
+ # Produces specs for the given type(s)
22
+ #
23
+ # To produce all spec you simply run:
24
+ # SpecProducer.produce
25
+ #
26
+ # You can provide some options to tell the producer to include or exclude some specs
27
+ # Example with <tt>:include</tt> option
28
+ #
29
+ # SpecProducer.produce(only: [:models, :controllers])
30
+ #
31
+ # Example with <tt>exclude</tt> option
32
+ # SpecProducer.produce(except: :views)
33
+ #
34
+ #
35
+ # We can produce specs for a single type by calling
36
+ #
37
+ # SpecProducer.produce_spec(:models)
38
+ #
39
+ #
40
+ # To add a new type of a Producer (Concrete class under spec_producer/producers) for a given type
41
+ # we need to register it in this class. For example if we want to implement ControllersProducer
42
+ # we would register it as follows:
43
+ #
44
+ # register(:models, Producers::ControllersProducer)
45
+ #
46
+ # This gives as the convention to lookup producers using a symbol and later on we can add
47
+ # extra functionality when registering a producer (optional params, init with lambdas etc.)
48
+ #
49
+
50
+ @registry = Producers::Registry.new
51
+
52
+ class << self
53
+ attr_reader :registry
54
+ delegate :run, to: SpecRunner
55
+
56
+ def produce(*args)
57
+ opts = args.extract_options!
58
+ spec_types = registry.types
59
+
60
+ if only = opts[:only]
61
+ spec_types &= Array(only).map(&:to_sym)
62
+ elsif except = opts[:except]
63
+ spec_types -= Array(except).map(&:to_sym)
64
+ end
65
+
66
+ # Produce the specs
67
+ spec_types.each { |t| produce_spec(t) }
68
+
69
+ # Run the specs
70
+ run(spec_types) if opts[:run_specs]
71
+ end
72
+
73
+ def register(type_name, klass)
74
+ registry.register(type_name, klass)
75
+ end
76
+
77
+ # Produces a single spec type. For example
78
+ #
79
+ # SpecProducer.produce_spec(:models)
80
+ #
81
+ # Will fetch and execute the Producers::ModelsSpecProducer
82
+ #
83
+ def produce_spec(spec_type)
84
+ lookup!(spec_type)
85
+ end
86
+
87
+ # Fetches the producer from the registry. Note: that the #lookup method
88
+ # on registry instantiates and executes the producer to start generating
89
+ # the spec files. This execution is hidden in repository and the client
90
+ # is not aware of what is going on underneath. TODO: Refactor this so
91
+ # registry#lookup! does not executes the regitered Producer.
92
+ #
93
+ # If no Producer is Found an ArgumentError exception is thown.
94
+ #
95
+ def lookup!(spec_type)
96
+ registry.lookup!(spec_type)
97
+ end
98
+ end
99
+
100
+ # Register new producers
101
+ register(:models, Producers::ModelsProducer)
102
+ register(:views, Producers::ViewsProducer)
103
+ register(:controllers, Producers::ControllersProducer)
104
+ register(:helpers, Producers::HelpersProducer)
105
+ register(:routes, Producers::RoutesProducer)
106
+ register(:mailers, Producers::MailersProducer)
107
+ register(:jobs, Producers::JobsProducer)
108
+ register(:serializers, Producers::SerializersProducer)
109
+
7
110
  def self.produce_specs_for_all_types
8
- set_up_necessities
9
- SpecProductionModule.produce_specs_for_models
10
111
  SpecProductionModule.produce_specs_for_routes
11
112
  SpecProductionModule.produce_specs_for_views
12
- SpecProductionModule.produce_specs_for_controllers
13
- SpecProductionModule.produce_specs_for_helpers
14
113
  SpecProductionModule.produce_specs_for_mailers
15
114
  SpecProductionModule.produce_specs_for_jobs
16
- SpecProductionModule.produce_specs_for_serializers
17
- end
18
115
 
19
- def self.produce_specs_for_models
20
- SpecProductionModule.produce_specs_for_models
116
+ run_spec_tests
21
117
  end
22
118
 
23
119
  def self.produce_specs_for_routes
24
120
  SpecProductionModule.produce_specs_for_routes
121
+
122
+ run_spec_tests 'routes'
25
123
  end
26
124
 
27
125
  def self.produce_specs_for_views
28
126
  SpecProductionModule.produce_specs_for_views
29
- end
30
127
 
31
- def self.produce_specs_for_controllers
32
- SpecProductionModule.produce_specs_for_controllers
33
- end
34
-
35
- def self.produce_specs_for_helpers
36
- SpecProductionModule.produce_specs_for_helpers
128
+ run_spec_tests 'views'
37
129
  end
38
130
 
39
131
  def self.produce_specs_for_mailers
40
132
  SpecProductionModule.produce_specs_for_mailers
133
+
134
+ run_spec_tests 'mailers'
41
135
  end
42
136
 
43
137
  def self.produce_specs_for_jobs
44
138
  SpecProductionModule.produce_specs_for_jobs
45
- end
46
139
 
47
- def self.produce_specs_for_serializers
48
- SpecProductionModule.produce_specs_for_serializers
140
+ run_spec_tests 'jobs'
49
141
  end
50
142
 
51
143
  def self.set_up_necessities
52
- gemfiles = Dir.glob(Rails.root.join('Gemfile'))
53
-
54
- if gemfiles.size > 0
55
- contents = File.read(gemfiles.first)
56
- gems = contents.scan(/gem \'(?<gem>\S*)\'/).flatten.uniq
57
- missing_gems = []
58
-
59
- missing_gems << 'rspec-rails' unless (gems.include? 'rspec-rails')
60
- missing_gems << 'factory_girl_rails' unless (gems.include? 'factory_girl_rails')
61
- missing_gems << 'shoulda-matchers' unless (gems.include? 'shoulda-matchers')
62
- missing_gems << 'capybara' unless (gems.include? 'capybara')
63
-
64
- if missing_gems.size > 0
65
- contents << "\ngroup :test do\n"
66
-
67
- missing_gems.each do |gem|
68
- contents << " gem '#{gem}'\n"
69
- end
70
-
71
- contents << "end"
72
- end
73
-
74
- f = File.open(gemfiles.first, 'wb+')
75
- f.write(contents)
76
- f.close
77
- else
78
- puts "We couldn't find a Gemfile and setting up halted!"
79
- end
144
+ MissingGemsModule.set_up_necessities
80
145
  end
81
146
 
82
147
  def self.produce_factories
@@ -90,6 +155,8 @@ module SpecProducer
90
155
  MissingFilesModule.print_missing_view_specs(options)
91
156
  MissingFilesModule.print_missing_mailer_specs(options)
92
157
  MissingFilesModule.print_missing_job_specs(options)
158
+ MissingFilesModule.print_missing_serializer_specs(options)
159
+ MissingFilesModule.print_missing_route_specs(options)
93
160
  end
94
161
 
95
162
  def self.print_missing_model_specs(options = {})
@@ -115,4 +182,30 @@ module SpecProducer
115
182
  def self.print_missing_job_specs(options = {})
116
183
  MissingFilesModule.print_missing_job_specs(options)
117
184
  end
118
- end
185
+
186
+ def self.print_missing_serializer_specs(options = {})
187
+ MissingFilesModule.print_missing_serializer_specs(options)
188
+ end
189
+
190
+ def self.print_missing_route_specs(options = {})
191
+ MissingFilesModule.print_missing_route_specs(options)
192
+ end
193
+
194
+ def self.run_spec_tests(type = nil)
195
+ puts "\nRunning related spec tests...\n"
196
+
197
+ if type.nil?
198
+ system 'bundle exec rake'
199
+ else
200
+ command = case type
201
+ when 'views' then 'bundle exec rspec spec/views'
202
+ when 'mailers' then 'bundle exec rspec spec/mailers'
203
+ when 'jobs' then 'bundle exec rspec spec/jobs'
204
+ when 'routes' then 'bundle exec rspec spec/routes'
205
+ else 'bundle exec rspec'
206
+ end
207
+
208
+ system command
209
+ end
210
+ end
211
+ end