You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/02/16 22:25:56 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-10483: GroovyMain: use default encoding if none supplied

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new d71c61f  GROOVY-10483: GroovyMain: use default encoding if none supplied
d71c61f is described below

commit d71c61f47c930ac249a1ec6f691e8d0281171fd8
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 16 15:22:50 2022 -0600

    GROOVY-10483: GroovyMain: use default encoding if none supplied
    
    2_5_X backport
---
 src/main/groovy/groovy/ui/GroovyMain.java |  2 +-
 src/test/groovy/ui/GroovyMainTest.groovy  | 20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/main/groovy/groovy/ui/GroovyMain.java b/src/main/groovy/groovy/ui/GroovyMain.java
index 070c4c7..420ebbd 100644
--- a/src/main/groovy/groovy/ui/GroovyMain.java
+++ b/src/main/groovy/groovy/ui/GroovyMain.java
@@ -239,7 +239,7 @@ public class GroovyMain {
             GroovyMain main = new GroovyMain();
 
             // add the ability to parse scripts with a specified encoding
-            main.conf.setSourceEncoding(encoding);
+            if (encoding != null) main.conf.setSourceEncoding(encoding);
 
             main.debug = debug;
             main.conf.setDebug(main.debug);
diff --git a/src/test/groovy/ui/GroovyMainTest.groovy b/src/test/groovy/ui/GroovyMainTest.groovy
index 6b8ed29..e256927 100644
--- a/src/test/groovy/ui/GroovyMainTest.groovy
+++ b/src/test/groovy/ui/GroovyMainTest.groovy
@@ -20,7 +20,8 @@ package groovy.ui
 
 import static groovy.test.GroovyAssert.isAtLeastJdk
 
-class GroovyMainTest extends GroovyTestCase {
+final class GroovyMainTest extends GroovyTestCase {
+
     private baos = new ByteArrayOutputStream()
     private ps = new PrintStream(baos)
 
@@ -139,6 +140,23 @@ assert new MyConcreteClass() != null"""
         }
     }
 
+    // GROOVY-10483
+    void testSourceEncoding() {
+        def configScript = File.createTempFile('config', '.groovy')
+        def sourceCoding = System.setProperty('groovy.source.encoding', 'US-ASCII')
+        try {
+            configScript.text = 'assert configuration.sourceEncoding == "US-ASCII"'
+            GroovyMain.main('--configscript', configScript.path, '-e', '42')
+        } finally {
+            if (sourceCoding) {
+                System.setProperty('groovy.source.encoding', sourceCoding)
+            } else {
+                System.clearProperty('groovy.source.encoding')
+            }
+            configScript.delete()
+        }
+    }
+
     void testGroovyASTDump() {
         // current xstream causes illegal access errors on JDK9+ - skip on those JDK versions, get coverage on older versions
         if (isAtLeastJdk('9.0')) return