use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestRunningState method patchExecutor.
@Override
protected GoExecutor patchExecutor(@NotNull GoExecutor executor) throws ExecutionException {
executor.withParameters("test", "-v");
executor.withParameterString(myConfiguration.getGoToolParams());
switch(myConfiguration.getKind()) {
case DIRECTORY:
String relativePath = FileUtil.getRelativePath(myConfiguration.getWorkingDirectory(), myConfiguration.getDirectoryPath(), File.separatorChar);
// TODO Once Go gets support for covering multiple packages the ternary condition should be reverted
// See https://golang.org/issues/6909
String pathSuffix = myCoverageFilePath == null ? "..." : ".";
if (relativePath != null && !".".equals(relativePath)) {
executor.withParameters("./" + relativePath + "/" + pathSuffix);
} else {
executor.withParameters("./" + pathSuffix);
executor.withWorkDirectory(myConfiguration.getDirectoryPath());
}
addFilterParameter(executor, ObjectUtils.notNull(myFailedTestsPattern, myConfiguration.getPattern()));
break;
case PACKAGE:
executor.withParameters(myConfiguration.getPackage());
addFilterParameter(executor, ObjectUtils.notNull(myFailedTestsPattern, myConfiguration.getPattern()));
break;
case FILE:
String filePath = myConfiguration.getFilePath();
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(filePath);
if (virtualFile == null) {
throw new ExecutionException("Test file doesn't exist");
}
PsiFile file = PsiManager.getInstance(myConfiguration.getProject()).findFile(virtualFile);
if (file == null || !GoTestFinder.isTestFile(file)) {
throw new ExecutionException("File '" + filePath + "' is not test file");
}
String importPath = ((GoFile) file).getImportPath(false);
if (StringUtil.isEmpty(importPath)) {
throw new ExecutionException("Cannot find import path for " + filePath);
}
executor.withParameters(importPath);
addFilterParameter(executor, myFailedTestsPattern != null ? myFailedTestsPattern : buildFilterPatternForFile((GoFile) file));
break;
}
if (myCoverageFilePath != null) {
executor.withParameters("-coverprofile=" + myCoverageFilePath, "-covermode=atomic");
}
return executor;
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestRunConfigurationProducerBase method isConfigurationFromContext.
@Override
public boolean isConfigurationFromContext(@NotNull GoTestRunConfiguration configuration, ConfigurationContext context) {
PsiElement contextElement = GoRunUtil.getContextElement(context);
if (contextElement == null)
return false;
Module module = ModuleUtilCore.findModuleForPsiElement(contextElement);
if (!Comparing.equal(module, configuration.getConfigurationModule().getModule()))
return false;
if (!Comparing.equal(myFramework, configuration.getTestFramework()))
return false;
PsiFile file = contextElement.getContainingFile();
switch(configuration.getKind()) {
case DIRECTORY:
if (contextElement instanceof PsiDirectory) {
String directoryPath = ((PsiDirectory) contextElement).getVirtualFile().getPath();
return FileUtil.pathsEqual(configuration.getDirectoryPath(), directoryPath) && FileUtil.pathsEqual(configuration.getWorkingDirectory(), directoryPath);
}
case PACKAGE:
if (!GoTestFinder.isTestFile(file))
return false;
if (!Comparing.equal(((GoFile) file).getImportPath(false), configuration.getPackage()))
return false;
if (GoRunUtil.isPackageContext(contextElement) && configuration.getPattern().isEmpty())
return true;
GoFunctionOrMethodDeclaration contextFunction = findTestFunctionInContext(contextElement);
return contextFunction != null && myFramework.isAvailableOnFunction(contextFunction) ? configuration.getPattern().equals("^" + contextFunction.getName() + "$") : configuration.getPattern().isEmpty();
case FILE:
GoFunctionOrMethodDeclaration contextTestFunction = findTestFunctionInContext(contextElement);
return contextTestFunction == null && GoTestFinder.isTestFile(file) && FileUtil.pathsEqual(configuration.getFilePath(), file.getVirtualFile().getPath());
}
return false;
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestRunConfigurationProducerBase method setupConfigurationFromContext.
@Override
protected boolean setupConfigurationFromContext(@NotNull GoTestRunConfiguration configuration, ConfigurationContext context, Ref sourceElement) {
PsiElement contextElement = GoRunUtil.getContextElement(context);
if (contextElement == null) {
return false;
}
Module module = ModuleUtilCore.findModuleForPsiElement(contextElement);
Project project = contextElement.getProject();
if (module == null || !GoSdkService.getInstance(project).isGoModule(module))
return false;
if (!myFramework.isAvailable(module))
return false;
configuration.setModule(module);
configuration.setTestFramework(myFramework);
if (contextElement instanceof PsiDirectory) {
configuration.setName(getPackageConfigurationName(((PsiDirectory) contextElement).getName()));
configuration.setKind(GoTestRunConfiguration.Kind.DIRECTORY);
String directoryPath = ((PsiDirectory) contextElement).getVirtualFile().getPath();
configuration.setDirectoryPath(directoryPath);
configuration.setWorkingDirectory(directoryPath);
return true;
}
PsiFile file = contextElement.getContainingFile();
if (myFramework.isAvailableOnFile(file)) {
String importPath = ((GoFile) file).getImportPath(false);
if (GoRunUtil.isPackageContext(contextElement) && StringUtil.isNotEmpty(importPath)) {
configuration.setKind(GoTestRunConfiguration.Kind.PACKAGE);
configuration.setPackage(importPath);
configuration.setName(getPackageConfigurationName(importPath));
return true;
} else {
GoFunctionOrMethodDeclaration function = findTestFunctionInContext(contextElement);
if (function != null) {
if (myFramework.isAvailableOnFunction(function)) {
configuration.setName(getFunctionConfigurationName(function, file.getName()));
configuration.setPattern("^" + function.getName() + "$");
configuration.setKind(GoTestRunConfiguration.Kind.PACKAGE);
configuration.setPackage(StringUtil.notNullize(((GoFile) file).getImportPath(false)));
return true;
}
} else if (hasSupportedFunctions((GoFile) file)) {
configuration.setName(getFileConfigurationName(file.getName()));
configuration.setKind(GoTestRunConfiguration.Kind.FILE);
configuration.setFilePath(file.getVirtualFile().getPath());
return true;
}
}
}
return false;
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoRunConfigurationWithMain method checkFileConfiguration.
protected void checkFileConfiguration() throws RuntimeConfigurationError {
VirtualFile file = findFile(getFilePath());
if (file == null) {
throw new RuntimeConfigurationError("Main file is not specified");
}
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
if (!(psiFile instanceof GoFile)) {
throw new RuntimeConfigurationError("Main file is invalid");
}
if (!GoRunUtil.isMainGoFile(psiFile)) {
throw new RuntimeConfigurationError("Main file has non-main package or doesn't contain main function");
}
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoPackageUtil method getAllPackageFiles.
@NotNull
public static List<GoFile> getAllPackageFiles(@Nullable PsiDirectory directory, @Nullable String packageName) {
if (directory == null) {
return Collections.emptyList();
}
PsiElement[] children = directory.getChildren();
List<GoFile> files = ContainerUtil.newArrayListWithCapacity(children.length);
for (PsiElement element : children) {
if (element instanceof GoFile && (packageName == null || Comparing.equal(((GoFile) element).getPackageName(), packageName))) {
files.add((GoFile) element);
}
}
return files;
}
Aggregations