Skip to content

Commit

Permalink
Fix CI (#492)
Browse files Browse the repository at this point in the history
closes #491
  • Loading branch information
mzarnowski authored Feb 12, 2020
1 parent b001d96 commit d095b8b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,41 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.MapDataContext;
import com.twitter.intellij.pants.PantsManager;
import com.twitter.intellij.pants.util.ProjectTestJvms;
import com.twitter.intellij.pants.service.task.PantsTaskManager;
import com.twitter.intellij.pants.settings.PantsExecutionSettings;
import com.twitter.intellij.pants.testFramework.OSSPantsIntegrationTest;
import com.twitter.intellij.pants.util.PantsUtil;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class OSSPantsJvmRunConfigurationIntegrationTest extends OSSPantsIntegrationTest {
public void testClassRunConfiguration() throws Throwable {
doImport("testprojects/tests/java/org/pantsbuild/testproject/testjvms");

String classReference = "org.pantsbuild.testproject.testjvms.TestSix";

PsiClass testClass = JavaPsiFacade.getInstance(myProject).findClass(classReference, GlobalSearchScope.allScope(myProject));
assertNotNull(testClass);
PsiClass testClass = ProjectTestJvms.anyTestClass(myProject, getProjectPath());

ExternalSystemRunConfiguration esc = getExternalSystemRunConfiguration(testClass);

// Make sure task name is `test` goal.
assertEquals(Collections.singletonList("test"), esc.getSettings().getTaskNames());
String testTask = "test";
assertEquals(Collections.singletonList(testTask), esc.getSettings().getTaskNames());

List<String> configScriptParameters = PantsUtil.parseCmdParameters(Optional.ofNullable(esc.getSettings().getScriptParameters()));

List<String> expectedConfigScriptParameters = Arrays.asList(
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:eight-test-platform",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:six",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:seven",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:eight",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:base",
"--test-junit-test=" + classReference
);
assertEquals(expectedConfigScriptParameters, configScriptParameters);
assertContains(configScriptParameters, "--test-junit-test=" + testClass.getQualifiedName());

// Make sure this configuration does not contain any task before running if added to the project.
assertEmptyBeforeRunTask(esc);
Expand All @@ -76,34 +69,21 @@ public void testClassRunConfiguration() throws Throwable {

String debuggerSetup = "dummy_debugger_setup";

GeneralCommandLine finalDebugCommandline = getFinalCommandline(esc, debuggerSetup, taskManagerClass);
List<String> debugParameters = Arrays.asList("--no-test-junit-timeouts", "--jvm-test-junit-options=" + debuggerSetup, "test");
List<String> expectedDebugParameters = Stream.of(debugParameters, configScriptParameters)
.flatMap(Collection::stream)
.collect(Collectors.toList());

List<String> expectedFinalDebugCommandlineParameters = Arrays.asList(
"--no-test-junit-timeouts",
"--jvm-test-junit-options=" + debuggerSetup,
"test",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:eight-test-platform",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:six",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:seven",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:eight",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:base",
"--test-junit-test=" + classReference
);
GeneralCommandLine finalDebugCommandline = getFinalCommandline(esc, debuggerSetup, taskManagerClass);
assertEquals(expectedDebugParameters, finalDebugCommandline.getParametersList().getParameters());

assertEquals(expectedFinalDebugCommandlineParameters, finalDebugCommandline.getParametersList().getParameters());
List<String> runParameters = Collections.singletonList("test");
List<String> expectedRunParameters = Stream.of(runParameters, configScriptParameters)
.flatMap(Collection::stream)
.collect(Collectors.toList());

GeneralCommandLine finalRunCommandline = getFinalCommandline(esc, null, taskManagerClass);

List<String> expectedFinalRunCommandlineParameters = Arrays.asList(
"test",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:eight-test-platform",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:six",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:seven",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:eight",
"testprojects/tests/java/org/pantsbuild/testproject/testjvms:base",
"--test-junit-test=" + classReference
);
assertEquals(expectedFinalRunCommandlineParameters, finalRunCommandline.getParametersList().getParameters());
assertEquals(expectedRunParameters, finalRunCommandline.getParametersList().getParameters());
}

@NotNull
Expand All @@ -127,20 +107,19 @@ private GeneralCommandLine getFinalCommandline(
public void testMethodRunConfiguration() throws Throwable {
doImport("testprojects/tests/java/org/pantsbuild/testproject/testjvms");

String classReference = "org.pantsbuild.testproject.testjvms.TestSix";
String methodName = "testSix";
PsiClass testClass = ProjectTestJvms.testClasses(myProject, getProjectPath())
.findFirst()
.orElseThrow(() -> new IllegalStateException("Couldn't find a test class"));

PsiClass testClass = JavaPsiFacade.getInstance(myProject).findClass(classReference, GlobalSearchScope.allScope(myProject));
assertNotNull(testClass);
PsiMethod[] testMethods = testClass.findMethodsByName(methodName, false);
assertEquals(testMethods.length, 1);
PsiMethod testMethod = testMethods[0];
assertNotNull(testMethod);
PsiMethod testMethod = Arrays.stream(testClass.getMethods())
.filter(m -> Arrays.stream(m.getAnnotations()).anyMatch(a -> a.getQualifiedName().equals("org.junit.Test")))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No method annotated with @org.junit.Test"));

ExternalSystemRunConfiguration esc = getExternalSystemRunConfiguration(testMethod);

Set<String> items = new HashSet<>(Arrays.asList(esc.getSettings().getScriptParameters().split(" ")));
assertTrue(items.contains("--test-junit-test=" + classReference + "#" + methodName));
assertContains(items, "--test-junit-test=" + testClass.getQualifiedName() + "#" + testMethod.getName());
}

public void testModuleRunConfiguration() throws Throwable {
Expand All @@ -151,10 +130,13 @@ public void testModuleRunConfiguration() throws Throwable {

ExternalSystemRunConfiguration esc = getExternalSystemRunConfiguration(testPackage.getDirectories()[0]);

Set<String> expectedItems = ProjectTestJvms.testClasses(myProject, getProjectPath())
.map(aClass -> "--test-junit-test=" + aClass.getQualifiedName())
.collect(Collectors.toSet());
assertNotEmpty(expectedItems);

Set<String> items = new HashSet<>(Arrays.asList(esc.getSettings().getScriptParameters().split(" ")));
assertTrue(items.contains("--test-junit-test=org.pantsbuild.testproject.testjvms.TestSix"));
assertTrue(items.contains("--test-junit-test=org.pantsbuild.testproject.testjvms.TestSeven"));
assertTrue(items.contains("--test-junit-test=org.pantsbuild.testproject.testjvms.TestEight"));
assertContains(items, expectedItems);
}

@NotNull
Expand Down Expand Up @@ -187,4 +169,21 @@ private ConfigurationContext createContext(@NotNull PsiElement psiClass, @NotNul
dataContext.put(Location.DATA_KEY, PsiLocation.fromPsiElement(psiClass));
return ConfigurationContext.getFromContext(dataContext);
}

private void assertContains(Collection<String> collection, String expected) {
assertContains(collection, Collections.singleton(expected));
}

private void assertContains(Collection<String> collection, Collection<String> expectedElements) {
Set<String> missing = expectedElements.stream()
.filter(expected -> !collection.contains(expected))
.collect(Collectors.toSet());

if (!missing.isEmpty()) {
String actual = collection.stream().collect(Collectors.joining(",", "[", "]"));
String expected = missing.stream().collect(Collectors.joining(",", "[", "]"));
String message = "Elements missing from " + actual + ": " + expected;
Assert.fail(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.indexing.FileBasedIndex;
import com.twitter.intellij.pants.index.PantsAddressesIndex;
import com.twitter.intellij.pants.index.PantsTargetIndex;
import com.twitter.intellij.pants.testFramework.OSSPantsIntegrationTest;
import com.twitter.intellij.pants.util.PantsConstants;
import com.twitter.intellij.pants.util.PantsUtil;

import java.util.LinkedList;
Expand Down Expand Up @@ -86,10 +91,20 @@ public void testJavaLibCompletions() {
completionTest(toComplete, expected);
}

private void invalidateCaches() {
PropertiesComponent.getInstance().unsetValue(PantsConstants.PANTS_AVAILABLE_TARGETS_KEY);

// NOTE: FileBasedIndex.invalidateCaches method does not clear the pants addresses index
FileBasedIndex.getInstance().requestRebuild(PantsAddressesIndex.NAME);
FileBasedIndex.getInstance().requestRebuild(PantsTargetIndex.NAME);
}

private void completionTest(String stringToComplete, String[] expected) {
String fullStringToComplete = "\n\n" + stringToComplete;
// should be only tested with pants versions above 1.24.0
if (PantsUtil.isCompatiblePantsVersion(myProjectRoot.getPath(), "1.24.0")) {
invalidateCaches();

String helloProjectPath = "examples/src/scala/org/pantsbuild/example/hello/";
doImport(helloProjectPath);
VirtualFile vfile = myProjectRoot.findFileByRelativePath(helloProjectPath + "BUILD");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import com.twitter.intellij.pants.util.ProjectTestJvms;
import com.twitter.intellij.pants.testFramework.OSSPantsIntegrationTest;
import gnu.trove.THashMap;

Expand All @@ -21,16 +20,15 @@ public class FilePathRelativeToBuiltRootMacroTest extends OSSPantsIntegrationTes
public void testFilePathRelativeMacro() throws Throwable {
doImport("testprojects/tests/java/org/pantsbuild/testproject/testjvms");

String classReference = "org.pantsbuild.testproject.testjvms.TestSix";
PsiClass testClass = JavaPsiFacade.getInstance(myProject).findClass(classReference, GlobalSearchScope.allScope(myProject));
assertNotNull(testClass);
PsiClass testClass = ProjectTestJvms.anyTestClass(myProject, getProjectPath());

String githubRepo = "https://github.com/pantsbuild/pants/blob/master";
String expected = String.format("%s/testprojects/tests/java/%s.java", githubRepo, testClass.getQualifiedName().replace('.', '/'));

// fileSelected would be testprojects/tests/java/org/pantsbuild/testproject/testjvms/TestSix.java
VirtualFile fileSelected = testClass.getContainingFile().getVirtualFile();
String actual = MacroManager.getInstance()
.expandMacrosInString("https://github.com/pantsbuild/pants/blob/master/$FileRelativePath$", false, getFakeContext(fileSelected));
assertEquals(
"https://github.com/pantsbuild/pants/blob/master/testprojects/tests/java/org/pantsbuild/testproject/testjvms/TestSix.java", actual);
String actual = MacroManager.getInstance().expandMacrosInString(githubRepo + "/$FileRelativePath$", false, getFakeContext(fileSelected));

assertEquals(expected, actual);
}

private DataContext getFakeContext(VirtualFile file) {
Expand Down
40 changes: 40 additions & 0 deletions tests/com/twitter/intellij/pants/util/ProjectTestJvms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package com.twitter.intellij.pants.util;

import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public final class ProjectTestJvms {
private static final String PACKAGE_NAME = "org.pantsbuild.testproject.testjvms";

public static PsiClass anyTestClass(Project project, String projectPath) throws IOException {
return testClasses(project, projectPath)
.findFirst()
.orElseThrow(() -> new IllegalStateException("Couldn't find a test class"));
}

public static Stream<PsiClass> testClasses(Project project, String projectPath) throws IOException {
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
return testClassFiles(projectPath)
.map(path -> path.getFileName().toString())
.map(name -> name.substring(0, name.length() - ".java".length()))
.map(name -> PACKAGE_NAME + "." + name)
.map(qualifiedName -> psiFacade.findClass(qualifiedName, GlobalSearchScope.allScope(project)));
}

private static Stream<Path> testClassFiles(String projectPath) throws IOException {
return Files.list(Paths.get(projectPath))
.filter(path -> path.getFileName().toString().endsWith(".java"))
.filter(path -> !path.getFileName().toString().equals("TestBase.java"));
}
}

0 comments on commit d095b8b

Please sign in to comment.