spec_producer 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ca6e22c07edbf180a24cea44d49e20766b84f90
4
- data.tar.gz: 32b9e5e6ca66434d6fe562d1d1d8a3ab19f018e7
3
+ metadata.gz: 9dc0df225c68f60eaba14c10ce0df5d1e034a700
4
+ data.tar.gz: 154ac15712e0627720352253248b53f55e788544
5
5
  SHA512:
6
- metadata.gz: 76bb1000583dd9b8da82d325975e491a994346a19308a1954b66fbc294b213af7a09823460cde128e1f71a586a0e7d2b1768806589f9f6e2687d12c2049a9192
7
- data.tar.gz: 326c949ae2375a7c3f6f7c01b89088f1af67a5420ee39199ea1e533b98629b5731bca92888c8342d7032e584b5c8f9175e79b474632bce5d658d4e17ca7aa853
6
+ metadata.gz: c22e5ee62ae1a302e6db6d206e0fbb96df5c34900ddc3ff4761505587c3ea992f85dddebe117d1d28188f8deac07767e3b0fd8a79a8a5301997e34dc70a11c51
7
+ data.tar.gz: 2db9ee13638bf2f18ce909e14cb587bfda6c050447e12c5fef095a1fb07abb2a0a936c82424f6732fe645907a42b0cd6e6e47bdcb5e44dcf9aa2443de204f74d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.11.0 (2016-10-18)
2
+
3
+ Features:
4
+ * Add serializer specs production
5
+ * Fixes in view specs
6
+ * Handle missing Delayed Jobssetup
7
+ * Update helper specs to include helper methods
8
+ * Logs in name error now describe the error as well.
9
+
1
10
  ## 0.10.0 (2016-10-07)
2
11
 
3
12
  Features:
data/lib/spec_producer.rb CHANGED
@@ -13,6 +13,7 @@ module SpecProducer
13
13
  SpecProductionModule.produce_specs_for_helpers
14
14
  SpecProductionModule.produce_specs_for_mailers
15
15
  SpecProductionModule.produce_specs_for_jobs
16
+ SpecProductionModule.produce_specs_for_serializers
16
17
  end
17
18
 
18
19
  def self.produce_specs_for_models
@@ -43,6 +44,10 @@ module SpecProducer
43
44
  SpecProductionModule.produce_specs_for_jobs
44
45
  end
45
46
 
47
+ def self.produce_specs_for_serializers
48
+ SpecProductionModule.produce_specs_for_serializers
49
+ end
50
+
46
51
  def self.set_up_necessities
47
52
  gemfiles = Dir.glob(Rails.root.join('Gemfile'))
48
53
 
@@ -75,62 +80,6 @@ module SpecProducer
75
80
  end
76
81
 
77
82
  def self.produce_factories
78
- Dir.glob(Rails.root.join('app/models/*.rb')).each do |x|
79
- require x
80
- end
81
-
82
- not_valid_descendants = [ ActiveRecord::SchemaMigration, Delayed::Backend::ActiveRecord::Job ]
83
-
84
- ActiveRecord::Base.descendants.reject { |descendant| not_valid_descendants.include? descendant }.each do |descendant|
85
- final_text = "FactoryGirl.define do\n"
86
- final_text << " factory :#{descendant.name.underscore}, :class => #{descendant.name} do\n"
87
-
88
- descendant.columns.each do |column|
89
- value = case column.type
90
- when :string then "'#{descendant.name.underscore.upcase}_#{column.name.underscore.upcase}'"
91
- when :text then "'#{descendant.name.underscore.upcase}_#{column.name.underscore.upcase}'"
92
- when :integer then "#{rand(1...10)}"
93
- when :decimal then "#{rand(1.0...100.0)}"
94
- when :float then "#{rand(1.0...100.0)}"
95
- when :datetime then "'#{DateTime.now + 365}'"
96
- when :time then "'#{Time.now + 365*24*60*60}'"
97
- when :date then "'#{Date.today + 365}'"
98
- when :boolean then "#{rand(2) == 1 ? true : false}"
99
- when :binary then "#{5.times.collect { rand(0..1).to_s }.join('')}"
100
- end
101
-
102
- final_text << " #{column.name} #{value}\n"
103
- end
104
-
105
- final_text << " end\n"
106
- final_text << "end"
107
-
108
- if File.exists?(Rails.root.join("spec/factories/#{descendant.name.underscore}.rb"))
109
- puts '#'*100
110
- puts "Please, check whether the following lines are included in: spec/factories/" + descendant.name.underscore + ".rb\n"
111
- puts '#'*100
112
- puts "\n"
113
- puts final_text
114
- else
115
- unless Dir.exists? Rails.root.join("spec")
116
- puts "Generating spec directory"
117
- Dir.mkdir(Rails.root.join("spec"))
118
- end
119
-
120
- unless Dir.exists? Rails.root.join("spec/factories")
121
- puts "Generating spec/factories directory"
122
- Dir.mkdir(Rails.root.join("spec/factories"))
123
- end
124
-
125
- path = "spec/factories/#{descendant.name.underscore}.rb"
126
- puts "Producing factory file for: #{path}"
127
- f = File.open("#{Rails.root.join(path)}", 'wb+')
128
- f.write(final_text)
129
- f.close
130
- end
131
- end
132
-
133
- nil
134
83
  FactoriesProductionModule.produce_factories
