From 31c13af7b47cd2d25c0eeabff1988adb47a56d56 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Sun, 15 Mar 2015 23:23:38 +0000 Subject: [PATCH] Let os.getenv() return System.getenv() values first for JSE, then fall back to properties --- README.html | 1 + src/core/org/luaj/vm2/lib/OsLib.java | 15 ++++++++-- src/jse/org/luaj/vm2/lib/jse/JseOsLib.java | 5 ++++ .../junit/org/luaj/vm2/lib/jse/OsLibTest.java | 29 +++++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.html b/README.html index 1ed986fe..9ba601e8 100644 --- a/README.html +++ b/README.html @@ -976,6 +976,7 @@ Files are no longer hosted at LuaForge.
  • List keyeq() and keyindex() methods as abstract on LuaTable.Entry (issue #37).
  • Fix return value for table.remove() and table.insert() (fixes issue #39)
  • Fix aliasing issue for some multiple assignments from varargs return values (fixes issue #38)
  • +
  • Let os.getenv() return System.getenv() values first for JSE, then fall back to properties (fixes issue #25)
  • diff --git a/src/core/org/luaj/vm2/lib/OsLib.java b/src/core/org/luaj/vm2/lib/OsLib.java index 2ab21878..1a1ca40a 100644 --- a/src/core/org/luaj/vm2/lib/OsLib.java +++ b/src/core/org/luaj/vm2/lib/OsLib.java @@ -404,8 +404,19 @@ public class OsLib extends TwoArgFunction { } /** - * Returns the value of the process environment variable varname, - * or null if the variable is not defined. + * Returns the value of the process environment variable varname, + * or the System property value for varname, + * or null if the variable is not defined in either environment. + * + * The default implementation, which is used by the JmePlatform, + * only queryies System.getProperty(). + * + * The JsePlatform overrides this behavior and returns the + * environment variable value using System.getenv() if it exists, + * or the System property value if it does not. + * + * A SecurityException may be thrown if access is not allowed + * for 'varname'. * @param varname * @return String value, or null if not defined */ diff --git a/src/jse/org/luaj/vm2/lib/jse/JseOsLib.java b/src/jse/org/luaj/vm2/lib/jse/JseOsLib.java index 5f62c51b..c4e54745 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseOsLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseOsLib.java @@ -84,6 +84,11 @@ public class JseOsLib extends org.luaj.vm2.lib.OsLib { public JseOsLib() { } + protected String getenv(String varname) { + String s = System.getenv(varname); + return s != null? s : System.getProperty(varname); + } + protected Varargs execute(String command) { int exitValue; try { diff --git a/test/junit/org/luaj/vm2/lib/jse/OsLibTest.java b/test/junit/org/luaj/vm2/lib/jse/OsLibTest.java index d8e19d09..6c698acc 100644 --- a/test/junit/org/luaj/vm2/lib/jse/OsLibTest.java +++ b/test/junit/org/luaj/vm2/lib/jse/OsLibTest.java @@ -1,21 +1,25 @@ package org.luaj.vm2.lib.jse; +import org.luaj.vm2.LuaValue; import org.luaj.vm2.lib.OsLib; +import org.luaj.vm2.lib.jme.JmePlatform; import junit.framework.TestCase; public class OsLibTest extends TestCase { - OsLib lib; + LuaValue jme_lib; + LuaValue jse_lib; double time; public void setUp() { - lib = new OsLib(); + jse_lib = JsePlatform.standardGlobals().get("os");; + jme_lib = JmePlatform.standardGlobals().get("os");; time = new java.util.Date(2001-1900, 7, 23, 14, 55, 02).getTime() / 1000.0; } void t(String format, String expected) { - String actual = lib.date(format, time); + String actual = jme_lib.get("date").call(LuaValue.valueOf(format), LuaValue.valueOf(time)).tojstring(); assertEquals(expected, actual); } @@ -52,4 +56,23 @@ public class OsLibTest extends TestCase { public void testStringDate_UW_pos2() { time+=2*DAY; t("%c %U %W", "Sat Aug 25 14:55:02 2001 33 34"); } public void testStringDate_UW_pos3() { time+=3*DAY; t("%c %U %W", "Sun Aug 26 14:55:02 2001 34 34"); } public void testStringDate_UW_pos4() { time+=4*DAY; t("%c %U %W", "Mon Aug 27 14:55:02 2001 34 35"); } + + public void testJseOsGetenvForEnvVariables() { + LuaValue USER = LuaValue.valueOf("USER"); + LuaValue jse_user = jse_lib.get("getenv").call(USER); + LuaValue jme_user = jme_lib.get("getenv").call(USER); + assertFalse(jse_user.isnil()); + assertTrue(jme_user.isnil()); + System.out.println("User: " + jse_user); + } + + public void testJseOsGetenvForSystemProperties() { + System.setProperty("test.key.foo", "test.value.bar"); + LuaValue key = LuaValue.valueOf("test.key.foo"); + LuaValue value = LuaValue.valueOf("test.value.bar"); + LuaValue jse_value = jse_lib.get("getenv").call(key); + LuaValue jme_value = jme_lib.get("getenv").call(key); + assertEquals(value, jse_value); + assertEquals(value, jme_value); + } }