From 281d9f903394e72277c31741ec6459a5f8c890dd Mon Sep 17 00:00:00 2001 From: januszn Date: Tue, 4 Sep 2018 23:12:27 -0400 Subject: [PATCH] Allow optional CR before LF when probing collapsed stacks files (#154) This fixes #152, in that it allows "collapsed stacks" files generated with tools using Windows line endings to be imported into the tool verbatim. --- sample/profiles/stackcollapse/simple-crlf.txt | 5 ++ .../__snapshots__/bg-flamegraph.test.ts.snap | 54 +++++++++++++++++++ src/import/bg-flamegraph.test.ts | 4 ++ src/import/index.ts | 2 +- 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 sample/profiles/stackcollapse/simple-crlf.txt diff --git a/sample/profiles/stackcollapse/simple-crlf.txt b/sample/profiles/stackcollapse/simple-crlf.txt new file mode 100644 index 0000000..8dc6a2a --- /dev/null +++ b/sample/profiles/stackcollapse/simple-crlf.txt @@ -0,0 +1,5 @@ +a;b;c 1 +a;b;c 1 +a;b;d 4 +a;b;c 3 +a;b 5 \ No newline at end of file diff --git a/src/import/__snapshots__/bg-flamegraph.test.ts.snap b/src/import/__snapshots__/bg-flamegraph.test.ts.snap index a2556da..68bb66d 100644 --- a/src/import/__snapshots__/bg-flamegraph.test.ts.snap +++ b/src/import/__snapshots__/bg-flamegraph.test.ts.snap @@ -50,6 +50,60 @@ Object { } `; +exports[`importFromBGFlameGraph with CRLF 1`] = ` +Object { + "frames": Array [ + Frame { + "col": undefined, + "file": undefined, + "key": "a", + "line": undefined, + "name": "a", + "selfWeight": 0, + "totalWeight": 14, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "b", + "line": undefined, + "name": "b", + "selfWeight": 5, + "totalWeight": 14, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "c", + "line": undefined, + "name": "c", + "selfWeight": 5, + "totalWeight": 5, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "d", + "line": undefined, + "name": "d", + "selfWeight": 4, + "totalWeight": 4, + }, + ], + "name": "simple-crlf.txt", + "stacks": Array [ + "a;b;c 2", + "a;b;d 4", + "a;b;c 3", + "a;b 5", + ], +} +`; + +exports[`importFromBGFlameGraph with CRLF: indexToView 1`] = `0`; + +exports[`importFromBGFlameGraph with CRLF: profileGroup.name 1`] = `"simple-crlf.txt"`; + exports[`importFromBGFlameGraph: indexToView 1`] = `0`; exports[`importFromBGFlameGraph: profileGroup.name 1`] = `"simple.txt"`; diff --git a/src/import/bg-flamegraph.test.ts b/src/import/bg-flamegraph.test.ts index 2cef442..ffe5c5b 100644 --- a/src/import/bg-flamegraph.test.ts +++ b/src/import/bg-flamegraph.test.ts @@ -3,3 +3,7 @@ import {checkProfileSnapshot} from '../lib/test-utils' test('importFromBGFlameGraph', async () => { await checkProfileSnapshot('./sample/profiles/stackcollapse/simple.txt') }) + +test('importFromBGFlameGraph with CRLF', async () => { + await checkProfileSnapshot('./sample/profiles/stackcollapse/simple-crlf.txt') +}) diff --git a/src/import/index.ts b/src/import/index.ts index dd16999..0395972 100644 --- a/src/import/index.ts +++ b/src/import/index.ts @@ -103,7 +103,7 @@ async function _importProfileGroup( // If every line ends with a space followed by a number, it's probably // the collapsed stack format. const lineCount = contents.split(/\n/).length - if (lineCount >= 1 && lineCount === contents.split(/ \d+\n/).length) { + if (lineCount >= 1 && lineCount === contents.split(/ \d+\r?\n/).length) { console.log('Importing as collapsed stack format') return toGroup(importFromBGFlameGraph(contents)) }