135
84
  end
136
85
 
@@ -3,8 +3,12 @@ module SpecProducer::FactoriesProductionModule
3
3
  Dir.glob(Rails.root.join('app/models/*.rb')).each do |x|
4
4
  require x
5
5
  end
6
-
7
- not_valid_descendants = [ ActiveRecord::SchemaMigration, Delayed::Backend::ActiveRecord::Job ]
6
+
7
+ not_valid_descendants = [ ActiveRecord::SchemaMigration ]
8
+
9
+ if Object.const_defined?('Delayed')
10
+ not_valid_descendants << Delayed::Backend::ActiveRecord::Job
11
+ end
8
12
 
9
13
  ActiveRecord::Base.descendants.reject { |descendant| not_valid_descendants.include? descendant }.each do |descendant|
10
14
  final_text = "FactoryGirl.define do\n"
@@ -56,8 +60,8 @@ module SpecProducer::FactoriesProductionModule
56
60
  end
57
61
 
58
62
  nil
59
- rescue NameError
60
- puts "ActiveRecord is not set for this project. Can't produce factories for this project."
63
+ rescue NameError => e
64
+ puts "NameError '#{e}' was raised. Can't produce factories for this project."
61
65
  rescue Exception => e
62
66
  puts "Exception '#{e}' was raised. Skipping factories production."
63
67
  end
@@ -211,6 +211,7 @@ module SpecProducer::SpecProductionModule
211
211
  objects_in_file = file_content.scan(/@(?<field>[a-zA-Z_]*)/).flatten.uniq
