From d0c50f13f666b05b99bbada4d3bc5e72512b8e4b Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 5 May 2023 11:02:31 +0300 Subject: [PATCH 01/12] migrating to JUnit5 & adding ExecutableFinderTest --- .../idf/core/DefaultSystemWrapper.java | 24 ++++ .../espressif/idf/core/ExecutableFinder.java | 107 +-------------- .../idf/core/SystemExecutableFinder.java | 123 ++++++++++++++++++ .../com/espressif/idf/core/SystemWrapper.java | 11 ++ .../com/espressif/idf/core/util/IDFUtil.java | 6 +- .../wizard/pages/InstallPreRquisitePage.java | 4 +- .../idf/ui/update/AbstractToolsHandler.java | 4 +- .../idf/core/test/IDFVersionReaderTest.java | 18 ++- .../unittest/DefaultSystemWrapperTest.java | 33 +++++ .../core/unittest/ExecutableFinderTest.java | 100 ++++++++++++++ 10 files changed, 316 insertions(+), 114 deletions(-) create mode 100644 bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java create mode 100644 bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java create mode 100644 bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java create mode 100644 tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java create mode 100644 tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java new file mode 100644 index 000000000..9a4967b41 --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core; + +public class DefaultSystemWrapper implements SystemWrapper +{ + private static final String PATH = "PATH"; //$NON-NLS-1$ + private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + + @Override + public String getPathEnv() + { + return System.getenv(PATH); + } + + @Override + public String getEnvExecutables() + { + return System.getenv(PATHEXT); + } + +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java index f68bc287d..94729b3e1 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java @@ -1,106 +1,13 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ package com.espressif.idf.core; -import java.io.File; -import java.lang.reflect.Method; - -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; - -import com.espressif.idf.core.util.StringUtil; -public class ExecutableFinder +public interface ExecutableFinder { - private static final String PATH = "PATH"; //$NON-NLS-1$ - private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ - - public static IPath find(String executableName, boolean appendExtension) - { - if (executableName == null) - { - return null; - } - - String pathENV = System.getenv(PATH); - if (StringUtil.isEmpty(pathENV)) - { - return null; - } - String[] paths = pathENV.split(File.pathSeparator); - for (String pathString : paths) - { - IPath path = Path.fromOSString(pathString).append(executableName); - IPath execPath = findExecutable(path, appendExtension); - if (execPath != null) - { - return execPath; - } - } - return null; - } - - public static IPath findExecutable(IPath path, boolean appendExtension) - { - if (Platform.OS_WIN32.equals(Platform.getOS()) && appendExtension) - { - String pathExt = System.getenv(PATHEXT); - if (StringUtil.isEmpty(pathExt)) - { - return null; - } - String[] extensions = System.getenv(PATHEXT).split(File.pathSeparator); - for (String ext : extensions) - { - if (ext.length() > 0 && ext.charAt(0) == '.') // $NON-NLS-1$ - { - ext = ext.substring(1); - } - IPath pathWithExt = path.addFileExtension(ext); - if (isExecutable(pathWithExt)) - { - return pathWithExt; - } - } - - } - else if (isExecutable(path)) - { - return path; - } - return null; - } - - private static boolean isExecutable(IPath path) - { - if (path == null) - { - return false; - } - File file = path.toFile(); - if (file == null || !file.exists() || file.isDirectory()) - { - return false; - } - - try - { - Method m = File.class.getMethod("canExecute"); //$NON-NLS-1$ - if (m != null) - { - return (Boolean) m.invoke(file); - } - } - catch (Exception e) - { - } - - if (Platform.OS_WIN32.equals(Platform.getOS())) - { - return true; - } - IFileStore fileStore = EFS.getLocalFileSystem().getStore(path); - return fileStore.fetchInfo().getAttribute(EFS.ATTRIBUTE_EXECUTABLE); - } + IPath find(String executableName, boolean appendExtension); + IPath findExecutable(IPath path, boolean appendExtension); } diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java new file mode 100644 index 000000000..bf9c56348 --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core; + +import java.io.File; +import java.lang.reflect.Method; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; + +import com.espressif.idf.core.util.StringUtil; + +public class SystemExecutableFinder implements ExecutableFinder +{ + + private SystemWrapper systemWrapper; + + public SystemExecutableFinder() + { + this.systemWrapper = new DefaultSystemWrapper(); + } + + public SystemExecutableFinder(SystemWrapper systemWrapper) + { + this.systemWrapper = systemWrapper; + } + @Override + public IPath find(String executableName, boolean appendExtension) + { + if (executableName == null) + { + return null; + } + + String pathENV = systemWrapper.getPathEnv(); + + if (StringUtil.isEmpty(pathENV)) + { + return null; + } + String[] paths = pathENV.split(File.pathSeparator); + for (String pathString : paths) + { + IPath path = Path.fromOSString(pathString).append(executableName); + IPath execPath = findExecutable(path, appendExtension); + if (execPath != null) + { + return execPath; + } + } + return null; + } + + @Override + public IPath findExecutable(IPath path, boolean appendExtension) + { + if (Platform.OS_WIN32.equals(Platform.getOS()) && appendExtension) + { + String pathExt = systemWrapper.getEnvExecutables(); + if (StringUtil.isEmpty(pathExt)) + { + return null; + } + String[] extensions = systemWrapper.getEnvExecutables().split(File.pathSeparator); + for (String ext : extensions) + { + if (ext.length() > 0 && ext.charAt(0) == '.') // $NON-NLS-1$ + { + ext = ext.substring(1); + } + IPath pathWithExt = path.addFileExtension(ext); + if (isExecutable(pathWithExt)) + { + return pathWithExt; + } + } + + } + else if (isExecutable(path)) + { + return path; + } + return null; + } + + private boolean isExecutable(IPath path) + { + if (path == null) + { + return false; + } + File file = path.toFile(); + + if (file == null || !file.exists() || file.isDirectory()) + { + return false; + } + + try + { + Method m = File.class.getMethod("canExecute"); //$NON-NLS-1$ + if (m != null) + { + return (Boolean) m.invoke(file); + } + } + catch (Exception e) + { + } + + if (Platform.OS_WIN32.equals(Platform.getOS())) + { + return true; + } + IFileStore fileStore = EFS.getLocalFileSystem().getStore(path); + return fileStore.fetchInfo().getAttribute(EFS.ATTRIBUTE_EXECUTABLE); + } +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java new file mode 100644 index 000000000..97007f06d --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java @@ -0,0 +1,11 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core; + +public interface SystemWrapper +{ + public String getPathEnv(); + public String getEnvExecutables(); +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java index 7e2f658d9..04af32302 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java @@ -32,11 +32,11 @@ import org.eclipse.launchbar.core.ILaunchBarManager; import org.osgi.service.prefs.BackingStoreException; -import com.espressif.idf.core.ExecutableFinder; import com.espressif.idf.core.IDFConstants; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.IDFEnvironmentVariables; import com.espressif.idf.core.ProcessBuilderFactory; +import com.espressif.idf.core.SystemExecutableFinder; import com.espressif.idf.core.build.ESP32C2ToolChain; import com.espressif.idf.core.build.ESP32C3ToolChain; import com.espressif.idf.core.build.ESP32C6ToolChain; @@ -199,10 +199,10 @@ public static boolean checkIfIdfSupportsSpaces() public static String getPythonExecutable() { - IPath pythonPath = ExecutableFinder.find(IDFConstants.PYTHON3_CMD, true); // look for python3 + IPath pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON3_CMD, true); // look for python3 if (pythonPath == null) { - pythonPath = ExecutableFinder.find(IDFConstants.PYTHON_CMD, true); // look for python + pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON_CMD, true); // look for python } if (pythonPath != null) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java index 900f65108..ce7cd2e51 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java @@ -33,10 +33,10 @@ import org.eclipse.ui.PlatformUI; import org.osgi.service.prefs.Preferences; -import com.espressif.idf.core.ExecutableFinder; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.IDFEnvironmentVariables; import com.espressif.idf.core.ProcessBuilderFactory; +import com.espressif.idf.core.SystemExecutableFinder; import com.espressif.idf.core.logging.Logger; import com.espressif.idf.core.util.GitWinRegistryReader; import com.espressif.idf.core.util.IDFUtil; @@ -161,7 +161,7 @@ public Composite getPageComposite() private void loadGitExecutablePath() { - IPath gitPath = ExecutableFinder.find("git", true); //$NON-NLS-1$ + IPath gitPath = new SystemExecutableFinder().find("git", true); //$NON-NLS-1$ Logger.log("GIT path:" + gitPath); //$NON-NLS-1$ if (gitPath != null) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java index ff85a2ad4..43f51c873 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java @@ -22,10 +22,10 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.ui.console.MessageConsoleStream; -import com.espressif.idf.core.ExecutableFinder; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.IDFEnvironmentVariables; import com.espressif.idf.core.ProcessBuilderFactory; +import com.espressif.idf.core.SystemExecutableFinder; import com.espressif.idf.core.Version; import com.espressif.idf.core.logging.Logger; import com.espressif.idf.core.util.IDFUtil; @@ -69,7 +69,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException Logger.log("IDF_PATH :" + idfPath); //$NON-NLS-1$ // Look for git path - IPath gitPath = ExecutableFinder.find("git", true); //$NON-NLS-1$ + IPath gitPath = new SystemExecutableFinder().find("git", true); //$NON-NLS-1$ Logger.log("GIT path:" + gitPath); //$NON-NLS-1$ if (gitPath != null) { diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java index ff52ae62d..75a95fbf9 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java @@ -4,16 +4,19 @@ *******************************************************************************/ package com.espressif.idf.core.test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; import com.espressif.idf.core.IDFVersion; import com.espressif.idf.core.IDFVersionsReader; @@ -22,18 +25,19 @@ * @author Kondal Kolipaka * */ +@TestInstance(Lifecycle.PER_CLASS) public class IDFVersionReaderTest { private IDFVersionsReader reader; - @Before + @BeforeAll public void setup() { reader = new IDFVersionsReader(); } - @After + @AfterAll public void teardown() { reader = null; diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java new file mode 100644 index 000000000..6a54caca9 --- /dev/null +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core.unittest; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import com.espressif.idf.core.DefaultSystemWrapper; + +class DefaultSystemWrapperTest +{ + private static final String PATH = "PATH"; //$NON-NLS-1$ + private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + + @Test + void testGetPathEnv() + { + assertEquals(System.getenv(PATH), new DefaultSystemWrapper().getPathEnv()); + } + + @Test + void testGetEnvExecutables() + { + + assertEquals(System.getenv(PATHEXT), new DefaultSystemWrapper().getEnvExecutables()); + + } + +} diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java new file mode 100644 index 000000000..0f3760623 --- /dev/null +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core.unittest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.runtime.IPath; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import com.espressif.idf.core.ExecutableFinder; +import com.espressif.idf.core.SystemExecutableFinder; +import com.espressif.idf.core.SystemWrapper; + +class ExecutableFinderTest +{ + + @TempDir + private static File foundableTempDir; + + private static File foundableExecutableFile; + private static File foundableNonExecutableFile; + + private static SystemWrapper systemWrapper; + private static ExecutableFinder executableFinder; + + private static final String FOUNDABLE_EXE_FILE_STRING = "foundable.exe"; //$NON-NLS-1$ + private static final String FOUNDABLE_TEXT_FILE_STRING = "foundable.txt"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundable.exe"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundable.txt"; //$NON-NLS-1$ + private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + + @BeforeAll + public static void setUp() throws IOException + { + foundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING); + foundableExecutableFile.createNewFile(); + foundableExecutableFile.setExecutable(true); + + foundableNonExecutableFile = new File(foundableTempDir, FOUNDABLE_TEXT_FILE_STRING); + foundableNonExecutableFile.createNewFile(); + + systemWrapper = new SystemWrapper() + { + @Override + public String getPathEnv() + { + return foundableTempDir.toString(); + } + + @Override + public String getEnvExecutables() + { + return System.getenv(PATHEXT); + } + }; + + executableFinder = new SystemExecutableFinder(systemWrapper); + } + + @Test + void testFindReturnsExpectedResultonFoundableExecutable() + { + + String foundExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true) + .toString(); + assertEquals(foundExecutable, foundExecutable.toString()); + } + + @Test + void testFindReturnsNullOnNonFoundableExecutable() + { + IPath nonFoundableExecutable = executableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, + true); + assertNull(nonFoundableExecutable); + } + + @Test + void testFindReturnsNullOnFoundableNonExecutable() + { + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testFindReturnsNullOnNonFoundableNonExecutable() + { + IPath nonFoudableNonExecutable = executableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, + true); + assertNull(nonFoudableNonExecutable); + } + +} From 4909ee5bdb03326b6b6e8ab3270117db10728c00 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 5 May 2023 11:48:05 +0300 Subject: [PATCH 02/12] update core.test dependencies --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index f7b99e63f..4a68ac0d4 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -6,8 +6,8 @@ Bundle-Version: 0.0.1.qualifier Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 -Require-Bundle: org.junit, - com.espressif.idf.core;bundle-version="1.0.1" +Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", + junit-jupiter-api;bundle-version="5.9.1" Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, @@ -65,3 +65,4 @@ Export-Package: com.espressif.idf.core.test, org.jmock.api, org.jmock.auto, org.jmock.lib +Import-Package: org.eclipse.core.runtime From 802e162f62a2ab71275d4bc3ffcaa0c925cc4a77 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 5 May 2023 11:50:28 +0300 Subject: [PATCH 03/12] remove specific version from junit-jupiter-api dependency --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index 4a68ac0d4..51e22f76a 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", - junit-jupiter-api;bundle-version="5.9.1" + junit-jupiter-api Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, From 5eb456476c16423d218676672d8b1f77394b2b9d Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 12 May 2023 13:42:36 +0300 Subject: [PATCH 04/12] pushed to 100% test coverage --- .../idf/core/SystemExecutableFinder.java | 11 +- .../META-INF/MANIFEST.MF | 2 +- .../core/unittest/ExecutableFinderTest.java | 182 ++++++++++++++++-- 3 files changed, 171 insertions(+), 24 deletions(-) diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java index bf9c56348..0af96ea0c 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java @@ -29,6 +29,7 @@ public SystemExecutableFinder(SystemWrapper systemWrapper) { this.systemWrapper = systemWrapper; } + @Override public IPath find(String executableName, boolean appendExtension) { @@ -59,7 +60,7 @@ public IPath find(String executableName, boolean appendExtension) @Override public IPath findExecutable(IPath path, boolean appendExtension) { - if (Platform.OS_WIN32.equals(Platform.getOS()) && appendExtension) + if (isPlatformWindows() && appendExtension) { String pathExt = systemWrapper.getEnvExecutables(); if (StringUtil.isEmpty(pathExt)) @@ -88,6 +89,12 @@ else if (isExecutable(path)) return null; } + // seam for testing + protected boolean isPlatformWindows() + { + return Platform.OS_WIN32.equals(Platform.getOS()); + } + private boolean isExecutable(IPath path) { if (path == null) @@ -113,7 +120,7 @@ private boolean isExecutable(IPath path) { } - if (Platform.OS_WIN32.equals(Platform.getOS())) + if (isPlatformWindows()) { return true; } diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index 51e22f76a..cb290450f 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", - junit-jupiter-api + org.junit.jupiter.api Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java index 0f3760623..beac955c3 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -29,23 +29,62 @@ class ExecutableFinderTest private static File foundableNonExecutableFile; private static SystemWrapper systemWrapper; - private static ExecutableFinder executableFinder; - - private static final String FOUNDABLE_EXE_FILE_STRING = "foundable.exe"; //$NON-NLS-1$ - private static final String FOUNDABLE_TEXT_FILE_STRING = "foundable.txt"; //$NON-NLS-1$ - private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundable.exe"; //$NON-NLS-1$ - private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundable.txt"; //$NON-NLS-1$ + private static TestableSystemExecutableFinderWindows windowsExecutableFinder; + private static SystemWrapper emptyPathSystemWrapper; + private static SystemWrapper emptyPathExtSystemWrapper; + private static TestableSystemExecutableFinderUnix unixExecutableFinder; + + private static File unixFoundableExecutableFile; + + private static final String EXE_STRING = ".EXE"; //$NON-NLS-1$ + private static final String TXT_STRING = ".TXT"; //$NON-NLS-1$ + private static final String FOUNDABLE_EXE_FILE_STRING = "foundableExe"; //$NON-NLS-1$ + private static final String FOUNDABLE_TEXT_FILE_STRING = "foundableTxt"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundableExe"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundableTxt"; //$NON-NLS-1$ private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + private static class TestableSystemExecutableFinderWindows extends SystemExecutableFinder + { + public TestableSystemExecutableFinderWindows(SystemWrapper systemWrapper) + { + super(systemWrapper); + } + + @Override + protected boolean isPlatformWindows() + { + return true; + } + } + + private static class TestableSystemExecutableFinderUnix extends SystemExecutableFinder + { + public TestableSystemExecutableFinderUnix(SystemWrapper systemWrapper) + { + super(systemWrapper); + } + + @Override + protected boolean isPlatformWindows() + { + return false; + } + } + @BeforeAll public static void setUp() throws IOException { - foundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING); + foundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING + EXE_STRING); foundableExecutableFile.createNewFile(); foundableExecutableFile.setExecutable(true); + unixFoundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING); + unixFoundableExecutableFile.createNewFile(); + unixFoundableExecutableFile.setExecutable(true); - foundableNonExecutableFile = new File(foundableTempDir, FOUNDABLE_TEXT_FILE_STRING); + foundableNonExecutableFile = new File(foundableTempDir, FOUNDABLE_TEXT_FILE_STRING + TXT_STRING); foundableNonExecutableFile.createNewFile(); + foundableNonExecutableFile.setExecutable(false); systemWrapper = new SystemWrapper() { @@ -62,38 +101,139 @@ public String getEnvExecutables() } }; - executableFinder = new SystemExecutableFinder(systemWrapper); + emptyPathSystemWrapper = new SystemWrapper() + { + + public String getPathEnv() + { + return null; + } + + public String getEnvExecutables() + { + return System.getenv(PATHEXT); + } + }; + + emptyPathExtSystemWrapper = new SystemWrapper() + { + + public String getPathEnv() + { + + return foundableTempDir.toString(); + } + + public String getEnvExecutables() + { + return null; + } + }; + + windowsExecutableFinder = new TestableSystemExecutableFinderWindows(systemWrapper); + unixExecutableFinder = new TestableSystemExecutableFinderUnix(systemWrapper); } @Test - void testFindReturnsExpectedResultonFoundableExecutable() - { + void testWindowsFindReturnsExpectedResultOnFoundableExecutable() + { + IPath foundExecutable = windowsExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + assertEquals(foundableExecutableFile.toString(), foundExecutable.toOSString()); + } - String foundExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true) - .toString(); - assertEquals(foundExecutable, foundExecutable.toString()); + @Test + void testWindowsFindReturnsNullOnNonFoundableExecutable() + { + IPath nonFoundableExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, true); + assertNull(nonFoundableExecutable); } @Test - void testFindReturnsNullOnNonFoundableExecutable() + void testWindowsFindReturnsNullOnNull() { - IPath nonFoundableExecutable = executableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, - true); + IPath nullPath = windowsExecutableFinder.find(null, true); + assertNull(nullPath); + } + + @Test + void testWindowsFindReturnsNullWithEmpyPath() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(emptyPathSystemWrapper); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testWindowsFindReturnsNullWithEmpyPathExt() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(emptyPathExtSystemWrapper); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testWindowsFindReturnsNullOnFoundableNonExecutable() + { + IPath foundNonExecutable = windowsExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testWindowsFindReturnsNullOnNonFoundableNonExecutable() + { + IPath nonFoudableNonExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(nonFoudableNonExecutable); + } + + @Test + void testUnixFindReturnsExpectedResultOnFoundableExecutable() + { + + IPath foundExecutable = unixExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + assertEquals(unixFoundableExecutableFile.toString(), foundExecutable.toOSString()); + } + + @Test + void testUnixFindReturnsNullOnNonFoundableExecutable() + { + IPath nonFoundableExecutable = unixExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, true); assertNull(nonFoundableExecutable); } @Test - void testFindReturnsNullOnFoundableNonExecutable() + void testUnixFindReturnsNullOnNull() { + IPath nullPath = unixExecutableFinder.find(null, true); + assertNull(nullPath); + } + + @Test + void testUnixFindReturnsNullWithEmpyPath() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderUnix(emptyPathSystemWrapper); IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); assertNull(foundNonExecutable); } @Test - void testFindReturnsNullOnNonFoundableNonExecutable() + void testUnixFindReturnsExpectedResultWithEmpyPathExt() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderUnix(emptyPathExtSystemWrapper); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + assertEquals(unixFoundableExecutableFile.toString(), foundNonExecutable.toOSString()); + } + + @Test + void testUnixFindReturnsNullOnFoundableNonExecutable() + { + IPath foundNonExecutable = unixExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testUnixFindReturnsNullOnNonFoundableNonExecutable() { - IPath nonFoudableNonExecutable = executableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, - true); + IPath nonFoudableNonExecutable = unixExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, true); assertNull(nonFoudableNonExecutable); } From 5306412a7a1dc76d398015eafc682c5a496f0856 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 12 May 2023 13:54:54 +0300 Subject: [PATCH 05/12] remove System dependency from test --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 2 +- .../idf/core/unittest/ExecutableFinderTest.java | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index cb290450f..51e22f76a 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", - org.junit.jupiter.api + junit-jupiter-api Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java index beac955c3..b0a7ea8a3 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -42,7 +42,6 @@ class ExecutableFinderTest private static final String FOUNDABLE_TEXT_FILE_STRING = "foundableTxt"; //$NON-NLS-1$ private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundableExe"; //$NON-NLS-1$ private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundableTxt"; //$NON-NLS-1$ - private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ private static class TestableSystemExecutableFinderWindows extends SystemExecutableFinder { @@ -97,33 +96,37 @@ public String getPathEnv() @Override public String getEnvExecutables() { - return System.getenv(PATHEXT); + return EXE_STRING; } }; emptyPathSystemWrapper = new SystemWrapper() { + @Override public String getPathEnv() { return null; } + @Override public String getEnvExecutables() { - return System.getenv(PATHEXT); + return EXE_STRING; } }; emptyPathExtSystemWrapper = new SystemWrapper() { + @Override public String getPathEnv() { return foundableTempDir.toString(); } + @Override public String getEnvExecutables() { return null; From ed5955743ade3bcb4a004429e2611f3d05382c41 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 5 May 2023 11:02:31 +0300 Subject: [PATCH 06/12] migrating to JUnit5 & adding ExecutableFinderTest --- .../idf/core/DefaultSystemWrapper.java | 24 ++++ .../espressif/idf/core/ExecutableFinder.java | 107 +-------------- .../idf/core/SystemExecutableFinder.java | 123 ++++++++++++++++++ .../com/espressif/idf/core/SystemWrapper.java | 11 ++ .../com/espressif/idf/core/util/IDFUtil.java | 6 +- .../wizard/pages/InstallPreRquisitePage.java | 4 +- .../idf/ui/update/AbstractToolsHandler.java | 4 +- .../idf/core/test/IDFVersionReaderTest.java | 18 ++- .../unittest/DefaultSystemWrapperTest.java | 33 +++++ .../core/unittest/ExecutableFinderTest.java | 100 ++++++++++++++ 10 files changed, 316 insertions(+), 114 deletions(-) create mode 100644 bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java create mode 100644 bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java create mode 100644 bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java create mode 100644 tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java create mode 100644 tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java new file mode 100644 index 000000000..9a4967b41 --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/DefaultSystemWrapper.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core; + +public class DefaultSystemWrapper implements SystemWrapper +{ + private static final String PATH = "PATH"; //$NON-NLS-1$ + private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + + @Override + public String getPathEnv() + { + return System.getenv(PATH); + } + + @Override + public String getEnvExecutables() + { + return System.getenv(PATHEXT); + } + +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java index f68bc287d..94729b3e1 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java @@ -1,106 +1,13 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ package com.espressif.idf.core; -import java.io.File; -import java.lang.reflect.Method; - -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; - -import com.espressif.idf.core.util.StringUtil; -public class ExecutableFinder +public interface ExecutableFinder { - private static final String PATH = "PATH"; //$NON-NLS-1$ - private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ - - public static IPath find(String executableName, boolean appendExtension) - { - if (executableName == null) - { - return null; - } - - String pathENV = System.getenv(PATH); - if (StringUtil.isEmpty(pathENV)) - { - return null; - } - String[] paths = pathENV.split(File.pathSeparator); - for (String pathString : paths) - { - IPath path = Path.fromOSString(pathString).append(executableName); - IPath execPath = findExecutable(path, appendExtension); - if (execPath != null) - { - return execPath; - } - } - return null; - } - - public static IPath findExecutable(IPath path, boolean appendExtension) - { - if (Platform.OS_WIN32.equals(Platform.getOS()) && appendExtension) - { - String pathExt = System.getenv(PATHEXT); - if (StringUtil.isEmpty(pathExt)) - { - return null; - } - String[] extensions = System.getenv(PATHEXT).split(File.pathSeparator); - for (String ext : extensions) - { - if (ext.length() > 0 && ext.charAt(0) == '.') // $NON-NLS-1$ - { - ext = ext.substring(1); - } - IPath pathWithExt = path.addFileExtension(ext); - if (isExecutable(pathWithExt)) - { - return pathWithExt; - } - } - - } - else if (isExecutable(path)) - { - return path; - } - return null; - } - - private static boolean isExecutable(IPath path) - { - if (path == null) - { - return false; - } - File file = path.toFile(); - if (file == null || !file.exists() || file.isDirectory()) - { - return false; - } - - try - { - Method m = File.class.getMethod("canExecute"); //$NON-NLS-1$ - if (m != null) - { - return (Boolean) m.invoke(file); - } - } - catch (Exception e) - { - } - - if (Platform.OS_WIN32.equals(Platform.getOS())) - { - return true; - } - IFileStore fileStore = EFS.getLocalFileSystem().getStore(path); - return fileStore.fetchInfo().getAttribute(EFS.ATTRIBUTE_EXECUTABLE); - } + IPath find(String executableName, boolean appendExtension); + IPath findExecutable(IPath path, boolean appendExtension); } diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java new file mode 100644 index 000000000..bf9c56348 --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core; + +import java.io.File; +import java.lang.reflect.Method; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; + +import com.espressif.idf.core.util.StringUtil; + +public class SystemExecutableFinder implements ExecutableFinder +{ + + private SystemWrapper systemWrapper; + + public SystemExecutableFinder() + { + this.systemWrapper = new DefaultSystemWrapper(); + } + + public SystemExecutableFinder(SystemWrapper systemWrapper) + { + this.systemWrapper = systemWrapper; + } + @Override + public IPath find(String executableName, boolean appendExtension) + { + if (executableName == null) + { + return null; + } + + String pathENV = systemWrapper.getPathEnv(); + + if (StringUtil.isEmpty(pathENV)) + { + return null; + } + String[] paths = pathENV.split(File.pathSeparator); + for (String pathString : paths) + { + IPath path = Path.fromOSString(pathString).append(executableName); + IPath execPath = findExecutable(path, appendExtension); + if (execPath != null) + { + return execPath; + } + } + return null; + } + + @Override + public IPath findExecutable(IPath path, boolean appendExtension) + { + if (Platform.OS_WIN32.equals(Platform.getOS()) && appendExtension) + { + String pathExt = systemWrapper.getEnvExecutables(); + if (StringUtil.isEmpty(pathExt)) + { + return null; + } + String[] extensions = systemWrapper.getEnvExecutables().split(File.pathSeparator); + for (String ext : extensions) + { + if (ext.length() > 0 && ext.charAt(0) == '.') // $NON-NLS-1$ + { + ext = ext.substring(1); + } + IPath pathWithExt = path.addFileExtension(ext); + if (isExecutable(pathWithExt)) + { + return pathWithExt; + } + } + + } + else if (isExecutable(path)) + { + return path; + } + return null; + } + + private boolean isExecutable(IPath path) + { + if (path == null) + { + return false; + } + File file = path.toFile(); + + if (file == null || !file.exists() || file.isDirectory()) + { + return false; + } + + try + { + Method m = File.class.getMethod("canExecute"); //$NON-NLS-1$ + if (m != null) + { + return (Boolean) m.invoke(file); + } + } + catch (Exception e) + { + } + + if (Platform.OS_WIN32.equals(Platform.getOS())) + { + return true; + } + IFileStore fileStore = EFS.getLocalFileSystem().getStore(path); + return fileStore.fetchInfo().getAttribute(EFS.ATTRIBUTE_EXECUTABLE); + } +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java new file mode 100644 index 000000000..97007f06d --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemWrapper.java @@ -0,0 +1,11 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core; + +public interface SystemWrapper +{ + public String getPathEnv(); + public String getEnvExecutables(); +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java index 7e2f658d9..04af32302 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java @@ -32,11 +32,11 @@ import org.eclipse.launchbar.core.ILaunchBarManager; import org.osgi.service.prefs.BackingStoreException; -import com.espressif.idf.core.ExecutableFinder; import com.espressif.idf.core.IDFConstants; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.IDFEnvironmentVariables; import com.espressif.idf.core.ProcessBuilderFactory; +import com.espressif.idf.core.SystemExecutableFinder; import com.espressif.idf.core.build.ESP32C2ToolChain; import com.espressif.idf.core.build.ESP32C3ToolChain; import com.espressif.idf.core.build.ESP32C6ToolChain; @@ -199,10 +199,10 @@ public static boolean checkIfIdfSupportsSpaces() public static String getPythonExecutable() { - IPath pythonPath = ExecutableFinder.find(IDFConstants.PYTHON3_CMD, true); // look for python3 + IPath pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON3_CMD, true); // look for python3 if (pythonPath == null) { - pythonPath = ExecutableFinder.find(IDFConstants.PYTHON_CMD, true); // look for python + pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON_CMD, true); // look for python } if (pythonPath != null) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java index 900f65108..ce7cd2e51 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java @@ -33,10 +33,10 @@ import org.eclipse.ui.PlatformUI; import org.osgi.service.prefs.Preferences; -import com.espressif.idf.core.ExecutableFinder; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.IDFEnvironmentVariables; import com.espressif.idf.core.ProcessBuilderFactory; +import com.espressif.idf.core.SystemExecutableFinder; import com.espressif.idf.core.logging.Logger; import com.espressif.idf.core.util.GitWinRegistryReader; import com.espressif.idf.core.util.IDFUtil; @@ -161,7 +161,7 @@ public Composite getPageComposite() private void loadGitExecutablePath() { - IPath gitPath = ExecutableFinder.find("git", true); //$NON-NLS-1$ + IPath gitPath = new SystemExecutableFinder().find("git", true); //$NON-NLS-1$ Logger.log("GIT path:" + gitPath); //$NON-NLS-1$ if (gitPath != null) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java index ff85a2ad4..43f51c873 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java @@ -22,10 +22,10 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.ui.console.MessageConsoleStream; -import com.espressif.idf.core.ExecutableFinder; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.IDFEnvironmentVariables; import com.espressif.idf.core.ProcessBuilderFactory; +import com.espressif.idf.core.SystemExecutableFinder; import com.espressif.idf.core.Version; import com.espressif.idf.core.logging.Logger; import com.espressif.idf.core.util.IDFUtil; @@ -69,7 +69,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException Logger.log("IDF_PATH :" + idfPath); //$NON-NLS-1$ // Look for git path - IPath gitPath = ExecutableFinder.find("git", true); //$NON-NLS-1$ + IPath gitPath = new SystemExecutableFinder().find("git", true); //$NON-NLS-1$ Logger.log("GIT path:" + gitPath); //$NON-NLS-1$ if (gitPath != null) { diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java index ff52ae62d..75a95fbf9 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/test/IDFVersionReaderTest.java @@ -4,16 +4,19 @@ *******************************************************************************/ package com.espressif.idf.core.test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; import com.espressif.idf.core.IDFVersion; import com.espressif.idf.core.IDFVersionsReader; @@ -22,18 +25,19 @@ * @author Kondal Kolipaka * */ +@TestInstance(Lifecycle.PER_CLASS) public class IDFVersionReaderTest { private IDFVersionsReader reader; - @Before + @BeforeAll public void setup() { reader = new IDFVersionsReader(); } - @After + @AfterAll public void teardown() { reader = null; diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java new file mode 100644 index 000000000..6a54caca9 --- /dev/null +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/DefaultSystemWrapperTest.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core.unittest; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import com.espressif.idf.core.DefaultSystemWrapper; + +class DefaultSystemWrapperTest +{ + private static final String PATH = "PATH"; //$NON-NLS-1$ + private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + + @Test + void testGetPathEnv() + { + assertEquals(System.getenv(PATH), new DefaultSystemWrapper().getPathEnv()); + } + + @Test + void testGetEnvExecutables() + { + + assertEquals(System.getenv(PATHEXT), new DefaultSystemWrapper().getEnvExecutables()); + + } + +} diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java new file mode 100644 index 000000000..0f3760623 --- /dev/null +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved. + * Use is subject to license terms. + *******************************************************************************/ +package com.espressif.idf.core.unittest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.runtime.IPath; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import com.espressif.idf.core.ExecutableFinder; +import com.espressif.idf.core.SystemExecutableFinder; +import com.espressif.idf.core.SystemWrapper; + +class ExecutableFinderTest +{ + + @TempDir + private static File foundableTempDir; + + private static File foundableExecutableFile; + private static File foundableNonExecutableFile; + + private static SystemWrapper systemWrapper; + private static ExecutableFinder executableFinder; + + private static final String FOUNDABLE_EXE_FILE_STRING = "foundable.exe"; //$NON-NLS-1$ + private static final String FOUNDABLE_TEXT_FILE_STRING = "foundable.txt"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundable.exe"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundable.txt"; //$NON-NLS-1$ + private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + + @BeforeAll + public static void setUp() throws IOException + { + foundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING); + foundableExecutableFile.createNewFile(); + foundableExecutableFile.setExecutable(true); + + foundableNonExecutableFile = new File(foundableTempDir, FOUNDABLE_TEXT_FILE_STRING); + foundableNonExecutableFile.createNewFile(); + + systemWrapper = new SystemWrapper() + { + @Override + public String getPathEnv() + { + return foundableTempDir.toString(); + } + + @Override + public String getEnvExecutables() + { + return System.getenv(PATHEXT); + } + }; + + executableFinder = new SystemExecutableFinder(systemWrapper); + } + + @Test + void testFindReturnsExpectedResultonFoundableExecutable() + { + + String foundExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true) + .toString(); + assertEquals(foundExecutable, foundExecutable.toString()); + } + + @Test + void testFindReturnsNullOnNonFoundableExecutable() + { + IPath nonFoundableExecutable = executableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, + true); + assertNull(nonFoundableExecutable); + } + + @Test + void testFindReturnsNullOnFoundableNonExecutable() + { + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testFindReturnsNullOnNonFoundableNonExecutable() + { + IPath nonFoudableNonExecutable = executableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, + true); + assertNull(nonFoudableNonExecutable); + } + +} From 6406d061069dbcd175a1e2e2e0b8a4711bb5c199 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 5 May 2023 11:48:05 +0300 Subject: [PATCH 07/12] update core.test dependencies --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index f7b99e63f..4a68ac0d4 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -6,8 +6,8 @@ Bundle-Version: 0.0.1.qualifier Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 -Require-Bundle: org.junit, - com.espressif.idf.core;bundle-version="1.0.1" +Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", + junit-jupiter-api;bundle-version="5.9.1" Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, @@ -65,3 +65,4 @@ Export-Package: com.espressif.idf.core.test, org.jmock.api, org.jmock.auto, org.jmock.lib +Import-Package: org.eclipse.core.runtime From e216ba56c6d0b16359e94a2b7d5b2fb28b005cb0 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 5 May 2023 11:50:28 +0300 Subject: [PATCH 08/12] remove specific version from junit-jupiter-api dependency --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index 4a68ac0d4..51e22f76a 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", - junit-jupiter-api;bundle-version="5.9.1" + junit-jupiter-api Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, From 9f66e9500c9566818eb202f0aed8b5730551b391 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 12 May 2023 13:42:36 +0300 Subject: [PATCH 09/12] pushed to 100% test coverage --- .../idf/core/SystemExecutableFinder.java | 11 +- .../META-INF/MANIFEST.MF | 2 +- .../core/unittest/ExecutableFinderTest.java | 182 ++++++++++++++++-- 3 files changed, 171 insertions(+), 24 deletions(-) diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java index bf9c56348..0af96ea0c 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java @@ -29,6 +29,7 @@ public SystemExecutableFinder(SystemWrapper systemWrapper) { this.systemWrapper = systemWrapper; } + @Override public IPath find(String executableName, boolean appendExtension) { @@ -59,7 +60,7 @@ public IPath find(String executableName, boolean appendExtension) @Override public IPath findExecutable(IPath path, boolean appendExtension) { - if (Platform.OS_WIN32.equals(Platform.getOS()) && appendExtension) + if (isPlatformWindows() && appendExtension) { String pathExt = systemWrapper.getEnvExecutables(); if (StringUtil.isEmpty(pathExt)) @@ -88,6 +89,12 @@ else if (isExecutable(path)) return null; } + // seam for testing + protected boolean isPlatformWindows() + { + return Platform.OS_WIN32.equals(Platform.getOS()); + } + private boolean isExecutable(IPath path) { if (path == null) @@ -113,7 +120,7 @@ private boolean isExecutable(IPath path) { } - if (Platform.OS_WIN32.equals(Platform.getOS())) + if (isPlatformWindows()) { return true; } diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index 51e22f76a..cb290450f 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", - junit-jupiter-api + org.junit.jupiter.api Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java index 0f3760623..beac955c3 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -29,23 +29,62 @@ class ExecutableFinderTest private static File foundableNonExecutableFile; private static SystemWrapper systemWrapper; - private static ExecutableFinder executableFinder; - - private static final String FOUNDABLE_EXE_FILE_STRING = "foundable.exe"; //$NON-NLS-1$ - private static final String FOUNDABLE_TEXT_FILE_STRING = "foundable.txt"; //$NON-NLS-1$ - private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundable.exe"; //$NON-NLS-1$ - private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundable.txt"; //$NON-NLS-1$ + private static TestableSystemExecutableFinderWindows windowsExecutableFinder; + private static SystemWrapper emptyPathSystemWrapper; + private static SystemWrapper emptyPathExtSystemWrapper; + private static TestableSystemExecutableFinderUnix unixExecutableFinder; + + private static File unixFoundableExecutableFile; + + private static final String EXE_STRING = ".EXE"; //$NON-NLS-1$ + private static final String TXT_STRING = ".TXT"; //$NON-NLS-1$ + private static final String FOUNDABLE_EXE_FILE_STRING = "foundableExe"; //$NON-NLS-1$ + private static final String FOUNDABLE_TEXT_FILE_STRING = "foundableTxt"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundableExe"; //$NON-NLS-1$ + private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundableTxt"; //$NON-NLS-1$ private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ + private static class TestableSystemExecutableFinderWindows extends SystemExecutableFinder + { + public TestableSystemExecutableFinderWindows(SystemWrapper systemWrapper) + { + super(systemWrapper); + } + + @Override + protected boolean isPlatformWindows() + { + return true; + } + } + + private static class TestableSystemExecutableFinderUnix extends SystemExecutableFinder + { + public TestableSystemExecutableFinderUnix(SystemWrapper systemWrapper) + { + super(systemWrapper); + } + + @Override + protected boolean isPlatformWindows() + { + return false; + } + } + @BeforeAll public static void setUp() throws IOException { - foundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING); + foundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING + EXE_STRING); foundableExecutableFile.createNewFile(); foundableExecutableFile.setExecutable(true); + unixFoundableExecutableFile = new File(foundableTempDir, FOUNDABLE_EXE_FILE_STRING); + unixFoundableExecutableFile.createNewFile(); + unixFoundableExecutableFile.setExecutable(true); - foundableNonExecutableFile = new File(foundableTempDir, FOUNDABLE_TEXT_FILE_STRING); + foundableNonExecutableFile = new File(foundableTempDir, FOUNDABLE_TEXT_FILE_STRING + TXT_STRING); foundableNonExecutableFile.createNewFile(); + foundableNonExecutableFile.setExecutable(false); systemWrapper = new SystemWrapper() { @@ -62,38 +101,139 @@ public String getEnvExecutables() } }; - executableFinder = new SystemExecutableFinder(systemWrapper); + emptyPathSystemWrapper = new SystemWrapper() + { + + public String getPathEnv() + { + return null; + } + + public String getEnvExecutables() + { + return System.getenv(PATHEXT); + } + }; + + emptyPathExtSystemWrapper = new SystemWrapper() + { + + public String getPathEnv() + { + + return foundableTempDir.toString(); + } + + public String getEnvExecutables() + { + return null; + } + }; + + windowsExecutableFinder = new TestableSystemExecutableFinderWindows(systemWrapper); + unixExecutableFinder = new TestableSystemExecutableFinderUnix(systemWrapper); } @Test - void testFindReturnsExpectedResultonFoundableExecutable() - { + void testWindowsFindReturnsExpectedResultOnFoundableExecutable() + { + IPath foundExecutable = windowsExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + assertEquals(foundableExecutableFile.toString(), foundExecutable.toOSString()); + } - String foundExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true) - .toString(); - assertEquals(foundExecutable, foundExecutable.toString()); + @Test + void testWindowsFindReturnsNullOnNonFoundableExecutable() + { + IPath nonFoundableExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, true); + assertNull(nonFoundableExecutable); } @Test - void testFindReturnsNullOnNonFoundableExecutable() + void testWindowsFindReturnsNullOnNull() { - IPath nonFoundableExecutable = executableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, - true); + IPath nullPath = windowsExecutableFinder.find(null, true); + assertNull(nullPath); + } + + @Test + void testWindowsFindReturnsNullWithEmpyPath() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(emptyPathSystemWrapper); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testWindowsFindReturnsNullWithEmpyPathExt() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(emptyPathExtSystemWrapper); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testWindowsFindReturnsNullOnFoundableNonExecutable() + { + IPath foundNonExecutable = windowsExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testWindowsFindReturnsNullOnNonFoundableNonExecutable() + { + IPath nonFoudableNonExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(nonFoudableNonExecutable); + } + + @Test + void testUnixFindReturnsExpectedResultOnFoundableExecutable() + { + + IPath foundExecutable = unixExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + assertEquals(unixFoundableExecutableFile.toString(), foundExecutable.toOSString()); + } + + @Test + void testUnixFindReturnsNullOnNonFoundableExecutable() + { + IPath nonFoundableExecutable = unixExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, true); assertNull(nonFoundableExecutable); } @Test - void testFindReturnsNullOnFoundableNonExecutable() + void testUnixFindReturnsNullOnNull() { + IPath nullPath = unixExecutableFinder.find(null, true); + assertNull(nullPath); + } + + @Test + void testUnixFindReturnsNullWithEmpyPath() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderUnix(emptyPathSystemWrapper); IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); assertNull(foundNonExecutable); } @Test - void testFindReturnsNullOnNonFoundableNonExecutable() + void testUnixFindReturnsExpectedResultWithEmpyPathExt() + { + ExecutableFinder executableFinder = new TestableSystemExecutableFinderUnix(emptyPathExtSystemWrapper); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + assertEquals(unixFoundableExecutableFile.toString(), foundNonExecutable.toOSString()); + } + + @Test + void testUnixFindReturnsNullOnFoundableNonExecutable() + { + IPath foundNonExecutable = unixExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + assertNull(foundNonExecutable); + } + + @Test + void testUnixFindReturnsNullOnNonFoundableNonExecutable() { - IPath nonFoudableNonExecutable = executableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, - true); + IPath nonFoudableNonExecutable = unixExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, true); assertNull(nonFoudableNonExecutable); } From 934f394d0162e914b48f69db47fde33a4770a280 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 12 May 2023 13:54:54 +0300 Subject: [PATCH 10/12] remove System dependency from test --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 2 +- .../idf/core/unittest/ExecutableFinderTest.java | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index cb290450f..51e22f76a 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Vendor: Espressif Systems Automatic-Module-Name: com.espressif.idf.core.test Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1", - org.junit.jupiter.api + junit-jupiter-api Bundle-ClassPath: ., lib/jmock-2.12.0.jar, lib/commons-collections4-4.4.jar, diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java index beac955c3..b0a7ea8a3 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -42,7 +42,6 @@ class ExecutableFinderTest private static final String FOUNDABLE_TEXT_FILE_STRING = "foundableTxt"; //$NON-NLS-1$ private static final String NON_FOUNDABLE_EXE_FILE_STRING = "non_foundableExe"; //$NON-NLS-1$ private static final String NON_FOUNDABLE_TEXT_FILE_STRING = "non_foundableTxt"; //$NON-NLS-1$ - private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$ private static class TestableSystemExecutableFinderWindows extends SystemExecutableFinder { @@ -97,33 +96,37 @@ public String getPathEnv() @Override public String getEnvExecutables() { - return System.getenv(PATHEXT); + return EXE_STRING; } }; emptyPathSystemWrapper = new SystemWrapper() { + @Override public String getPathEnv() { return null; } + @Override public String getEnvExecutables() { - return System.getenv(PATHEXT); + return EXE_STRING; } }; emptyPathExtSystemWrapper = new SystemWrapper() { + @Override public String getPathEnv() { return foundableTempDir.toString(); } + @Override public String getEnvExecutables() { return null; From 6a7219fe826ecb29e692e71f85a9832851ad3772 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Fri, 19 May 2023 17:41:25 +0300 Subject: [PATCH 11/12] removing unused method from ExecutableFinder --- .../espressif/idf/core/ExecutableFinder.java | 3 +- .../idf/core/SystemExecutableFinder.java | 36 +++---------- .../com/espressif/idf/core/util/IDFUtil.java | 4 +- .../wizard/pages/InstallPreRquisitePage.java | 2 +- .../idf/ui/update/AbstractToolsHandler.java | 2 +- .../core/unittest/ExecutableFinderTest.java | 53 ++++++++++++++----- 6 files changed, 50 insertions(+), 50 deletions(-) diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java index 94729b3e1..b970ab8f7 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ExecutableFinder.java @@ -8,6 +8,5 @@ public interface ExecutableFinder { - IPath find(String executableName, boolean appendExtension); - IPath findExecutable(IPath path, boolean appendExtension); + IPath find(String executableName); } diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java index 0af96ea0c..cda63ef63 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/SystemExecutableFinder.java @@ -5,10 +5,7 @@ package com.espressif.idf.core; import java.io.File; -import java.lang.reflect.Method; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; @@ -31,7 +28,7 @@ public SystemExecutableFinder(SystemWrapper systemWrapper) } @Override - public IPath find(String executableName, boolean appendExtension) + public IPath find(String executableName) { if (executableName == null) { @@ -48,7 +45,7 @@ public IPath find(String executableName, boolean appendExtension) for (String pathString : paths) { IPath path = Path.fromOSString(pathString).append(executableName); - IPath execPath = findExecutable(path, appendExtension); + IPath execPath = findExecutable(path); if (execPath != null) { return execPath; @@ -57,10 +54,9 @@ public IPath find(String executableName, boolean appendExtension) return null; } - @Override - public IPath findExecutable(IPath path, boolean appendExtension) + private IPath findExecutable(IPath path) { - if (isPlatformWindows() && appendExtension) + if (isPlatformWindows()) { String pathExt = systemWrapper.getEnvExecutables(); if (StringUtil.isEmpty(pathExt)) @@ -74,6 +70,7 @@ public IPath findExecutable(IPath path, boolean appendExtension) { ext = ext.substring(1); } + IPath pathWithExt = path.addFileExtension(ext); if (isExecutable(pathWithExt)) { @@ -97,10 +94,6 @@ protected boolean isPlatformWindows() private boolean isExecutable(IPath path) { - if (path == null) - { - return false; - } File file = path.toFile(); if (file == null || !file.exists() || file.isDirectory()) @@ -108,23 +101,6 @@ private boolean isExecutable(IPath path) return false; } - try - { - Method m = File.class.getMethod("canExecute"); //$NON-NLS-1$ - if (m != null) - { - return (Boolean) m.invoke(file); - } - } - catch (Exception e) - { - } - - if (isPlatformWindows()) - { - return true; - } - IFileStore fileStore = EFS.getLocalFileSystem().getStore(path); - return fileStore.fetchInfo().getAttribute(EFS.ATTRIBUTE_EXECUTABLE); + return file.canExecute(); } } diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java index 04af32302..51cb7c273 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java @@ -199,10 +199,10 @@ public static boolean checkIfIdfSupportsSpaces() public static String getPythonExecutable() { - IPath pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON3_CMD, true); // look for python3 + IPath pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON3_CMD); // look for python3 if (pythonPath == null) { - pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON_CMD, true); // look for python + pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON_CMD); // look for python } if (pythonPath != null) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java index ce7cd2e51..5d6428fa9 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/wizard/pages/InstallPreRquisitePage.java @@ -161,7 +161,7 @@ public Composite getPageComposite() private void loadGitExecutablePath() { - IPath gitPath = new SystemExecutableFinder().find("git", true); //$NON-NLS-1$ + IPath gitPath = new SystemExecutableFinder().find("git"); //$NON-NLS-1$ Logger.log("GIT path:" + gitPath); //$NON-NLS-1$ if (gitPath != null) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java index 43f51c873..61451c697 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/update/AbstractToolsHandler.java @@ -69,7 +69,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException Logger.log("IDF_PATH :" + idfPath); //$NON-NLS-1$ // Look for git path - IPath gitPath = new SystemExecutableFinder().find("git", true); //$NON-NLS-1$ + IPath gitPath = new SystemExecutableFinder().find("git"); //$NON-NLS-1$ Logger.log("GIT path:" + gitPath); //$NON-NLS-1$ if (gitPath != null) { diff --git a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java index b0a7ea8a3..3d9511ad1 100644 --- a/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java +++ b/tests/com.espressif.idf.core.test/src/com/espressif/idf/core/unittest/ExecutableFinderTest.java @@ -140,21 +140,21 @@ public String getEnvExecutables() @Test void testWindowsFindReturnsExpectedResultOnFoundableExecutable() { - IPath foundExecutable = windowsExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + IPath foundExecutable = windowsExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING); assertEquals(foundableExecutableFile.toString(), foundExecutable.toOSString()); } @Test void testWindowsFindReturnsNullOnNonFoundableExecutable() { - IPath nonFoundableExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, true); + IPath nonFoundableExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING); assertNull(nonFoundableExecutable); } @Test void testWindowsFindReturnsNullOnNull() { - IPath nullPath = windowsExecutableFinder.find(null, true); + IPath nullPath = windowsExecutableFinder.find(null); assertNull(nullPath); } @@ -162,7 +162,7 @@ void testWindowsFindReturnsNullOnNull() void testWindowsFindReturnsNullWithEmpyPath() { ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(emptyPathSystemWrapper); - IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING); assertNull(foundNonExecutable); } @@ -170,43 +170,68 @@ void testWindowsFindReturnsNullWithEmpyPath() void testWindowsFindReturnsNullWithEmpyPathExt() { ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(emptyPathExtSystemWrapper); - IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING); assertNull(foundNonExecutable); } @Test void testWindowsFindReturnsNullOnFoundableNonExecutable() { - IPath foundNonExecutable = windowsExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + IPath foundNonExecutable = windowsExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING); assertNull(foundNonExecutable); } @Test void testWindowsFindReturnsNullOnNonFoundableNonExecutable() { - IPath nonFoudableNonExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, true); + IPath nonFoudableNonExecutable = windowsExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING); assertNull(nonFoudableNonExecutable); } + @Test + void testWindowsReturnsNullWhenSystemReturnsIncorrectExecutable() + { + SystemWrapper incorrectSystemWrapper = new SystemWrapper() + { + + @Override + public String getPathEnv() + { + return foundableTempDir.toString(); + } + + @Override + public String getEnvExecutables() + { + return "/EXE"; //$NON-NLS-1$ + } + }; + ExecutableFinder executableFinder = new TestableSystemExecutableFinderWindows(incorrectSystemWrapper); + + IPath foundExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING); + + assertNull(foundExecutable); + } + @Test void testUnixFindReturnsExpectedResultOnFoundableExecutable() { - IPath foundExecutable = unixExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + IPath foundExecutable = unixExecutableFinder.find(FOUNDABLE_EXE_FILE_STRING); assertEquals(unixFoundableExecutableFile.toString(), foundExecutable.toOSString()); } @Test void testUnixFindReturnsNullOnNonFoundableExecutable() { - IPath nonFoundableExecutable = unixExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING, true); + IPath nonFoundableExecutable = unixExecutableFinder.find(NON_FOUNDABLE_EXE_FILE_STRING); assertNull(nonFoundableExecutable); } @Test void testUnixFindReturnsNullOnNull() { - IPath nullPath = unixExecutableFinder.find(null, true); + IPath nullPath = unixExecutableFinder.find(null); assertNull(nullPath); } @@ -214,7 +239,7 @@ void testUnixFindReturnsNullOnNull() void testUnixFindReturnsNullWithEmpyPath() { ExecutableFinder executableFinder = new TestableSystemExecutableFinderUnix(emptyPathSystemWrapper); - IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_TEXT_FILE_STRING); assertNull(foundNonExecutable); } @@ -222,21 +247,21 @@ void testUnixFindReturnsNullWithEmpyPath() void testUnixFindReturnsExpectedResultWithEmpyPathExt() { ExecutableFinder executableFinder = new TestableSystemExecutableFinderUnix(emptyPathExtSystemWrapper); - IPath foundNonExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING, true); + IPath foundNonExecutable = executableFinder.find(FOUNDABLE_EXE_FILE_STRING); assertEquals(unixFoundableExecutableFile.toString(), foundNonExecutable.toOSString()); } @Test void testUnixFindReturnsNullOnFoundableNonExecutable() { - IPath foundNonExecutable = unixExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING, true); + IPath foundNonExecutable = unixExecutableFinder.find(FOUNDABLE_TEXT_FILE_STRING); assertNull(foundNonExecutable); } @Test void testUnixFindReturnsNullOnNonFoundableNonExecutable() { - IPath nonFoudableNonExecutable = unixExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING, true); + IPath nonFoudableNonExecutable = unixExecutableFinder.find(NON_FOUNDABLE_TEXT_FILE_STRING); assertNull(nonFoudableNonExecutable); } From 1fb96915020b6169c53350ee94bbf7d70cd5c329 Mon Sep 17 00:00:00 2001 From: Denys Almazov Date: Tue, 4 Jul 2023 17:19:14 +0300 Subject: [PATCH 12/12] build fix after merge --- tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF index e18a8902a..088fadb58 100644 --- a/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF +++ b/tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF @@ -67,6 +67,6 @@ Export-Package: com.espressif.idf.core.test, org.jmock.api, org.jmock.auto, org.jmock.lib -Import-Package: org.eclipse.core.runtime +Import-Package: org.eclipse.core.runtime, org.junit.jupiter.params, org.junit.jupiter.params.provider