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

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/test.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



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
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
# File 'lib/brut/cli/apps/test.rb', line 108

def execute
  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" ||
         pathname.basename.to_s == "app_data_model.rb" ||
         pathname.basename.to_s == "db.rb"

        hash[:type] = :infrastructure
        hash[:test_expected] = false
      else
        hash[:type] = type.to_sym
      end
    else
      hash[:type] = :other
      hash[:test_expected] = false
    end
    hash
  }.compact

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

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