Skip to content

Commit

Permalink
Merge pull request #133 from snyk/fix/output-string-array
Browse files Browse the repository at this point in the history
fix: do not accumulate output into a single string
  • Loading branch information
gitphill committed Jun 7, 2023
2 parents 9705135 + b266411 commit 6889328
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 41 deletions.
16 changes: 7 additions & 9 deletions lib/parse-sbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import * as types from './types';

export { parse, parseSbtPluginResults };

function convertStrToTree(dependenciesTextTree) {
const lines = dependenciesTextTree.toString().split('\n') || [];
function convertStrToTree(lines) {
const newLines = lines
.map((line) => {
return line.replace(/\u001b\[0m/g, '');
Expand Down Expand Up @@ -37,8 +36,7 @@ function convertStrToTree(dependenciesTextTree) {
return tree;
}

function convertCoursierStrToTree(dependenciesTextTree) {
const lines = dependenciesTextTree.toString().split('\n') || [];
function convertCoursierStrToTree(lines) {
const newLines = lines
.map((line) => {
return line.replace(/\u001b\[0m/g, '');
Expand Down Expand Up @@ -187,17 +185,17 @@ function parse(text, name, version, isCoursier): DepTree {
}

function parseSbtPluginResults(
sbtOutput: string,
sbtOutput: string[],
packageName: string,
packageVersion: string,
): DepTree {
// remove all other output
const outputStart = 'Snyk Output Start';
const outputEnd = 'Snyk Output End';
const sbtProjectOutput = sbtOutput.substring(
sbtOutput.indexOf(outputStart) + outputStart.length,
sbtOutput.indexOf(outputEnd),
);
const sbtProjectOutput = sbtOutput.slice(
sbtOutput.findIndex(line => line.indexOf(outputStart)!=-1) + 1,
sbtOutput.findIndex(line => line.indexOf(outputEnd)!=-1),
).join("\n");
const sbtOutputJson: types.SbtModulesGraph = JSON.parse(sbtProjectOutput);

if (Object.keys(sbtOutputJson).length === 1) {
Expand Down
23 changes: 16 additions & 7 deletions lib/sub-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const execute = (
command: string,
args: string[],
options: { cwd?: string },
): Promise<string> => {
): Promise<string[]> => {
const spawnOptions: { cwd?: string; shell: boolean } = { shell: true };
if (options && options.cwd) {
spawnOptions.cwd = options.cwd;
Expand All @@ -23,22 +23,27 @@ export const execute = (

return new Promise((resolve, reject) => {
const out = {
stdout: '',
stdout: [],
stderr: '',
};
let lastLine = '';

const proc = childProcess.spawn(command, args, spawnOptions);
if (PROC_TIMEOUT !== 0) {
setTimeout(kill(proc.pid, out), PROC_TIMEOUT);
}

proc.stdout.on('data', (data) => {
const strData = data.toString();
out.stdout = out.stdout + strData;
strData.split('\n').forEach((str) => {
const lines = data.toString().split('\n');

lines[0] = lastLine + lines[0];
lastLine = lines.pop();

lines.forEach((str) => {
out.stdout.push(str);
debugLogging(str);
});
if (strData.includes('(q)uit')) {
if (lastLine.includes('(q)uit')) {
proc.stdin.write('q\n');
debugLogging(
'sbt is requiring input. Provided (q)uit signal. ' +
Expand All @@ -58,12 +63,16 @@ export const execute = (
});

proc.on('close', (code) => {
out.stdout.push(lastLine);
debugLogging(lastLine);
lastLine = '';

if (code !== 0) {
const fullCommand = command + ' ' + args.join(' ');
const errorMessage =
`>>> command: ${fullCommand} ` +
(code ? `>>> exit code: ${code} ` : '') +
(out.stdout ? `>>> stdout: ${out.stdout} ` : '') +
`>>> stdout: ${out.stdout.join('\n')} ` +
(out.stderr ? `>>> stderr: ${out.stderr}` : 'null');
return reject(new Error(errorMessage));
}
Expand Down
1 change: 0 additions & 1 deletion lib/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export async function getSbtVersion(
try {
const stdout = await subProcess.execute('sbt', ['--version'], {});
return stdout
.split('\n')
.find((line) => !!line.match(/sbt script version/))!
.split(':')[1]
.trim();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"jest-junit": "^12.2.0",
"prettier": "^2.8.0",
"ts-jest": "^27.1.4",
"ts-node": "^10.2.1",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
},
"dependencies": {
Expand Down
50 changes: 29 additions & 21 deletions test/functional/parse-sbt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ function flatten(dependencies: DepTree): string[] {
}

test('parse `sbt dependencies` output: multi configuration', async () => {
const sbtOutput = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'sbt-dependency-output.txt'),
'utf8',
);
const sbtOutput = fs
.readFileSync(
path.join(__dirname, '..', 'fixtures', 'sbt-dependency-output.txt'),
'utf8',
)
.split('\n');
const depTree = parser.parse(sbtOutput, 'testApp', '1.0.1', false);

expect(depTree.name).toBe('testApp');
Expand All @@ -48,15 +50,17 @@ test('parse `sbt dependencies` output: multi configuration', async () => {
});

test('parse `sbt dependencies` output: single configuration', async () => {
const sbtOutput = fs.readFileSync(
path.join(
__dirname,
'..',
'fixtures',
'sbt-single-config-dependency-output.txt',
),
'utf8',
);
const sbtOutput = fs
.readFileSync(
path.join(
__dirname,
'..',
'fixtures',
'sbt-single-config-dependency-output.txt',
),
'utf8',
)
.split('\n');

const depTree = parser.parse(sbtOutput, 'unused', 'unused', false);

Expand Down Expand Up @@ -90,10 +94,12 @@ test('parse `sbt dependencies` output: single configuration', async () => {
});

test('parse `sbt dependencies` output: plugin 1.2.8', async () => {
const sbtOutput = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'sbt-plugin-1.2.8-output.txt'),
'utf8',
);
const sbtOutput = fs
.readFileSync(
path.join(__dirname, '..', 'fixtures', 'sbt-plugin-1.2.8-output.txt'),
'utf8',
)
.split('\n');
const depTree = parser.parseSbtPluginResults(
sbtOutput,
'com.example:hello_2.12',
Expand All @@ -114,10 +120,12 @@ test('parse `sbt dependencies` output: plugin 1.2.8', async () => {
});

test('parse `sbt dependencies` output: plugin 0.13', async () => {
const sbtOutput = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'sbt-plugin-0.13-output.txt'),
'utf8',
);
const sbtOutput = fs
.readFileSync(
path.join(__dirname, '..', 'fixtures', 'sbt-plugin-0.13-output.txt'),
'utf8',
)
.split('\n');
const depTree = parser.parseSbtPluginResults(
sbtOutput,
'com.example:hello_2.12',
Expand Down
6 changes: 4 additions & 2 deletions test/functional/version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ describe('version test', () => {

it('fall back onto sbt --version if checking build.properties fails', async () => {
const root = path.join(fixtures, 'empty-homdir');
jest.spyOn(subProcess, 'execute').mockResolvedValue(`
jest.spyOn(subProcess, 'execute').mockResolvedValue(
`
sbt version in this project: 1.5.5
sbt script version: 1.5.5
`);
`.split('\n'),
);
const received = await getSbtVersion(root, '');
expect(received).toBe('1.5.5');
});
Expand Down

0 comments on commit 6889328

Please sign in to comment.