From 94dadaa42af7e59cee6f97eb660de1a14ad45409 Mon Sep 17 00:00:00 2001 From: Nuruddin Ashr Date: Mon, 24 Jul 2017 21:16:34 +0700 Subject: [PATCH] Process all output at once to avoid partial data (#1108) * Process all output at once to avoid partial data * Use `{env: {}}` to make the execution faster * Add support for large test output --- src/goPackages.ts | 20 ++++++++++++-------- src/goTest.ts | 23 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/goPackages.ts b/src/goPackages.ts index 2bff3d719..e0018b991 100644 --- a/src/goPackages.ts +++ b/src/goPackages.ts @@ -23,13 +23,11 @@ export function goListAll(): Promise> { return Promise.resolve(allPkgs); } return new Promise>((resolve, reject) => { - const cmd = cp.spawn(goRuntimePath, ['list', '-f', '{{.Name}};{{.ImportPath}}', 'all']); + // Use `{env: {}}` to make the execution faster + const cmd = cp.spawn(goRuntimePath, ['list', '-f', '{{.Name}};{{.ImportPath}}', 'all'], {env: {}}); + const chunks = []; cmd.stdout.on('data', (d) => { - d.toString().split('\n').forEach(pkgDetail => { - if (!pkgDetail || !pkgDetail.trim() || pkgDetail.indexOf(';') === -1) return; - let [pkgName, pkgPath] = pkgDetail.trim().split(';'); - allPkgs.set(pkgPath, pkgName); - }); + chunks.push(d); }); cmd.on('close', (status) => { @@ -40,6 +38,11 @@ export function goListAll(): Promise> { return reject(); } + chunks.toString().split('\n').forEach(pkgDetail => { + if (!pkgDetail || !pkgDetail.trim() || pkgDetail.indexOf(';') === -1) return; + let [pkgName, pkgPath] = pkgDetail.trim().split(';'); + allPkgs.set(pkgPath, pkgName); + }); goListAllCompleted = true; return resolve(allPkgs); }); @@ -118,12 +121,13 @@ export function getNonVendorPackages(folderPath: string): Promise { } return new Promise((resolve, reject) => { const childProcess = cp.spawn(goRuntimePath, ['list', './...'], { cwd: folderPath }); - let pkgs = []; + const chunks = []; childProcess.stdout.on('data', (stdout) => { - pkgs = pkgs.concat(stdout.toString().split('\n').filter(pkgPath => pkgPath && pkgPath.indexOf('/vendor/') === -1)); + chunks.push(stdout); }); childProcess.on('close', (status) => { + const pkgs = chunks.toString().split('\n').filter(pkgPath => pkgPath && !pkgPath.includes('/vendor/')); return resolve(pkgs); }); }); diff --git a/src/goTest.ts b/src/goTest.ts index 725e86216..b8918008b 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -243,17 +243,30 @@ export function goTest(testconfig: TestConfig): Thenable { args.push(...targets); let proc = cp.spawn(goRuntimePath, args, { env: testEnvVars, cwd: testconfig.dir }); + let leftOver = ''; + let errChunks = []; proc.stdout.on('data', chunk => { - let testOutput = expandFilePathInOutput(chunk.toString(), testconfig.dir); - outputChannel.append(testOutput); + let s = chunk.toString(); + let lastNewLineIndex = s.lastIndexOf('\n'); + if (lastNewLineIndex > -1) { + let sub = leftOver + s.substring(0, lastNewLineIndex); + leftOver = s.substring(lastNewLineIndex + 1); + let testOutput = expandFilePathInOutput(sub, testconfig.dir); + outputChannel.appendLine(testOutput); + } else { + leftOver += s; + } }); - proc.stderr.on('data', chunk => outputChannel.append(chunk.toString())); + proc.stderr.on('data', chunk => errChunks.push(chunk)); proc.on('close', code => { if (code) { - outputChannel.append('Error: Tests failed.'); + if (errChunks.length) { + outputChannel.append(errChunks.toString()); + } + outputChannel.appendLine('Error: Tests failed.'); } else { - outputChannel.append('Success: Tests passed.'); + outputChannel.appendLine('Success: Tests passed.'); } resolve(code === 0); });