Skip to content

Commit 575038f

Browse files
Update LcovParser field value parsing (#141)
Fixed the parsing of field values that contain colons. This unfortunate bug removed everything up to the last colon in the field value. With these changes, the bug mentioned in #140 is fixed.
2 parents 8a980f2 + 45856e9 commit 575038f

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

.changeset/old-kiwis-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@friedemannsommer/lcov-parser": patch
3+
---
4+
5+
Fixed a bug which stripped everything up to the last colon, when parsing field values that contain colons.

src/parser.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ export class LcovParser {
177177
continue
178178
}
179179

180-
const result = nonEmptyField ? LcovParser._parseValue(buf, byteIndex + 1, isComment) : null
180+
// if the current field is a comment, offset the current index by one
181+
// (so that it points to the value just after the field token).
182+
// for any other non-empty field, we've already checked that the next value is a colon,
183+
// so we can offset the index by two.
184+
const result = nonEmptyField ? LcovParser._parseValue(buf, byteIndex + (isComment ? 1 : 2)) : null
181185
let value = null
182186

183187
if (result !== null) {
@@ -219,19 +223,16 @@ export class LcovParser {
219223
/**
220224
* @internal
221225
*/
222-
private static _parseValue(buf: Buffer, offset: number, startAtOffset: boolean): ParseValueResult | null {
226+
private static _parseValue(buf: Buffer, offset: number): ParseValueResult | null {
223227
const length = buf.byteLength
224-
let start = offset
225228

226229
for (let index = offset; index < length; index++) {
227230
const byte = buf[index]
228231

229-
if (!startAtOffset && byte === 58 /* ':' (colon) */) {
230-
start = index + 1
231-
} else if (byte === 10 /* '\n' (new line) */) {
232+
if (byte === 10 /* '\n' (new line) */) {
232233
return {
233234
lastIndex: index,
234-
value: LcovParser._parseSlice(buf, start, index)
235+
value: LcovParser._parseSlice(buf, offset, index)
235236
}
236237
}
237238
}

src/tests/parser.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,32 @@ describe('LcovParser - empty fields', (): void => {
225225
expect(parser.read()).to.eql(getParseResult(Variant.None, null, false, true))
226226
})
227227
})
228+
229+
describe('LcovParser - Paths containing colons', (): void => {
230+
it('should process valid Windows like path', (): void => {
231+
const parser = new LcovParser(defaultFieldNames)
232+
233+
parser.write(
234+
Buffer.from(
235+
getRawLcov(
236+
defaultFieldNames.filePath,
237+
'C:\\Users\\Example\\Documents\\Projects\\example\\src\\example.file'
238+
)
239+
)
240+
)
241+
242+
expect(parser.read()).to.eql(
243+
getParseResult(Variant.FilePath, ['C:\\Users\\Example\\Documents\\Projects\\example\\src\\example.file'])
244+
)
245+
expect(parser.read()).to.eql(getParseResult(Variant.None, null, true))
246+
})
247+
248+
it('should process a path containing multiple colons', (): void => {
249+
const parser = new LcovParser(defaultFieldNames)
250+
251+
parser.write(Buffer.from(getRawLcov(defaultFieldNames.filePath, '/this/is/a/path:with:colons')))
252+
253+
expect(parser.read()).to.eql(getParseResult(Variant.FilePath, ['/this/is/a/path:with:colons']))
254+
expect(parser.read()).to.eql(getParseResult(Variant.None, null, true))
255+
})
256+
})

0 commit comments

Comments
 (0)