Skip to content

Commit

Permalink
FORGE-2046: Fixed ls output when resource is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 26, 2014
1 parent 66e8d64 commit 418ffd6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@ public Result execute(UIExecutionContext context) throws Exception
{
String value = it.next();
boolean searching = (value.matches(".*(\\?|\\*)+.*"));
resourceList = new ResourcePathResolver(resourceFactory, currentResource, value).resolve();
try
{
resourceList = new ResourcePathResolver(resourceFactory, currentResource, value).resolve();
}
catch (RuntimeException re)
{
if (re.getMessage() == null || !re.getMessage().contains("no such child"))
{
throw re;
}
else
{
return Results.fail(value + ": No such file or directory");
}
}
if (!searching && !resourceList.isEmpty() && resourceList.get(0).exists())
{
resourceList = resourceList.get(0).listResources();
Expand All @@ -104,7 +118,8 @@ public Result execute(UIExecutionContext context) throws Exception

private String listMany(Iterable<Resource<?>> resources, Shell shell)
{
if (resources == null) {
if (resources == null)
{
return "";
}

Expand All @@ -117,45 +132,57 @@ private String listMany(Iterable<Resource<?>> resources, Shell shell)

for (Resource<?> resource : resources)
{
if (resource instanceof FileResource) {
if (resource instanceof FileResource)
{
fileResources.add((FileResource) resource);
} else if (resource instanceof JavaFieldResource) {
}
else if (resource instanceof JavaFieldResource)
{
fieldResources.add((JavaFieldResource) resource);
} else if (resource instanceof JavaMethodResource) {
}
else if (resource instanceof JavaMethodResource)
{
methodResources.add((JavaMethodResource) resource);
} else {
}
else
{
otherResources.add(resource);
}
}

StringBuilder sb = new StringBuilder();

if (fileResources.size() > 0) {
if (fileResources.size() > 0)
{
sb.append(getFileFormattedList(fileResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

if (fieldResources.size() > 0) {
if (fieldResources.size() > 0)
{
sb.append(Config.getLineSeparator());
sb.append(ShellUtil.colorizeLabel("[fields]"));
sb.append(Config.getLineSeparator());
sb.append(getJavaFieldFormattedList(fieldResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

if (methodResources.size() > 0) {
if (methodResources.size() > 0)
{
sb.append(Config.getLineSeparator());
sb.append(ShellUtil.colorizeLabel("[methods]"));
sb.append(Config.getLineSeparator());
sb.append(getJavaMethodFormattedList(methodResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

if (otherResources.size() > 0) {
if (otherResources.size() > 0)
{
sb.append(getFormattedList(otherResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

return sb.toString();
}

private String getFileFormattedList(List<FileResource> resources, int termHeight, int termWidth) {
private String getFileFormattedList(List<FileResource> resources, int termHeight, int termWidth)
{

boolean showAll = all.getValue();
List<TerminalString> display = new ArrayList<>();
Expand All @@ -174,7 +201,8 @@ private String getFileFormattedList(List<FileResource> resources, int termHeight
private PosixFileNameComparator posixFileNameComparator =
new PosixFileNameComparator();

@Override public int compare(TerminalString o1, TerminalString o2)
@Override
public int compare(TerminalString o1, TerminalString o2)
{
return posixFileNameComparator.compare(o1.getCharacters(), o2.getCharacters());
}
Expand All @@ -185,30 +213,36 @@ private String getFileFormattedList(List<FileResource> resources, int termHeight
return Parser.formatDisplayCompactListTerminalString(display, termWidth);
}

private String getJavaFieldFormattedList(List<JavaFieldResource> resources, int termHeight, int termWidth) {
private String getJavaFieldFormattedList(List<JavaFieldResource> resources, int termHeight, int termWidth)
{
List<String> display = new ArrayList<>();

for (JavaFieldResource resource : resources) {
for (JavaFieldResource resource : resources)
{
display.add(ShellUtil.colorizeJavaFieldResource(resource));
}

return Parser.formatDisplayList(display, termHeight, termWidth);
}

private String getJavaMethodFormattedList(List<JavaMethodResource> resources, int termHeight, int termWidth) {
private String getJavaMethodFormattedList(List<JavaMethodResource> resources, int termHeight, int termWidth)
{
List<String> display = new ArrayList<>();

for (JavaMethodResource resource : resources) {
for (JavaMethodResource resource : resources)
{
display.add(ShellUtil.colorizeJavaMethodResource(resource));
}

return Parser.formatDisplayList(display, termHeight, termWidth);
}

private String getFormattedList(List<Resource> resources, int termHeight, int termWidth) {
private String getFormattedList(List<Resource> resources, int termHeight, int termWidth)
{
List<String> display = new ArrayList<>();

for (Resource resource : resources) {
for (Resource resource : resources)
{
display.add(resource.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

package org.jboss.forge.addon.shell.command;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
Expand All @@ -17,6 +20,8 @@
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.shell.test.ShellTest;
import org.jboss.forge.addon.ui.result.Failed;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
Expand Down Expand Up @@ -76,4 +81,18 @@ public void testLsCommand() throws Exception
shellTest.execute("ls *file*", 5, TimeUnit.SECONDS);
Assert.assertThat(shellTest.getStdOut(), CoreMatchers.containsString("file.txt"));
}

@Test
public void testLsCommandFailed() throws Exception
{
Project project = projectFactory.createTempProject();
String projectPath = project.getRoot().getFullyQualifiedName();
shellTest.execute("cd " + projectPath, 5, TimeUnit.SECONDS);
shellTest.clearScreen();
Result result = shellTest.execute(
"ls foo" + File.separator + "jee-example-app-1.0.0.ear", 5,
TimeUnit.SECONDS);
Assert.assertThat(result, instanceOf(Failed.class));
Assert.assertThat(result.getMessage(), CoreMatchers.containsString("No such file or directory"));
}
}

0 comments on commit 418ffd6

Please sign in to comment.