Module: Pray::Verify
- Defined in:
- lib/pray/verify.rb
Class Method Summary collapse
- .collect_verification_report(project, lockfile) ⇒ Object
- .drift_project(project, lockfile) ⇒ Object
- .drift_section_for_kind(kind) ⇒ Object
- .find_orphan_marker_findings(spans, markers, target_path) ⇒ Object
- .format_drift_report(report) ⇒ Object
- .format_verification_report(report) ⇒ Object
- .inspect_project(project, lockfile) ⇒ Object
- .lockfile_targets(lockfile) ⇒ Object
- .marker_positions(lines) ⇒ Object
- .parse_marker(line) ⇒ Object
- .verify_project(project, lockfile, strict: false) ⇒ Object
Class Method Details
.collect_verification_report(project, lockfile) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/pray/verify.rb', line 64 def collect_verification_report(project, lockfile) report = VerificationReport.new rendered_targets = {} fresh_targets = Render.render_project(project).to_h { |target| [target.path, target.content] } if project.manifest_hash != lockfile.manifest_hash report.findings << VerificationFinding.new( kind: "verify_error", message: "Prayfile changed since `Prayfile.lock` was generated. Run `pray install` to refresh the lockfile." ) end locked_packages = lockfile.package.to_h { |entry| [entry.name, entry] } project.packages.each do |package| locked = locked_packages.delete(package.declaration.name) if locked if locked.tree_hash != package.tree_hash report.findings << VerificationFinding.new( kind: "package_integrity", message: "Package `#{package.declaration.name}` no longer matches the locked tree hash. Run `pray install` to re-resolve packages." ) end if locked.version != package.spec.version report.findings << VerificationFinding.new( kind: "verify_error", message: "Package `#{package.declaration.name}` resolved to version #{package.spec.version} but `Prayfile.lock` has #{locked.version}. Run `pray install` to refresh the lockfile." ) end else report.findings << VerificationFinding.new( kind: "verify_error", message: "Package `#{package.declaration.name}` is declared in Prayfile but missing from `Prayfile.lock`. Run `pray install` to update the lockfile." ) end end locked_packages.each_value do |locked| report.findings << VerificationFinding.new( kind: "verify_error", message: "Package `#{locked.name}` is in `Prayfile.lock` but not declared in Prayfile. Remove it from the lockfile with `pray install` or add it back to Prayfile." ) end target_spans = lockfile.managed_span.group_by(&:target) target_spans.each do |target_path, spans| absolute_path = File.join(project.project_root, target_path) unless File.exist?(absolute_path) report.findings << VerificationFinding.new( kind: "verify_error", message: "Rendered file `#{target_path}` is missing. Run `pray install` to generate it." ) next end text = File.read(absolute_path) rendered_targets[target_path] = text lines = text.lines(chomp: true) markers = marker_positions(lines) spans.each do |span| marker = markers[span.id] if marker.nil? report.findings << VerificationFinding.new( kind: "removed_prayer", message: "`#{target_path}` is missing managed marker `#{span.id}` for `#{span.package}::#{span.export}`. Run `pray install` to restore the managed span." ) else _open_line, _close_line, checksum = marker if checksum != span.ideal_checksum report.findings << VerificationFinding.new( kind: "custom_implementation", message: "`#{target_path}` marker `#{span.id}` (`#{span.package}::#{span.export}`) was edited. Restore the managed block or run `pray install` to regenerate it." ) end end end fresh_lines = fresh_targets[target_path]&.lines(chomp: true) summary = VerifyPosition.summarize_position_drift( target_path, spans, markers, lines, fresh_lines, project.local_files ) if summary report.findings << VerificationFinding.new( kind: "position_drift", message: VerifyPosition.(summary) ) end find_orphan_marker_findings(spans, markers, target_path).each do |finding| report.findings << finding end end project.local_files.each do |local| next if local.optional next if File.exist?(local.path) report.findings << VerificationFinding.new( kind: "verify_error", message: Resolve.(local.manifest_path) ) end [report, rendered_targets, fresh_targets] end |
.drift_project(project, lockfile) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/pray/verify.rb', line 37 def drift_project(project, lockfile) report, rendered_targets, fresh_targets = collect_verification_report(project, lockfile) fresh_targets.each do |path, fresh_content| normalized_fresh = Hashing.normalize_line_endings(fresh_content) on_disk = rendered_targets[path] on_disk = Hashing.normalize_line_endings(on_disk) if on_disk unless on_disk == normalized_fresh report.findings << VerificationFinding.new( kind: "renderer_drift", message: "#{path} differs from fresh render" ) end unless lockfile_targets(lockfile).include?(path) report.findings << VerificationFinding.new( kind: "renderer_drift", message: "#{path} is not tracked in lockfile" ) end end if report.clean? report else raise Error.verify(format_drift_report(report)) end end |
.drift_section_for_kind(kind) ⇒ Object
257 258 259 260 261 262 263 264 265 |
# File 'lib/pray/verify.rb', line 257 def drift_section_for_kind(kind) case kind when "verify_error" then "Lockfile changes" when "package_integrity" then "Package changes" when "custom_implementation", "removed_prayer", "position_drift", "orphan_marker" then "Managed span changes" when "renderer_drift" then "Rendered file changes" else "Warnings" end end |
.find_orphan_marker_findings(spans, markers, target_path) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/pray/verify.rb', line 171 def find_orphan_marker_findings(spans, markers, target_path) tracked_ids = spans.to_h { |span| [span.id, true] } markers.keys.filter_map do |marker_id| next if marker_id == "0" || tracked_ids[marker_id] VerificationFinding.new( kind: "orphan_marker", message: "`#{target_path}` contains marker `#{marker_id}` that is not tracked in `Prayfile.lock`. Remove the marker or run `pray install` to reconcile." ) end end |
.format_drift_report(report) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/pray/verify.rb', line 229 def format_drift_report(report) sections = {} report.findings.each do |finding| section = drift_section_for_kind(finding.kind) sections[section] ||= [] sections[section] << finding end ordered = [ "Lockfile changes", "Package changes", "Managed span changes", "Rendered file changes", "Warnings" ] lines = [] ordered.each do |section| findings = sections[section] next unless findings lines << section findings.each do |finding| lines << " #{finding.kind}: #{finding.}" end end lines.join("\n") end |
.format_verification_report(report) ⇒ Object
225 226 227 |
# File 'lib/pray/verify.rb', line 225 def format_verification_report(report) report.findings.map { |finding| "#{finding.kind}: #{finding.}" }.join("\n") end |
.inspect_project(project, lockfile) ⇒ Object
22 23 24 |
# File 'lib/pray/verify.rb', line 22 def inspect_project(project, lockfile) collect_verification_report(project, lockfile)[0] end |
.lockfile_targets(lockfile) ⇒ Object
221 222 223 |
# File 'lib/pray/verify.rb', line 221 def lockfile_targets(lockfile) lockfile.target.flat_map(&:outputs) end |
.marker_positions(lines) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/pray/verify.rb', line 183 def marker_positions(lines) markers = {} active = nil lines.each_with_index do |line, index| parsed = parse_marker(line) if parsed.nil? active[2] << line if active next end case parsed when :ignore next else identifier = parsed if active.nil? active = [identifier, index + 1, []] elsif active[0] == identifier checksum = Hashing.checksum_managed_body_line_refs(active[2]) markers[active[0]] = [active[1], index + 1, checksum] active = nil end end end markers end |
.parse_marker(line) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/pray/verify.rb', line 209 def parse_marker(line) trimmed = line.strip remainder = trimmed.delete_prefix("<!--").strip return nil unless remainder.start_with?("pray:") content = remainder.delete_prefix("pray:").strip.delete_suffix("-->").strip return :ignore if content == "0 ignore-comments" return content if content.match?(/\A[a-z0-9]+\z/) nil end |
.verify_project(project, lockfile, strict: false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pray/verify.rb', line 26 def verify_project(project, lockfile, strict: false) report = inspect_project(project, lockfile) return report if report.clean? if strict || report.errors? raise Error.verify(format_verification_report(report)) end report end |