Skip to content

Commit

Permalink
update: executeCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Brianspha committed Jan 16, 2025
1 parent 8eb4f36 commit 342ebc9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
43 changes: 12 additions & 31 deletions crates/solidity/src/tests/cli-tests/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
import * as shell from 'shelljs';
import * as fs from 'fs';
import { spawn } from 'child_process';
import { spawnSync } from 'child_process';

interface CommandResult {
output: string;
exitCode: number;
}

export const executeCommandWithStdin = (command: string, stdin: string): Promise<CommandResult> => {
return new Promise((resolve, reject) => {
const process = spawn(command, [], { shell: true });

let stdout = '';
let stderr = '';

process.stdout.on('data', (chunk) => {
stdout += chunk;
});

process.stderr.on('data', (chunk) => {
stderr += chunk;
});

process.on('close', (exitCode) => {
resolve({
exitCode: exitCode || 0,
output: (stdout || stderr).toString()
});
export const executeCommand = (command: string, stdin?: string): CommandResult => {
if (stdin) {
const process = spawnSync(command, [], {
input: stdin,
shell: true,
encoding: 'utf-8'
});

process.on('error', (error) => {
reject(new Error(`Failed to execute command: ${error.message}`));
});

process.stdin.write(stdin);
process.stdin.end();
});
};

return {
exitCode: process.status || 0,
output: (process.stdout || process.stderr || '').toString()
};
}

export const executeCommand = (command: string): CommandResult => {
const result = shell.exec(command, { silent: true, async: false });
return {
exitCode: result.code,
Expand Down
7 changes: 4 additions & 3 deletions crates/solidity/src/tests/cli-tests/tests/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { executeCommand, isFolderExist, isFileExist, isFileEmpty, executeCommandWithStdin } from "../src/helper";
import { executeCommand, isFolderExist, isFileExist, isFileEmpty } from "../src/helper";
import { paths } from '../src/entities';
import * as shell from 'shelljs';
import * as path from 'path';
Expand Down Expand Up @@ -171,20 +171,21 @@ describe("Standard JSON compilation with path options", () => {
describe("Output with all path options", () => {
let result: { exitCode: number; output: string };

beforeAll(async () => {
beforeAll(() => {
const tempInputFile = path.join(contractsDir, 'temp-input.json');
shell.cp(inputFile, tempInputFile);
const inputContent = shell.cat(inputFile).toString();

const command = `resolc --standard-json --base-path "${contractsDir}" --include-path "${contractsDir}" --allow-paths "${contractsDir}"`;

result = await executeCommandWithStdin(command, inputContent);
result = executeCommand(command, inputContent);

shell.rm(tempInputFile);

});

it("Compiler run successful without emiting warnings", () => {
console.log("result.output: ", result.output)
const parsedResults = JSON.parse(result.output)
expect(parsedResults.errors.filter((error: { type: string; }) => error.type != 'Warning')).toEqual([]);
});
Expand Down

0 comments on commit 342ebc9

Please sign in to comment.