Skip to content

Commit

Permalink
Merge pull request #387 from gdgib/G2-1667-GetArgumentAsPath
Browse files Browse the repository at this point in the history
G2-1667 CommandInvocation Path Argument Accessor
  • Loading branch information
gdgib authored Oct 24, 2024
2 parents e92c905 + ec278f6 commit 57db69d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ public static final CommandInvocation<InputStream, PrintStream> of(String... arg
protected final Path working;

protected final IEnvironment environment;

public Path getArgumentAsPath(int index) {
final String string = getArguments().get(index);
if (string == null) return null;

final Path path = Paths.get(string);
if (path.isAbsolute()) return path;
final Path working = getWorking();
if (working == null) return path;
return working.resolve(path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.g2forge.alexandria.command.invocation;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.test.HAssert;

public class TestCommandInvocation {
@Test
public void getArgumentAsPathAbsolute() {
final Path absolutePath = CommandInvocation.of().getWorking().toAbsolutePath();
HAssert.assertEquals(absolutePath, new CommandInvocation<>(null, HCollection.asList(absolutePath.toString()), null, null, null).getArgumentAsPath(0));
}

@Test
public void getArgumentAsPathNull() {
HAssert.assertNull(new CommandInvocation<>(null, HCollection.asList((String) null), null, null, null).getArgumentAsPath(0));
}

@Test
public void getArgumentAsPathRelative() {
final Path relativePath = CommandInvocation.of().getWorking();
HAssert.assertEquals(relativePath, new CommandInvocation<>(null, HCollection.asList(relativePath.toString()), null, null, null).getArgumentAsPath(0));
}

@Test
public void getArgumentAsPathWorkingRelative() {
final Path relativePath = Paths.get("relative");
final Path working = CommandInvocation.of().getWorking();
HAssert.assertEquals(working.resolve(relativePath), new CommandInvocation<>(null, HCollection.asList(relativePath.toString()), null, working, null).getArgumentAsPath(0));
}
}

0 comments on commit 57db69d

Please sign in to comment.