Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -407,44 +407,6 @@ class Console {
return null
}

private def writeTraverserToErrorLines(Traverser t, List errorLines) {
// every traverser has an object so toString() that. pad with spaces to cover "side-effects" width
errorLines << "Traverser> $t"

def optGenerator = t.asAdmin().generator
if (optGenerator.isPresent()) {
def width = "Traverser".length()
def generator = optGenerator.get()
if (generator.providedRequirements.contains(TraverserRequirement.BULK)) {
errorLines << " Bulk".padRight(width) + "> " + t.bulk()
}

if (generator.providedRequirements.contains(TraverserRequirement.SACK)) {
errorLines << " Sack".padRight(width) + "> " + t.sack()
}

if (generator.providedRequirements.contains(TraverserRequirement.PATH)) {
errorLines << " Path".padRight(width) + "> " + t.path()
}

if (generator.providedRequirements.contains(TraverserRequirement.SINGLE_LOOP) ||
generator.providedRequirements.contains(TraverserRequirement.NESTED_LOOP)) {
// flatten loops/names if present
def loopNames = t.asAdmin().loopNames
def loopsLine = loopNames.isEmpty() ? t.loops() : loopNames.collect { [(it): t.loops(it)]}
errorLines << " Loops".padRight(width) + "> " + loopsLine
}

if (generator.providedRequirements.contains(TraverserRequirement.SIDE_EFFECTS)) {
// convert side-effects to a map
def sideEffects = t.asAdmin().sideEffects
def keys = sideEffects.keys()
errorLines << " S/E".padRight(width) + "> " + keys.collectEntries { [(it): sideEffects.get(it)]}
}

}
}

private static String buildResultPrompt() {
final String groovyshellProperty = System.getProperty("gremlin.prompt")
if (groovyshellProperty != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,4 @@ class PluggedIn {
void deactivate() {
this.activated = false
}

public class GroovyGremlinShellEnvironment implements GremlinShellEnvironment {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a part of the codebase I'm not too familiar with, but as I understand it this code only existed to support the old remote-console infrastructure. If so, I agree it should be removed.


@Override
def <T> T getVariable(final String variableName) {
return (T) shell.interp.context.getVariable(variableName)
}

@Override
def <T> void setVariable(final String variableName, final T variableValue) {
shell.interp.context.setVariable(variableName, variableValue)
}

@Override
void println(final String line) {
io.println(line)
}

@Override
void errPrintln(final String line) {
if (!Preferences.warnings) {
return;
}
io.err.println("[warn] " + line);
}

@Override
def <T> T execute(final String line) {
return (T) shell.execute(line)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@ package org.apache.tinkerpop.gremlin.console

import org.apache.tinkerpop.gremlin.console.jsr223.AbstractGremlinServerIntegrationTest
import org.apache.tinkerpop.gremlin.console.jsr223.RemoteGremlinPlugin
import org.apache.tinkerpop.gremlin.console.commands.GremlinSetCommand
import org.apache.tinkerpop.gremlin.structure.io.Storage
import org.apache.tinkerpop.gremlin.util.TestSupport
import org.apache.groovy.groovysh.Command
import org.apache.groovy.groovysh.commands.SetCommand
import org.codehaus.groovy.tools.shell.IO
import org.junit.Test

import java.nio.file.Files

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertSame
import static org.junit.Assert.assertTrue

class GremlinGroovyshTest extends AbstractGremlinServerIntegrationTest {
private IO testio
private ByteArrayOutputStream out
private ByteArrayOutputStream err
private Mediator mediator
private GremlinGroovysh shell
private File propertiesFile

Expand All @@ -43,7 +52,8 @@ class GremlinGroovyshTest extends AbstractGremlinServerIntegrationTest {
out = new ByteArrayOutputStream()
err = new ByteArrayOutputStream()
testio = new IO(new ByteArrayInputStream(), out, err)
shell = new GremlinGroovysh(new Mediator(null), testio)
mediator = new Mediator(null)
shell = new GremlinGroovysh(mediator, testio)

prepareConfigFiles()
}
Expand Down Expand Up @@ -73,6 +83,76 @@ class GremlinGroovyshTest extends AbstractGremlinServerIntegrationTest {
assertTrue(out.toString().endsWith("6" + System.lineSeparator()))
}

@Test
void shouldParseLineIntoWhitespaceDelimitedTokens() {
assertEquals(["g.V().count()"], shell.parseLine("g.V().count()"))
assertEquals([":set", "a", "b"], shell.parseLine(" :set a b "))
assertTrue(shell.parseLine(" ").isEmpty())
}

@Test
void shouldReturnNullCommandForEmptyOrBlankLine() {
assertNull(shell.findCommand(""))
assertNull(shell.findCommand(" "))
}

@Test
void shouldReturnNullCommandForNonCommandLine() {
assertNull(shell.findCommand("g.V().count()", []))
}

@Test
void shouldFindCommandWithoutPopulatingArgsForSingleToken() {
final List<String> parsed = []
final Command cmd = shell.findCommand(":help", parsed)
assertNotNull(cmd)
assertTrue(parsed.isEmpty())
}

@Test
void shouldPopulateParsedArgsForNonSetCommand() {
// the standard :set command (still registered on a bare shell) drives the non-GremlinSetCommand
// branch which simply appends the remaining tokens
final List<String> parsed = []
final Command cmd = shell.findCommand(":set foo bar", parsed)
assertNotNull(cmd)
assertEquals(["foo", "bar"], parsed)
}

@Test
void shouldParseQuotedArgsForGremlinSetCommand() {
// replace the standard SetCommand with the Gremlin variant, as the real Console does, so that the
// GremlinSetCommand branch (which honours quoted arguments) is exercised
shell.getRegistry().commands().findAll { it instanceof SetCommand }.each { shell.getRegistry().remove(it) }
shell.register(new GremlinSetCommand(shell))

final List<String> parsed = []
final Command cmd = shell.findCommand(":set result.prompt \"==> \"", parsed)
assertTrue(cmd instanceof GremlinSetCommand)
// the quoted value is preserved as a single argument rather than being split on whitespace
assertEquals("result.prompt", parsed[0])
assertEquals("==> ", parsed[1])
}

@Test
void shouldEvaluateLocalExpressionAndResetEvaluatingFlag() {
out.reset()
final def result = shell.execute("1 + 1")
assertEquals(2, result)
// the evaluating flag is toggled during execution and reset in the finally block
assertFalse(mediator.evaluating.get())
}

@Test
void shouldResetEvaluatingFlagAfterFailedExecution() {
try {
shell.execute("this is not valid groovy ^^^")
} catch (Throwable ignored) {
// a compilation/evaluation failure is expected here
}
assertFalse(mediator.evaluating.get())
}

@Override
void tearDown() {
super.tearDown()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.tinkerpop.gremlin.console

import org.junit.Test

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertTrue

/**
* Unit tests for {@link Mediator} that avoid a live Gremlin server or a fully constructed {@link Console}.
*/
class MediatorTest {

@Test
void shouldConstructWithNullConsole() {
final Mediator mediator = new Mediator(null)
assertNotNull(mediator)
assertTrue(mediator.availablePlugins.isEmpty())
assertFalse(mediator.evaluating.get())
}

@Test
void shouldReturnEmptyActivePluginsWhenNoneAvailable() {
final Mediator mediator = new Mediator(null)
assertTrue(mediator.activePlugins().isEmpty())
}

@Test
void shouldReadEmptyPluginStateWhenConfigMissing() {
// with no plugin-config file present, readPluginState() returns an empty list (not null)
new File(ConsoleFs.PLUGIN_CONFIG_FILE).delete()
assertEquals([], Mediator.readPluginState())
}

@Test
void shouldNotWritePluginStateWhenConfigNotWritable() {
// File.canWrite() is false when the config file does not exist, so writePluginState() reports false
// and writes nothing
new File(ConsoleFs.PLUGIN_CONFIG_FILE).delete()
final Mediator mediator = new Mediator(null)
assertFalse(mediator.writePluginState())
}
}
Loading
Loading