Skip to content

Commit

Permalink
chore: Can confirm the npm is swallowing standard streams in test scr…
Browse files Browse the repository at this point in the history
…ipts.
  • Loading branch information
Anadian committed Jun 20, 2020
1 parent 834d96d commit 5f1e432
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 144 deletions.
118 changes: 65 additions & 53 deletions cli_test.sh
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
#!/bin/bash
declare -A test_results;
all_test_passed=1;
# Test: HelpData
test_name='HelpData';
test_results[$test_name]=0;
echo 'node source/main.js -Vhc';
node source/main.js -Vhc 1>temp_stdout.txt 2>temp_stderr.txt;
test_code=$?;
test_stdout=`cat temp_stdout.txt`;
test_stderr=`cat temp_stderr.txt`;
rm temp_stdout.txt temp_stderr.txt;
if [[ $test_code ]]; then
test_results[$test_name]=1;
else
test_results[$test_name]=0;
echo "Test: $test_name failed with erroneous exit code $test_code (stderr: '$test_stderr')";
fi
rm temp_stderr.txt;
node -p 'const FileSystem = require("fs"); FileSystem.writeSync(2, "something");' 2>temp_stderr.txt;
echo "stdout: stderr: $(cat temp_stderr.txt)";
# env;
# echo $0 $SHELL;
# # which node;
# # tty;
# # uname -a;
# # whoami;
# declare -A test_results;
# all_test_passed=1;
# # Test: HelpData
# test_name='HelpData';
# test_results[$test_name]=0;
# echo 'node source/main.js -Vhc';
# node source/main.js -Vhc 1>temp_stdout.txt 2>temp_stderr.txt;
# test_code=$?;
# test_stdout=`cat temp_stdout.txt`;
# test_stderr=`cat temp_stderr.txt`;
# # rm temp_stdout.txt temp_stderr.txt;
# if [[ $test_code ]]; then
# test_results[$test_name]=1;
# echo "Test: $test_name passed. (stdout: '$test_stdout' stderr: '$test_stderr')";
# else
# test_results[$test_name]=0;
# echo "Test: $test_name failed with erroneous exit code $test_code (stderr: '$test_stderr')";
# fi

# Test: InputRegexStringToSTDOUT
test_name='InputRegexStringToSTDOUT';
test_results[$test_name]=0;
node source/main.js --input-regex-string 'pcre/(simple)? regex/replace/vim' -o 1>temp_stdout.txt 2>temp_stderr.txt;
test_code=$?;
test_stdout=`cat temp_stdout.txt`;
test_stderr=`cat temp_stderr.txt`;
rm temp_stdout.txt temp_stderr.txt;
expected_stdout='\(simple\)\= regex';
if [[ $test_code ]]; then
echo $test_stdout;
if [[ "$test_stdout" == "$expected_stdout" ]]; then
test_results[$test_name]=1;
echo "Test: $test_name passed.";
else
test_results[$test_name]=0;
echo "Test: $test_name failed: actual stdout: '$test_stdout' didn't match expected stdout: '$expected_stdout' (stderr: '$test_stderr')";
fi
else
test_results[help_data]=0;
echo "Test: $test_name returned an erroneous exit code: $test_code stderr: '$test_stderr'";
fi
# $1est_name='InputRegexStringToSTDOUT';
# $1est_results[$test_name]=0;
# $1ode source/main.js --input-regex-string 'pcre/(simple)? regex/replace/vim' -o 1>temp_stdout.txt 2>temp_stderr.txt;
# $1est_code=$?;
# $1est_stdout=`cat temp_stdout.txt`;
# $1est_stderr=`cat temp_stderr.txt`;
# $1 rm temp_stdout.txt temp_stderr.txt;
# $1xpected_stdout='\(simple\)\= regex';
# $1f [[ $test_code ]]; then
# $1echo $test_stdout;
# $1if [[ "$test_stdout" == "$expected_stdout" ]]; then
# $1 test_results[$test_name]=1;
# $1 echo "Test: $test_name passed.";
# $1else
# $1 test_results[$test_name]=0;
# $1 echo "Test: $test_name failed: actual stdout: '$test_stdout' didn't match expected stdout: '$expected_stdout' (stderr: '$test_stderr')";
# $1fi
# $1lse
# $1test_results[help_data]=0;
# $1echo "Test: $test_name returned an erroneous exit code: $test_code stderr: '$test_stderr'";
# $1i

