summaryrefslogtreecommitdiff
path: root/plugins/code-modernization/hooks/reader/uplift.ts
blob: 7cff7d36988608e75fc2f332d027084e8e0c9ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
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
170
171
172
173
174
175
176
177
178
179
180
181
182
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { join } from '../paths'
import { linesOf } from '../text'
import { unitOfPath, type EstateModel, type EstateUnit } from './estate-model'
import type { ReaderFs } from './fs'
import { readTests, stateOfUplift, type ModernizedModule, type ModuleState, type TestTotals } from './modernized'

/**
 * What a same-stack uplift leaves on disk, read without knowing the stack: the
 * baseline's per-module results, the delta catalog's size, and which modules of the
 * working copy differ from the legacy tree.
 */

export type BaselineRow = { pass: number; fail: number; error: number; skip: number }

export type Baseline = {
  /** The file says the source runtime could not run here, so there is no per-test table. */
  isTargetOnly: boolean
  /** Test results in all, from the table or the file's own totals line. */
  results: number | null
  /** One row per module, keyed by the module path lower-cased with no trailing slash. */
  rows: Map<string, BaselineRow>
  /**
   * A baseline of one module has no per-module table: its title names the module and a small table gives the totals
   * (`| Passed | 945 |`). Those totals are that module's row.
   */
  headline: { title: string; row: BaselineRow } | null
}

const int = (text: string | undefined): number => {
  const value = Number((text ?? '').replace(/[,\s_]/g, ''))

  return Number.isFinite(value) ? value : 0
}

const cellsOf = (line: string): string[] =>
  line
    .trim()
    .replace(/^\|/, '')
    .replace(/\|$/, '')
    .split('|')
    .map(cell => cell.trim())

const keyOf = (text: string): string =>
  text
    .replace(/[`*]/g, '')
    .replace(/^\.\//, '')
    .replace(/\/+$/, '')
    .trim()
    .toLowerCase()

/** Reads a baseline file: any table with pass and fail columns gives a row per module. */
export function parseBaseline(text: string): Baseline {
  const rows = new Map<string, BaselineRow>()
  const lines = linesOf(text)

  for (let index = 0; index < lines.length - 1; index += 1) {
    const head = lines[index] ?? ''

    if (!head.trim().startsWith('|') || !/^\s*\|?\s*:?-{2,}/.test(lines[index + 1] ?? '')) {
      continue
    }

    const names = cellsOf(head).map(name => name.replace(/[`*]/g, '').toLowerCase())
    const passAt = names.findIndex(name => /^pass/.test(name))
    const failAt = names.findIndex(name => /^fail/.test(name))

    if (passAt < 0 || failAt < 0) {
      continue
    }

    const errorAt = names.findIndex(name => /^err/.test(name))
    const skipAt = names.findIndex(name => /^(skip|ignor|pending)/.test(name))
    const found = names.findIndex(name => /^(module|project|unit|suite|package|assembly|component|name)/.test(name))
    const keyAt = found >= 0 ? found : 0

    for (let row = index + 2; row < lines.length && (lines[row] ?? '').trim().startsWith('|'); row += 1) {
      const cells = cellsOf(lines[row] ?? '')
      const key = keyOf(cells[keyAt] ?? '')

      if (key === '' || /^(total|totals|sum|all|overall)\b/.test(key)) {
        continue
      }

      rows.set(key, {
        pass: int(cells[passAt]),
        fail: int(cells[failAt]),
        error: errorAt >= 0 ? int(cells[errorAt]) : 0,
        skip: skipAt >= 0 ? int(cells[skipAt]) : 0,
      })
    }
  }

  const totals = /(?<![\d,])(\d[\d,]{0,14})\s+test\s+results?/i.exec(text)?.[1] ?? /\btotal\b[^\n]{0,40}?([\d,]{2,15})\s+tests?\b/i.exec(text)?.[1]
  const summed = [...rows.values()].reduce((sum, row) => sum + row.pass + row.fail + row.error + row.skip, 0)

  // The first `| Passed | 945 |` style lines, whatever else the file holds.
  const counts: Partial<Record<keyof BaselineRow, number>> = {}

  for (const line of lines) {
    const found = /^\s*\|\s*\**\s*(pass(?:ed)?|fail(?:ed|ures?)?|errors?|skipped|skip)\s*\**\s*\|\s*\**\s*([\d,]+)\s*\**\s*\|?\s*$/i.exec(line)

    if (found !== null) {
      const word = (found[1] ?? '').toLowerCase()
      const key: keyof BaselineRow = word.startsWith('pass') ? 'pass' : word.startsWith('fail') ? 'fail' : word.startsWith('err') ? 'error' : 'skip'

      counts[key] ??= int(found[2])
    }
  }

  const title = (lines.find(line => /^#\s+/.test(line)) ?? '').replace(/^#\s+/, '').replace(/[`*]/g, '').trim()

  return {
    isTargetOnly: /^\s*target-only:/im.test(text),
    results: totals !== undefined ? int(totals) : rows.size > 0 ? summed : null,
    rows,
    headline:
      counts.pass !== undefined && counts.fail !== undefined && title !== ''
        ? { title, row: { pass: counts.pass, fail: counts.fail, error: counts.error ?? 0, skip: counts.skip ?? 0 } }
        : null,
  }
}

/**
 * The baseline's row for one module: its own row, else the file's headline totals when its title names the module
 * (a pilot's baseline is one module's, and says so in its title). Null when there is nothing to compare with.
 */
export function baselineRowOf(baseline: Baseline | null, dir: string): BaselineRow | null {
  if (baseline === null || dir === '') {
    return null
  }

  const own = baseline.rows.get(keyOf(dir))

  if (own !== undefined) {
    return own
  }

  if (baseline.headline === null) {
    return null
  }

  const escaped = keyOf(dir).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')

  return new RegExp(`(?<![\\w./-])${escaped}(?![\\w./-])`, 'i').test(baseline.headline.title) ? baseline.headline.row : null
}

export type Catalog = {
  count: number
  /** Lower-cased base name of a file the catalog cites, to the delta ids it is cited under. */
  byFileBase: Map<string, string[]>
}

/**
 * What the delta catalog holds: how many deltas (its own statement of the count, else its `D<n>` ids), and which
 * delta each file it names is cited under: the delta a table row or a heading names last, before the file.
 */
export function parseCatalog(text: string): Catalog | null {
  const stated = /\b(\d+)\s+(?:distinct\s+)?deltas\b/i.exec(text)?.[1]
  const ids = new Set<string>()
  const byFileBase = new Map<string, string[]>()
  let current: string | null = null

  for (const line of linesOf(text)) {
    const opens = /^\s*(?:\|\s*\**|#{1,6}\s+\**)(D-?\d{1,3})\b/.exec(line)?.[1]

    if (opens !== undefined) {
      current = opens
      ids.add(opens)
    }

    for (const found of line.matchAll(/D-?\d{1,3}\b/g)) {
      ids.add(found[0])
    }

    if (current === null) {
      continue
    }

    for (const cited of line.matchAll(/`([\w./@+-]+\.[A-Za-z0-9]{1,8})(?::\d+(?:[-–]\d+)?)?`/g)) {
      const base = (cited[1] ?? '').split('/').at(-1)?.toLowerCase() ?? ''
      const held = byFileBase.get(base) ?? []

      if (base !== '' && !held.includes(current)) {
        held.push(current)
        byFileBase.set(base, held)
      }
    }
  }

  const count = stated !== undefined && int(stated) > 0 ? int(stated) : ids.size

  return count > 0 ? { count, byFileBase } : null
}

/**
 * The paths that differ between a tree and the working copy made from it: a file present in both with another size,
 * one only the copy has, and one only the tree has. It compares names and sizes, so an edit that keeps a file's size
 * exactly is not seen here; a test run in that module, or an edit made during the session, shows it.
 */
export function changedPathsOf(before: ReadonlyMap<string, number>, after: ReadonlyMap<string, number>): string[] {
  const out: string[] = []

  for (const [path, size] of after) {
    if (before.get(path) !== size) {
      out.push(path)
    }
  }

  for (const path of before.keys()) {
    if (!after.has(path)) {
      out.push(path)
    }
  }

  return out
}

/** The estate's unit ids that hold at least one of the changed paths. */
export function changedUnitsOf(estate: EstateModel, changed: readonly string[]): Set<string> {
  const ids = new Set<string>()

  for (const path of changed) {
    const unit = unitOfPath(estate, null, path)

    if (unit !== null) {
      ids.add(unit.id)
    }
  }

  return ids
}

/**
 * Each unit of the working copy that the uplift has touched or tested, with its state:
 * changed, tested, matching the baseline, or worse than it. `observed` holds the totals of test
 * runs seen this session, used where no report file was left.
 */
export async function readUpliftUnits(
  fs: ReaderFs,
  workingRoot: string,
  estate: EstateModel,
  changed: ReadonlySet<string>,
  baseline: Baseline | null,
  observed: ReadonlyMap<string, TestTotals>,
): Promise<{ unit: EstateUnit; module: ModernizedModule }[]> {
  const out: { unit: EstateUnit; module: ModernizedModule }[] = []

  for (const unit of estate.units) {
    if (unit.dir === undefined) {
      if (changed.has(unit.id)) {
        out.push({ unit, module: moduleOf(unit.name, join(workingRoot, unit.file ?? unit.id), null, 'scaffolded') })
      }

      continue
    }

    const path = unit.dir === '' ? workingRoot : join(workingRoot, unit.dir)
    const tests = (await readTests(fs, path)) ?? observed.get(path) ?? null
    const row = baselineRowOf(baseline, unit.dir)
    const state = stateOfUplift(tests, row, changed.has(unit.id))

    if (state !== 'untouched') {
      out.push({ unit, module: moduleOf(unit.dir === '' ? unit.name : unit.dir, path, tests, state) })
    }
  }

  return out
}

function moduleOf(dir: string, path: string, tests: TestTotals | null, state: ModuleState): ModernizedModule {
  return {
    dir,
    path,
    hasMain: true,
    hasTests: tests !== null,
    hasNotes: false,
    hasReviewSection: false,
    isPortedNotSwitched: false,
    isSwitched: false,
    followUps: 0,
    tests,
    state,
    mtimeMs: 0,
  }
}