mirror of
https://github.com/electron/node-gyp.git
synced 2025-09-16 22:13:39 +02:00
test: formatting and minor fixes for execFileSync replacement
PR-URL: https://github.com/nodejs/node-gyp/pull/1521 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
parent
60e421363f
commit
6cd84b84fc
3 changed files with 140 additions and 143 deletions
138
test/process-exec-sync.js
Normal file
138
test/process-exec-sync.js
Normal file
|
@ -0,0 +1,138 @@
|
|||
'use strict'
|
||||
|
||||
var fs = require('graceful-fs')
|
||||
var child_process = require('child_process')
|
||||
|
||||
if (!String.prototype.startsWith) {
|
||||
String.prototype.startsWith = function(search, pos) {
|
||||
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search
|
||||
}
|
||||
}
|
||||
|
||||
function processExecSync(file, args, options) {
|
||||
var child, error, timeout, tmpdir, command, quote
|
||||
command = makeCommand(file, args)
|
||||
|
||||
/*
|
||||
this function emulates child_process.execSync for legacy node <= 0.10.x
|
||||
derived from https://github.com/gvarsanyi/sync-exec/blob/master/js/sync-exec.js
|
||||
*/
|
||||
|
||||
options = options || {}
|
||||
// init timeout
|
||||
timeout = Date.now() + options.timeout
|
||||
// init tmpdir
|
||||
var os_temp_base = '/tmp'
|
||||
var os = determine_os()
|
||||
os_temp_base = '/tmp'
|
||||
|
||||
if (process.env.TMP) {
|
||||
os_temp_base = process.env.TMP
|
||||
}
|
||||
|
||||
if (os_temp_base[os_temp_base.length - 1] !== '/') {
|
||||
os_temp_base += '/'
|
||||
}
|
||||
|
||||
tmpdir = os_temp_base + 'processExecSync.' + Date.now() + Math.random()
|
||||
fs.mkdirSync(tmpdir)
|
||||
|
||||
// init command
|
||||
if (os === 'linux') {
|
||||
command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir +
|
||||
'/stderr); echo $? > ' + tmpdir + '/status'
|
||||
} else {
|
||||
command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir +
|
||||
'/stderr) | echo %errorlevel% > ' + tmpdir + '/status | exit'
|
||||
}
|
||||
|
||||
// init child
|
||||
child = child_process.exec(command, options)
|
||||
|
||||
var maxTry = 100000 // increases the test time by 6 seconds on win-2016-node-0.10
|
||||
var tryCount = 0
|
||||
while (tryCount < maxTry) {
|
||||
try {
|
||||
var x = fs.readFileSync(tmpdir + '/status')
|
||||
if (x.toString() === '0') {
|
||||
break
|
||||
}
|
||||
} catch (ignore) {}
|
||||
tryCount++
|
||||
if (Date.now() > timeout) {
|
||||
error = child
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
['stdout', 'stderr', 'status'].forEach(function (file) {
|
||||
child[file] = fs.readFileSync(tmpdir + '/' + file, options.encoding)
|
||||
setTimeout(unlinkFile, 500, tmpdir + '/' + file)
|
||||
})
|
||||
|
||||
child.status = Number(child.status)
|
||||
if (child.status !== 0) {
|
||||
error = child
|
||||
}
|
||||
|
||||
try {
|
||||
fs.rmdirSync(tmpdir)
|
||||
} catch (ignore) {}
|
||||
if (error) {
|
||||
throw error
|
||||
}
|
||||
return child.stdout
|
||||
}
|
||||
|
||||
function makeCommand(file, args) {
|
||||
var command, quote
|
||||
command = file
|
||||
if (args.length > 0) {
|
||||
for(var i in args) {
|
||||
command = command + ' '
|
||||
if (args[i][0] === '-') {
|
||||
command = command + args[i]
|
||||
} else {
|
||||
if (!quote) {
|
||||
command = command + '\"'
|
||||
quote = true
|
||||
}
|
||||
command = command + args[i]
|
||||
if (quote) {
|
||||
if (args.length === (parseInt(i) + 1)) {
|
||||
command = command + '\"'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return command
|
||||
}
|
||||
|
||||
function determine_os() {
|
||||
var os = ''
|
||||
var tmpVar = ''
|
||||
if (process.env.OSTYPE) {
|
||||
tmpVar = process.env.OSTYPE
|
||||
} else if (process.env.OS) {
|
||||
tmpVar = process.env.OS
|
||||
} else {
|
||||
//default is linux
|
||||
tmpVar = 'linux'
|
||||
}
|
||||
|
||||
if (tmpVar.startsWith('linux')) {
|
||||
os = 'linux'
|
||||
}
|
||||
if (tmpVar.startsWith('win')) {
|
||||
os = 'win'
|
||||
}
|
||||
|
||||
return os
|
||||
}
|
||||
|
||||
function unlinkFile(file) {
|
||||
fs.unlinkSync(file)
|
||||
}
|
||||
|
||||
module.exports = processExecSync
|
|
@ -1,141 +0,0 @@
|
|||
var fs = require('graceful-fs')
|
||||
var child_process = require('child_process')
|
||||
var exec = child_process.exec
|
||||
|
||||
if (!String.prototype.startsWith) {
|
||||
String.prototype.startsWith = function(search, pos) {
|
||||
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
|
||||
};
|
||||
}
|
||||
|
||||
function processExecSync(file, args, options) {
|
||||
var child, error, timeout, tmpdir, command, quote;
|
||||
command = makeCommand(file, args);
|
||||
|
||||
/*
|
||||
this function emulates child_process.execSync for legacy node <= 0.10.x
|
||||
derived from https://github.com/gvarsanyi/sync-exec/blob/master/js/sync-exec.js
|
||||
*/
|
||||
|
||||
options = options || {};
|
||||
// init timeout
|
||||
timeout = Date.now() + options.timeout;
|
||||
// init tmpdir
|
||||
var os_temp_base = "/tmp";
|
||||
var os = determine_os();
|
||||
os_temp_base = "/tmp"
|
||||
|
||||
if(process.env.TMP){
|
||||
os_temp_base = process.env.TMP;
|
||||
}
|
||||
|
||||
if(os_temp_base[os_temp_base.length - 1] !== "/"){
|
||||
os_temp_base += "/";
|
||||
}
|
||||
|
||||
tmpdir = os_temp_base+'processExecSync.' + Date.now() + Math.random();
|
||||
fs.mkdirSync(tmpdir);
|
||||
|
||||
// init command
|
||||
if(os === "linux"){
|
||||
command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir +
|
||||
'/stderr); echo $? > ' + tmpdir + '/status';
|
||||
}else{
|
||||
command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir +
|
||||
'/stderr) | echo %errorlevel% > ' + tmpdir + '/status | exit';
|
||||
}
|
||||
|
||||
// init child
|
||||
child = exec(command, options, function () {
|
||||
return;
|
||||
});
|
||||
|
||||
var maxTry = 100000; // increases the test time by 6 seconds on win-2016-node-0.10
|
||||
var tryCount = 0;
|
||||
while (tryCount < maxTry) {
|
||||
try {
|
||||
var x = fs.readFileSync(tmpdir + '/status');
|
||||
if(x.toString() === "0"){
|
||||
break;
|
||||
}
|
||||
} catch (ignore) {
|
||||
}
|
||||
tryCount++;
|
||||
if (Date.now() > timeout) {
|
||||
error = child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
['stdout', 'stderr', 'status'].forEach(function (file) {
|
||||
child[file] = fs.readFileSync(tmpdir + '/' + file, options.encoding);
|
||||
setTimeout(unlinkFile, 500, tmpdir + '/' + file);
|
||||
});
|
||||
|
||||
child.status = Number(child.status);
|
||||
if (child.status !== 0) {
|
||||
error = child;
|
||||
}
|
||||
|
||||
try {
|
||||
fs.rmdirSync(tmpdir);
|
||||
} catch (ignore) {
|
||||
}
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return child.stdout;
|
||||
}
|
||||
|
||||
module.exports = processExecSync;
|
||||
|
||||
function makeCommand(file, args){
|
||||
var command, quote;
|
||||
command = file
|
||||
if(args.length > 0){
|
||||
for(var i in args){
|
||||
command = command + " ";
|
||||
if(args[i][0] === "-"){
|
||||
command = command + args[i];
|
||||
}else{
|
||||
if(!quote){
|
||||
command = command + "\"";
|
||||
quote = true;
|
||||
}
|
||||
command = command + args[i];
|
||||
if(quote){
|
||||
if(args.length === (parseInt(i) + 1)){
|
||||
command = command + "\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
function determine_os(){
|
||||
var os = "";
|
||||
var tmpVar = "";
|
||||
if(process.env.OSTYPE){
|
||||
tmpVar = process.env.OSTYPE;
|
||||
}else if(process.env.OS){
|
||||
tmpVar = process.env.OS;
|
||||
}else{
|
||||
//default is linux
|
||||
tmpVar = "linux";
|
||||
}
|
||||
|
||||
if(tmpVar.startsWith("linux")){
|
||||
os = "linux"
|
||||
}
|
||||
if(tmpVar.startsWith("win")){
|
||||
os = "win"
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
function unlinkFile(file){
|
||||
fs.unlinkSync(file);
|
||||
}
|
|
@ -6,12 +6,12 @@ var fs = require('graceful-fs')
|
|||
var child_process = require('child_process')
|
||||
var addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
|
||||
var nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
|
||||
var execFileSync = child_process.execFileSync || require('./processExecSync')
|
||||
var execFileSync = child_process.execFileSync || require('./process-exec-sync')
|
||||
var execFile = child_process.execFile
|
||||
|
||||
function runHello() {
|
||||
var testCode = "console.log(require('hello_world').hello())"
|
||||
return execFileSync('node', ['-e', testCode], { cwd: __dirname }).toString()
|
||||
return execFileSync(process.execPath, ['-e', testCode], { cwd: __dirname }).toString()
|
||||
}
|
||||
|
||||
function getEncoding() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue