Skip to content

Commit

Permalink
FORGE-1630: Fixed selected resource
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jul 7, 2014
1 parent 7fe0af3 commit 9f406a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public void validate(UIValidationContext validator)
public Result execute(UIExecutionContext context) throws Exception
{
Result result = Results.fail("Error executing script.");
Resource<?> currentResource = (Resource<?>) context.getUIContext().getInitialSelection().get();
final UIOutput output = context.getUIContext().getProvider().getOutput();
UIContext uiContext = context.getUIContext();
Resource<?> currentResource = (Resource<?>) uiContext.getInitialSelection().get();
final UIOutput output = uiContext.getProvider().getOutput();
if (command.hasValue())
{
String[] commands = command.getValue().split(" ");
Expand Down Expand Up @@ -155,6 +156,7 @@ public void run()
}
else
{
Resource<?> selectedResource = currentResource;
ALL: for (String path : arguments.getValue())
{
List<Resource<?>> resources = new ResourcePathResolver(resourceFactory, currentResource, path).resolve();
Expand All @@ -174,11 +176,10 @@ public void run()
.outputStreamError(stderr)
.create();

Shell scriptShell = shellFactory.createShell(((FileResource<?>) context.getUIContext()
Shell scriptShell = shellFactory.createShell(((FileResource<?>) uiContext
.getInitialSelection().get()).getUnderlyingResourceObject(), settings);

scriptShell.getConsole().setPrompt(new Prompt(""));

try (BufferedReader reader = new BufferedReader(new InputStreamReader(
resource.getResourceInputStream())))
{
Expand All @@ -197,10 +198,13 @@ public void run()
result = execute(scriptShell, writer, line, timeoutValue,
TimeUnit.SECONDS, startTime);

if (result != null)
if (result instanceof Failed)
{
break ALL;
}
else
{
if (result instanceof Failed)
break ALL;
selectedResource = scriptShell.getCurrentResource();
}
}
catch (TimeoutException e)
Expand All @@ -222,6 +226,10 @@ public void run()
}
}
}
if (!(result instanceof Failed))
{
uiContext.setSelection(selectedResource);
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.shell.Shell;
import org.jboss.forge.addon.shell.mock.command.ThrowExceptionCommand;
import org.jboss.forge.addon.shell.test.ShellTest;
import org.jboss.forge.addon.ui.result.Failed;
Expand Down Expand Up @@ -198,4 +199,21 @@ public void testRunCommandLinux() throws Exception
Assert.assertThat(shellTest.getStdOut(), CoreMatchers.containsString("file.txt"));
}

@Test
public void testKeepShellContext() throws Exception
{
DirectoryResource temp = (DirectoryResource) resourceFactory.create(OperatingSystemUtils.createTempDir());
temp.deleteOnExit();
Shell shell = shellTest.getShell();
shell.setCurrentResource(temp);

FileResource<?> script = (FileResource<?>) temp.getChild("script.fsh");
script.setContents("mkdir foo\ncd foo");
Result result = shellTest.execute("run script.fsh", COMMAND_TIMEOUT, TimeUnit.SECONDS);
Assert.assertFalse(result instanceof Failed);
Resource<?> child = temp.getChild("foo");
Assert.assertTrue(child.exists());
Assert.assertEquals(child, shell.getCurrentResource());
}

}

0 comments on commit 9f406a0

Please sign in to comment.