From ec278f622f253b66969cc59c03f710b6c6a46dec Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Thu, 24 Oct 2024 15:42:57 -0700 Subject: [PATCH] G2-1667 CommandInvocation Path Argument Accessor --- .../command/invocation/CommandInvocation.java | 11 ++++++ .../invocation/TestCommandInvocation.java | 35 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 ax-command/src/test/java/com/g2forge/alexandria/command/invocation/TestCommandInvocation.java diff --git a/ax-command/src/main/java/com/g2forge/alexandria/command/invocation/CommandInvocation.java b/ax-command/src/main/java/com/g2forge/alexandria/command/invocation/CommandInvocation.java index 3a1ac86f..404ea350 100644 --- a/ax-command/src/main/java/com/g2forge/alexandria/command/invocation/CommandInvocation.java +++ b/ax-command/src/main/java/com/g2forge/alexandria/command/invocation/CommandInvocation.java @@ -45,4 +45,15 @@ public static final CommandInvocation 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); + } } diff --git a/ax-command/src/test/java/com/g2forge/alexandria/command/invocation/TestCommandInvocation.java b/ax-command/src/test/java/com/g2forge/alexandria/command/invocation/TestCommandInvocation.java new file mode 100644 index 00000000..0817c629 --- /dev/null +++ b/ax-command/src/test/java/com/g2forge/alexandria/command/invocation/TestCommandInvocation.java @@ -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)); + } +}