dan-s1 commented on code in PR #8510:
URL: https://github.com/apache/nifi/pull/8510#discussion_r1526252034


##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/test/java/org/apache/nifi/py4j/PythonProcessTest.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.nifi.py4j;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.nifi.python.ControllerServiceTypeLookup;
+import org.apache.nifi.python.PythonProcessConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.CleanupMode;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class PythonProcessTest {
+
+    private PythonProcess pythonProcess;
+
+    @TempDir(cleanup = CleanupMode.ON_SUCCESS)
+    private File virtualEnvHome;
+
+    @Mock
+    private PythonProcessConfig pythonProcessConfig;
+
+    @Mock
+    private ControllerServiceTypeLookup controllerServiceTypeLookup;
+
+    @BeforeEach
+    public void setUp() {
+        this.pythonProcess = new PythonProcess(this.pythonProcessConfig, 
this.controllerServiceTypeLookup, virtualEnvHome, "Controller", "Controller");
+    }

Review Comment:
   ```suggestion
   
       }
   
   ```



##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/test/java/org/apache/nifi/py4j/PythonProcessTest.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.nifi.py4j;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.nifi.python.ControllerServiceTypeLookup;
+import org.apache.nifi.python.PythonProcessConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.CleanupMode;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class PythonProcessTest {
+
+    private PythonProcess pythonProcess;
+
+    @TempDir(cleanup = CleanupMode.ON_SUCCESS)
+    private File virtualEnvHome;
+
+    @Mock
+    private PythonProcessConfig pythonProcessConfig;
+
+    @Mock
+    private ControllerServiceTypeLookup controllerServiceTypeLookup;
+
+    @BeforeEach
+    public void setUp() {
+        this.pythonProcess = new PythonProcess(this.pythonProcessConfig, 
this.controllerServiceTypeLookup, virtualEnvHome, "Controller", "Controller");
+    }
+    @Test
+    void testResolvePythonCommandWindows() throws IOException {
+        File scriptsDir = new File(virtualEnvHome, "Scripts");
+        scriptsDir.mkdir();
+        when(pythonProcessConfig.getPythonCommand()).thenReturn("python");
+        String result = this.pythonProcess.resolvePythonCommand();
+        assertEquals(this.virtualEnvHome.getAbsolutePath() + File.separator + 
"Scripts" + File.separator + "python", result);
+    }
+
+    @Test
+    void testResolvePythonCommandUnix() throws IOException {

Review Comment:
   Shouldn't this test be dependent on running on Unix/Linux?
   ```suggestion
       @EnabledOnOs({OS.LINUX})
       @Test
       void testResolvePythonCommandUnix() throws IOException {
   
   ```



##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/test/java/org/apache/nifi/py4j/PythonProcessTest.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.nifi.py4j;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.nifi.python.ControllerServiceTypeLookup;
+import org.apache.nifi.python.PythonProcessConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.CleanupMode;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class PythonProcessTest {
+
+    private PythonProcess pythonProcess;
+
+    @TempDir(cleanup = CleanupMode.ON_SUCCESS)
+    private File virtualEnvHome;
+
+    @Mock
+    private PythonProcessConfig pythonProcessConfig;
+
+    @Mock
+    private ControllerServiceTypeLookup controllerServiceTypeLookup;
+
+    @BeforeEach
+    public void setUp() {
+        this.pythonProcess = new PythonProcess(this.pythonProcessConfig, 
this.controllerServiceTypeLookup, virtualEnvHome, "Controller", "Controller");
+    }
+    @Test
+    void testResolvePythonCommandWindows() throws IOException {

Review Comment:
   Shouldn't this test be dependent on running on Windows?
   ```suggestion
       @EnabledOnOs({OS.WINDOWS})
       @Test
       void testResolvePythonCommandWindows() throws IOException {
   
   ```



##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/main/java/org/apache/nifi/py4j/PythonProcess.java:
##########
@@ -267,6 +264,27 @@ private Process launchPythonProcess(final int 
listeningPort, final String authTo
         return processBuilder.start();
     }
 
+    String resolvePythonCommand() throws IOException {
+        final File pythonCmdFile = new File(processConfig.getPythonCommand());
+        final String pythonCmd = pythonCmdFile.getName();
+
+        // Find command directories according to standard Python venv 
conventions
+        final File[] virtualEnvFileList = virtualEnvHome.listFiles((file, 
name) -> file.isDirectory() && (name.equals("bin") || name.equals("Scripts")));
+
+        //Prefer bin directory
+        final String commandFileExecutableDirectory;
+        if(virtualEnvFileList == null || virtualEnvFileList.length == 0) {
+            throw new IOException("Python binary directory could not be found 
in " + virtualEnvHome);
+        }else if( virtualEnvFileList.length == 1) {

Review Comment:
   ```suggestion
           } else if( virtualEnvFileList.length == 1) {
   ```



##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/test/java/org/apache/nifi/py4j/PythonProcessTest.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.nifi.py4j;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.nifi.python.ControllerServiceTypeLookup;
+import org.apache.nifi.python.PythonProcessConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.CleanupMode;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class PythonProcessTest {
+
+    private PythonProcess pythonProcess;
+
+    @TempDir(cleanup = CleanupMode.ON_SUCCESS)
+    private File virtualEnvHome;
+
+    @Mock
+    private PythonProcessConfig pythonProcessConfig;
+
+    @Mock
+    private ControllerServiceTypeLookup controllerServiceTypeLookup;
+
+    @BeforeEach
+    public void setUp() {
+        this.pythonProcess = new PythonProcess(this.pythonProcessConfig, 
this.controllerServiceTypeLookup, virtualEnvHome, "Controller", "Controller");
+    }
+    @Test
+    void testResolvePythonCommandWindows() throws IOException {
+        File scriptsDir = new File(virtualEnvHome, "Scripts");
+        scriptsDir.mkdir();
+        when(pythonProcessConfig.getPythonCommand()).thenReturn("python");
+        String result = this.pythonProcess.resolvePythonCommand();
+        assertEquals(this.virtualEnvHome.getAbsolutePath() + File.separator + 
"Scripts" + File.separator + "python", result);

Review Comment:
   I see three tests where the expected path is built. Perhaps make a reusable 
method e.g.
   ```
   private String getExpectedPath(String ... elements) {
           return String.join(File.separator, elements);
       }
   ```
   So the assertion would then look like
   ```suggestion
           assertEquals(getExpectedPath(this.virtualEnvHome.getAbsolutePath() , 
"Scripts",  "python"),  result);
   
   ```



-- 
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]

Reply via email to