Skip to content

Commit

Permalink
Added ls --all/-a with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 26, 2013
1 parent 489f32c commit 6237936
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Expand Up @@ -19,6 +19,7 @@
import org.jboss.forge.addon.shell.util.ShellUtil;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.result.Result;
Expand All @@ -38,6 +39,10 @@ public class LsCommand extends AbstractShellCommand
@WithAttributes(label = "Arguments", type = InputType.DIRECTORY_PICKER)
private UIInputMany<String> arguments;

@Inject
@WithAttributes(label = "all", shortName = 'a', description = "do not ignore entries starting with .", type = InputType.CHECKBOX, defaultValue = "false")
private UIInput<Boolean> all;

@Override
public Metadata getMetadata()
{
Expand All @@ -47,7 +52,7 @@ public Metadata getMetadata()
@Override
public void initializeUI(UIBuilder builder) throws Exception
{
builder.add(arguments);
builder.add(arguments).add(all);
}

@Override
Expand Down Expand Up @@ -77,14 +82,20 @@ private String listMany(Iterable<Resource<?>> files, ShellContext context)
{
TerminalSize terminalSize = context.getProvider().getConsole().getTerminalSize();
List<String> display = new ArrayList<String>();
boolean showAll = all.getValue();
if (files != null)
{
for (Resource<?> file : files)
{
String name;
if (file instanceof FileResource)
{
name = ShellUtil.colorizeResource((FileResource<?>) file);
FileResource<?> fileResource = (FileResource<?>) file;
if (!showAll && fileResource.getName().startsWith("."))
{
continue;
}
name = ShellUtil.colorizeResource(fileResource);
}
else
{
Expand Down
Expand Up @@ -7,13 +7,15 @@

package org.jboss.forge.addon.shell;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;

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

import javax.inject.Inject;

import org.hamcrest.CoreMatchers;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.resource.DirectoryResource;
Expand Down Expand Up @@ -158,7 +160,42 @@ public void testListDirCommand()
shell.setCurrentResource(tempResource);
Result lsResult = shellTest.execute("ls");
Assert.assertNotNull(lsResult);
Assert.assertThat(lsResult.getMessage(), CoreMatchers.containsString("child"));
Assert.assertThat(lsResult.getMessage(), containsString("child"));
childDirectory.delete();
tempDir.delete();
}

@Test
public void testListAllDirCommand()
{
File tempDir = OperatingSystemUtils.createTempDir();
DirectoryResource tempResource = resourceFactory.create(tempDir).reify(DirectoryResource.class);
DirectoryResource childDirectory = tempResource.getChildDirectory("child");
childDirectory.mkdir();
FileResource<?> afile = tempResource.getChild(".afile").reify(FileResource.class);
afile.createNewFile();
afile.deleteOnExit();
childDirectory.deleteOnExit();
tempDir.deleteOnExit();

Shell shell = shellTest.getShell();
shell.setCurrentResource(tempResource);

Result lsResult = shellTest.execute("ls");
Assert.assertNotNull(lsResult);
Assert.assertThat(lsResult.getMessage(), not(containsString(".afile")));

lsResult = shellTest.execute("ls -a");
Assert.assertNotNull(lsResult);
Assert.assertThat(lsResult.getMessage(), containsString("child"));
Assert.assertThat(lsResult.getMessage(), containsString(".afile"));

lsResult = shellTest.execute("ls --all");
Assert.assertNotNull(lsResult);
Assert.assertThat(lsResult.getMessage(), containsString("child"));
Assert.assertThat(lsResult.getMessage(), containsString(".afile"));

afile.delete();
childDirectory.delete();
tempDir.delete();
}
Expand Down

0 comments on commit 6237936

Please sign in to comment.