backport fix GROOVY-7562 for the 2_4_X branch Groovysh: Fix imports not working at all in interpreter mode
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/2aa0a54e Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/2aa0a54e Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/2aa0a54e Branch: refs/heads/GROOVY_2_4_X Commit: 2aa0a54ed0944bd06e7b52af6a9b810c16b6d419 Parents: ba00a0a Author: John Wagenleitner <[email protected]> Authored: Sat Mar 3 20:21:16 2018 -0800 Committer: John Wagenleitner <[email protected]> Committed: Sun Mar 4 13:35:57 2018 -0800 ---------------------------------------------------------------------- .../codehaus/groovy/tools/shell/Groovysh.groovy | 26 +++++++++++++------- .../groovy/tools/shell/GroovyshTest.groovy | 11 +++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/2aa0a54e/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy index da81429..af93404 100644 --- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy +++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy @@ -196,7 +196,7 @@ class Groovysh extends Shell { setLastResult(result = interp.evaluate(buff)) } else { // Evaluate Buffer wrapped with code storing bounded vars - result = evaluateWithStoredBoundVars(current) + result = evaluateWithStoredBoundVars(importsSpec, current) } buffers.clearSelected() @@ -232,14 +232,14 @@ class Groovysh extends Shell { * to simulate an interpreter mode, this method wraps the statements into a try/finally block that * stores bound variables like unbound variables */ - private Object evaluateWithStoredBoundVars(final List<String> current) { + private Object evaluateWithStoredBoundVars(String importsSpec, final List<String> current) { Object result - String variableBlocks = '' - // To make groovysh behave more like an interpreter, we need to retrive all bound + String variableBlocks = null + // To make groovysh behave more like an interpreter, we need to retrieve all bound // vars at the end of script execution, and then update them into the groovysh Binding context. - Set<String> boundVars = ScriptVariableAnalyzer.getBoundVars(current.join(Parser.NEWLINE), interp.classLoader) - variableBlocks += "$COLLECTED_BOUND_VARS_MAP_VARNAME = new HashMap();" + Set<String> boundVars = ScriptVariableAnalyzer.getBoundVars(importsSpec + Parser.NEWLINE + current.join(Parser.NEWLINE), interp.classLoader) if (boundVars) { + variableBlocks = "$COLLECTED_BOUND_VARS_MAP_VARNAME = new HashMap();" boundVars.each({ String varname -> // bound vars can be in global or some local scope. // We discard locally scoped vars by ignoring MissingPropertyException @@ -250,12 +250,20 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname; } // Evaluate the current buffer w/imports and dummy statement - List<String> buff = imports + ['try {', 'true'] + current + ['} finally {' + variableBlocks + '}'] + List<String> buff + if (variableBlocks) { + buff = [importsSpec] + ['try {', 'true'] + current + ['} finally {' + variableBlocks + '}'] + } else { + buff = [importsSpec] + ['true'] + current + } setLastResult(result = interp.evaluate(buff)) - Map<String, Object> boundVarValues = interp.context.getVariable(COLLECTED_BOUND_VARS_MAP_VARNAME) - boundVarValues.each({ String name, Object value -> interp.context.setVariable(name, value) }) + if (variableBlocks) { + Map<String, Object> boundVarValues = interp.context.getVariable(COLLECTED_BOUND_VARS_MAP_VARNAME) + boundVarValues.each({ String name, Object value -> interp.context.setVariable(name, value) }) + } + return result } http://git-wip-us.apache.org/repos/asf/groovy/blob/2aa0a54e/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy index 7a0f6e1..7791827 100644 --- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy +++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy @@ -315,6 +315,17 @@ class GroovyshTest extends GroovyTestCase { } catch (ArithmeticException e) {} } + void testImports() { + Groovysh groovysh = new Groovysh(testio) + groovysh.execute('import java.rmi.Remote ') + assert mockOut.toString().length() > 0 + assert 'java.rmi.Remote\n' == mockOut.toString().normalize()[-('java.rmi.Remote\n'.length())..-1] + groovysh.execute('Remote r') + assert mockOut.toString().length() > 0 + // mostly assert no exception + assert 'null\n' == mockOut.toString().normalize()[-5..-1] + } + static File createTemporaryGroovyScriptFile(content) { String testName = 'GroovyshTest' + System.currentTimeMillis() File groovyCode = new File(System.getProperty('java.io.tmpdir'), testName)
