require 'fileutils' require 'rspec/core/rake_task' module Henry class Task # The Henry Task implementation for Rspec class RspecTask < RakeTask attr_accessor :generated_reports, :report_recipients # The temporary output file path for the RspecTask execution. OUT_PATH = 'rspec.out' # The reports path template. REPORTS_DIR = '.henry/reports/${FORMAT}' # Default time format to be used in the reports filepath. TIME_FORMAT = '%Y-%m-%dT%H%M%S' # The Rspec Rake Application name. # # @return [String] the rake application name. APPLICATION_NAME = 'spec' def application_name APPLICATION_NAME end def out_path OUT_PATH end def after_execute if !(self.generated_reports || []).empty? && !(self.report_recipients || []).empty? tgz_path = ".henry/#{DateTime.now.strftime(TIME_FORMAT)}_#{self.name}.tgz" `tar czf #{tgz_path} #{self.generated_reports.join(' ')}` Henry::EmailClient.send_file(tgz_path, self.report_recipients.uniq, "[Henry Reports] #{self.name}") `rm #{tgz_path}` end super end # Configures the Task. # # @param [Hash] params the task params. # @param [Hash] extended_context the task extended context. def configure(params, extended_context={}) File.open(OUT_PATH, 'w') { |f| } # Makes available the spec rake task. Rake.application.clear RSpec::Core::RakeTask.new do |t| if self.data.options t.pattern = self.data.options['pattern'] || 'spec/*' t.rspec_opts = "#{self.custom_options} #{extended_context['options']}" else t.pattern = 'spec/*' t.rspec_opts = "#{self.default_options} #{extended_context['options']}" end end super end protected # Returns the custom rspec_opts that user may have passed. # # @return [String] def custom_options "#{self.format_options} #{self.tags_options} #{self.report_options}" end # Returns the default rspec_opts. # # @return [String] def default_options "--color --format documentation --format documentation --out #{OUT_PATH}" end # Returns the rspec_opts related with formatting. # # @return [String] def format_options "--color --format #{self.data.options['format'] || 'documentation'} --format #{self.data.options['format'] || 'documentation'} --out #{OUT_PATH}" end # Returns the rspec_opts related with tags to be run. # # @return [String] def tags_options return '' if self.data.options['tags'].nil? self.data.options['tags'].collect do |tag| "--tag #{tag}" end.join(' ') end # Returns the rspec_opts related with report paaths and formats. # # @return [String] def report_options self.generated_reports = [] self.report_recipients = [] return '' if self.data.reports.nil? self.data.reports.collect do |report_options| report_options['name'] ||= "${DATE}_${TASK_NAME}.#{report_options['format']}" self.generated_reports << self.report_file_path(report_options['format'], report_options['name']) self.report_recipients += report_options['recipients'] FileUtils.mkdir_p(self.reports_dir(report_options['format'])) "--format #{report_options['format']} --out #{self.report_file_path(report_options['format'], report_options['name'])}" end.join(' ') end # Returns the report file path for the given format and file name. # # @param [String] format the rspec formatter name. # @param [String] file_name the report file name template. # @return [String] the report file path. def report_file_path(format, file_name) "#{self.reports_dir(format)}/#{self.report_file_name(file_name)}" end # Interpolates and returns the report file name. # # @param [String] file_name the report file name. # @returns [String] the report file name. def report_file_name(file_name) file_name.gsub(/\${[A-Z_]+}/, '${TASK_NAME}' => self.name, '${DATE}' => DateTime.now.strftime(TIME_FORMAT)).gsub(' ', '_') end # Interpolates and returns the reports directory for the given format. # # @param [String] format the formatter name. # @return [Stiring] the reports directory. def reports_dir(format) REPORTS_DIR.gsub(/\${[A-Z_]+}/, '${FORMAT}' => format).gsub(' ', '_') end end end end