Class: GradingChecker::GradingYAMLValidator

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

Constant Summary collapse

VALID_GRADE_TYPES =
%w[pass float integer points].freeze
VALID_SUBGRADE_TYPES =
%w[integer pass boolean float].freeze
DEFAULT_GRADE_TYPE =
"float"
GREEN =
Rainbow(":)").green
RED =
Rainbow(":(").red

Instance Method Summary collapse

Constructor Details

#initialize(file_manager) ⇒ GradingYAMLValidator

Returns a new instance of GradingYAMLValidator.



58
59
60
# File 'lib/grading_checker.rb', line 58

def initialize(file_manager)
    @file_manager = file_manager
end

Instance Method Details

#report_error(message) ⇒ Object



170
171
172
# File 'lib/grading_checker.rb', line 170

def report_error(message)
    puts "#{RED} #{message}"
end

#report_success(message) ⇒ Object



166
167
168
# File 'lib/grading_checker.rb', line 166

def report_success(message)
    puts "#{GREEN} #{message}"
end

#validate_duplicate_submit_names!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/grading_checker.rb', line 76

def validate_duplicate_submit_names!
    submit_names = Hash.new { |hash, key| hash[key] = [] }

    submit_ymls = @file_manager.find_submit_ymls
    submit_ymls.each do |path|
        submit_config = @file_manager.load_yaml(path)
        next unless submit_config.is_a?(Hash) && submit_config.key?("name")

        submit_names[submit_config["name"]] << path.relative_path_from(@file_manager.root_path).to_s
    end

    duplicate_submits = submit_names.select { |_, paths| paths.size > 1 }

    if duplicate_submits.any?
        report_error("duplicate submit names found in submit.yml:")
        duplicate_submits.each do |name, paths|
            puts "  - #{name} appears in: #{paths.join(", ")}"
        end
        exit(1)
    else
        report_success("no duplicate submit names found in #{submit_ymls.size} submit.yml configs")
    end
end

#validate_module_definitions(config) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/grading_checker.rb', line 144

def validate_module_definitions(config)
    return unless config.is_a?(Hash)

    grade_keys = config["grades"]&.keys || []

    config.each do |key, value|
        next unless value.is_a?(Hash) && value.key?("submits")

        unless value["submits"].is_a?(Hash)
            report_error("module definition #{key} has an invalid submits section: it must be a hash.")
            exit(1)
        end

        value["submits"].each_key do |submit_key|
            unless grade_keys.include?(submit_key)
                report_error("module definition #{key} references a non-existent grade: #{submit_key}")
                exit(1)
            end
        end
    end
end

#validate_root_grading_config!Object



62
63
64
65
# File 'lib/grading_checker.rb', line 62

def validate_root_grading_config!
    @root_config = @file_manager.load_yaml(@file_manager.root_path.join("grading.yml"))
    validate_yaml_structure(@root_config, "grading.yml")
end

#validate_section(config, section) {|| ... } ⇒ Object

Yields:

  • ()


134
135
136
137
138
139
140
141
142
# File 'lib/grading_checker.rb', line 134

def validate_section(config, section)
    return unless config.key?(section)

    unless config[section].is_a?(Hash)
        report_error("#{section.capitalize} section must be a hash")
        exit(1)
    end
    yield(config[section]) if block_given?
end

#validate_subdir_configs!Object



67
68
69
70
71
72
73
74
# File 'lib/grading_checker.rb', line 67

def validate_subdir_configs!
    @file_manager.find_grading_ymls.each do |path|
        next if path.dirname == @file_manager.root_path

        merged_config = @file_manager.merge_configs(@root_config, @file_manager.load_yaml(path))
        validate_yaml_structure(merged_config, path.relative_path_from(@file_manager.root_path).to_s)
    end
end

#validate_yaml_structure(config, file_name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/grading_checker.rb', line 100

def validate_yaml_structure(config, file_name)
    validate_section(config, "templates") do |templates|
        templates.each do |key, value|
            unless value.is_a?(Hash) && value.key?("type")
                report_error("template #{key} must be a hash with a 'type' key")
                exit(1)
            end
        end
    end

    validate_section(config, "grades") do |grades|
        grades.each do |grade, details|
            grade_type = details["type"] || DEFAULT_GRADE_TYPE
            unless VALID_GRADE_TYPES.include?(grade_type)
                report_error("grade #{grade} has an invalid type: #{grade_type}. Must be one of #{VALID_GRADE_TYPES.join(", ")}")
                exit(1)
            end
        end
    end

    validate_module_definitions(config)

    validate_section(config, "calculation") do |calculation|
        calculation.each do |calc, details|
            unless details.is_a?(Hash)
                report_error("calculation #{calc} must be a hash of weighted components")
                exit(1)
            end
        end
    end

    report_success(file_name)
end