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, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #puts, #stdin, #system!, #theme

Instance Method Details

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

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

#optsObject



137
138
139
140
141
# File 'lib/brut/cli/apps/test.rb', line 137

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



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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/brut/cli/apps/test.rb', line 143

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 }

  rows = []
  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]
          rows << [ file_audit[:type].to_s, file_audit[:source_file].to_s ]
        end
      end
    end
  end
  if rows.empty?
    puts theme.success.render("All tests exists!")
    0
  else
    puts
    puts theme.warning.render("The following source files are missing tests:")
    puts
    table = Lipgloss::Table.new.
      headers(["Type", "Path"]).
      rows(rows).
      style_func(rows: rows.length, columns: 2) { |row,column|
        if row == Lipgloss::Table::HEADER_ROW
          Lipgloss::Style.new.inherit(theme.header).padding_left(1).padding_right(1)
        else
          Lipgloss::Style.new.inherit(theme.none).padding_left(1).padding_right(1)
        end
      }
    puts table.render
    if options.show_scaffold?
      puts
      files_missing_args = rows.map { |(_,file)|
        '                "' + Shellwords.escape(file.to_s) + '"'
      }.join(" \\\n")

      puts theme.subheader.render("Run this command to generate empty tests")
      puts
      puts theme.code.render("  brut scaffold test \\\n#{files_missing_args}")
      puts
    end
    1
  end
end