mocobeta commented on a change in pull request #550:
URL: https://github.com/apache/lucene/pull/550#discussion_r773771220



##########
File path: 
lucene/distribution.tests/src/test/org/apache/lucene/distribution/TestScripts.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.distribution;
+
+import com.carrotsearch.procfork.ForkedProcess;
+import com.carrotsearch.procfork.Launcher;
+import com.carrotsearch.procfork.ProcessBuilderLauncher;
+import com.carrotsearch.randomizedtesting.LifecycleScope;
+import com.carrotsearch.randomizedtesting.RandomizedTest;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.function.Supplier;
+import org.assertj.core.api.Assertions;
+import org.assertj.core.api.ThrowingConsumer;
+import org.junit.Test;
+
+/** Verify that scripts included in the distribution work. */
+public class TestScripts extends AbstractLuceneDistributionTest {
+  @Test
+  public void testLukeCanBeLaunched() throws Exception {
+    Path distributionPath;
+    if (randomBoolean()) {
+      // Occasionally, be evil: put the distribution in a folder with a space 
inside. For Uwe.
+      distributionPath = 
RandomizedTest.newTempDir(LifecycleScope.TEST).resolve("uh oh");
+      Files.createDirectory(distributionPath);
+      new Sync().sync(getDistributionPath(), distributionPath);
+    } else {
+      distributionPath = getDistributionPath();
+    }
+
+    Path lukeScript = 
resolveScript(distributionPath.resolve("bin").resolve("luke"));
+
+    Launcher launcher =
+        new ProcessBuilderLauncher()
+            .executable(lukeScript)
+            // tweak Windows launcher scripts so that they don't fork 
asynchronous java.
+            .envvar("DISTRIBUTION_TESTING", "true")
+            .viaShellLauncher()
+            .cwd(distributionPath)
+            .args("--sanity-check");
+
+    execute(
+        launcher,
+        0,
+        (output) -> {
+          Assertions.assertThat(output).contains("[Vader] Hello, Luke.");
+        });
+  }
+
+  /** The value of <code>System.getProperty("os.name")</code>. * */
+  public static final String OS_NAME = System.getProperty("os.name");
+  /** True iff running on Windows. */
+  public static final boolean WINDOWS = OS_NAME.startsWith("Windows");
+
+  protected Path resolveScript(Path scriptPath) {
+    List<Path> candidates = new ArrayList<>();
+    candidates.add(scriptPath);
+
+    String fileName = scriptPath.getFileName().toString();
+    if (WINDOWS) {
+      candidates.add(scriptPath.resolveSibling(fileName + ".cmd"));
+      candidates.add(scriptPath.resolveSibling(fileName + ".bat"));
+    } else {
+      candidates.add(scriptPath.resolveSibling(fileName + ".sh"));
+    }
+
+    Optional<Path> script = 
candidates.stream().sequential().filter(Files::exists).findFirst();
+    if (script.isEmpty()) {
+      throw new AssertionError("No script found for the base path: " + 
scriptPath);
+    }
+
+    return script.get();
+  }
+
+  private static Supplier<Charset> forkedProcessCharset =
+      () -> {
+        // The default charset for a forked java process could be computed for 
the current
+        // platform but it adds more complexity. For now, assume it's just 
parseable ascii.
+        return StandardCharsets.US_ASCII;
+      };
+
+  protected String execute(
+      Launcher launcher, int expectedExitCode, ThrowingConsumer<String> 
consumer) throws Exception {
+
+    try (ForkedProcess forkedProcess = launcher.execute()) {
+      String command = 
forkedProcess.getProcess().info().command().orElse("(unset command name)");
+
+      Charset charset = forkedProcessCharset.get();
+      try {
+        int exitStatus = forkedProcess.waitFor();

Review comment:
       Sorry, I didn't notice that the raw level Process object is available. 
Then some timeout would be easily set here if it makes sense. I am not quite 
sure whether it should be handled here or in a more global context (the test 
framework).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to