Class: Brut::CLI::Apps::Test::Audit

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/test.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #env, #env_vars, #execute, #name, #options, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#default_rack_envObject



123
# File 'lib/brut/cli/apps/test.rb', line 123

def default_rack_env = "development"

#descriptionObject



124
# File 'lib/brut/cli/apps/test.rb', line 124

def description = "Audits all of the app's classes to see if test files exist"

#optsObject



126
127
128
129
130
# File 'lib/brut/cli/apps/test.rb', line 126

def opts = [
  [ "--ignore PATH[,PATH]","Ignore any files in these paths, relative to app root",Array ],
  [ "--type TYPE","Only audit this type of file" ],
  [ "--show-scaffold","If set, shows the command to scaffold the missing tests" ],
]

#runObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/brut/cli/apps/test.rb', line 132

def run
  app_files = Dir["#{Brut.container.app_src_dir}/**/*"].select { |file|
    if file.start_with?(Brut.container.app_specs_dir.to_s)
      false
    elsif Pathname(file).extname != ".rb"
      false
    else
      true
    end
  }
  audit = app_files.map { |file|
    Pathname(file)
  }.select { |pathname|
    relative_to_root = pathname.relative_path_from(Brut.container.project_root)
    if options.ignore(default: []).include?(relative_to_root.to_s)
      false
    else
      true
    end
  }.map { |pathname|
    relative = pathname.relative_path_from(Brut.container.app_src_dir)
    test_file = Brut.container.project_root / "specs" / relative.dirname / "#{relative.basename(relative.extname)}.spec.rb"
    hash = {
      source_file: pathname.relative_path_from(Brut.container.project_root),
      test_file: test_file,
      test_expected: true,
    }
    if pathname.fnmatch?( (Brut.container.components_src_dir / "**").to_s )
      if pathname.basename.to_s == "app_component.rb" || 
         pathname.basename.to_s == "custom_element_registration.rb"
        hash[:type] = :infrastructure
        hash[:test_expected] = false
      else
        hash[:type] = :component
      end
    elsif pathname.fnmatch?( (Brut.container.forms_src_dir / "**").to_s )
      if pathname.basename.to_s == "app_form.rb"
        hash[:type] = :infrastructure
      else
        hash[:type] = :form
      end
      hash[:test_expected] = false
    elsif pathname.fnmatch?( (Brut.container.handlers_src_dir / "**").to_s )
      if pathname.basename.to_s == "app_handler.rb"
        hash[:type] = :infrastructure
        hash[:test_expected] = false
      else
        hash[:type] = :handler
      end
    elsif pathname.fnmatch?( (Brut.container.pages_src_dir / "**").to_s )
      if pathname.basename.to_s == "app_page.rb"
        hash[:type] = :infrastructure
        hash[:test_expected] = false
      else
        hash[:type] = :page
      end
    elsif pathname.fnmatch?( (Brut.container.back_end_src_dir / "**").to_s )
      type = pathname.parent.basename.to_s
      if pathname.basename.to_s == "app_#{type}.rb" ||
         type == "back_end" ||
         type == "seed" ||
         type == "migrations" ||
         type == "segments" ||
         pathname.basename.to_s == "app_data_model.rb" ||
         pathname.basename.to_s == "app_job.rb" ||
         pathname.basename.to_s == "db.rb"

        hash[:type] = :infrastructure
        hash[:test_expected] = false
      else
        hash[:type] = pathname.relative_path_from(Brut.container.back_end_src_dir).dirname
      end
    else
      hash[:type] = :other
      hash[:test_expected] = false
    end
    hash
  }.compact.sort_by { it[:type].to_s + it[:source_file].to_s }

  files_missing = []
  printed_header = false
  audit.each do |file_audit|
    if !file_audit[:test_file].exist?
      if options.type.nil? || file_audit[:type] == options.type.to_sym
        if file_audit[:test_expected]
          files_missing << file_audit[:source_file]
          if !printed_header
            stdout.puts "These files are missing tests:"
            stdout.puts ""
            stdout.printf "%-25s   %s\n","Type", "Path"
            stdout.puts "-------------------------------------------"
            printed_header = true
          end
          stdout.puts "#{file_audit[:type].to_s.ljust(25)} - #{file_audit[:source_file]}"
        end
      end
    end
  end
  if files_missing.empty?
    stdout.puts "All tests exists!"
    0
  else
    if options.show_scaffold?
      stdout.puts
      files_missing_args = files_missing.map { |file|
        '             "' + Shellwords.escape(file.to_s) + '"'
      }.join(" \\\n")

      stdout.puts "Run this command to generate empty tests:\n\nbrut scaffold test \\\n#{files_missing_args}"
    end
    1
  end
end