Search in sources :

Example 21 with GoFile

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoFile(com.goide.psi.GoFile) PsiFile(com.intellij.psi.PsiFile) ExecutionException(com.intellij.execution.ExecutionException)

Example 22 with GoFile

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;
}
Also used : GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 23 with GoFile

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;
}
Also used : Project(com.intellij.openapi.project.Project) GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 24 with GoFile

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");
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoFile(com.goide.psi.GoFile) PsiFile(com.intellij.psi.PsiFile) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError)

Example 25 with GoFile

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;
}
Also used : GoFile(com.goide.psi.GoFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GoFile (com.goide.psi.GoFile)31 PsiFile (com.intellij.psi.PsiFile)20 PsiElement (com.intellij.psi.PsiElement)12 Project (com.intellij.openapi.project.Project)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiDirectory (com.intellij.psi.PsiDirectory)8 Module (com.intellij.openapi.module.Module)6 NotNull (org.jetbrains.annotations.NotNull)6 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)4 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)4 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)4 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)4 WorkspaceLanguageSettings (com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings)4 WorkspacePathResolverImpl (com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl)4 Nullable (javax.annotation.Nullable)4 Test (org.junit.Test)4 GoPackageClause (com.goide.psi.GoPackageClause)3 Nullable (org.jetbrains.annotations.Nullable)3 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)2 GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)2