Class: KnapsackPro::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_pro/report.rb

Class Method Summary collapse

Class Method Details

.create_build_subset(test_files) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/knapsack_pro/report.rb', line 62

def self.create_build_subset(test_files)
  repository_adapter = KnapsackPro::RepositoryAdapterInitiator.call
  test_files = KnapsackPro::Utils.unsymbolize(test_files)
  encrypted_test_files = KnapsackPro::Crypto::Encryptor.call(test_files)
  encrypted_branch = KnapsackPro::Crypto::BranchEncryptor.call(repository_adapter.branch)
  action = KnapsackPro::Client::API::V1::BuildSubsets.create(
    commit_hash: repository_adapter.commit_hash,
    branch: encrypted_branch,
    node_total: KnapsackPro::Config::Env.ci_node_total,
    node_index: KnapsackPro::Config::Env.ci_node_index,
    test_files: encrypted_test_files,
  )
  connection = KnapsackPro::Client::Connection.new(action)
  response = connection.call
  if connection.success?
    raise ArgumentError.new(response) if connection.errors?
    KnapsackPro.logger.debug('Saved time execution report on Knapsack Pro API server.')
  else
    KnapsackPro.logger.warn('Time execution report was not saved on Knapsack Pro API server due to connection problem.')
  end
end

.queue_pathObject



86
87
88
# File 'lib/knapsack_pro/report.rb', line 86

def self.queue_path
  "#{KnapsackPro::Config::TempFiles::TEMP_DIRECTORY_PATH}/queue/#{KnapsackPro::Config::Env.queue_id}"
end

.save(tests = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/knapsack_pro/report.rb', line 5

def self.save(tests = nil)
  if tests.nil?
    tests = KnapsackPro.tracker.to_a
  end

  if tests.empty?
    KnapsackPro.logger.warn("No test files were executed on this CI node.")
    KnapsackPro.logger.debug("When you use knapsack_pro Regular Mode, the reason for no tests executing might be a very narrow tests list. Most likely, you run only tests with a specified tag, and there were fewer test files with the tag than parallel CI nodes.")
  end

  create_build_subset(tests)
end

.save_node_queue_to_api(test_files = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/knapsack_pro/report.rb', line 35

def self.save_node_queue_to_api(test_files = nil)
  if test_files.nil?
    test_files = []
    Dir.glob("#{queue_path}/*.json").each do |file|
      report = JSON.parse(File.read(file))
      test_files += report
    end
  end

  if test_files.empty?
    KnapsackPro.logger.info("No tests were executed because the test queue is empty.")
  end

  measured_test_files = test_files
    .map { |t| t['time_execution'] }
    .select { |time_execution| time_execution != KnapsackPro::Tracker::DEFAULT_TEST_FILE_TIME }

  if test_files.size > 0 && measured_test_files.size == 0
    KnapsackPro.logger.warn("#{test_files.size} test files were executed, but their execution time could not be recorded. This usually happens when one of the following is true:")
    KnapsackPro.logger.warn('- The `.knapsack_pro` directory (or its contents) were removed during the run.')
    KnapsackPro.logger.warn("- The `bind` method was not called in the test runner configuration. See: #{KnapsackPro::Urls::INSTALLATION_GUIDE}")
    KnapsackPro.logger.warn('- The test files did not run any tests because they are empty, contain only pending tests, or have syntax errors.')
  end

  create_build_subset(test_files)
end

.save_subset_queue_to_fileObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/knapsack_pro/report.rb', line 18

def self.save_subset_queue_to_file
  test_files = KnapsackPro.tracker.to_a

  subset_queue_id = KnapsackPro::Config::Env.subset_queue_id

  KnapsackPro::Config::TempFiles.ensure_temp_directory_exists!
  FileUtils.mkdir_p(queue_path)

  subset_queue_file_name = "#{subset_queue_id}.json"
  report_path = File.join(queue_path, subset_queue_file_name)
  report_json = JSON.pretty_generate(test_files)

  File.open(report_path, 'w+') do |f|
    f.write(report_json)
  end
end