Skip to content

Commit

Permalink
Merge pull request #94 from cyber-dojo/update-coverage-script
Browse files Browse the repository at this point in the history
Use appropriate image for coverage script
  • Loading branch information
JonJagger authored Nov 4, 2024
2 parents de944ac + 278036c commit f4b1bbe
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
12 changes: 9 additions & 3 deletions bin/check_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ check_args()
check_coverage()
{
check_args "$@"
export $(echo_versioner_env_vars)

local -r TYPE="${1}" # {server|client}
local -r TEST_LOG=test.log
local -r HOST_TEST_DIR="${ROOT_DIR}/test/${TYPE}"
local -r HOST_REPORTS_DIR="${ROOT_DIR}/reports/${TYPE}" # where report json files have been written to
local -r CONTAINER_TMP_DIR=/tmp

exit_non_zero_unless_file_exists "${HOST_REPORTS_DIR}/${TEST_LOG}"
exit_non_zero_unless_file_exists "${HOST_REPORTS_DIR}/test_metrics.json"
exit_non_zero_unless_file_exists "${HOST_REPORTS_DIR}/coverage_metrics.json"
exit_non_zero_unless_file_exists "${HOST_TEST_DIR}/config/check_test_metrics.rb"

set +e
docker run \
--read-only \
--rm \
--entrypoint="" \
--env COVERAGE_ROOT="${CONTAINER_TMP_DIR}" \
Expand All @@ -60,10 +65,11 @@ check_coverage()
--volume ${HOST_REPORTS_DIR}/test_metrics.json:${CONTAINER_TMP_DIR}/test_metrics.json:ro \
--volume ${HOST_REPORTS_DIR}/coverage_metrics.json:${CONTAINER_TMP_DIR}/coverage_metrics.json:ro \
--volume ${HOST_TEST_DIR}/config/check_test_metrics.rb:${CONTAINER_TMP_DIR}/check_test_metrics.rb:ro \
cyberdojo/saver:latest \
sh -c "ruby ${CONTAINER_TMP_DIR}/check_test_metrics.rb"
"${CYBER_DOJO_SAVER_IMAGE}:${CYBER_DOJO_SAVER_TAG}" \
sh -c "ruby ${CONTAINER_TMP_DIR}/check_test_metrics.rb" \
| tee -a "${HOST_REPORTS_DIR}/${TEST_LOG}"

local -r STATUS=$?
local -r STATUS=${PIPESTATUS[0]}
set -e

echo "${TYPE} coverage status == ${STATUS}"
Expand Down
17 changes: 9 additions & 8 deletions test/client/config/check_test_metrics.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# frozen_string_literal: true

# Uses data from two json files:
# - reports/client/test_metrics.json generated in slim_json_reporter.rb by minitest. See id58_test_base.rb
# - reports/client/coverage_metrics.json generated in simplecov_formatter_json.rb by simplecov. See coverage.rb

require 'json'

def coloured(tf)
def coloured(arg)
red = 31
green = 32
colourize(tf ? green : red, tf)
colourize(arg ? green : red, arg)
end

def colourize(code, word)
"\e[#{code}m #{word} \e[0m"
end

def table_data
cov_root = ENV['COVERAGE_ROOT']
stats = JSON.parse(IO.read("#{cov_root}/test_metrics.json"))
cov_root = ENV.fetch('COVERAGE_ROOT')
stats = JSON.parse(File.read("#{cov_root}/test_metrics.json"))

cov_json = JSON.parse(IO.read("#{cov_root}/coverage_metrics.json"))
test_cov = cov_json['groups'][ENV['COVERAGE_TEST_TAB_NAME']]
code_cov = cov_json['groups'][ENV['COVERAGE_CODE_TAB_NAME']]
cov_json = JSON.parse(File.read("#{cov_root}/coverage_metrics.json"))
test_cov = cov_json['groups'][ENV.fetch('COVERAGE_TEST_TAB_NAME')]
code_cov = cov_json['groups'][ENV.fetch('COVERAGE_CODE_TAB_NAME')]

[
[ nil ],
Expand Down Expand Up @@ -52,7 +53,7 @@ def table_data
end
# puts "name=#{name}, value=#{value}, op=#{op}, limit=#{limit}" # debug
result = eval("#{value} #{op} #{limit}")
puts "%s | %s %s %s | %s" % [
puts '%s | %s %s %s | %s' % [
name.rjust(25), value.to_s.rjust(5), " #{op}", limit.to_s.rjust(5), coloured(result)
]
results << result
Expand Down
14 changes: 6 additions & 8 deletions test/client/slim_json_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
require 'minitest/reporters'

class Minitest::Reporters::SlimJsonReporter < Minitest::Reporters::BaseReporter

def report
super
filename = "#{ENV.fetch('COVERAGE_ROOT')}/test_metrics.json"
metrics = {
"total_time": "%.2f" % total_time,
"assertion_count": assertions,
"test_count": count,
"failure_count": failures,
"error_count": errors,
"skip_count": skips
total_time: "%.2f" % total_time,
assertion_count: assertions,
test_count: count,
failure_count: failures,
error_count: errors,
skip_count: skips
}
File.write(filename, JSON.pretty_generate(metrics))
end

end
17 changes: 9 additions & 8 deletions test/server/config/check_test_metrics.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# frozen_string_literal: true

# Uses data from two json files:
# - reports/server/test_metrics.json generated in slim_json_reporter.rb by minitest. See id58_test_base.rb
# - reports/server/coverage_metrics.json generated in simplecov_formatter_json.rb by simplecov. See coverage.rb

require 'json'

def coloured(tf)
def coloured(arg)
red = 31
green = 32
colourize(tf ? green : red, tf)
colourize(arg ? green : red, arg)
end

def colourize(code, word)
"\e[#{code}m #{word} \e[0m"
end

def table_data
cov_root = ENV['COVERAGE_ROOT']
stats = JSON.parse(IO.read("#{cov_root}/test_metrics.json"))
cov_root = ENV.fetch('COVERAGE_ROOT')
stats = JSON.parse(File.read("#{cov_root}/test_metrics.json"))

cov_json = JSON.parse(IO.read("#{cov_root}/coverage_metrics.json"))
test_cov = cov_json['groups'][ENV['COVERAGE_TEST_TAB_NAME']]
code_cov = cov_json['groups'][ENV['COVERAGE_CODE_TAB_NAME']]
cov_json = JSON.parse(File.read("#{cov_root}/coverage_metrics.json"))
test_cov = cov_json['groups'][ENV.fetch('COVERAGE_TEST_TAB_NAME')]
code_cov = cov_json['groups'][ENV.fetch('COVERAGE_CODE_TAB_NAME')]

[
[ nil ],
Expand Down Expand Up @@ -52,7 +53,7 @@ def table_data
end
# puts "name=#{name}, value=#{value}, op=#{op}, limit=#{limit}" # debug
result = eval("#{value} #{op} #{limit}")
puts "%s | %s %s %s | %s" % [
puts '%s | %s %s %s | %s' % [
name.rjust(25), value.to_s.rjust(5), " #{op}", limit.to_s.rjust(5), coloured(result)
]
results << result
Expand Down
14 changes: 6 additions & 8 deletions test/server/slim_json_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
require 'minitest/reporters'

class Minitest::Reporters::SlimJsonReporter < Minitest::Reporters::BaseReporter

def report
super
filename = "#{ENV.fetch('COVERAGE_ROOT')}/test_metrics.json"
metrics = {
"total_time": "%.2f" % total_time,
"assertion_count": assertions,
"test_count": count,
"failure_count": failures,
"error_count": errors,
"skip_count": skips
total_time: "%.2f" % total_time,
assertion_count: assertions,
test_count: count,
failure_count: failures,
error_count: errors,
skip_count: skips
}
File.write(filename, JSON.pretty_generate(metrics))
end

end

0 comments on commit f4b1bbe

Please sign in to comment.