Module: Mutineer::Migrate

Defined in:
lib/mutineer/migrate.rb

Overview

Rewrites old-format mutant ids in a .mutineer.yml ignore: list to the current id format. The map is the inverse of the id_map and dropped_id_map from JobPlan.collect_jobs: each old id goes to every new id that still hashes to it, reported ids first, each group in collection order. A dropped repeat of an edit is not reported, but its id is still current (#275). The map covers only the configured operators and the sources on the command line. Ids are static hashes, so this runs no tests.

The file is edited as text. A YAML load and dump would drop comments and reorder keys. Only a bare id inside ignore: is replaced. A flow list may span lines. A scalar changes only when its whole value is an id. A block scalar under ignore: raises ArgumentError. In a { id:, reason: } mapping the id is replaced in place: - id: x lines, - { id: x } items, and { id: x } entries inside ignore: [...]. Comments and reason text are never touched. An old id that now matches several mutants cannot fit one mapping, so it is left in place and reported for a hand edit. A baseline file is never read.

Defined Under Namespace

Classes: Outcome, Replacement

Constant Summary collapse

ITEM =

A block-list item whose value is one bare id.

/\A([ \t]*-[ \t]+)(['"]?)([0-9a-f]{12})\2([ \t]*(?:\#.*)?)(\r?\n)?\z/
MAPPING_ID_LINE =

A block-mapping line whose whole value is the id: key: - id: x or an indented id: x. Comments and reason text never match.

/\A([ \t]*(?:-[ \t]+)?)(id[ \t]*:[ \t]*)(['"]?)([0-9a-f]{12})\3([ \t]*(?:\#.*)?)(\r?\n)?\z/
FLOW_MAPPING_ITEM =

A block-list item whose value is a flow mapping, - { id: x, ... }.

/\A([ \t]*-[ \t]+)(\{.*\})([ \t]*(?:\#.*)?)(\r?\n)?\z/
MAPPING_UNSUPPORTED_MESSAGE =

Told to the user when a flow mapping's id cannot be located safely.

"this ignore: { id:, reason: } entry is not supported. " \
"Write id: on its own line under the list item."
SCALAR =

ignore: followed by one bare id on the same line.

/\A(ignore:[ \t]*)(['"]?)([0-9a-f]{12})\2([ \t]*(?:\#.*)?)(\r?\n)?\z/
KEY_BLOCK_SCALAR =

ignore: | or ignore: >, including chomp and indent markers.

/\Aignore:[ \t]*[|>]/
ITEM_BLOCK_SCALAR =

A list item whose value is a block scalar, such as - |.

/\A[ \t]*-[ \t]+[|>]/
VALUE_BLOCK_SCALAR =

An indented | or > that is the whole ignore: value.

/\A[ \t]+[|>]/
BLOCK_SCALAR_MESSAGE =

Told to the user when ignore: is a YAML block scalar. Exit 2.

"a block scalar under ignore: is not supported. " \
"Write ignore as a list of ids, one per line."
FLOW_UNSUPPORTED_MESSAGE =

Told to the user when a flow ignore: list cannot be rewritten safely.

"this ignore: flow list is not supported. " \
"Write ignore as a list of ids, one per line."
UNCLOSED_FLOW_MESSAGE =

Told to the user when ignore: [ never finds its closing bracket.

"an ignore: flow list has no closing ]. " \
"Write ignore as a list of ids, one per line."

Class Method Summary collapse

Class Method Details

.block_scalar_entry?(line, saw_entry) ⇒ Boolean (private)

Whether line is a block scalar used as the ignore value or as one list item. A | under a later key, such as reason: |, is not.

Parameters:

  • line (String)
  • saw_entry (Boolean) —

    true after the first real entry in the value.

Returns:

  • (Boolean)


565
566
567
# File 'lib/mutineer/migrate.rb', line 565

def block_scalar_entry?(line, saw_entry)
  line.match?(ITEM_BLOCK_SCALAR) || (!saw_entry && line.match?(VALUE_BLOCK_SCALAR))
end

.collect_flow(line, pending) ⇒ String (private)

Lines from ignore: [ through the line that closes the flow list. Quotes and comments do not hide the closing bracket, and they do not supply a false one.

Parameters:

  • line (String) —

    the opening line, its terminator included.

  • pending (Array<String>) —

    the lines still to read.

Returns:

  • (String) —

    the whole flow value, newlines included.

Raises:

  • (ArgumentError) —

    when the closing bracket is missing.



247
248
249
250
251
252
253
254
255
256
# File 'lib/mutineer/migrate.rb', line 247

def collect_flow(line, pending)
  chunk = line.dup
  return chunk if matching_index(chunk, chunk.index("["), "[", "]")

  while (next_line = pending.shift)
    chunk << next_line
    return chunk if matching_index(chunk, chunk.index("["), "[", "]")
  end
  raise ArgumentError, UNCLOSED_FLOW_MESSAGE
end

.comment_at?(text, index) ⇒ Boolean (private)

Whether index starts a YAML comment. A # inside a token does not.

Parameters:

  • text (String)
  • index (Integer)

Returns:

  • (Boolean)


317
318
319
320
321
# File 'lib/mutineer/migrate.rb', line 317

def comment_at?(text, index)
  return false unless text[index] == "#"

  index.zero? || text[index - 1].match?(/\s/)
end

.flow_scalar_id(raw) ⇒ Array(String, String, String, String)? (private)

The quote, the id, and the gaps around it when raw is exactly one id. Nil when raw is any other scalar.

Parameters:

  • raw (String) —

    one flow entry, quotes included.

Returns:

  • (Array(String, String, String, String), nil)


489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/mutineer/migrate.rb', line 489

def flow_scalar_id(raw)
  if (match = raw.match(/\A([0-9a-f]{12})([ \t]*)\z/))
    return ["", match[1], "", match[2]]
  end
  if raw.start_with?("'") && raw.end_with?("'")
    value = raw[1..-2].gsub("''", "'")
    return ["'", value, "", ""] if value.match?(/\A[0-9a-f]{12}\z/)
  end
  if raw.start_with?('"') && raw.end_with?('"')
    value = raw[1..-2]
    # A backslash can hide an id. Do not guess the decoded text.
    raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE if value.include?("\\")

    return ['"', value, "", ""] if value.match?(/\A[0-9a-f]{12}\z/)
  end
  nil
end

.id_maps(config) ⇒ Array(Set<String>, Hash{String => Array<String>})

Builds the id maps for config's sources and operators.

Parameters:

Returns:

  • (Array(Set<String>, Hash{String => Array<String>})) —

    the current new ids, and each old id => its new ids.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mutineer/migrate.rb', line 43

def self.id_maps(config)
  names = config.operators || MutatorRegistry::DEFAULT_NAMES
  _jobs, _ignored, _source_map, extras = JobPlan.collect_jobs(config, MutatorRegistry.resolve(names))
  all = extras[:id_map].merge(extras[:dropped_id_map])
  old_to_new = {}
  all.each do |new_id, old_id|
    list = (old_to_new[old_id] ||= [])
    list << new_id unless list.include?(new_id)
  end
  [all.keys.to_set, old_to_new]
end

.ignore_entries(text) ⇒ Array(Array<String>, Array<String>)? (private)

The ids and reasons in a .mutineer.yml ignore: value, read as YAML, in file order. Ids are read as Config.ignore_id_text reads them.

Parameters:

  • text (String) —

    the file text.

Returns:

  • (Array(Array<String>, Array<String>), nil) —

    ids and reasons, or nil when the text is not valid YAML.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mutineer/migrate.rb', line 132

def self.ignore_entries(text)
  doc = YAML.safe_load(text)
  value = doc.is_a?(Hash) ? doc["ignore"] : nil
  items = value.is_a?(Array) ? value : [value].compact
  ids = []
  reasons = []
  items.each do |item|
    if item.is_a?(Hash)
      ids << Config.ignore_id_text(item["id"])
      reasons << item["reason"].to_s
    else
      ids << Config.ignore_id_text(item)
    end
  end
  [ids.compact, reasons]
rescue Psych::Exception
  nil
end

.inside_block?(line) ⇒ Boolean (private)

Whether line still belongs to a block-style ignore: value. A later root key does not. A blank line, a comment, an indented line, or a list item (indented or at column 0) does.

Parameters:

  • line (String) —

    the next line.

Returns:

  • (Boolean)


212
213
214
# File 'lib/mutineer/migrate.rb', line 212

def inside_block?(line)
  line.match?(/\A[ \t]*(?:\#.*)?\r?\n?\z/) || line.match?(/\A[ \t]*-[ \t]/) || line.match?(/\A[ \t]+\S/)
end

.line_kind(line) ⇒ Symbol? (private)

Which ignore: shape this line opens, or nil when it is some other line.

Parameters:

  • line (String) —

    one line of the file, its terminator included.

Returns:

  • (Symbol, nil) —

    :flow, :block_scalar, :block, :scalar, or nil.



197
198
199
200
201
202
203
204
# File 'lib/mutineer/migrate.rb', line 197

def line_kind(line)
  return :flow if line.match?(/\Aignore:[ \t]*\[/)
  return :block_scalar if line.match?(KEY_BLOCK_SCALAR)
  return :block if line.match?(/\Aignore:[ \t]*(?:\#.*)?\r?\n?\z/)
  return :scalar if line.match?(/\Aignore:[ \t]*\S/)

  nil
end

.mapped_ids(id, current, old_to_new, replacements, unmapped) ⇒ Array<String> (private)

The ids to write for one bare id. Records a replacement or an unmapped id. An id that is already current is returned unchanged and unrecorded, even when it also equals some other mutant's old id.

Parameters:

  • id (String) —

    the id in the file.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes, in file order.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

Returns:

  • (Array<String>) —

    the ids to write in this position.



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/mutineer/migrate.rb', line 226

def mapped_ids(id, current, old_to_new, replacements, unmapped)
  return [id] if current.include?(id)

  news = old_to_new[id]
  if news
    replacements << Replacement.new(old_id: id, new_ids: news.dup) unless news == [id]
    news.dup
  else
    unmapped << id unless unmapped.include?(id)
    [id]
  end
end

.mapping_id(id, current, old_to_new, replacements, unmapped, split) ⇒ String (private)

The one id a mapping entry can hold. An old id that now matches several mutants stays and is recorded in split for a hand edit.

Parameters:

  • id (String) —

    the id in the file.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes, in file order.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (String)


673
674
675
676
677
678
679
680
681
# File 'lib/mutineer/migrate.rb', line 673

def mapping_id(id, current, old_to_new, replacements, unmapped, split)
  news = old_to_new[id]
  if news && news.size > 1 && !current.include?(id)
    split << Replacement.new(old_id: id, new_ids: news.dup)
    return id
  end

  mapped_ids(id, current, old_to_new, replacements, unmapped).first
end

.matching_index(text, start, opener, closer) ⇒ Integer? (private)

Index of the closer that matches the opener at start, or nil. Quoted text and comments are not structure.

Parameters:

  • text (String)
  • start (Integer) —

    index of the opening bracket.

  • opener (String) —

    [ or {.

  • closer (String) —

    ] or }.

Returns:

  • (Integer, nil)


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/mutineer/migrate.rb', line 266

def matching_index(text, start, opener, closer)
  index = start + 1
  depth = 1
  state = :plain
  while index < text.length
    char = text[index]
    case state
    when :plain
      if char == "'"
        state = :single
      elsif char == '"'
        state = :double
      elsif comment_at?(text, index)
        newline = text.index("\n", index)
        return nil unless newline

        index = newline + 1
        next
      elsif char == opener
        depth += 1
      elsif char == closer
        depth -= 1
        return index if depth.zero?
      end
    when :single
      if char == "'"
        if text[index + 1] == "'"
          index += 1
        else
          state = :plain
        end
      end
    when :double
      if char == "\\"
        return nil if index + 1 >= text.length

        index += 1
      elsif char == '"'
        state = :plain
      end
    end
    index += 1
  end
  nil
end

.read_double_quoted(text, start) ⇒ Array(String, Integer) (private)

A double-quoted scalar, including its quotes. Backslash escapes are skipped so they cannot end the scalar early.

Parameters:

  • text (String)
  • start (Integer) —

    index of the opening quote.

Returns:

  • (Array(String, Integer))

Raises:

  • (ArgumentError) —

    when the quote does not close.



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/mutineer/migrate.rb', line 428

def read_double_quoted(text, start)
  index = start + 1
  while index < text.length
    char = text[index]
    if char == "\\"
      raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE if index + 1 >= text.length

      index += 2
      next
    end
    return [text[start..index], index + 1] if char == '"'

    index += 1
  end
  raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE
end

.read_flow_item(text, index) ⇒ Array(String, Integer) (private)

One flow entry starting at index: a quoted scalar, a plain scalar, or a whole { ... } entry (its id is rewritten by rewrite_flow_mapping).

Parameters:

  • text (String)
  • index (Integer)

Returns:

  • (Array(String, Integer)) —

    the raw entry and the index after it.

Raises:

  • (ArgumentError) —

    when the entry is not one of those shapes.



388
389
390
391
392
393
394
395
396
397
398
# File 'lib/mutineer/migrate.rb', line 388

def read_flow_item(text, index)
  case text[index]
  when "'" then read_single_quoted(text, index)
  when '"' then read_double_quoted(text, index)
  when "{" then read_wrapped(text, index, "{", "}")
  when "[", "|", ">", "!", "&", "*", "?"
    raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE
  else
    read_plain(text, index)
  end
end

.read_plain(text, start) ⇒ Array(String, Integer) (private)

A plain flow scalar. It ends at a comma, a comment, or a line break. A colon means the entry may be a mapping, which this rewrite does not split safely.

Parameters:

  • text (String)
  • start (Integer)

Returns:

  • (Array(String, Integer))

Raises:

  • (ArgumentError) —

    when the entry is not a plain scalar.



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/mutineer/migrate.rb', line 468

def read_plain(text, start)
  index = start
  while index < text.length
    char = text[index]
    break if char == "," || char == "\n" || char == "\r"
    break if comment_at?(text, index)
    if char == ":" || char == "[" || char == "]" || char == "{" || char == "}" ||
       char == "'" || char == '"'
      raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE
    end

    index += 1
  end
  [text[start...index], index]
end

.read_single_quoted(text, start) ⇒ Array(String, Integer) (private)

A single-quoted scalar, including its quotes. '' is an escaped quote.

Parameters:

  • text (String)
  • start (Integer) —

    index of the opening quote.

Returns:

  • (Array(String, Integer))

Raises:

  • (ArgumentError) —

    when the quote does not close.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/mutineer/migrate.rb', line 406

def read_single_quoted(text, start)
  index = start + 1
  while index < text.length
    if text[index] == "'"
      if text[index + 1] == "'"
        index += 2
        next
      end
      return [text[start..index], index + 1]
    end
    index += 1
  end
  raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE
end

.read_wrapped(text, start, opener, closer) ⇒ Array(String, Integer) (private)

The { ... } or other wrapped entry starting at start, copied whole.

Parameters:

  • text (String)
  • start (Integer) —

    index of the opener.

  • opener (String)
  • closer (String)

Returns:

  • (Array(String, Integer))

Raises:

  • (ArgumentError) —

    when the closer is missing.



453
454
455
456
457
458
# File 'lib/mutineer/migrate.rb', line 453

def read_wrapped(text, start, opener, closer)
  stop = matching_index(text, start, opener, closer)
  raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE unless stop

  [text[start..stop], stop + 1]
end

.reject_block_scalar ⇒ void (private)

This method returns an undefined value.

Rejects a block scalar ignore: value. The constant lives on this singleton, so rewrite calls this method instead of reading it.

Raises:

  • (ArgumentError) —

    always.



555
556
557
# File 'lib/mutineer/migrate.rb', line 555

def reject_block_scalar
  raise ArgumentError, BLOCK_SCALAR_MESSAGE
end

.render_lines(prefix, quote, ids, suffix, ending) ⇒ Array<String> (private)

One list item per id. The original comment stays on the first item. Only the last item keeps the original line ending, so a file that does not end in a newline still does not.

Parameters:

  • prefix (String) —

    the indentation and list marker.

  • quote (String) —

    " or ' or "".

  • ids (Array<String>) —

    ids to write.

  • suffix (String) —

    the original trailing comment, spaces included.

  • ending (String) —

    the original line ending, or "".

Returns:

  • (Array<String>)


693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/mutineer/migrate.rb', line 693

def render_lines(prefix, quote, ids, suffix, ending)
  last = ids.size - 1
  ids.each_with_index.map do |id, i|
    tail = i.zero? ? suffix : ""
    mark = if i == last
             ending
           else
             ending.empty? ? "\n" : ending
           end
    "#{prefix}#{quote}#{id}#{quote}#{tail}#{mark}"
  end
end

.replace_flow_scalar(raw, current, old_to_new, replacements, unmapped) ⇒ String (private)

Replaces raw when its whole scalar value is an id. Several new ids become several entries. Any other entry is returned unchanged.

Parameters:

  • raw (String) —

    one flow entry.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

Returns:

  • (String)


516
517
518
519
520
521
522
523
524
525
# File 'lib/mutineer/migrate.rb', line 516

def replace_flow_scalar(raw, current, old_to_new, replacements, unmapped)
  parts = flow_scalar_id(raw)
  return raw unless parts

  quote, value, lead, trail = parts
  ids = mapped_ids(value, current, old_to_new, replacements, unmapped)
  return raw if ids == [value]

  "#{lead}#{ids.map { |id| "#{quote}#{id}#{quote}" }.join(", ")}#{trail}"
end

.rewrite(text, current, old_to_new) ⇒ Outcome

Rewrites ignore: ids in text. An id that is already a current new id stays, so a second run does not change the file. An old id is replaced by every new id it maps to. Any other bare id stays and is listed in Mutineer::Migrate::Outcome#unmapped.

A flow list may span lines. Only a scalar whose whole value is an id changes. A block scalar under ignore: raises ArgumentError, and so does a flow list this rewrite cannot read safely. The caller writes nothing in that case.

Parameters:

  • text (String) —

    the .mutineer.yml contents.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

Returns:

Raises:

  • (ArgumentError) —

    when an ignore: shape cannot be rewritten safely.



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
# File 'lib/mutineer/migrate.rb', line 70

def self.rewrite(text, current, old_to_new)
  replacements = []
  unmapped = []
  split = []
  lines = []
  pending = text.lines
  while (line = pending.shift)
    case line_kind(line)
    when :flow
      lines << rewrite_flow(collect_flow(line, pending), current, old_to_new, replacements, unmapped, split)
    when :scalar
      lines.concat(rewrite_scalar(line, current, old_to_new, replacements, unmapped))
    when :block_scalar
      reject_block_scalar
    when :block
      lines << line
      lines.concat(rewrite_block(pending, current, old_to_new, replacements, unmapped, split))
    else
      lines << line
    end
  end
  out = lines.join
  verify_rewrite!(text, out, current, old_to_new, split)
  Outcome.new(replacements: replacements, unmapped: unmapped, split: split, text: out)
end

.rewrite_block(pending, current, old_to_new, replacements, unmapped, split) ⇒ Array<String> (private)

The lines of a block-style ignore: value. A block scalar here is rejected. A { id:, reason: } entry is rewritten by rewrite_item.

Parameters:

  • pending (Array<String>) —

    the lines still to read.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (Array<String>)

Raises:

  • (ArgumentError) —

    when the value or a list item is a block scalar.



538
539
540
541
542
543
544
545
546
547
548
# File 'lib/mutineer/migrate.rb', line 538

def rewrite_block(pending, current, old_to_new, replacements, unmapped, split)
  written = []
  saw_entry = false
  while (next_line = pending.first) && inside_block?(next_line)
    raise ArgumentError, BLOCK_SCALAR_MESSAGE if block_scalar_entry?(next_line, saw_entry)

    saw_entry = true if next_line.match?(/\A[ \t]*[^ \t#\r\n]/)
    written.concat(rewrite_item(pending.shift, current, old_to_new, replacements, unmapped, split))
  end
  written
end

.rewrite_flow(chunk, current, old_to_new, replacements, unmapped, split) ⇒ String (private)

Rewrites whole id scalars between the brackets of a flow list. The list may span lines. A scalar that only contains an id is kept. A { ... } entry is kept. Nested lists and other shapes raise.

Parameters:

  • chunk (String) —

    ignore: [ through the closing bracket.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (String) —

    the text to write.

Raises:

  • (ArgumentError) —

    when the list is not a flat list of scalars.



335
336
337
338
339
340
341
342
# File 'lib/mutineer/migrate.rb', line 335

def rewrite_flow(chunk, current, old_to_new, replacements, unmapped, split)
  open = chunk.index("[")
  close = matching_index(chunk, open, "[", "]")
  raise ArgumentError, UNCLOSED_FLOW_MESSAGE unless close

  inner = rewrite_flow_items(chunk[(open + 1)...close], current, old_to_new, replacements, unmapped, split)
  "#{chunk[0..open]}#{inner}#{chunk[close..]}"
end

.rewrite_flow_items(inner, current, old_to_new, replacements, unmapped, split) ⇒ String (private)

Rewrites the text between the flow brackets.

Parameters:

  • inner (String) —

    text between [ and ].

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (String)

Raises:

  • (ArgumentError) —

    when an entry is not a scalar or a { ... } entry.



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/mutineer/migrate.rb', line 354

def rewrite_flow_items(inner, current, old_to_new, replacements, unmapped, split)
  out = +""
  index = 0
  while index < inner.length
    char = inner[index]
    if char.match?(/\s/) || char == ","
      out << char
      index += 1
    elsif comment_at?(inner, index)
      newline = inner.index("\n", index) || inner.length
      out << inner[index...newline]
      index = newline
    else
      raw, next_index = read_flow_item(inner, index)
      raise ArgumentError, FLOW_UNSUPPORTED_MESSAGE if next_index <= index

      out << if raw.start_with?("{")
               rewrite_flow_mapping(raw, current, old_to_new, replacements, unmapped, split)
             else
               replace_flow_scalar(raw, current, old_to_new, replacements, unmapped)
             end
      index = next_index
    end
  end
  out
end

.rewrite_flow_mapping(raw, current, old_to_new, replacements, unmapped, split) ⇒ String (private)

Replaces the id value in one flow mapping, { id: x, reason: y }. YAML reads the entry first, and the id is replaced only at a key position (after { or ,), so an id quoted inside a reason stays. A mapping with no 12-hex id is returned unchanged.

Parameters:

  • raw (String) —

    the { ... } text.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes, in file order.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (String)

Raises:

  • (ArgumentError) —

    when the entry does not parse or its id is not at exactly one key position.



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/mutineer/migrate.rb', line 647

def rewrite_flow_mapping(raw, current, old_to_new, replacements, unmapped, split)
  parsed = begin
    YAML.safe_load(raw)
  rescue Psych::SyntaxError
    raise ArgumentError, MAPPING_UNSUPPORTED_MESSAGE
  end
  id = parsed.is_a?(Hash) ? Config.ignore_id_text(parsed["id"]) : nil
  return raw unless id&.match?(/\A[0-9a-f]{12}\z/)

  key = /([{,][ \t\r\n]*id[ \t]*:[ \t]*)(['"]?)#{id}\2(?=[ \t\r\n]*[,}])/
  raise ArgumentError, MAPPING_UNSUPPORTED_MESSAGE unless raw.scan(key).size == 1

  new_id = mapping_id(id, current, old_to_new, replacements, unmapped, split)
  raw.sub(key) { "#{Regexp.last_match(1)}#{Regexp.last_match(2)}#{new_id}#{Regexp.last_match(2)}" }
end

.rewrite_item(line, current, old_to_new, replacements, unmapped, split) ⇒ Array<String> (private)

Rewrites one block-list item, or returns the line unchanged when it is not a bare id (a comment, a blank line, or a { id:, reason: } entry).

Parameters:

  • line (String) —

    one line inside an ignore: block.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (Array<String>) —

    the lines to write.



603
604
605
606
607
608
609
# File 'lib/mutineer/migrate.rb', line 603

def rewrite_item(line, current, old_to_new, replacements, unmapped, split)
  match = ITEM.match(line)
  return [rewrite_mapping_id(line, current, old_to_new, replacements, unmapped, split)] unless match

  ids = mapped_ids(match[3], current, old_to_new, replacements, unmapped)
  render_lines(match[1], match[2], ids, match[4], match[5].to_s)
end

.rewrite_mapping_id(line, current, old_to_new, replacements, unmapped, split) ⇒ String (private)

Replaces the id in a block-mapping id: line or a - { ... } item, keeping the reason, quotes and comment. Other lines are returned as is.

Parameters:

  • line (String) —

    one line inside the ignore: block.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes, in file order.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

  • split (Array<Replacement>) —

    mapping ids that now match several mutants.

Returns:

  • (String) —

    the line to write.

Raises:

  • (ArgumentError) —

    when a flow mapping's id cannot be located safely.



622
623
624
625
626
627
628
629
630
631
632
# File 'lib/mutineer/migrate.rb', line 622

def rewrite_mapping_id(line, current, old_to_new, replacements, unmapped, split)
  if (match = MAPPING_ID_LINE.match(line))
    new_id = mapping_id(match[4], current, old_to_new, replacements, unmapped, split)
    return "#{match[1]}#{match[2]}#{match[3]}#{new_id}#{match[3]}#{match[5]}#{match[6]}"
  end
  if (match = FLOW_MAPPING_ITEM.match(line))
    mapping = rewrite_flow_mapping(match[2], current, old_to_new, replacements, unmapped, split)
    return "#{match[1]}#{mapping}#{match[3]}#{match[4]}"
  end
  line
end

.rewrite_scalar(line, current, old_to_new, replacements, unmapped) ⇒ Array<String> (private)

Rewrites a one-line ignore: <id>. Several new ids become a block list, so each id stays a bare string. The comment stays on the key line.

Parameters:

  • line (String) —

    an ignore: <id> line.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • replacements (Array<Replacement>) —

    recorded changes.

  • unmapped (Array<String>) —

    bare ids that matched nothing.

Returns:

  • (Array<String>) —

    the lines to write.



578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/mutineer/migrate.rb', line 578

def rewrite_scalar(line, current, old_to_new, replacements, unmapped)
  match = SCALAR.match(line)
  return [line] unless match

  ids = mapped_ids(match[3], current, old_to_new, replacements, unmapped)
  quote = match[2]
  if ids.size == 1
    ["#{match[1]}#{quote}#{ids.first}#{quote}#{match[4]}#{match[5]}"]
  else
    ending = match[5].to_s
    ending = "\n" if ending.empty?
    ["ignore:#{match[4]}#{ending}", *render_lines("  - ", quote, ids, "", ending)]
  end
end

.verify_rewrite!(before, after, current, old_to_new, split) ⇒ void (private)

This method returns an undefined value.

Reads both texts as YAML and checks the text edit did what it claims. An id that still maps to a new id, and was not left for a hand split, means an entry shape the line edit missed (a mapping over several lines, a quoted key). A changed reason means the edit touched reason text. Either is refused, so nothing is written and nothing passes silently.

Parameters:

  • before (String) —

    the original file text.

  • after (String) —

    the rewritten text.

  • current (Set<String>) —

    new-format ids in this run.

  • old_to_new (Hash{String => Array<String>}) —

    old id => new ids.

  • split (Array<Replacement>) —

    mapping ids left for a hand split.

Raises:

  • (ArgumentError) —

    when an old id was missed or a reason changed.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mutineer/migrate.rb', line 110

def self.verify_rewrite!(before, after, current, old_to_new, split)
  old_ids, old_reasons = ignore_entries(before) || [[], []]
  parsed = ignore_entries(after)
  raise ArgumentError, "migrate would write a .mutineer.yml that is not valid YAML. Edit the ignore ids by hand." unless parsed

  new_ids, new_reasons = parsed
  left = split.map(&:old_id)
  missed = (new_ids & old_ids).find { |id| old_to_new.key?(id) && !current.include?(id) && !left.include?(id) }
  if missed
    raise ArgumentError, "ignore entry #{missed} is in a shape migrate cannot edit. " \
                         "Write it as \"- id: #{missed}\" on its own line, or as a bare id."
  end
  return if old_reasons == new_reasons

  raise ArgumentError, "migrate would change the text of an ignore: reason. Edit those ids by hand."
end