212
212
  templates_in_file = file_content.scan(/render ('|")(?<template>\S*)('|")/).flatten.uniq
213
213
  partials_in_file = file_content.scan(/render :partial => ('|")(?<partial>\S*)('|")/).flatten.uniq
214
+ links_in_file = file_content.scan(/<a.*href=\"(\S*)\".*>(.*)<\/a>/).uniq
214
215
 
215
216
  file_name = "#{file.gsub('app/', 'spec/')}_spec.rb"
216
217
  final_text = "require '#{require_helper_string}'\n\n"
@@ -231,27 +232,29 @@ module SpecProducer::SpecProductionModule
231
232
  final_text << " render\n"
232
233
  final_text << " end\n\n"
233
234
 
234
- if fields_in_file.size > 0 || templates_in_file.size > 0 || partials_in_file.size > 0
235
- final_text << " describe 'content' do\n"
235
+ final_text << " describe 'content' do\n"
236
236
 
237
- fields_in_file.each do |field_name|
238
- final_text << " it { should have_field '#{field_name }' }\n"
239
- end
240
-
241
- templates_in_file.each do |template_name|
242
- final_text << " it { should render_template '#{template_name }' }\n"
243
- end
237
+ fields_in_file.each do |field_name|
238
+ final_text << " it { should have_field '#{ field_name }' }\n"
239
+ end
244
240
 
245
- partials_in_file.each do |partial_name|
246
- final_text << " it { should render_template(:partial => '#{partial_name }') }\n"
247
- end
241
+ templates_in_file.each do |template_name|
242
+ template_path_elements = template_name.split('/')
243
+ template_path_elements.last.gsub!(/^/, '_')
248
244
 
249
- final_text << " pending 'view content test'\n"
250
- final_text << " end\n"
251
- else
252
- final_text << " pending 'view content test'\n"
245
+ final_text << " it { should render_template '#{ template_path_elements.join('/') }' }\n"
253
246
  end
254
-
247
+
248
+ partials_in_file.each do |partial_name|
249
+ final_text << " it { should render_template(:partial => '#{ partial_name }') }\n"
250
+ end
251
+
252
+ links_in_file.each do |link|
253
+ final_text << " it { should have_link '#{link[1]}', :href => '#{link[0]}' }\n"
254
+ end
255
+
256
+ final_text << " pending 'view content test'\n"
257
+ final_text << " end\n"
255
258
  final_text << "end\n"
256
259
 
257
260
  check_if_spec_folder_exists
@@ -280,11 +283,12 @@ module SpecProducer::SpecProductionModule
280
283
  end
281
284
 
282
285
  def self.produce_specs_for_helpers
283
- files_list = Dir["app/helpers/**/*.rb"]
286
+ helpers_list = ActionController::Base.modules_for_helpers(ActionController::Base.all_helpers_from_path 'app/helpers')
284
287
 
285
- files_list.each do |file|
288
+ helpers_list.each do |helper|
289
+ file_name = "spec/helpers/#{helper.name.gsub("::", "/").underscore}_spec.rb"
286
290
  full_path = 'spec'
287
- File.dirname(file.gsub('app/', 'spec/')).split('/').reject { |path| path == 'spec' }.each do |path|
291
+ file_name.gsub("spec/", "").split("/").each do |path|
288
292
  unless /.*\.rb/.match path
289
293
  full_path << "/#{path}"
290
294
 
@@ -294,10 +298,12 @@ module SpecProducer::SpecProductionModule
294
298
  end
295
299
  end
296
300
 
297
- file_name = "#{file.gsub('app/', 'spec/').gsub('.rb', '')}_spec.rb"
298
301
  final_text = "require '#{require_helper_string}'\n\n"
299
- final_text << "describe #{file.gsub('app/helpers/', '').gsub('.rb', '').split('/').map { |x| x.camelcase }.join('::')}, :type => :helper do\n"
300
- final_text << " pending 'view helper tests'\n"
302
+ final_text << "describe #{helper}, :type => :helper do\n"
303
+ helper.instance_methods.each do |method_name|
304
+ final_text << " pending '#{method_name.to_s}'\n"
305
+ end
306
+
301
307
  final_text << "end"
302
308
 
303
309
  check_if_spec_folder_exists
@@ -405,7 +411,7 @@ module SpecProducer::SpecProductionModule
405
411
  puts final_text
406
412
  end
407
413
  else
408
- puts "Producing helper spec file for: #{file_name}"
414
+ puts "Producing job spec file for: #{file_name}"
409
415
  f = File.open(file_name, 'wb+')
410
416
  f.write(final_text)
411
417
  f.close
@@ -417,6 +423,56 @@ module SpecProducer::SpecProductionModule
417
423
  puts "Exception '#{e}' was raised. Skipping job specs production."
418
424
  end
419
425
 
426
+ def self.produce_specs_for_serializers
427
+ Dir.glob(Rails.root.join('app/serializers/*.rb')).each do |x|
428
+ require x
429
+ end
430
+
431
+ not_valid_descendants = [ ActiveModel::Serializer::ErrorSerializer ]
432
+
433
+ ActiveModel::Serializer.descendants.reject { |descendant| not_valid_descendants.include? descendant }.each do |descendant|
434
+ final_text = "require '#{require_helper_string}'\n\n"
435
+ final_text << "describe #{descendant.name}, :type => :serializer do\n"
436
+
437
+ final_text << " describe 'serializer tests' do\n"
438
+ final_text << " subject { #{descendant.name}.new(FactoryGirl.build(:#{descendant.name.underscore.gsub('_serializer', '')})) }\n\n"
439
+ final_text << " it 'includes the expected attributes' do\n"
440
+ final_text << " expect(subject.attributes.keys).to contain_exactly(#{descendant._attributes.map { |x| ":#{x.to_s}" }.join(', ')})\n"
441
+ final_text << " expect(subject.attributes).to eq({})\n"
442
+ final_text << " end\n"
443
+ final_text << " end\n"
444
+ final_text << "end"
445
+
446
+ if File.exists?(Rails.root.join("spec/serializers/#{descendant.name.underscore}_spec.rb"))
447
+ if File.open(Rails.root.join("spec/serializers/#{descendant.name.underscore}_spec.rb")).read == final_text
448
+ # nothing to do here, pre-existing content is the same :)
449
+ else
450
+ puts '#'*100
451
+ puts "Please, check whether the following lines are included in: " + descendant.name.underscore + "_spec.rb\n"
452
+ puts '#'*100
453
+ puts "\n"
454
+ puts final_text
455
+ end
456
+ else
457
+ check_if_spec_folder_exists
458
+
459
+ unless Dir.exists? Rails.root.join("spec/serializers")
460
+ puts "Generating spec/serializers directory"
461
+ Dir.mkdir(Rails.root.join("spec/serializers"))
462
+ end
463
+
464
+ path = "spec/serializers/#{descendant.name.underscore}_spec.rb"
465
+ puts "Producing serializer spec file for: #{path}"
466
+ f = File.open("#{Rails.root.join(path)}", 'wb+')
467
+ f.write(final_text)
468
+ f.close
469
+ end
470
+ end
471
+
472
+ nil
473
+ rescue Exception => e
474
+ puts "Exception '#{e}' was raised. Skipping serializer specs production."
475
+ end
420
476
 
421
477
  def self.produce_specs_for_controllers
422
478
  Dir.glob(Rails.root.join('app/controllers/**/*.rb')).each do |x|
@@ -1,3 +1,3 @@
1
1
  module SpecProducer
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spec_producer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasilis Kalligas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-06 00:00:00.000000000 Z
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler