You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2016/03/21 17:56:19 UTC

svn commit: r1736025 - in /felix/trunk/gogo: jline/src/test/ jline/src/test/java/ jline/src/test/java/org/ jline/src/test/java/org/apache/ jline/src/test/java/org/apache/felix/ jline/src/test/java/org/apache/felix/gogo/ jline/src/test/java/org/apache/f...

Author: gnodet
Date: Mon Mar 21 16:56:19 2016
New Revision: 1736025

URL: http://svn.apache.org/viewvc?rev=1736025&view=rev
Log:
Make sure we capture the output of evaluation if there’s no explicit result

Added:
    felix/trunk/gogo/jline/src/test/
    felix/trunk/gogo/jline/src/test/java/
    felix/trunk/gogo/jline/src/test/java/org/
    felix/trunk/gogo/jline/src/test/java/org/apache/
    felix/trunk/gogo/jline/src/test/java/org/apache/felix/
    felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/
    felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/
    felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/AbstractParserTest.java
    felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/Context.java
    felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/ShellTest.java
Modified:
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java

Added: felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/AbstractParserTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/AbstractParserTest.java?rev=1736025&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/AbstractParserTest.java (added)
+++ felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/AbstractParserTest.java Mon Mar 21 16:56:19 2016
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.gogo.jline;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+import junit.framework.TestCase;
+import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
+
+public abstract class AbstractParserTest extends TestCase {
+
+    private ThreadIOImpl threadIO;
+    private InputStream sin;
+    private PrintStream sout;
+    private PrintStream serr;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        sin = new NoCloseInputStream(System.in);
+        sout = new NoClosePrintStream(System.out);
+        serr = new NoClosePrintStream(System.err);
+        threadIO = new ThreadIOImpl();
+        threadIO.start();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        threadIO.stop();
+        super.tearDown();
+    }
+
+    public class Context extends org.apache.felix.gogo.jline.Context {
+        public Context() {
+            super(AbstractParserTest.this.threadIO, sin, sout, serr);
+        }
+    }
+
+    private static class NoCloseInputStream extends FilterInputStream {
+        public NoCloseInputStream(InputStream in) {
+            super(in);
+        }
+        @Override
+        public void close() throws IOException {
+        }
+    }
+
+    private static class NoClosePrintStream extends PrintStream {
+        public NoClosePrintStream(OutputStream out) {
+            super(out);
+        }
+        @Override
+        public void close() {
+        }
+    }
+
+}

Added: felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/Context.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/Context.java?rev=1736025&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/Context.java (added)
+++ felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/Context.java Mon Mar 21 16:56:19 2016
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.gogo.jline;
+
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.nio.file.Path;
+
+import org.apache.felix.gogo.runtime.CommandProcessorImpl;
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.threadio.ThreadIO;
+
+public class Context extends CommandProcessorImpl
+{
+    public static final String EMPTY = "";
+    
+    private final CommandSession session;
+
+    public Context(ThreadIO threadio, InputStream in, PrintStream out, PrintStream err)
+    {
+        super(threadio);
+        Shell shell = new Shell(new MyContext(), this);
+        addCommand("gogo", this, "addCommand");
+        addCommand("gogo", this, "removeCommand");
+        addCommand("gogo", this, "eval");
+        register(this, new Builtin(), Builtin.functions);
+        register(this, new Procedural(), Procedural.functions);
+        register(this, new Posix(this), Posix.functions);
+        register(this, shell, Shell.functions);
+        session = createSession(in, out, err);
+    }
+
+    static void register(CommandProcessorImpl processor, Object target, String[] functions) {
+        for (String function : functions) {
+            processor.addCommand("gogo", target, function);
+        }
+    }
+
+    private static class MyContext implements Shell.Context {
+
+        public String getProperty(String name) {
+            return System.getProperty(name);
+        }
+
+        public void exit() throws Exception {
+            System.exit(0);
+        }
+    }
+
+    public Object execute(CharSequence source) throws Exception
+    {
+        Object result = new Exception();
+        try
+        {
+            return result = session.execute(source);
+        }
+        finally
+        {
+            System.err.println("execute<" + source + "> = ("
+                + (null == result ? "Null" : result.getClass().getSimpleName()) + ")("
+                + result + ")\n");
+        }
+    }
+
+    public void addCommand(String function, Object target)
+    {
+        addCommand("test", target, function);
+    }
+
+    public Object set(String name, Object value)
+    {
+        return session.put(name, value);
+    }
+
+    public Object get(String name)
+    {
+        return session.get(name);
+    }
+
+    public void currentDir(Path path) {
+        session.currentDir(path);
+    }
+
+    public Path currentDir() {
+        return session.currentDir();
+    }
+}

Added: felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/ShellTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/ShellTest.java?rev=1736025&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/ShellTest.java (added)
+++ felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/ShellTest.java Mon Mar 21 16:56:19 2016
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.gogo.jline;
+
+import org.junit.Test;
+
+public class ShellTest extends AbstractParserTest {
+
+
+    @Test
+    public void test() throws Exception {
+        Context context = new Context();
+        context.execute("a = \"foo\"");
+        assertEquals("foo", context.get("a"));
+        context.execute("a = $(echo bar)");
+        assertEquals("bar", context.get("a"));
+    }
+
+}

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java?rev=1736025&r1=1736024&r2=1736025&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java Mon Mar 21 16:56:19 2016
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.gogo.runtime;
 
+import java.io.ByteArrayOutputStream;
 import java.io.EOFException;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
@@ -144,11 +145,16 @@ public class Closure implements Function
     // implements Function interface
     public Object execute(CommandSession x, List<Object> values) throws Exception
     {
+        return execute(x, values, null);
+    }
+
+    public Object execute(CommandSession x, List<Object> values, Channel capturingOutput) throws Exception
+    {
         try
         {
             location.remove();
             session.put(LOCATION, null);
-            return execute(values);
+            return execute(values, capturingOutput);
         }
         catch (Exception e)
         {
@@ -157,7 +163,7 @@ public class Closure implements Function
     }
 
     @SuppressWarnings("unchecked")
-    private Object execute(List<Object> values) throws Exception
+    private Object execute(List<Object> values, Channel capturingOutput) throws Exception
     {
         if (null != values)
         {
@@ -213,6 +219,9 @@ public class Closure implements Function
                 streams = new Channel[10];
                 System.arraycopy(session.channels, 0, streams, 0, 3);
             }
+            if (capturingOutput != null) {
+                streams[1] = capturingOutput;
+            }
 
             List<Pipe> pipes = new ArrayList<>();
             if (executable instanceof Pipeline) {
@@ -317,8 +326,19 @@ public class Closure implements Function
         }
         else if (t instanceof Sequence)
         {
-            return new Closure(session, this, ((Sequence) t).program())
-                    .execute(session, parms);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            Channel out = Channels.newChannel(baos);
+            Object result = new Closure(session, this, ((Sequence) t).program())
+                    .execute(session, parms, out);
+            if (result != null) {
+                return result;
+            } else {
+                String s = baos.toString();
+                while (s.charAt(s.length() - 1) == '\n') {
+                    s = s.substring(0, s.length() - 1);
+                }
+                return s;
+            }
         }
         else if (t instanceof Array)
         {
@@ -342,7 +362,7 @@ public class Closure implements Function
         }
         else if (executable instanceof Sequence)
         {
-            return new Closure(session, this, ((Sequence) executable).program()).execute(new ArrayList<>());
+            return new Closure(session, this, ((Sequence) executable).program()).execute(new ArrayList<>(), null);
         }
         else
         {