Commit 14b4425f authored by Maurelian's avatar Maurelian Committed by GitHub

Add allow-empty overload field on Process.run (#10736)

parent ab663b0b
...@@ -10,15 +10,28 @@ library Process { ...@@ -10,15 +10,28 @@ library Process {
/// @notice Foundry cheatcode VM. /// @notice Foundry cheatcode VM.
Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code"))))); Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
function run(string[] memory cmd) internal returns (bytes memory stdout_) { /// @notice Run a command in a subprocess. Fails if no output is returned.
Vm.FfiResult memory result = vm.tryFfi(cmd); /// @param _command Command to run.
function run(string[] memory _command) internal returns (bytes memory stdout_) {
stdout_ = run({ _command: _command, _allowEmpty: false });
}
/// @notice Run a command in a subprocess.
/// @param _command Command to run.
/// @param _allowEmpty Allow empty output.
function run(string[] memory _command, bool _allowEmpty) internal returns (bytes memory stdout_) {
Vm.FfiResult memory result = vm.tryFfi(_command);
string memory command;
for (uint256 i = 0; i < _command.length; i++) {
command = string.concat(command, _command[i], " ");
}
if (result.exitCode != 0) { if (result.exitCode != 0) {
string memory command;
for (uint256 i = 0; i < cmd.length; i++) {
command = string.concat(command, cmd[i], " ");
}
revert FfiFailed(string.concat("Command: ", command, "\nError: ", string(result.stderr))); revert FfiFailed(string.concat("Command: ", command, "\nError: ", string(result.stderr)));
} }
// If the output is empty, result.stdout is "[]".
if (!_allowEmpty && keccak256(result.stdout) == keccak256(bytes("[]"))) {
revert FfiFailed(string.concat("No output from Command: ", command));
}
stdout_ = result.stdout; stdout_ = result.stdout;
} }
} }
...@@ -37,7 +37,7 @@ contract L2GenesisTest is Test { ...@@ -37,7 +37,7 @@ contract L2GenesisTest is Test {
commands[0] = "bash"; commands[0] = "bash";
commands[1] = "-c"; commands[1] = "-c";
commands[2] = string.concat("rm ", path); commands[2] = string.concat("rm ", path);
Process.run(commands); Process.run({ _command: commands, _allowEmpty: true });
} }
/// @notice Returns the number of top level keys in a JSON object at a given /// @notice Returns the number of top level keys in a JSON object at a given
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment