Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IEP-946 Test coverage for ExecutableFinder #759

Merged
merged 14 commits into from
Jul 5, 2023
Merged
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -1,106 +1,12 @@
/*******************************************************************************
* 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
alirana01 marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*******************************************************************************
* 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 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;
}
sigmaaa marked this conversation as resolved.
Show resolved Hide resolved

@Override
public IPath find(String executableName)
{
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);
if (execPath != null)
{
return execPath;
}
}
return null;
}

private IPath findExecutable(IPath path)
{
if (isPlatformWindows())
{
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;
}

// seam for testing
protected boolean isPlatformWindows()
{
return Platform.OS_WIN32.equals(Platform.getOS());
}

private boolean isExecutable(IPath path)
{
File file = path.toFile();

if (file == null || !file.exists() || file.isDirectory())
{
return false;
}

return file.canExecute();
}
}
Original file line number Diff line number Diff line change
@@ -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
alirana01 marked this conversation as resolved.
Show resolved Hide resolved
{
public String getPathEnv();
public String getEnvExecutables();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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); // look for python3
if (pythonPath == null)
{
pythonPath = ExecutableFinder.find(IDFConstants.PYTHON_CMD, true); // look for python
pythonPath = new SystemExecutableFinder().find(IDFConstants.PYTHON_CMD); // look for python
}
if (pythonPath != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"); //$NON-NLS-1$
Logger.log("GIT path:" + gitPath); //$NON-NLS-1$
if (gitPath != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"); //$NON-NLS-1$
Logger.log("GIT path:" + gitPath); //$NON-NLS-1$
if (gitPath != null)
{
Expand Down
5 changes: 3 additions & 2 deletions tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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-ClassPath: .,
lib/jmock-2.12.0.jar,
lib/commons-collections4-4.4.jar,
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
*******************************************************************************/
package com.espressif.idf.core.test;
Copy link
Collaborator Author

@sigmaaa sigmaaa May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we can move this class to unittest package and leave the test package for other kinds of tests


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;
Expand All @@ -22,18 +25,19 @@
* @author Kondal Kolipaka <kondal.kolipaka@espressif.com>
*
*/
@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;
Expand Down
Loading