Skip to content

Commit 49b5773

Browse files
committed
groovysh additional tests
1 parent f5bcbec commit 49b5773

20 files changed

Lines changed: 924 additions & 166 deletions

subprojects/groovy-groovysh/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ dependencies {
3434
implementation projects.groovyJson
3535
implementation projects.groovyNio
3636
testImplementation projects.groovyTest
37+
testImplementation projects.groovyCsv // for /slurp .csv default coverage
38+
testImplementation projects.groovyTestJunit6 // for @ForkedJvm in CSV fallback tests
3739
implementation "net.java.dev.jna:jna:${versions.jna}"
3840
implementation "org.jline:jansi:${versions.jline}"
3941
implementation "org.jline:jline-builtins:${versions.jline}"
@@ -51,6 +53,16 @@ plugins.withId('eclipse') {
5153
}
5254
}
5355

56+
// Forward -Djunit.network from the Gradle invocation to the test JVM so that
57+
// @EnabledIfSystemProperty in network-gated tests (e.g. SlurpCsvFallbackTest)
58+
// can see it.
59+
tasks.named('test') {
60+
def network = System.getProperty('junit.network')
61+
if (network) {
62+
systemProperty 'junit.network', network
63+
}
64+
}
65+
5466
tasks.named('rat') {
5567
excludes << '**/jline/GroovyEngine.java' // BSD license as per NOTICE/LICENSE files
5668
excludes << '**/jline/ObjectInspector.groovy' // BSD license as per NOTICE/LICENSE files

subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Main.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class Main {
393393
}
394394
name('groovysh')
395395
}.build()
396-
if (terminal.width == 0 || terminal.height == 0) {
396+
if (terminal.columns == 0 || terminal.rows == 0) {
397397
terminal.size = new Size(120, 40) // hard-coded terminal size when redirecting
398398
}
399399
Thread executeThread = Thread.currentThread()
@@ -502,7 +502,7 @@ class Main {
502502
println render(messages['startup_banner.1'])
503503
println render(messages['startup_banner.2'])
504504
}
505-
println '-' * (terminal.width - 1)
505+
println '-' * (terminal.columns - 1)
506506
// for debugging
507507
// def index = 0
508508
// def lines = ['/slurp /Users/paulk/Projects/groovy/subprojects/groovy-json/src/test/resources/groovy9802.json',
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.groovy.groovysh.commands
20+
21+
import org.junit.jupiter.api.Test
22+
23+
/**
24+
* Tests for the {@code /classloader} command.
25+
*
26+
* The command renders the engine's class loader using the {@code COLUMNS}
27+
* option of {@code Printer.println(options, object)}; the support class's
28+
* {@code DummyPrinter} extracts each column as a {@code name=value} entry
29+
* so substring assertions remain straightforward.
30+
*/
31+
class ClassLoaderTest extends SystemTestSupport {
32+
33+
@Test
34+
void viewExposesColumnDataForLoadedClassLoader() {
35+
// Define a type so the class loader has at least one entry to render.
36+
engine.execute('class ClassLoaderProbe {}')
37+
system.execute('/classloader')
38+
def out = printer.output.join()
39+
// Each column name from the /classloader command appears as a
40+
// `name=...` entry. Don't assert on values' exact shape (lists vary
41+
// by JDK and previous test state); just confirm the columns rendered.
42+
assert out.contains('loadedClasses=')
43+
assert out.contains('definedPackages=')
44+
assert out.contains('classPath=')
45+
}
46+
}

subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/commands/ConsoleTestSupport.groovy

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.apache.groovy.groovysh.Main
2222
import org.apache.groovy.groovysh.jline.GroovyCommands
2323
import org.apache.groovy.groovysh.jline.GroovyConsoleEngine
2424
import org.apache.groovy.groovysh.jline.GroovyEngine
25+
import org.jline.console.Printer
2526
import org.jline.builtins.ClasspathResourceUtil
2627
import org.jline.builtins.ConfigurationPath
2728
import org.jline.builtins.SyntaxHighlighter
@@ -34,6 +35,7 @@ import org.jline.reader.impl.DefaultParser
3435
import org.junit.jupiter.api.BeforeEach
3536

3637
import java.nio.file.Path
38+
import java.util.function.Supplier
3739

3840
/**
3941
* Support for testing {@link ConsoleEngine} instances.
@@ -44,9 +46,10 @@ abstract class ConsoleTestSupport {
4446
private Path root = ClasspathResourceUtil.getResourcePath(rootURL)
4547
private Path temp = File.createTempDir().toPath()
4648
protected ConfigurationPath configPath = new ConfigurationPath(root, temp)
49+
protected Supplier<Path> workDir = { -> temp } as Supplier<Path>
4750
protected DummyPrinter printer = new DummyPrinter(configPath)
4851
private highlighter = SyntaxHighlighter.build(root, "DUMMY")
49-
protected CommandRegistry groovy = new GroovyCommands(engine, null, printer, highlighter)
52+
protected CommandRegistry groovy = new GroovyCommands(engine, workDir, printer, highlighter)
5053
protected ConsoleEngine console
5154
protected CommandRegistry.CommandSession session = new CommandRegistry.CommandSession()
5255
protected LineReader reader
@@ -70,7 +73,19 @@ abstract class ConsoleTestSupport {
7073

7174
@Override
7275
void println(Map<String, Object> options, Object object) {
73-
output << object.toString()
76+
// /classloader --view passes the EngineClassLoader as `object`
77+
// with COLUMNS option naming the fields to render. DefaultPrinter
78+
// would format those as a table; in tests we capture each
79+
// column as `name=value` so substring assertions still work.
80+
if (object instanceof GroovyEngine.EngineClassLoader) {
81+
options?[Printer.COLUMNS]?.each { col ->
82+
output << "$col=" + object."$col"
83+
}
84+
} else {
85+
// .toString() (not String.valueOf) preserves Groovy MetaClass
86+
// extensions — Map renders as [k:v] not {k=v}.
87+
output << object.toString()
88+
}
7489
}
7590

7691
@Override
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.groovy.groovysh.commands
20+
21+
import org.junit.jupiter.api.Test
22+
23+
/**
24+
* Tests for the {@code /del} command. Uses the full {@link SystemTestSupport}
25+
* stack so each invocation passes through registry parsing, pipe handling,
26+
* and console dispatch — exercising the same path as a real REPL session.
27+
*/
28+
class DelTest extends SystemTestSupport {
29+
30+
@Test
31+
void testDelVariable() {
32+
console.execute('dummyName', "x = 42")
33+
assert console.hasVariable('x')
34+
system.execute('/del x')
35+
assert !console.hasVariable('x')
36+
}
37+
38+
@Test
39+
void testDelMultipleVariables() {
40+
console.execute('dummyName', "a = 1; b = 2; c = 3")
41+
['a', 'b', 'c'].each { assert console.hasVariable(it) }
42+
system.execute('/del a c')
43+
assert !console.hasVariable('a')
44+
assert console.hasVariable('b')
45+
assert !console.hasVariable('c')
46+
}
47+
48+
@Test
49+
void testDelNonexistentIsHarmless() {
50+
// /del on an unknown variable should be a no-op, not a hard failure
51+
// that aborts the REPL session.
52+
system.execute('/del thisVariableDoesNotExist')
53+
// No exception — pre-existing variables (none) are unaffected.
54+
assert !console.hasVariable('thisVariableDoesNotExist')
55+
}
56+
}

subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/commands/GrabCommandTest.groovy

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.groovy.groovysh.commands
20+
21+
import groovy.junit6.plugin.ForkedJvm
22+
import org.junit.jupiter.api.Test
23+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
24+
25+
/**
26+
* Tests for the {@code /grab} command — Maven-coordinate dependency
27+
* resolution via Grape. The actual artifact-fetching test is forked and
28+
* network-gated; the no-arg test runs always to lock in the documented
29+
* "no args is a no-op" behaviour that {@code GroovyCommands.grab} relies
30+
* on for {@code grab(input)} when no xargs are supplied.
31+
*/
32+
class GrabTest extends SystemTestSupport {
33+
34+
@Test
35+
void grabWithNoArgsIsNoOp() {
36+
// grab() returns null when input.xargs() is empty; this should
37+
// succeed silently rather than throw.
38+
system.execute('/grab')
39+
}
40+
41+
@Test
42+
@ForkedJvm
43+
@EnabledIfSystemProperty(named = 'junit.network', matches = 'true')
44+
void grabFetchesArtifactAndMakesItLoadable() {
45+
// commons-lang3 is small, stable, and uses well-known coordinates.
46+
// After the grab, the artifact's classes should resolve through
47+
// the engine's classloader.
48+
system.execute('/grab org.apache.commons:commons-lang3:3.14.0')
49+
def cls = engine.execute("Class.forName('org.apache.commons.lang3.StringUtils')")
50+
assert cls != null
51+
assert cls.name == 'org.apache.commons.lang3.StringUtils'
52+
}
53+
}

0 commit comments

Comments
 (0)