-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestJythonCli.java
More file actions
193 lines (174 loc) · 6.26 KB
/
TestJythonCli.java
File metadata and controls
193 lines (174 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/// usr/bin/env jbang "$0" "$@" ; exit $?
//SOURCES JythonCli.java
//DEPS org.tomlj:tomlj:1.1.1
//DEPS org.junit.jupiter:junit-jupiter:5.13.3
//DEPS org.junit.platform:junit-platform-console:1.13.3
import java.io.IOException;
import java.io.StringReader;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.platform.console.ConsoleLauncher;
/**
* A class to run tests on aspects of {@link JythonCli} by delegating to
* the JUnit console {@link ConsoleLauncher}.
*/
public class TestJythonCli {
static final String[] ARGS_DEBUG_FOO =
{"--cli-debug", "foo.py", "bar", "baz"};
static final String[] ARGS_FOO =
{"--version", "foo.py", "bar.py", "baz"};
static final String[] ARGS_NONE = {"--cli-debug"};
/** The {@code --cli-debug} flag is spotted */
@Test
void testCliDebugFlag() throws IOException {
JythonCli cli = new JythonCli();
cli.initEnvironment(ARGS_DEBUG_FOO);
assertTrue(cli.debug);
}
/** A Python script argument is picked up. */
@Test
void testScriptFound() throws IOException {
JythonCli cli = new JythonCli();
cli.initEnvironment(ARGS_FOO);
assertFalse(cli.debug);
assertEquals("foo.py", cli.scriptFilename);
}
/** Arguments to the Jython command are assembled in order. */
@Test
void testJythonArgs() throws IOException {
JythonCli cli = new JythonCli();
cli.initEnvironment(ARGS_FOO);
assertEquals("--version", cli.jythonArgs.get(0));
assertEquals("foo.py", cli.jythonArgs.get(1));
assertEquals("bar.py", cli.jythonArgs.get(2));
assertEquals("baz", cli.jythonArgs.get(3));
}
/** An unterminated {@code jbang} block is an error. */
@Test
@Disabled("readJBangBlock does not throw on an unterminated block")
void testUnterminated() throws IOException {
String script =
"""
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "17"
import sys
""";
JythonCli cli = new JythonCli();
assertThrows(Exception.class, () -> processScript(cli, script));
}
/**
* An unterminated block may gobble up a {@code jbang} block. This
* is not detectable by {@link JythonCli} as the text of a
* {@code jbang} header could be legitimate content.
*/
@Test
@Disabled("readJBangBlock treats '/// jbang' inside another block as valid start")
void testGobbledBlock() throws IOException {
JythonCli cli = new JythonCli();
processScript(cli,
"""
# /// script
# requires-python = ">=3.11"
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "8"
# ///
""");
assertTrue(cli.tomlText.isEmpty(), "Check TOML text is empty");
assertNull(cli.tpr, "Check TOML parse not done");
}
/**
* An unterminated {@code jbang} block should gobble up a following
* block. This ought to be detectable by {@link JythonCli}. It isn't
* actually a fault with the block delimiting: but the gobbled
* block-start is not valid TOML.
*/
@Test
@Disabled("interpretJBangBlock treats '/// script' as valid terminator")
void testCollision() throws IOException {
String script =
"""
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "8"
# /// script
# requires-python = ">=3.11"
# ///
""";
JythonCli cli = new JythonCli();
assertThrows(Exception.class, () -> processScript(cli, script));
assertFalse(cli.tomlText.isEmpty(), "Detect TOML text is empty");
assertTrue(cli.tpr.hasErrors(), "Check TOML parse reports errors");
}
/** Two {@code jbang} blocks is an error. */
@Test
@Disabled("readJBangBlock does not throw on a second jbang block")
void testTwoBlocks() throws IOException {
String script =
"""
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "8"
# ///
# Valid but not for us
# /// script
# requires-python = ">=3.11"
# ///
# /// jbang
# requires-jython = "2.7.3"
# ///
""";
JythonCli cli = new JythonCli();
assertThrows(Exception.class, () -> processScript(cli, script));
}
/** Invalid TOML is an error. */
@Test
void testInvalidTOML() throws IOException {
String script =
"""
# /// jbang
# requires-jython = "2.7.4"
# requires-java = "21"
# dependencies = [
# "io.leego:banana:2.1.0"
# ]
# runtime-options = [
# "-Dpython.console.encoding=UTF-8"
# -
# ///
print("Hello World!")
""";
JythonCli cli = new JythonCli();
assertThrows(Exception.class, () -> processScript(cli, script));
assertTrue(cli.tpr.hasErrors(), "Check TOML parse reports errors");
}
/**
* Take an initialised {@link JythonCli} and have it process (but
* not run) the given {@code String} as if the contents of a file.
*
* @param cli to exercise
* @param script to process as script
* @throws IOException on StringReader errors
*/
void processScript(JythonCli cli, String script) throws IOException {
cli.initEnvironment(ARGS_NONE);
cli.readJBangBlock(new StringReader(script));
cli.interpretJBangBlock();
}
/**
* Run the JUnit console with arguments from our command line.
*
* @param args to pass to the JUnit console
*/
public static void main(String[] args) throws IOException {
// Run the JUnit console
if (true) {
ConsoleLauncher.main(args);
} else {
// Debugging code
new TestJythonCli().testInvalidTOML();
}
}
}