Skip to content

Commit

Permalink
fix false positive recursive echo exec error #593
Browse files Browse the repository at this point in the history
Now after each command prev exec is reset so it can detect actual recursive echo call
  • Loading branch information
jcubic committed Jul 4, 2020
1 parent 4ddd359 commit 91d75cf
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.17.5
### Bugfix
* fix eating last bracket from split_equal [#597](https://github.com/jcubic/jquery.terminal/issues/597)
* fix false positive recursive echo error [#593](https://github.com/jcubic/jquery.terminal/issues/593)

## 2.17.4
### Bugfix
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ emoji:
./scripts/mkemoji $(VERSION) > css/emoji.css

test:
$(JEST) --coverage --testMatch '**/__tests__/*.spec.js'
$(JEST) --coverage --no-cache --testMatch '**/__tests__/*.spec.js'

test-accept-snapshots:
$(JEST) --coverage --updateSnapshot --testMatch '**/__tests__/*.spec.js'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ http://terminal.jcubic.pl

[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-DEV-yellow.svg)
[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=devel&25298521aa10e757ee819e5438c3503dbe1f757e)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&fadf6b0d68a1abc51ffa69cc4939ff12)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=devel&0dd8893bbead2c5f9577fc5c4ad8161bbdca70e7)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&5750bf4c2bd025ae34802a8577d09ee0)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
![downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded)](https://www.jsdelivr.com/package/npm/jquery.terminal)
[![LICENSE MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jcubic/jquery.terminal/blob/master/LICENSE)
Expand Down
43 changes: 39 additions & 4 deletions __tests__/terminal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5619,7 +5619,7 @@ describe('Terminal plugin', function() {
expect(term.cmd().prompt()).toEqual('>>>');
});
it('should not execute command by typing', function() {
term.exec('[[ foo ]]');
enter(term, '[[ foo ]]');
expect(interpreter.foo).not.toHaveBeenCalled();
expect(a0(term.find('.terminal-command').text())).toEqual('> [[ foo ]]');
});
Expand Down Expand Up @@ -5675,14 +5675,15 @@ describe('Terminal plugin', function() {
var term = $('<div/>').terminal(interpreter, {
greetings: false
});
// recursive call guard execute two times before showing error
spy(interpreter, 'foo');
term.exec('foo');
expect(count(interpreter.foo)).toEqual(1);
enter(term, 'foo');
expect(count(interpreter.foo)).toEqual(2);
expect(term.find('.terminal-error').length).toEqual(1);
expect(a0(term.find('.terminal-error').text()))
.toEqual($.terminal.defaults.strings.recursiveLoop);
term.echo('[[ foo ]]');
expect(count(interpreter.foo)).toEqual(2);
expect(count(interpreter.foo)).toEqual(3);
expect(term.find('.terminal-error').length).toEqual(2);
var output = term.find('.terminal-error').map(function() {
return a0($(this).text());
Expand All @@ -5692,6 +5693,40 @@ describe('Terminal plugin', function() {
$.terminal.defaults.strings.recursiveLoop
]);
});
it('should not show recursvie error on two exec calls', async function() {
// bug #593
var interpreter = {
foo: function() {
this.echo('[[ bar ]]');
},
bar: function() {
this.echo('hello');
},
exec: async function() {
return '[[ terminal::clear() ]]';
}
};
var term = $('<div/>').terminal(interpreter, {
greetings: false,
invokeMethods: true
});
spy(interpreter, 'bar');
enter(term, 'foo');
// even if command is sync the extended command reset is async
// but it should not matter if user type the command
await delay(50);
enter(term, 'foo');
await delay(50);
expect(count(interpreter.bar)).toEqual(2);
expect(term.find('.terminal-error').length).toEqual(0);
spy(term, 'clear');
enter(term, 'exec');
await delay(50);
enter(term, 'exec');
await delay(50);
expect(count(term.clear)).toEqual(2);
expect(term.find('.terminal-error').length).toEqual(0);
});
});
});
describe('error', function() {
Expand Down
45 changes: 28 additions & 17 deletions js/jquery.terminal-2.17.4.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Sat, 04 Jul 2020 11:21:16 +0000
* Date: Sat, 04 Jul 2020 13:18:22 +0000
*/
/* global define, Map */
/* eslint-disable */
Expand Down Expand Up @@ -4436,7 +4436,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Sat, 04 Jul 2020 11:21:16 +0000',
date: 'Sat, 04 Jul 2020 13:18:22 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -5788,37 +5788,46 @@
var settings = $.extend({
invokeMethods: false
}, options);
var deferred = new $.Deferred();
try {
change_hash = false;
var m = string.match(extended_command_re);
if (m) {
if (!settings.invokeMethods) {
warn('To invoke terminal or cmd methods you need to enable ' +
'invokeMethods option');
return;
}
string = m[1];
var obj = m[2] === 'terminal' ? term : term.cmd();
var fn = m[3];
try {
var args = eval('[' + m[4] + ']');
if (!obj[fn]) {
term.error('Unknow function ' + fn);
} else {
obj[fn].apply(term, args);
deferred.reject();
} else {
string = m[1];
var obj = m[2] === 'terminal' ? term : term.cmd();
var fn = m[3];
try {
var args = eval('[' + m[4] + ']');
if (!obj[fn]) {
term.error('Unknow function ' + fn);
} else {
var ret = obj[fn].apply(term, args);
if (ret && ret.then) {
return ret;
}
}
deferred.resolve();
} catch (e) {
term.error('Invalid invocation in ' +
$.terminal.escape_brackets(string));
deferred.reject();
}
} catch (e) {
term.error('Invalid invocation in ' +
$.terminal.escape_brackets(string));
}
} else {
term.exec(string, true).done(function() {
return term.exec(string, true).done(function() {
change_hash = true;
});
}
} catch (e) {
// error is process in exec
deferred.reject();
}
return deferred.promise();
},
// ---------------------------------------------------------------------
// :: ES6 iterator for a given string that handle emoji and formatting
Expand Down Expand Up @@ -7123,6 +7132,8 @@
prev_exec_cmd = trim;
$.terminal.extended_command(self, string, {
invokeMethods: line_settings.invokeMethods
}).then(function() {
prev_exec_cmd = '';
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal-2.17.4.min.js

Large diffs are not rendered by default.

41 changes: 26 additions & 15 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -5788,37 +5788,46 @@
var settings = $.extend({
invokeMethods: false
}, options);
var deferred = new $.Deferred();
try {
change_hash = false;
var m = string.match(extended_command_re);
if (m) {
if (!settings.invokeMethods) {
warn('To invoke terminal or cmd methods you need to enable ' +
'invokeMethods option');
return;
}
string = m[1];
var obj = m[2] === 'terminal' ? term : term.cmd();
var fn = m[3];
try {
var args = eval('[' + m[4] + ']');
if (!obj[fn]) {
term.error('Unknow function ' + fn);
} else {
obj[fn].apply(term, args);
deferred.reject();
} else {
string = m[1];
var obj = m[2] === 'terminal' ? term : term.cmd();
var fn = m[3];
try {
var args = eval('[' + m[4] + ']');
if (!obj[fn]) {
term.error('Unknow function ' + fn);
} else {
var ret = obj[fn].apply(term, args);
if (ret && ret.then) {
return ret;
}
}
deferred.resolve();
} catch (e) {
term.error('Invalid invocation in ' +
$.terminal.escape_brackets(string));
deferred.reject();
}
} catch (e) {
term.error('Invalid invocation in ' +
$.terminal.escape_brackets(string));
}
} else {
term.exec(string, true).done(function() {
return term.exec(string, true).done(function() {
change_hash = true;
});
}
} catch (e) {
// error is process in exec
deferred.reject();
}
return deferred.promise();
},
// ---------------------------------------------------------------------
// :: ES6 iterator for a given string that handle emoji and formatting
Expand Down Expand Up @@ -7123,6 +7132,8 @@
prev_exec_cmd = trim;
$.terminal.extended_command(self, string, {
invokeMethods: line_settings.invokeMethods
}).then(function() {
prev_exec_cmd = '';
});
}
}
Expand Down
45 changes: 28 additions & 17 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Sat, 04 Jul 2020 11:21:16 +0000
* Date: Sat, 04 Jul 2020 13:18:22 +0000
*/
/* global define, Map */
/* eslint-disable */
Expand Down Expand Up @@ -4436,7 +4436,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Sat, 04 Jul 2020 11:21:16 +0000',
date: 'Sat, 04 Jul 2020 13:18:22 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -5788,37 +5788,46 @@
var settings = $.extend({
invokeMethods: false
}, options);
var deferred = new $.Deferred();
try {
change_hash = false;
var m = string.match(extended_command_re);
if (m) {
if (!settings.invokeMethods) {
warn('To invoke terminal or cmd methods you need to enable ' +
'invokeMethods option');
return;
}
string = m[1];
var obj = m[2] === 'terminal' ? term : term.cmd();
var fn = m[3];
try {
var args = eval('[' + m[4] + ']');
if (!obj[fn]) {
term.error('Unknow function ' + fn);
} else {
obj[fn].apply(term, args);
deferred.reject();
} else {
string = m[1];
var obj = m[2] === 'terminal' ? term : term.cmd();
var fn = m[3];
try {
var args = eval('[' + m[4] + ']');
if (!obj[fn]) {
term.error('Unknow function ' + fn);
} else {
var ret = obj[fn].apply(term, args);
if (ret && ret.then) {
return ret;
}
}
deferred.resolve();
} catch (e) {
term.error('Invalid invocation in ' +
$.terminal.escape_brackets(string));
deferred.reject();
}
} catch (e) {
term.error('Invalid invocation in ' +
$.terminal.escape_brackets(string));
}
} else {
term.exec(string, true).done(function() {
return term.exec(string, true).done(function() {
change_hash = true;
});
}
} catch (e) {
// error is process in exec
deferred.reject();
}
return deferred.promise();
},
// ---------------------------------------------------------------------
// :: ES6 iterator for a given string that handle emoji and formatting
Expand Down Expand Up @@ -7123,6 +7132,8 @@
prev_exec_cmd = trim;
$.terminal.extended_command(self, string, {
invokeMethods: line_settings.invokeMethods
}).then(function() {
prev_exec_cmd = '';
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/jquery.terminal.min.js.map

Large diffs are not rendered by default.

0 comments on commit 91d75cf

Please sign in to comment.