Last active
April 8, 2016 18:43
-
-
Save Nephos/c8ab172878149a238c54221063034d5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'singleton' | |
require 'yaml' | |
require 'digest' | |
require 'pry' if ENV["DEBUG"] == "true" | |
class TestSqlAvance | |
include Singleton | |
SUMS = YAML.load_file("moulinette_sql_avance.rb.yml") | |
def test_all | |
result = SUMS.keys.reduce(0) do |total, ex| | |
result = test_ex(ex) | |
max_points = SUMS[ex]["points"] | |
success = result["success"] | |
points = (success ? 1 : 0) * max_points | |
msg = success ? "ok" : ": #{result["msg"]}" | |
STDERR.puts "#{ex} [#{points} / #{max_points}]: #{msg}" | |
total + points | |
end | |
STDERR.print "Final mark: " | |
STDOUT.print result | |
max_mark = SUMS.values.reduce(0) {|l, r| l + r["points"]} | |
STDERR.print " / #{max_mark}\n" | |
end | |
private | |
MAX_FILE_SIZE = 10_000_000 | |
# @param ex_name [String] like "ex_??" | |
# @return [Array] contains {success: Boolean, msg: String} | |
def test_ex(ex_name) | |
raise RuntimeError, "No such exercice known by the moulinette `#{ex_name}`" unless ex = SUMS[ex_name] | |
dir_back = Dir.pwd | |
begin | |
Dir.chdir(ex_name) | |
ex["files"].map do |file, sum| | |
test_file_safe(file, sum) | |
end.reduce do |l, r| | |
{ "success" => l["success"] && r["success"], "msg" => [l["msg"], r["msg"]].join("\n") } | |
end | |
rescue => err | |
raise | |
ensure | |
Dir.chdir(dir_back) | |
end | |
end | |
def test_file(file, sum) | |
f = File.open(file, 'r') | |
if f.size > MAX_FILE_SIZE | |
{"success" => false, "msg" => "File `#{file}` too big"} | |
else | |
student_sum = Digest::MD5.hexdigest(f.read) | |
match = student_sum == sum | |
msg = if match | |
"File `#{file}` match" | |
else | |
"File `#{file}` not match (expected:#{sum}, received:#{student_sum})" | |
end | |
{"success" => match, "msg" => msg} | |
end | |
end | |
def test_file_safe(file, sum) | |
begin | |
test_file(file, sum) | |
rescue => err | |
{"success" => false, "msg" => err.message} | |
end | |
end | |
end | |
if __FILE__ == $0 | |
Dir.chdir(ARGV[0]) if !ARGV.empty? | |
TestSqlAvance.instance.test_all | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment