Module: Clean::Tests

Defined in:
lib/clean/tests.rb

Class Method Summary collapse

Class Method Details

.check_class_presence(class_name, source) ⇒ Object

Read source to determine if class with given name is present


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/clean/tests.rb', line 149

def check_class_presence(class_name, source)
  # Look for raw
  return true if source =~ /class\ +#{class_name}\ +/

  # Look for clean
  compounds = class_name.split('::')
  if compounds.size > 1
    # TODO: More reliability shoud be appreciable
    compounds.each_with_index do |name, depth|
      return false unless source =~ /^#{'  ' * depth}(module|class)\s+#{name}(\s+|\s*\<|$)/i
    end
    return true
  end

  false
end

.check_class_test(name, log, verbose = true) ⇒ Object

Check Class test for a dir in app/<name> Check mirror test in test/<name>/


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/clean/tests.rb', line 106

def check_class_test(name, log, verbose = true)
  errors_count = 0
  tests_dir = Rails.root.join('test', name)
  files = Dir.glob(tests_dir.join('**', '*_test.rb')).map(&:to_s)
  write_method = "write_#{name.singularize}_test_file".to_sym
  log.write("> Search for #{name}...\n") if verbose
  classes = Clean::Support.send("#{name}_in_file")
  log.write("> Check #{classes.count} #{name}\n")
  classes.each do |class_name|
    class_name = class_name.name unless class_name.is_a?(String)
    log.write("> #{class_name}\n") if verbose
    test_class_name = (class_name + '_test').classify
    file = tests_dir.join((test_class_name + '.rb').underscore)
    file_label = file.relative_path_from(Rails.root).to_s
    if File.exist?(file)
      source = File.read(file)
      source.gsub!(/^\#[^\n]*\n/, '')
      if source.blank?
        Clean::Tests.send(write_method, test_class_name)
        errors_count += 1
        log.write("   > Empty test file has been writed: #{file}\n")
      elsif !Clean::Tests.check_class_presence(test_class_name, source)
        errors_count += 1
        log.write(" - Error: Test file #{file_label} seems to be invalid. Class name #{test_class_name} expected but not found\n")
      end
    else
      errors_count += 1
      log.write(" - Error: Test file #{file_label} is missing\n")
      # Create missing file
      Clean::Tests.send(write_method, test_class_name)
      log.write("   > Test file has been created: #{file_label}\n")
    end
    files.delete(file.to_s)
  end
  files.sort.each do |file|
    errors_count += 1
    log.write(" - Error: Unexpected test file: #{Pathname.new(file).relative_path_from(Rails.root)}\n")
  end
  log.write("   > git rm #{files.map { |f| Pathname.new(f).relative_path_from(Rails.root) } .join(' ')}\n") if files.any?
  errors_count
end

.modularize(class_name, parent = nil) ⇒ Object


171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/clean/tests.rb', line 171

def modularize(class_name, parent = nil)
  modules = class_name.split('::')
  klass = modules.delete_at(-1)
  wrap_module(modules) do |c|
    c << "class #{klass}"
    c << " < #{parent}" if parent
    c << "\n"
    content = ''
    yield content
    c << content.dig
    c << 'end'
  end
end

166
167
168
169
# File 'lib/clean/tests.rb', line 166

def print_stat(name, count)
  count = count[name] if count.is_a?(Hash)
  puts "#{count.to_s.rjust(3, ' ')} errors on #{name}"
end

.wrap_module(mods, &block) ⇒ Object


185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/clean/tests.rb', line 185

def wrap_module(mods, &block)
  mod = mods.shift
  content = ''
  if mods.any?
    content = wrap_module(mods, &block)
  else
    yield content
  end
  return content if mod.nil?
  code = "module #{mod}\n"
  code << content.dig
  code << 'end'
  code
end

.write_concept_test_file(klass) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/clean/tests.rb', line 91

def write_concept_test_file(klass)
  code = ''
  code << "require 'test_helper'\n\n"
  code << modularize(klass, 'ActiveSupport::TestCase') do |c|
    c << "# Add tests here...\n"
  end
  file = Rails.root.join('test', 'concepts', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end

.write_controller_test_file(klass) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/clean/tests.rb', line 4

def write_controller_test_file(klass)
  code = ''
  code << "require 'test_helper'\n"
  code << modularize(klass, 'ActionController::TestCase') do |c|
    c << "test_restfully_all_actions\n"
  end
  file = Rails.root.join('test', 'controllers', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end

.write_exchanger_test_file(klass) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/clean/tests.rb', line 56

def write_exchanger_test_file(klass)
  code = ''
  code << "require 'test_helper'\n\n"
  main_klass = klass.gsub(/Test$/, '')
  code << modularize(klass, 'ActiveExchanger::TestCase') do |c|
    path = Rails.root.join('test', 'fixture-files', 'imports', main_klass.underscore.gsub(/\_exchanger\z/, '') + '.*').to_s
    files = Dir.glob(path)
    extension = 'csv'
    extension = files.first.split('.').last if files.any?
    x = "test 'import' do\n"
    x << "  #{main_klass}.import(fixture_files_path.join('imports', '" + main_klass.underscore.gsub(/\_exchanger\z/, '').split(/\//).join("', '") + ".#{extension}'))\n"
    x << "end\n"
    x.gsub!(/^/, '# ') if files.empty?
    c << x
  end
  file = Rails.root.join('test', 'exchangers', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end

.write_helper_test_file(klass) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/clean/tests.rb', line 30

def write_helper_test_file(klass)
  code = ''
  code << "require 'test_helper'\n\n"
  code << modularize(klass, 'ActionView::TestCase') do |c|
    c << "# Add tests here...\n"
  end
  file = Rails.root.join('test', 'helpers', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end

.write_job_test_file(klass) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/clean/tests.rb', line 43

def write_job_test_file(klass)
  code = ''
  code << "require 'test_helper'\n\n"
  code << modularize(klass, 'ActiveJob::TestCase') do |c|
    c << "# Add tests here...\n"
  end
  file = Rails.root.join('test', 'jobs', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end

.write_model_test_file(klass) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clean/tests.rb', line 17

def write_model_test_file(klass)
  code = ''
  code << "require 'test_helper'\n\n"
  code << modularize(klass, 'ActiveSupport::TestCase') do |c|
    c << "# Add tests here...\n"
  end
  file = Rails.root.join('test', 'models', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end

.write_service_test_file(klass) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/clean/tests.rb', line 78

def write_service_test_file(klass)
  code = ''
  code << "require 'test_helper'\n\n"
  code << modularize(klass, 'ActiveSupport::TestCase') do |c|
    c << "# Add tests here...\n"
  end
  file = Rails.root.join('test', 'services', klass.underscore + '.rb')
  FileUtils.mkdir_p(file.dirname)
  File.open(file, 'wb') do |f|
    f.write(code)
  end
end