# Test: InputFileToOutputFile
test_name='InputFileToOutputFile';
test_results[$test_name]=0;
echo ':([-0-9A-Za-z_ ]*)?:([0-9A-Za-z_]+)?:(([-0-9A-Za-z]+)(=([0-9A-Za-z]))?)?:(0|(([NIRBWAUFDnirbwaufd])[^!?=;]*([!?])(=(((\|)|(([-0-9A-Za-z_.\/\\ ]+)([!?])?)))+)?));' > temp_input.txt;
echo $'node source/main.js -vx --input temp_input.txt --input-flavour \'pcre\' --output-flavour \'vim\' --output \'temp_output.txt\'';
node source/main.js -vx --input temp_input.txt --input-flavour 'pcre' --output-flavour 'vim' --output 'temp_output.txt' 1>temp_stdout.txt 2>temp_stderr.txt;
test_code=$?;
test_stdout=`cat temp_stdout.txt`;
test_stderr=`cat temp_stderr.txt`;
rm temp_stdout.txt temp_stderr.txt;
if [[ $test_code ]]; then
cat temp_output.txt;
else
test_results[$test_name]=0;
echo "Test: $test_name failed with erroneous exit code $test_code (stderr: '$test_stderr')";
fi
# $1est_name='InputFileToOutputFile';
# $1est_results[$test_name]=0;
# $1cho ':([-0-9A-Za-z_ ]*)?:([0-9A-Za-z_]+)?:(([-0-9A-Za-z]+)(=([0-9A-Za-z]))?)?:(0|(([NIRBWAUFDnirbwaufd])[^!?=;]*([!?])(=(((\|)|(([-0-9A-Za-z_.\/\\ ]+)([!?])?)))+)?));' > temp_input.txt;
# $1cho $'node source/main.js -vx --input temp_input.txt --input-flavour \'pcre\' --output-flavour \'vim\' --output \'temp_output.txt\'';
# $1ode source/main.js -vx --input temp_input.txt --input-flavour 'pcre' --output-flavour 'vim' --output 'temp_output.txt' 1>temp_stdout.txt 2>temp_stderr.txt;
# $1est_code=$?;
# $1est_stdout=`cat temp_stdout.txt`;
# $1est_stderr=`cat temp_stderr.txt`;
# $1xpected_output_data=':\([-0-9A-Za-z_ ]*\)\=:\([0-9A-Za-z_]\+\)\=:\(\([-0-9A-Za-z]\+\)\(=\([0-9A-Za-z]\)\)\=\)\=:\(0\|\(\([NIRBWAUFDnirbwaufd]\)[^!\==;]*\([!\=]\)\(=\(\(\(|\)\|\(\([-0-9A-Za-z_.\/\\ ]\+\)\([!\=]\)\=\)\)\)\+\)\=\)\);\r';
# $1m temp_stdout.txt temp_stderr.txt;
# $1f [[ $test_code ]]; then
# $1output_data=`cat temp_output.txt`;

