You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2015/11/01 14:27:13 UTC

incubator-groovy git commit: Groovysh: refactor: Tests robust against race conditions on Preferences (closes #172)

Repository: incubator-groovy
Updated Branches:
  refs/heads/master a28c72afe -> 6ab4628b6


Groovysh: refactor: Tests robust against race conditions on Preferences (closes #172)


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/6ab4628b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/6ab4628b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/6ab4628b

Branch: refs/heads/master
Commit: 6ab4628b61083f29d46a9686bd0fa8f53907c79e
Parents: a28c72a
Author: Thibault Kruse <th...@gmx.de>
Authored: Sun Nov 1 12:57:22 2015 +0100
Committer: pascalschumacher <pa...@gmx.net>
Committed: Sun Nov 1 14:25:54 2015 +0100

----------------------------------------------------------------------
 .../codehaus/groovy/tools/shell/Groovysh.groovy | 16 +++++++---
 .../groovy/tools/shell/GroovyshTest.groovy      | 33 +++++++++++---------
 2 files changed, 31 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6ab4628b/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 316b4a3..13cf022 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
@@ -62,6 +62,9 @@ class Groovysh extends Shell {
     public static final String INTERPRETER_MODE_PREFERENCE_KEY = 'interpreterMode'
     public static final String AUTOINDENT_PREFERENCE_KEY = 'autoindent'
     public static final String COLORS_PREFERENCE_KEY = 'colors'
+    public static final String SANITIZE_PREFERENCE_KEY = 'sanitizeStackTrace'
+    public static final String SHOW_LAST_RESULT_PREFERENCE_KEY = 'showLastResult'
+
     // after how many prefix characters we start displaying all metaclass methods
     public static final String METACLASS_COMPLETION_PREFIX_LENGTH_PREFERENCE_KEY = 'meta-completion-prefix-length'
 
@@ -186,7 +189,7 @@ class Groovysh extends Shell {
                     displayBuffer(current)
                 }
 
-                if (!Boolean.valueOf(Preferences.get(INTERPRETER_MODE_PREFERENCE_KEY, 'false')) || isTypeOrMethodDeclaration(current)) {
+                if (!Boolean.valueOf(getPreference(INTERPRETER_MODE_PREFERENCE_KEY, 'false')) || isTypeOrMethodDeclaration(current)) {
                     // Evaluate the current buffer w/imports and dummy statement
                     List buff = [importsSpec] + [ 'true' ] + current
                     setLastResult(result = interp.evaluate(buff))
@@ -424,7 +427,7 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname;
         RecordCommand record = registry[RecordCommand.COMMAND_NAME]
 
         if (record != null) {
-            if (Preferences.sanitizeStackTrace) {
+            if (getPreference(SANITIZE_PREFERENCE_KEY, 'false')) {
                 cause = StackTraceUtils.deepSanitize(cause)
             }
             record.recordError(cause)
@@ -436,7 +439,7 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname;
     //
 
     final Closure defaultResultHook = {Object result ->
-        boolean showLastResult = !io.quiet && (io.verbose || Preferences.showLastResult)
+        boolean showLastResult = !io.quiet && (io.verbose || getPreference(SHOW_LAST_RESULT_PREFERENCE_KEY, 'false'))
         if (showLastResult) {
             // avoid String.valueOf here because it bypasses pretty-printing of Collections,
             // e.g. String.valueOf( ['a': 42] ) != ['a': 42].toString()
@@ -490,7 +493,7 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname;
                 log.debug(cause)
             }
             else {
-                boolean sanitize = Preferences.sanitizeStackTrace
+                boolean sanitize = getPreference(SANITIZE_PREFERENCE_KEY, 'false')
 
                 // Sanitize the stack trace unless we are in verbose mode, or the user has request otherwise
                 if (!io.verbose && sanitize) {
@@ -532,6 +535,11 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname;
         }
     }
 
+    // protected for mocking in tests
+    protected String getPreference(final String key, final String theDefault) {
+        return Preferences.get(key, theDefault)
+    }
+
     Closure errorHook = defaultErrorHook
 
     private void displayError(final Throwable cause) {

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6ab4628b/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 a8bd4de..9da59a0 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
@@ -300,20 +300,17 @@ class GroovyshTest extends GroovyTestCase {
  */
 class GroovyshInterpreterModeTest extends GroovyshTest {
 
-    @Override
-    void setUp() {
-        super.setUp()
-        Preferences.put(Groovysh.INTERPRETER_MODE_PREFERENCE_KEY, 'true')
-    }
-
-    @Override
-    void tearDown() {
-        super.tearDown()
-        Preferences.put(Groovysh.INTERPRETER_MODE_PREFERENCE_KEY, 'false')
-    }
-
     void testBoundVar() {
-        Groovysh groovysh = new Groovysh(testio)
+        Groovysh groovysh = new Groovysh(testio) {
+            @Override
+            protected String getPreference(String key, String theDefault) {
+                if (key == INTERPRETER_MODE_PREFERENCE_KEY) {
+                    return 'true'
+                }
+                return super.getPreference(key, theDefault)
+            }
+        }
+
         groovysh.execute('int x = 3')
         assert mockOut.toString().length() > 0
         assert ' 3\n' == mockOut.toString().normalize()[-3..-1]
@@ -323,7 +320,15 @@ class GroovyshInterpreterModeTest extends GroovyshTest {
     }
 
     void testBoundVarmultiple() {
-        Groovysh groovysh = new Groovysh(testio)
+        Groovysh groovysh = new Groovysh(testio) {
+            @Override
+            protected String getPreference(String key, String theDefault) {
+                if (key == INTERPRETER_MODE_PREFERENCE_KEY) {
+                    return 'true'
+                }
+                return super.getPreference(key, theDefault)
+            }
+        }
         groovysh.execute('int x, y, z')
         assert mockOut.toString().length() > 0
         assert ' 0\n' == mockOut.toString().normalize()[-3..-1]