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


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/clean/tests.rb', line 101

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*\<|$)/
    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>/


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/clean/tests.rb', line 58

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.join(' ')}\n") if files.any?
  errors_count
end

.modularize(class_name, parent = nil) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/clean/tests.rb', line 123

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

118
119
120
121
# File 'lib/clean/tests.rb', line 118

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


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/clean/tests.rb', line 137

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_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_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, 'ActionJob::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