Skip to content

Keep fixture annotations out of ERB blocks (#345)#353

Open
Halvanhelv wants to merge 1 commit into
drwl:mainfrom
Halvanhelv:fix/erb-fixture-insertion
Open

Keep fixture annotations out of ERB blocks (#345)#353
Halvanhelv wants to merge 1 commit into
drwl:mainfrom
Halvanhelv:fix/erb-fixture-insertion

Conversation

@Halvanhelv

Copy link
Copy Markdown

Summary

Fixes #345.

Schema annotations were being written inside ERB tags in dynamic fixture files. For example, given a fixture that starts with a multi-line ERB block:

<%
  total = 2
%>
<% total.times do |i| %>
user_<%= i %>:
  name: User <%= i %>
<% end %>

running annotaterb models produced:

<%
  total = 2
# == Schema Information
# ...
%>
<% total.times do |i| %>
...

i.e. the annotation landed in the middle of the ERB block, corrupting the file.

Root cause

ERB fixtures cannot be parsed by Psych, so YmlParser evaluated the ERB (ERB.new(input).result) and read Psych's line numbers off the evaluated output. Those line numbers were then used as indices into the original file.

When an ERB tag spans multiple lines, the evaluated output has a different line count than the source, so the offsets no longer line up and the annotation is inserted at the wrong position — inside an ERB tag, or inside a <% ... do %> loop. Evaluating the ERB also executes arbitrary Ruby, which can fail outright.

This affected every ERB fixture, not just files starting with <% — the existing <% 1.upto(100) do |i| %> case also had its annotation written inside the loop.

Fix

Stop evaluating the ERB. Instead derive the content bounds directly from the original lines: the first non-blank, non-comment line is the start, so annotations are written above the ERB body (and after any leading comments), never inside a tag. The result is also idempotent across repeated runs.

Tests

  • New YmlParser spec for a fixture that starts with a multi-line ERB block.
  • New Generator specs covering an ERB fixture that starts with <% and one preceded by a comment.
  • Updated the existing ERB specs in YmlParser and SingleFileAnnotator whose expectations encoded the buggy placement.

Verified these specs fail without the fix and pass with it. Full unit suite is green.

ERB fixtures cannot be parsed by Psych, so the parser used to evaluate
the ERB and read Psych line numbers off the *evaluated* output. Those
line numbers were then used as indices into the *original* file. When an
ERB tag spans multiple lines the offsets no longer line up, so the schema
annotation was written inside an ERB tag (e.g. between `<%` and `%>`, or
inside a `<% ... do %>` loop). Evaluating the ERB also runs arbitrary
code, which could fail outright.

Instead, derive the content bounds straight from the original lines: the
first non-blank, non-comment line is the start, so annotations land above
the ERB body (and after any leading comments), never inside a tag.

Fixes drwl#345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fixture files that start with <% (erb) insert doc inside the erb block

1 participant