# $1lse
# $1test_results[$test_name]=0;
# $1echo "Test: $test_name failed with erroneous exit code $test_code (stderr: '$test_stderr')";
# $1i

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Convert a Regular Expression from one flavour to another.",
"main": "source/main.js",
"scripts": {
"test": "ava -v ./source/main.test.js && ./cli_test.sh",
"test": "ava -v ./source/main.test.js && bash cli_test.sh",
"lint": "eslint ./source/main.js",
"generate-docs": "extract-documentation-comments -I source/main.js -O API.md",
"update-travis-config": "json2yaml --preserve-key-order ci/travis.json .travis.yml"
Expand Down
51 changes: 33 additions & 18 deletions source/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var MetaRegexObject = {
},
"CHARACTER_CLASS": {
to: {
search_regex: /([^\\])\[(([^\]]{1,2})|[^:]([^\]]*?)[^:])\]/g,
search_regex: /([^\\])\[(([^\]]{1,2})|([^:\]]([^\]]*?)[^:]))\]/g,
replace_string: '$1<CHARACTER_CLASS_START:$2:CHARACTER_CLASS_END>'
},
from: {
Expand Down Expand Up @@ -628,7 +628,7 @@ var MetaRegexObject = {
},
"CHARACTER_CLASS": {
to: {
search_regex: /([^\\])\[(([^\]]{1,2})|[^:]([^\]]*?)[^:])\]/g,
search_regex: /([^\\])\[(([^\]]{1,2})|([^:\]]([^\]]*?)[^:]))\]/g,
replace_string: '$1<CHARACTER_CLASS_START:$2:CHARACTER_CLASS_END>'
},
from: {
Expand Down Expand Up @@ -1160,7 +1160,7 @@ var MetaRegexObject = {
},
"CHARACTER_CLASS": {
to: {
search_regex: /([^\\])\[(([^\]]{1,2})|[^:]([^\]]*?)[^:])\]/g,
search_regex: /([^\\])\[(([^\]]{1,2})|([^:\]]([^\]]*?)[^:]))\]/g,
replace_string: '$1<CHARACTER_CLASS_START:$2:CHARACTER_CLASS_END>'
},
from: {
Expand Down Expand Up @@ -1873,7 +1873,7 @@ Parametres:
| name | type | description |
| --- | --- | --- |
| regex_string | {string} | The regular expression string to be converted to a mediary object. |
| input_flavour | {string} | The flavour of the regex string. \[default: \] |
| flavour_string | {string} | The flavour of the regex string. \[default: \] |
| options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] |
Returns:
Expand All @@ -1891,15 +1891,17 @@ Status:
| --- | --- |
| 0.2.3 | Introduced: Breaking change; function now returns an object with an `intermediary_string` property and a `character_class_codes_array` property if necessary. |
*/
function getMediaryObjectFromRegexString( regex_string, input_flavour = 'pcre', options = {},){
function getMediaryObjectFromRegexString( regex_string, flavour_string = 'pcre', options = {},){
var arguments_array = Array.from(arguments);
var _return;
var return_error;
const FUNCTION_NAME = 'getMediaryObjectFromRegexString';
Logger.log({process: PROCESS_NAME, module: MODULE_NAME, file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${arguments_array}`});
//Variables
var length = 0;
var to_object = {};
var to_values_array = [];
var character_class_code_matches = null;
var character_classes_array = [];
var character_class_codes = [];
var intermediary_string = regex_string;
Expand All @@ -1909,27 +1911,38 @@ function getMediaryObjectFromRegexString( regex_string, input_flavour = 'pcre',
return_error.code = 'ERR_INVALID_ARG_TYPE';
throw return_error;
}
if( typeof(input_flavour_string) !== 'string' ){
return_error = new TypeError('Param "input_flavour_string" is not string.');
if( typeof(flavour_string) !== 'string' ){
return_error = new TypeError('Param "flavour_string" is not string.');
return_error.code = 'ERR_INVALID_ARG_TYPE';
throw return_error;
}

//Function
to_object = MetaRegexObject[input_flavour_string];
to_object = MetaRegexObject[flavour_string];
//console.log('to_object: %o', to_object);
Logger.log({process: PROCESS_NAME, module: MODULE_NAME, file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `to_object: ${to_object}`});
to_values_array = Array.from(Object.values(to_object));
//console.log('to_values_array: %o', to_values_array);
Logger.log({process: PROCESS_NAME, module: MODULE_NAME, file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `to_values_array: ${to_values_array}`});
//LLT
intermediary_string = intermediary_string.replace( to_object['LLT'].to.serch_regex, to_object['LLT'].to.replace_string );
//LGT
intermediary_string = intermediary_string.replace( to_object['LGT'].to.serch_regex, to_object['LGT'].to.replace_string );
//CHARACTER_CLASS
intermediary_string = intermediary_string.replace( to_object['CHARACTER_CLASS'].to.serch_regex, to_object['CHARACTER_CLASS'].to.replace_string );
try{
character_classes_array = Array.from( intermediary_string.matchAll( to_object['CHARACTER_CLASS_CODE'].to.search_regex ) );
for( var i = 0; i < character_classes_array; i++ ){
character_class_codes.push( character_classes_array[i][1] );
//LLT
intermediary_string = intermediary_string.replace( to_object['LLT'].to.search_regex, to_object['LLT'].to.replace_string );
//LGT
intermediary_string = intermediary_string.replace( to_object['LGT'].to.search_regex, to_object['LGT'].to.replace_string );
//CHARACTER_CLASS
intermediary_string = intermediary_string.replace( to_object['CHARACTER_CLASS'].to.search_regex, to_object['CHARACTER_CLASS'].to.replace_string );
} catch(error){
return_error = new Error(`Caught an unexpected error processing the special meta-translational symbol: ${error}`);
throw return_error;
}
try{
character_class_code_matches = intermediary_string.matchAll( to_object['CHARACTER_CLASS_CODE'].to.search_regex );
//console.log('character_class_code_matches: %o', character_class_code_matches);
character_classes_array = Array.from( character_class_code_matches );
//console.log('character_classes_array: %o', character_classes_array);
for( var i = 0; i < character_classes_array.length; i++ ){
length = character_class_codes.push( character_classes_array[i][1] );
intermediary_string = intermediary_string.replace( to_object['CHARACTER_CLASS'].from.search_regex, `<CHARACTER_CLASS_CODE_START:${(length-1)}:CHARACTER_CLASS_CODE_END>` );
}
} catch(error){
return_error = new Error(`Caught an unexpected error when creating character classes code arrays: ${error}`);
Expand All @@ -1941,7 +1954,7 @@ function getMediaryObjectFromRegexString( regex_string, input_flavour = 'pcre',
}
_return = {
mediary_string: intermediary_string,
character_class_codes_array: characer_class_codes
character_class_codes_array: character_class_codes
};

//Return
Expand Down Expand Up @@ -2336,5 +2349,7 @@ if(require.main === module){
exports.getMediaryStringFromRegexString = getMediaryStringFromRegexString;
exports.getRegexStringFromMediaryString = getRegexStringFromMediaryString;
exports.getMultiPartObjectFromInputString = getMultiPartObjectFromInputString;
exports.getMediaryObjectFromRegexString = getMediaryObjectFromRegexString;
exports.getRegexStringFromMediaryObject = getRegexStringFromMediaryObject;
}

88 changes: 20 additions & 68 deletions source/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,78 +144,30 @@ AVA('getMultiPartObjectFromInputString:MaximalInputString', function(t){
var actual_output = RegexTranslator.getMultiPartObjectFromInputString( input_string );
t.deepEqual( actual_output, expected_output );
});
AVA('CLI:HelpData', async function(t){
//t.log(process.cwd());
//t.log(process.env);
var return_error = null;
var process_object = ChildProcess.fork( 'source/main.js', ['-Vhc'], { silent: true } );
var promisified_process_object = Pify( process_object, { multiArgs: true, errorFirst: false } );
//t.log( process_object );
var results = await promisified_process_object.on( 'exit' );
t.log(results);
if( results[0] === 0 ){
t.pass();
} else{
return_error = new Error(`Erroneous exit code: ${results[0]}, signal: ${results[1]}`);
t.fail(return_error);
}
AVA('getMediaryObjectFromRegexString:InvalidRegexString', function(t){
t.throws( RegexTranslator.getMediaryObjectFromRegexString.bind( null, {}, 'pcre' ), { instanceOf: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
});
AVA('CLI:INPUT-REGEX-STRING-to-STDOUT', async function(t){
//t.plan(2);
var return_error = null;
var process_object = ChildProcess.fork( 'source/main.js', ['--input-regex-string', '"pcre/(simple)? regex/replace/vim"', '-o'], { silent: true } );
var promisified_process_object = Pify( process_object, { multiArgs: true, errorFirst: false } );
//t.log(promisified_process_object);
var promisified_stdio_object = Pify( promisified_process_object.stdio );
var promisified_stdout_object = Pify( promisified_stdio_object[1] );
var stdout_data = await promisified_stdout_object.on( 'data' );
//var results = await promisified_process_object.on( 'exit' );
//t.log(results);
var expected_stdout_string = '\\(simple\\)\\= regex';
var stdout_string = stdout_data.toString( 'utf8' );
t.is(stdout_string,expected_stdout_string);
/*if( results[0] === 0 ){
t.pass();
} else{
return_error = new Error(`Erroneous exit code: ${results[0]}, signal: ${results[1]}`);
t.fail(return_error.message);
}*/
AVA('getMediaryObjectFromRegexString:InvalidFlavourString', function(t){
var params = {
regex_string: '^t*h+i?s{5,10} \\(is\\) [a] \\$+?i*?\\{m\\}\\[p\\].e\\^ \\| <pcre> (r|R)e{1,3}?\\{gex\\}\\.\\+\\*\\?=$\\/\\{[:digit:] \\d \\D \\w \\W [:alnum:] [:graph:] [:lower:] [:punct:] [:upper:] [:xdigit:] \\N [:blank:] \\h \\H [:space:] \\s \\v \\S \\V [:R:]\\\\/',
flavour_string: {}
};
t.throws( RegexTranslator.getMediaryObjectFromRegexString.bind( null, params.regex_string, params.flavour_string ), { instanceOf: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
});
AVA.skip('CLI:INPUT-FILE-to-OUTPUT-FILE', function(t){
var process_object = null;
var exit_func = function( code, signal ){
var expected_stdout_string = '\\(simple\\)\\= regex';
var output_string = '';
if( code === 0 ){
try{
output_string = FileSystem.readFileSync( 'temp_out2.txt', 'utf8' );
try{
FileSystem.unlinkSync( 'temp_out2.txt' );
FileSystem.unlinkSync( 'temp_in.txt' );
t.is(output_string,expected_stdout_string);
} catch(error){
return_error = new Error(`FileSystem.unlinkSync threw an error: ${error}`);
t.fail(return_error);
}
} catch(error){
return_error = new Error(`FileSystem.readFileSync threw an error: ${error}`);
t.fail(return_error);
}
} else{
return_error = new Error(`Erroneous exit code: ${code} signal: ${signal}`);
t.fail(return_error);
}
t.end();
AVA('getMediaryObjectFromRegexString:SuccessPCRE', function(t){
var params = {
regex_string: '^t*h+i?s{5,10} \\(is\\) [a] \\$+?i*?\\{m\\}\\[p\\].e\\^ \\| <pcre> (r|R)e{1,3}?\\{gex\\}\\.\\+\\*\\?=$\\/\\{[:digit:] \\d \\D \\w \\W [:alnum:] [:graph:] [:lower:] [:punct:] [:upper:] [:xdigit:] \\N [:blank:] \\h \\H [:space:] \\s \\v \\S \\V [:R:]\\\\/',
flavour_string: 'pcre'
};
var expected = {
mediary_string: '<SL>t<ZMQ>h<OMQ>i<ZOQ>s<VRQ_START:5:10:VRQ_END> <LOP>is<LCP> <CHARACTER_CLASS_CODE_START:0:CHARACTER_CLASS_CODE_END> <LDS><LOMQ>i<LZMQ><%LOC%>m<%LCC%><%LOB%>p<%LCB%><MAC>e<LCS> <LPIPE> <%LLT%>pcre<%LGT%> <MOP>r<ORA>R<MCP>e<LVRQ_START:1:3:LVRQ_END><%LOC%>gex<%LCC%><LP><LPS><LAS><LQM><LES><EL><LFS><%LOC%><CC_DIGIT> <CC_DIGIT> <CC_NOTDIGIT> <CC_WORD> <CC_NOTWORD> <CC_alnum> <CC_graph> <CC_lower> <CC_punct> <CC_upper> <CC_xdigit> <CC_NOTNEWLINE> <CC_HORIZONTALSPACE> <CC_HORIZONTALSPACE> <CC_NOTHORIZONTALSPACE> <CC_VERTICALSPACE> <CC_VERTICALSPACE> <CC_VERTICALSPACE> <CC_NOTVERTICALSPACE> <CC_NOTVERTICALSPACE> <CC_R><LBS><RS>',
character_class_codes_array: [ 'a' ]
};
//var true_exit_func = exit_func.bind( t );
try{
FileSystem.writeFileSync( 'temp_in.txt', '(simple)? regex', 'utf8' );
process_object = ChildProcess.fork( 'source/main.js', ['-vx', '-I', 'temp_in.txt', '--input-flavour', 'pcre', '--output-flavour', 'vim', '-O', 'temp_out2.txt'], { silent: true } );
t.log(process_object);
exit_func.bind
process_object.on('exit', exit_func);
var mediary_object = RegexTranslator.getMediaryObjectFromRegexString( params.regex_string, params.flavour_string );
console.log('%o', mediary_object);
t.deepEqual( mediary_object, expected );
} catch(error){
return_error = new Error(`FileSystem.writeFileSync threw an error: ${error}`);
t.fail(return_error);
t.fail(`RegexTranslator.getMediaryObjectFromRegexString threw an unexpected error: ${error}`);
}
});
2 changes: 2 additions & 0 deletions super_cli_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./cli_test.sh;
1 change: 0 additions & 1 deletion temp_in.txt

This file was deleted.

1 change: 0 additions & 1 deletion temp_input.txt

This file was deleted.

1 change: 0 additions & 1 deletion temp_out2.txt

This file was deleted.

1 change: 0 additions & 1 deletion temp_output.txt

This file was deleted.

Empty file added temp_stderr.txt
Empty file.

0 comments on commit 5f1e432

Please sign in to comment.