From 1a207c81002fd775f701c26ff349c0568818d023 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Wed, 19 Oct 2016 13:40:27 -0400 Subject: [PATCH] Script line number tracking, bug fix in cmd reading Signed-off-by: Emmanuel Bigeon --- gclc/changelog.md | 8 ++++++++ .../src/main/java/fr/bigeon/gclc/GCLCConstants.java | 4 ++-- .../fr/bigeon/gclc/command/ScriptExecution.java | 12 ++++++++++++ .../test/java/fr/bigeon/gclc/GCLCConstantsTest.java | 13 +++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/gclc/changelog.md b/gclc/changelog.md index b9d87b1..7b7390b 100644 --- a/gclc/changelog.md +++ b/gclc/changelog.md @@ -1,3 +1,11 @@ +## Version 1.2.6 + +* in script command failure report improved + +### Bug fixes + +* command line reading now succeed to parse last argument if it is a string + ## Version 1.1.0 * Parsing quoted string as one argument diff --git a/gclc/src/main/java/fr/bigeon/gclc/GCLCConstants.java b/gclc/src/main/java/fr/bigeon/gclc/GCLCConstants.java index 5045b6b..5427897 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/GCLCConstants.java +++ b/gclc/src/main/java/fr/bigeon/gclc/GCLCConstants.java @@ -98,8 +98,8 @@ public class GCLCConstants { inString = startIndex == index - 1; } } - final String arg = cmd.substring(startIndex, cmd.length()); - if (!arg.isEmpty()) { + if (startIndex < cmd.length()) { + final String arg = cmd.substring(startIndex, cmd.length()); args.add(arg); } return args; diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java b/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java index 11952fc..39a66a8 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java @@ -92,7 +92,9 @@ public class ScriptExecution extends Command { for (int i = 1; i < args.length; i++) { params[i - 1] = args[i]; } + int lineNo = -1; while ((cmd = reader.readLine()) != null) { + lineNo++; if (cmd.startsWith(" ") || cmd.startsWith("\t")) { //$NON-NLS-1$ //$NON-NLS-2$ reader.close(); fReader.close(); @@ -106,7 +108,17 @@ public class ScriptExecution extends Command { String cmdLine = MessageFormat.format(cmd, params); List ps = GCLCConstants.splitCommand(cmdLine); String command = ps.remove(0); + try { application.executeSub(command, ps.toArray(new String[0])); + } catch (CommandRunException e) { + // TODO: handle exception + throw new CommandRunException( + CommandRunExceptionType.EXECUTION, + MessageFormat.format( + "The script could not complete due to command failure at line {0} ({1})", + lineNo, e.getLocalizedMessage()), + e, this); + } } } catch (CommandParsingException e) { throw new CommandRunException("Invalid command in script (" + //$NON-NLS-1$ diff --git a/gclc/src/test/java/fr/bigeon/gclc/GCLCConstantsTest.java b/gclc/src/test/java/fr/bigeon/gclc/GCLCConstantsTest.java index 01ac901..e39a72b 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/GCLCConstantsTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/GCLCConstantsTest.java @@ -129,6 +129,19 @@ public class GCLCConstantsTest { assertTrue(res.get(2).equals("some")); assertTrue(res.get(3).equals("arguments")); + try { + res = GCLCConstants + .splitCommand("aCommand with \"some arguments\""); + } catch (CommandParsingException e) { + fail("Unable to parse command ending with string argument " + //$NON-NLS-1$ + e.getLocalizedMessage()); + return; + } + assertTrue(res.size() == 3); + assertTrue(res.get(0).equals("aCommand")); + assertTrue(res.get(1).equals("with")); + assertTrue(res.get(2).equals("some arguments")); + // Wrong lines? try { res = GCLCConstants