Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/cli-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Test with JBang
run: |
export PATH="$HOME/.jbang/bin:$PATH"
jbang TestJythonCli.java execute --disable-ansi-colors --select-class=TestJythonCli
jbang src/TestJythonCli.java execute --disable-ansi-colors --select-class=TestJythonCli
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ On Linux or MacOS the JythonCli.java script can be run directly for testing purp

## Java Source File Formatting

Use the `google-java-format` to format the `JythonCli.java` and `TestJythonCli.java` programs.
Use command `jython-java-fmt` to format the `JythonCli.java` and `TestJythonCli.java` source files.

```
jbang run com.google.googlejavaformat:google-java-format:1.29.0 --aosp -r *.java
jbang run jython-java-fmt
```

## Articles about Jython and JBang
Expand Down
17 changes: 16 additions & 1 deletion jbang-catalog.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
{
"aliases": {
"jython-cli": {
"script-ref": "JythonCli.java",
"script-ref": "src/JythonCli.java",
"description": "Run Jython 2.7"
},
"jython-java-fmt": {
"script-ref": "jbang-fmt@jbangdev/jbang-fmt",
"description": "Formats Java Source Code in src folder",
"arguments": [
"--line-length",
"90",
"--java",
"8",
"--indent-with",
"space",
"--indent-size",
"4",
"src"
]
}
}
}
13 changes: 7 additions & 6 deletions JythonCli.java → src/JythonCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void readJBangBlock(Reader script) throws IOException {
boolean found = false;
printIfDebug("");
printIfDebug("TOML data in Jython script:");
while ((line = lines.readLine())!=null) {
while ((line = lines.readLine()) != null) {
int lineno = lines.getLineNumber();
if (found && !line.startsWith("# ")) {
found = false;
Expand Down Expand Up @@ -150,19 +150,20 @@ void interpretJBangBlock() throws IOException {
int lineno = 0;
printIfDebug("");
printIfDebug("TOML data extracted from Jython script:");
for (String line: tomlText.toString().split("\\n", -1)) {
for (String line : tomlText.toString().split("\\n", -1)) {
lineno += 1;
printIfDebug(lineno, line);
}
tpr = Toml.parse(tomlText.toString());
if (tpr.hasErrors()) {
for (TomlParseError err: tpr.errors()) {
for (TomlParseError err : tpr.errors()) {
System.err.println(err.toString());
}
if (debug) {
throw new IOException("Error interpreting JBang TOML data.");
} else {
throw new IOException("Error interpreting JBang TOML data. Re-run with '--cli-debug' for details.");
throw new IOException(
"Error interpreting JBang TOML data. Re-run with '--cli-debug' for details.");
}
}
}
Expand Down Expand Up @@ -282,8 +283,8 @@ public static void main(String[] args) {
// Normally we have a script file (but it's optional)
if (jythonCli.scriptFilename != null) {
Reader script = new BufferedReader(
new InputStreamReader(
new FileInputStream(jythonCli.scriptFilename)));
new InputStreamReader(
new FileInputStream(jythonCli.scriptFilename)));
jythonCli.readJBangBlock(script);
jythonCli.interpretJBangBlock();
}
Expand Down
28 changes: 11 additions & 17 deletions TestJythonCli.java → src/TestJythonCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
*/
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"};
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
Expand Down Expand Up @@ -59,8 +57,7 @@ void testJythonArgs() throws IOException {
@Test
@Disabled("readJBangBlock does not throw on an unterminated block")
void testUnterminated() throws IOException {
String script =
"""
String script = """
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "17"
Expand All @@ -78,16 +75,16 @@ void testUnterminated() throws IOException {
@Test
@Disabled("readJBangBlock treats '/// jbang' inside another block as valid start")
void testGobbledBlock() throws IOException {
JythonCli cli = new JythonCli();
processScript(cli,
"""
String script = """
# /// script
# requires-python = ">=3.11"
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "8"
# ///
""");
""";
JythonCli cli = new JythonCli();
processScript(cli, script);
assertTrue(cli.tomlText.isEmpty(), "Check TOML text is empty");
assertNull(cli.tpr, "Check TOML parse not done");
}
Expand All @@ -101,8 +98,7 @@ void testGobbledBlock() throws IOException {
@Test
@Disabled("interpretJBangBlock treats '/// script' as valid terminator")
void testCollision() throws IOException {
String script =
"""
String script = """
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "8"
Expand All @@ -120,8 +116,7 @@ void testCollision() throws IOException {
@Test
@Disabled("readJBangBlock does not throw on a second jbang block")
void testTwoBlocks() throws IOException {
String script =
"""
String script = """
# /// jbang
# requires-jython = "2.7.2"
# requires-java = "8"
Expand All @@ -143,8 +138,7 @@ void testTwoBlocks() throws IOException {
/** Invalid TOML is an error. */
@Test
void testInvalidTOML() throws IOException {
String script =
"""
String script = """
# /// jbang
# requires-jython = "2.7.4"
# requires-java = "21"
Expand Down