Skip to content

Commit e3f1af0

Browse files
committed
Snapshot OpenCLI positional arguments before capability probes
1 parent 5a464a3 commit e3f1af0

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎src/main/java/io/github/easy4j/opencli/capability/OpenCliCapabilityDiscovery.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.easy4j.opencli.OpenCliInvocationContext;
55
import io.github.easy4j.opencli.OpenCliProperties;
66
import io.github.easy4j.opencli.capability.OpenCliCapabilitySnapshot.Status;
7+
import io.github.easy4j.opencli.core.OpenCliArgSupport;
78
import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason;
89
import io.github.easy4j.opencli.core.OpenCliExecutor;
910
import io.github.easy4j.opencli.core.OpenCliResult;
@@ -113,12 +114,13 @@ public OpenCliResult invoke(OpenCliHelpScope scope, String command, String sessi
113114
OpenCliProperties local = configured.copyForLocalCliExecution();
114115
Map<String, String> effectiveOptions =
115116
OpenCliInvocationContext.mergeOptions(local.getInvocationDefaults(), context, options);
117+
List<String> positionalSnapshot = OpenCliArgSupport.merge(null, positionals);
116118
OpenCliCapabilitySnapshot snapshot = refresh(scope, local);
117119
if (snapshot.getStatus() == Status.AVAILABLE && !VERIFIED_HELP_VERSION.equals(snapshot.getObservedVersion())) {
118120
throw new UnsupportedOperationException("Discovered command execution requires the verified help version");
119121
}
120122
List<Argument> globals = verifiedAdapterGlobals(snapshot, local, effectiveOptions);
121-
List<String> argv = OpenCliCapabilityCommand.bind(snapshot, command, session, positionals, effectiveOptions, globals);
123+
List<String> argv = OpenCliCapabilityCommand.bind(snapshot, command, session, positionalSnapshot, effectiveOptions, globals);
122124
local.setExecutable(snapshot.getExecutable());
123125
try (OpenCliExecutor invocation = new OpenCliExecutor(local, executor.getProcessRuntime())) {
124126
return invocation.invokeLocalOnly(argv);

‎src/test/java/io/github/easy4j/opencli/capability/OpenCliCapabilityDiscoveryTest.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import java.nio.file.Paths;
1212
import java.util.Arrays;
1313
import java.util.Collections;
14+
import java.util.List;
1415
import java.util.Objects;
1516
import java.util.Scanner;
17+
import java.util.concurrent.CopyOnWriteArrayList;
1618
import java.util.concurrent.ExecutorService;
1719
import java.util.concurrent.Executors;
1820
import java.util.concurrent.Future;
@@ -276,6 +278,34 @@ void freshnessIsScopedAndVersionChangesClearOldQueries(@TempDir Path directory)
276278
}
277279
}
278280

281+
@Test
282+
void invocationSnapshotsPositionalsBeforeWaitingForHelp(@TempDir Path directory) throws Exception {
283+
Path entered = directory.resolve("entered");
284+
Path release = directory.resolve("release");
285+
OpenCliProperties properties = properties("structured");
286+
properties.getEnvironment().put("OPENCLI_TEST_HOLD_SCOPED_HELP", directory.toString());
287+
List<String> positionals = new CopyOnWriteArrayList<>(Collections.singletonList("original"));
288+
ExecutorService caller = Executors.newSingleThreadExecutor();
289+
290+
try (OpenCliClient client = new OpenCliClient(properties)) {
291+
Future<String> invocation = caller.submit(() -> client.invokeDiscovered(OpenCliHelpScope.SITE_NPM_SEARCH,
292+
"opencli npm search", null, positionals, null).getStdout());
293+
try {
294+
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
295+
while (!Files.exists(entered) && System.nanoTime() < deadline) {
296+
Thread.sleep(10);
297+
}
298+
assertTrue(Files.exists(entered), "discovery must reach the help probe");
299+
positionals.set(0, "updated");
300+
} finally {
301+
Files.createFile(release);
302+
}
303+
assertEquals("original", invocation.get(5, TimeUnit.SECONDS));
304+
} finally {
305+
caller.shutdownNow();
306+
}
307+
}
308+
279309
@Test
280310
void invokesBoundBrowserLeafThroughLocalExecutor() {
281311
try (OpenCliClient client = new OpenCliClient(properties("structured"))) {
@@ -329,6 +359,12 @@ public static void main(String[] args) throws Exception {
329359
System.out.print("raw available");
330360
return;
331361
}
362+
String scopedHelp = System.getenv("OPENCLI_TEST_HOLD_SCOPED_HELP");
363+
if (Objects.nonNull(scopedHelp) && args.length == 4
364+
&& Arrays.asList("npm", "search", "--").equals(Arrays.asList(args).subList(0, 3))) {
365+
System.out.print(args[3]);
366+
return;
367+
}
332368
if (args.length > 3 && Arrays.asList("--help", "-f", "json")
333369
.equals(Arrays.asList(args).subList(args.length - 3, args.length))) {
334370
if ("new-version-error".equals(mode) || "error".equals(mode)) {
@@ -347,6 +383,13 @@ public static void main(String[] args) throws Exception {
347383
case "npm search": fixture = "site-npm-search.json"; break;
348384
default: throw new IllegalArgumentException("Unapproved fixture command");
349385
}
386+
if (Objects.nonNull(scopedHelp) && "npm search".equals(path)) {
387+
Path directory = Paths.get(scopedHelp);
388+
Files.createFile(directory.resolve("entered"));
389+
while (!Files.exists(directory.resolve("release"))) {
390+
Thread.sleep(10);
391+
}
392+
}
350393
String help = readFixture("wrong-help".equals(mode) ? "root.json" : fixture);
351394
if ("unverified-override".equals(mode) && "npm search".equals(path)) {
352395
String json = help.trim();

0 commit comments

Comments
 (0)