Search in sources :

Example 1 with Location

use of com.intellij.execution.Location in project kotlin by JetBrains.

the class KotlinTestNgConfigurationProducer method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(TestNGConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    // TODO: check TestNG Pattern running first, before method/class (see TestNGInClassConfigurationProducer for logic)
    // TODO: and PsiClassOwner not handled, which is in TestNGInClassConfigurationProducer
    Location location = context.getLocation();
    if (location == null) {
        return false;
    }
    Project project = context.getProject();
    PsiElement leaf = location.getPsiElement();
    if (!ProjectRootsUtil.isInProjectOrLibSource(leaf)) {
        return false;
    }
    if (!(leaf.getContainingFile() instanceof KtFile)) {
        return false;
    }
    KtFile ktFile = (KtFile) leaf.getContainingFile();
    if (TargetPlatformDetector.getPlatform(ktFile) != JvmPlatform.INSTANCE) {
        return false;
    }
    KtNamedDeclaration declarationToRun = getDeclarationToRun(leaf);
    if (declarationToRun instanceof KtNamedFunction) {
        KtNamedFunction function = (KtNamedFunction) declarationToRun;
        @SuppressWarnings("unchecked") KtElement owner = PsiTreeUtil.getParentOfType(function, KtFunction.class, KtClass.class);
        if (owner instanceof KtClass) {
            PsiClass delegate = toLightClass((KtClass) owner);
            if (delegate != null) {
                for (PsiMethod method : delegate.getMethods()) {
                    if (method.getNavigationElement() == function) {
                        if (TestNGUtil.hasTest(method)) {
                            return configure(configuration, location, context, project, delegate, method);
                        }
                        break;
                    }
                }
            }
        }
    }
    if (declarationToRun instanceof KtClass) {
        PsiClass delegate = toLightClass((KtClassOrObject) declarationToRun);
        if (!isTestNGClass(delegate)) {
            return false;
        }
        return configure(configuration, location, context, project, delegate, null);
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) PsiLocation(com.intellij.execution.PsiLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 2 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class PyCCRunTestsConfigurationProducer method getTestPath.

@Nullable
private static String getTestPath(@NotNull ConfigurationContext context) {
    Location location = context.getLocation();
    if (location == null) {
        return null;
    }
    VirtualFile file = location.getVirtualFile();
    if (file == null) {
        return null;
    }
    VirtualFile taskDir = StudyUtils.getTaskDir(file);
    if (taskDir == null) {
        return null;
    }
    Task task = StudyUtils.getTask(location.getProject(), taskDir);
    if (task == null) {
        return null;
    }
    String testsFileName = PyCCLanguageManager.getSubtaskTestsFileName(task.getActiveSubtaskIndex());
    String taskDirPath = FileUtil.toSystemDependentName(taskDir.getPath());
    String testsPath = taskDir.findChild(EduNames.SRC) != null ? FileUtil.join(taskDirPath, EduNames.SRC, testsFileName) : FileUtil.join(taskDirPath, testsFileName);
    String filePath = FileUtil.toSystemDependentName(file.getPath());
    return filePath.equals(testsPath) ? testsPath : null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.jetbrains.edu.learning.courseFormat.Task) Location(com.intellij.execution.Location) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class PythonTestLegacyConfigurationProducer method isConfigurationFromContext.

@Override
public boolean isConfigurationFromContext(AbstractPythonLegacyTestRunConfiguration configuration, ConfigurationContext context) {
    final Location location = context.getLocation();
    if (location == null || !isAvailable(location))
        return false;
    final PsiElement element = location.getPsiElement();
    final PsiFileSystemItem file = element.getContainingFile();
    if (file == null)
        return false;
    final VirtualFile virtualFile = element instanceof PsiDirectory ? ((PsiDirectory) element).getVirtualFile() : file.getVirtualFile();
    if (virtualFile == null)
        return false;
    final PyFunction pyFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class, false);
    final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    final AbstractPythonLegacyTestRunConfiguration.TestType confType = configuration.getTestType();
    final String workingDirectory = configuration.getWorkingDirectory();
    if (element instanceof PsiDirectory) {
        final String path = ((PsiDirectory) element).getVirtualFile().getPath();
        return confType == AbstractPythonLegacyTestRunConfiguration.TestType.TEST_FOLDER && path.equals(configuration.getFolderName()) || path.equals(new File(workingDirectory, configuration.getFolderName()).getAbsolutePath());
    }
    final String scriptName = configuration.getScriptName();
    final String path = virtualFile.getPath();
    final boolean isTestFileEquals = scriptName.equals(path) || path.equals(new File(workingDirectory, scriptName).getAbsolutePath());
    if (pyFunction != null) {
        final String methodName = configuration.getMethodName();
        if (pyFunction.getContainingClass() == null) {
            return confType == AbstractPythonLegacyTestRunConfiguration.TestType.TEST_FUNCTION && methodName.equals(pyFunction.getName()) && isTestFileEquals;
        } else {
            final String className = configuration.getClassName();
            return confType == AbstractPythonLegacyTestRunConfiguration.TestType.TEST_METHOD && methodName.equals(pyFunction.getName()) && pyClass != null && className.equals(pyClass.getName()) && isTestFileEquals;
        }
    }
    if (pyClass != null) {
        final String className = configuration.getClassName();
        return confType == AbstractPythonLegacyTestRunConfiguration.TestType.TEST_CLASS && className.equals(pyClass.getName()) && isTestFileEquals;
    }
    return confType == AbstractPythonLegacyTestRunConfiguration.TestType.TEST_SCRIPT && isTestFileEquals;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Location(com.intellij.execution.Location)

Example 4 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class PyToxTestLocator method getLocation.

@NotNull
@Override
public List<Location> getLocation(@NotNull final String protocol, @NotNull final String path, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    final PsiFile file = PyElementGenerator.getInstance(project).createDummyFile(LanguageLevel.PYTHON24, DUMMY_FILE_PADDING);
    file.putUserData(ENV_NAME_KEY, path);
    @SuppressWarnings("unchecked") final List<Location> locations = Collections.singletonList(new PsiLocation(file));
    return locations;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiLocation(com.intellij.execution.PsiLocation) PsiLocation(com.intellij.execution.PsiLocation) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class PyTestConfigurationProducer method isConfigurationFromContext.

@Override
public boolean isConfigurationFromContext(AbstractPythonLegacyTestRunConfiguration configuration, ConfigurationContext context) {
    final Location location = context.getLocation();
    if (location == null)
        return false;
    if (!(configuration instanceof PyTestRunConfiguration))
        return false;
    final PsiElement element = location.getPsiElement();
    final PsiFileSystemItem file = element instanceof PsiDirectory ? (PsiDirectory) element : element.getContainingFile();
    if (file == null)
        return false;
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null)
        return false;
    if (file instanceof PyFile || file instanceof PsiDirectory) {
        final List<PyStatement> testCases = PyTestUtil.getPyTestCasesFromFile(file, TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()));
        if (testCases.isEmpty())
            return false;
    } else {
        return false;
    }
    final Sdk sdk = PythonSdkType.findPythonSdk(context.getModule());
    if (sdk == null)
        return false;
    final String keywords = getKeywords(element, sdk);
    final String scriptName = ((PyTestRunConfiguration) configuration).getTestToRun();
    final String workingDirectory = configuration.getWorkingDirectory();
    final String path = virtualFile.getPath();
    final boolean isTestFileEquals = scriptName.equals(path) || path.equals(new File(workingDirectory, scriptName).getAbsolutePath());
    final String configurationKeywords = ((PyTestRunConfiguration) configuration).getKeywords();
    return isTestFileEquals && (configurationKeywords.equals(keywords) || StringUtil.isEmptyOrSpaces(((PyTestRunConfiguration) configuration).getKeywords()) && keywords == null);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFile(com.jetbrains.python.psi.PyFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PyStatement(com.jetbrains.python.psi.PyStatement) PsiDirectory(com.intellij.psi.PsiDirectory) Sdk(com.intellij.openapi.projectRoots.Sdk) PyFile(com.jetbrains.python.psi.PyFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) Location(com.intellij.execution.Location)

Aggregations

Location (com.intellij.execution.Location)79 PsiClass (com.intellij.psi.PsiClass)27 PsiElement (com.intellij.psi.PsiElement)26 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 PsiMethod (com.intellij.psi.PsiMethod)19 PsiLocation (com.intellij.execution.PsiLocation)18 Project (com.intellij.openapi.project.Project)18 PsiMemberParameterizedLocation (com.intellij.execution.junit2.PsiMemberParameterizedLocation)17 MethodLocation (com.intellij.execution.junit2.info.MethodLocation)16 Module (com.intellij.openapi.module.Module)14 Nullable (org.jetbrains.annotations.Nullable)9 PsiFile (com.intellij.psi.PsiFile)8 ArrayList (java.util.ArrayList)8 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)7 SMTestProxy (com.intellij.execution.testframework.sm.runner.SMTestProxy)6 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (javax.annotation.Nullable)5 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)4 PatternConfigurationProducer (com.intellij.execution.junit.PatternConfigurationProducer)4 Kind (com.google.idea.blaze.base.model.primitives.Kind)3