|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Lint: specs that need lax mode must declare `error_mode: lax`. |
| 5 | +# |
| 6 | +# A spec is "lax-dependent" if it parse-fails in the default mode (strict2) but |
| 7 | +# produces its expected output under lax. If it doesn't declare `error_mode: lax` |
| 8 | +# (and doesn't expect a parse error via `errors:`), it is functionally broken: |
| 9 | +# it runs in strict2 and fails for EVERY adapter, and it isn't auto-tagged |
| 10 | +# `lax_parsing` so lax-opt-out adapters can't skip it either. |
| 11 | +# |
| 12 | +# This lint requires the reference `liquid` gem to parse/render templates. |
| 13 | +# |
| 14 | +# Usage: ruby -Ilib scripts/verify_lax_mode_declared.rb |
| 15 | +# Exit non-zero if any violation is found. |
| 16 | + |
| 17 | +require "yaml" |
| 18 | +require "liquid" |
| 19 | + |
| 20 | +module LaxModeDeclaredVerifier |
| 21 | + class << self |
| 22 | + def run |
| 23 | + offenders = [] |
| 24 | + each_spec do |spec| |
| 25 | + next if spec[:lax_applied] # file/suite already applies lax globally |
| 26 | + next if spec[:error_mode] # declares a mode already |
| 27 | + next if expects_parse_error?(spec) # legitimately expects parse failure |
| 28 | + tmpl = spec[:template].to_s |
| 29 | + expected = spec[:expected] |
| 30 | + next if expected.nil? # no expected output to compare |
| 31 | + begin |
| 32 | + Liquid::Template.parse(tmpl, error_mode: :strict2) |
| 33 | + next # parses in strict2 -> not lax-dependent |
| 34 | + rescue Liquid::SyntaxError, StandardError |
| 35 | + # strict2 rejects it -> check lax |
| 36 | + end |
| 37 | + begin |
| 38 | + actual = Liquid::Template.parse(tmpl, error_mode: :lax).render |
| 39 | + next unless actual == expected # lax doesn't match -> different problem |
| 40 | + rescue StandardError |
| 41 | + next # lax also fails -> different problem |
| 42 | + end |
| 43 | + offenders << spec |
| 44 | + end |
| 45 | + |
| 46 | + if offenders.empty? |
| 47 | + puts "OK: all lax-dependent specs declare error_mode: lax." |
| 48 | + return 0 |
| 49 | + end |
| 50 | + |
| 51 | + puts "Found #{offenders.size} lax-dependent spec(s) missing `error_mode: lax`:\n\n" |
| 52 | + offenders.each do |o| |
| 53 | + puts " #{o[:file]}:#{o[:line]} #{o[:name]}" |
| 54 | + puts " #{o[:template].to_s.gsub(/\n/, " ")[0, 80]}" |
| 55 | + puts " fix: add `error_mode: lax` (auto-tags lax_parsing)\n\n" |
| 56 | + end |
| 57 | + 1 |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + def expects_parse_error?(spec) |
| 63 | + errs = spec[:errors] |
| 64 | + return false unless errs.is_a?(Hash) |
| 65 | + errs.key?("parse_error") || errs.key?(:parse_error) |
| 66 | + end |
| 67 | + |
| 68 | + def each_spec |
| 69 | + Dir.glob("specs/**/*.yml").sort.each do |file| |
| 70 | + doc = YAML.unsafe_load(File.read(file)) rescue next |
| 71 | + specs = specs_of(doc) |
| 72 | + next unless specs.is_a?(Array) |
| 73 | + starts = index_block_starts(file) |
| 74 | + lax_applied = file_applies_lax?(file, doc) |
| 75 | + specs.each do |s| |
| 76 | + next unless s.is_a?(Hash) |
| 77 | + yield( |
| 78 | + file: file, |
| 79 | + line: starts[s["name"].to_s], |
| 80 | + name: s["name"], |
| 81 | + template: s["template"], |
| 82 | + expected: s["expected"], |
| 83 | + error_mode: s["error_mode"], |
| 84 | + errors: s["errors"], |
| 85 | + lax_applied: lax_applied, |
| 86 | + ) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + # True if this file (or its suite) applies error_mode: lax globally, so |
| 92 | + # individual specs need not declare it. |
| 93 | + def file_applies_lax?(file, doc) |
| 94 | + meta = doc.is_a?(Hash) ? doc["_metadata"] : nil |
| 95 | + req = meta.is_a?(Hash) ? (meta["required_options"] || {}) : {} |
| 96 | + return true if req["error_mode"].to_s == "lax" |
| 97 | + suite_yml = File.join(File.dirname(file), "suite.yml") |
| 98 | + if File.file?(suite_yml) |
| 99 | + s = YAML.unsafe_load(File.read(suite_yml)) rescue nil |
| 100 | + defaults = s.is_a?(Hash) ? (s["defaults"] || {}) : {} |
| 101 | + return true if defaults["error_mode"].to_s == "lax" |
| 102 | + end |
| 103 | + false |
| 104 | + end |
| 105 | + |
| 106 | + def specs_of(d) |
| 107 | + return d if d.is_a?(Array) |
| 108 | + return d["specs"] || d["tests"] if d.is_a?(Hash) |
| 109 | + nil |
| 110 | + end |
| 111 | + |
| 112 | + def index_block_starts(file) |
| 113 | + map = {} |
| 114 | + lines = File.readlines(file) |
| 115 | + starts = lines.each_index.select { |i| lines[i] =~ /^- [A-Za-z]/ } |
| 116 | + starts.each_with_index do |st, k| |
| 117 | + en = (k + 1 < starts.size) ? starts[k + 1] : lines.size |
| 118 | + (st...en).each do |i| |
| 119 | + if lines[i] =~ /^ name:\s*(.+?)\s*$/ |
| 120 | + map[$1] = st + 1 unless map.key?($1) |
| 121 | + break |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + map |
| 126 | + end |
| 127 | + end |
| 128 | +end |
| 129 | + |
| 130 | +exit LaxModeDeclaredVerifier.run if $PROGRAM_NAME == __FILE__ |
0 commit comments