lib/korinthenkacker/cli.rb in korinthenkacker-0.0.1 vs lib/korinthenkacker/cli.rb in korinthenkacker-0.0.2

- old
+ new

@@ -1,82 +1,87 @@ require 'thor' require_relative 'api' require_relative 'configuration' require_relative 'test_report' +require_relative 'reporters/verbose' +require_relative 'reporters/simple' require_relative 'job_info' +require_relative 'filters/failed' +require_relative 'filters/flaky' module Korinthenkacker class CLI < Thor package_name 'korinthenkacker' desc 'jobs', 'Show all jobs' + method_option :simple, aliases: '-s', type: :boolean, desc: 'simple output formatting' def jobs - puts api.jobs["jobs"].map{ |job| job['name'] } + jobs = api.jobs['jobs'].map { |job| JobInfo.new(job) } + reporter(options.simple).print_jobs_report(jobs) end desc 'builds JOBNAME', 'Display JOBNAME builds and their status' - method_option :limit, :aliases => '-l', :type => :numeric + method_option :limit, aliases: '-l', type: :numeric + method_option :simple, aliases: '-s', type: :boolean, desc: 'simple output formatting' def builds(jobname) reports = test_reports(jobname, options.limit) - output = [] - output << "#\tsuccess\tduration" - reports.each{ |report| - output << "#{report.build}\t#{report.success?}\t#{report.duration}" - } - puts output.join("\n") + reporter(options.simple).print_builds_report(reports) end desc 'failed_scenarios JOBNAME [BUILD]', 'Display JOBNAME failing scenarios' - method_option :limit, :aliases => '-l', :type => :numeric + method_option :limit, aliases: '-l', type: :numeric + method_option :simple, aliases: '-s', type: :boolean, desc: 'simple output formatting' def failed_scenarios(jobname, build=nil) - if build - test_report = TestReport.new(jobname, build, api.test_report(jobname, build)) - print_failed_table_header unless test_report.failed_cases.empty? - test_report.failed_cases.each { |failed_case| - print_scenario_report(build, failed_case) - } - else - reports = test_reports(jobname, options.limit) - failed_build_reports = reports.select{ |report| !report.success? } - print_failed_table_header unless failed_build_reports.empty? - failed_build_reports.each{ |report| - report.failed_cases.each{ |failed_case| - print_scenario_report(report.build, failed_case) - } - } - end + failed_cases = failed_cases_for(jobname, build) + reporter(options.simple).print_scenario_reports(failed_cases) end + desc 'flaky_scenarios JOBNAME', 'Display JOBNAME flaky scenarios' + method_option :limit, aliases: '-l', type: :numeric + method_option :simple, aliases: '-s', type: :boolean, desc: 'simple output formatting' + def flaky_scenarios(jobname) + failed_cases = failed_cases_for(jobname) + flaky_cases = flaky_filter.filter(failed_cases) + reporter(options.simple).print_scenario_reports(flaky_cases) + end + desc 'report [JOBS...]', 'Display failing scenario for jobs' - method_option :limit, :aliases => '-l', :type => :numeric + method_option :limit, aliases: '-l', type: :numeric def report(*jobs) - jobs.each{ |job| + jobs.each do |job| puts job failed_scenarios(job, nil) - } + end end private - def print_failed_table_header - printf("%s\t%-15s\t%s\n", '#', 'duration', 'failed scenario name') + def failed_cases_for(jobname, build=nil) + test_reports = build.nil? ? test_reports(jobname, options.limit) : [test_report(jobname, build)] + failed_filter.filter(test_reports.map(&:cases).flatten) end - def print_scenario_report(build, scenario) - printf("%s\t%-15s\t'%s'\n", build, scenario.duration, scenario.name) + def test_report(jobname, build) + TestReport.new(jobname, build, api.test_report(jobname, build)) end def test_reports(jobname, limit=nil) job_info = JobInfo.new(api.job(jobname)) - builds = options.limit ? job_info.build_numbers.first(options.limit) : job_info.build_numbers - builds.map{ |build| - begin - TestReport.new(jobname, build, api.test_report(jobname, build)) - rescue Exception => ex - nil - end - }.compact + builds = limit ? job_info.build_numbers.first(limit) : job_info.build_numbers + builds.map { |build| test_report(jobname, build) rescue nil }.compact # TODO: proper catching + end + + def reporter(simple=false) + @reporter ||= simple ? Reporters::Simple.new : Reporters::Verbose.new + end + + def failed_filter + @failed_filter ||= Filters::Failed.new + end + + def flaky_filter + @flaky_filter ||= Filters::Flaky.new end def api @api ||= API.new(Configuration.jenkins_url) end