Skip to content

Commit 0402c4a

Browse files
committed
moved sandbox initialization to the test properties
1 parent 750b618 commit 0402c4a

3 files changed

Lines changed: 98 additions & 110 deletions

File tree

clwb/tests/headlesstests/com/google/idea/blaze/clwb/base/ClwbHeadlessTestCase.java

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,19 @@
1717
package com.google.idea.blaze.clwb.base;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static com.google.idea.testing.headless.Assertions.abort;
2120

2221
import com.google.idea.testing.headless.HeadlessTestCase;
2322
import com.intellij.codeInsight.CodeInsightSettings;
24-
import com.intellij.openapi.application.Application;
25-
import com.intellij.openapi.application.PathManager;
2623
import com.intellij.testFramework.HeavyPlatformTestCase;
2724
import com.jetbrains.cidr.lang.CLanguageKind;
2825
import com.jetbrains.cidr.lang.OCLanguageKind;
2926
import com.jetbrains.cidr.lang.workspace.OCCompilerSettings;
3027
import com.jetbrains.cidr.lang.workspace.OCResolveConfiguration;
3128
import com.jetbrains.cidr.lang.workspace.OCWorkspace;
32-
import java.io.IOException;
33-
import java.nio.file.Files;
34-
import java.nio.file.Path;
3529
import java.util.ArrayList;
3630

3731
public abstract class ClwbHeadlessTestCase extends HeadlessTestCase {
3832

39-
@Override
40-
protected void setUp() throws Exception {
41-
// Must run before super.setUp(): on 253 rider access <home>/lib during app
42-
// load (before setUpProject would get a chance to run).
43-
final var sdkRoot = findSdkRoot();
44-
linkSandboxDir(sdkRoot, "bin", Path.of(PathManager.getBinPath()));
45-
linkSandboxDir(sdkRoot, "lib", Path.of(PathManager.getLibPath()));
46-
47-
super.setUp();
48-
}
49-
5033
@Override
5134
protected void setUpProject() throws Exception {
5235
super.setUpProject();
@@ -67,31 +50,6 @@ protected void tearDown() throws Exception {
6750
super.tearDown();
6851
}
6952

70-
private static Path findSdkRoot() {
71-
// app.jar lives at <sdk_root>/lib/app.jar; PathManager.getJarPathForClass
72-
// works without the application being initialized.
73-
final var appJar = PathManager.getJarPathForClass(Application.class);
74-
assertThat(appJar).isNotNull();
75-
76-
final var libDir = Path.of(appJar).getParent();
77-
assertThat(libDir).isNotNull();
78-
assertThat(libDir.getFileName().toString()).isEqualTo("lib");
79-
80-
return libDir.getParent();
81-
}
82-
83-
private static void linkSandboxDir(Path sdkRoot, String dirName, Path link) {
84-
final var target = sdkRoot.resolve(dirName);
85-
assertExists(target.toFile());
86-
87-
try {
88-
Files.deleteIfExists(link);
89-
Files.createSymbolicLink(link, target);
90-
} catch (IOException e) {
91-
abort("could not create " + dirName + " path symlink", e);
92-
}
93-
}
94-
9553
protected void addAllowedVfsRoots(ArrayList<AllowedVfsRoot> roots) { }
9654

9755
protected OCWorkspace getWorkspace() {

clwb/tests/integrationtests/com/google/idea/blaze/clwb/CoptsProcessorTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.google.common.truth.Truth.assertThat
1919
import com.google.idea.blaze.clwb.base.ExecutionRootPathResolverStub
2020
import com.google.common.collect.ImmutableList
2121
import com.google.idea.blaze.cpp.copts.CoptsProcessor
22+
import com.intellij.codeInsight.CodeInsightSettings
2223
import com.intellij.testFramework.fixtures.BasePlatformTestCase
2324
import com.jetbrains.cidr.lang.workspace.compiler.ClangCompilerKind
2425
import com.jetbrains.cidr.lang.workspace.compiler.ClangClCompilerKind
@@ -39,6 +40,14 @@ private val ALL_COMPILER = listOf(ClangCompilerKind, GCCCompilerKind, ClangClCom
3940
@RunWith(JUnit4::class)
4041
class CoptsProcessorTest : BasePlatformTestCase() {
4142

43+
override fun setUp() {
44+
super.setUp()
45+
46+
// RadInitialConfigurator (clion-radler) flips AUTO_POPUP_JAVADOC_INFO on the
47+
// first launch. The tearDown checks compare against default settings.
48+
setDefaultCodeInsightSettings(CodeInsightSettings.getInstance())
49+
}
50+
4251
private fun doTest(
4352
compilers: List<OCCompilerKind>,
4453
copts: List<String>,

testing/src/com/google/idea/testing/BlazeTestSystemProperties.java

Lines changed: 89 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -34,90 +34,50 @@ class BlazeTestSystemProperties {
3434

3535
private BlazeTestSystemProperties() {}
3636

37-
/** The absolute path to the runfiles directory. */
38-
private static final String RUNFILES_PATH = TestUtils.getUserValue("TEST_SRCDIR");
39-
4037
/** Sets up the necessary system properties for running IntelliJ tests via blaze/bazel. */
4138
public static void configureSystemProperties() {
42-
File sandbox = new File(TestUtils.getTmpDirFile(), "_intellij_test_sandbox");
39+
final var sandbox = new File(TestUtils.getTmpDirFile(), "_intellij_test_sandbox");
40+
41+
redirectIdePathsToSandbox(sandbox);
42+
linkSdkDirectoriesIntoSandbox();
43+
configurePluginCompatibility();
44+
relaxFileSystemChecks();
45+
46+
setIfEmpty("idea.classpath.index.enabled", "false");
47+
setIfEmpty("idea.force.use.core.classloader", "true");
48+
}
4349

50+
/** Points all IDE state (config, caches, logs, preferences, home) at the test sandbox. */
51+
private static void redirectIdePathsToSandbox(File sandbox) {
4452
setSandboxPath("idea.home.path", new File(sandbox, "home"));
4553
setSandboxPath("idea.config.path", new File(sandbox, "config"));
4654
setSandboxPath("idea.system.path", new File(sandbox, "system"));
47-
String testUndeclaredOutputsDir = System.getenv("TEST_UNDECLARED_OUTPUTS_DIR");
55+
56+
final var testUndeclaredOutputsDir = System.getenv("TEST_UNDECLARED_OUTPUTS_DIR");
4857
if (testUndeclaredOutputsDir != null) {
4958
setSandboxPath("idea.log.path", new File(testUndeclaredOutputsDir, "logs"));
5059
}
5160

5261
setSandboxPath("java.util.prefs.userRoot", new File(sandbox, "userRoot"));
5362
setSandboxPath("java.util.prefs.systemRoot", new File(sandbox, "systemRoot"));
5463

55-
// On the radler/Rider code path the platform accesses <idea.home.path>/lib (and
56-
// /bin) very early during application load, before any test setUp would run. Since
57-
// idea.home.path points at the empty sandbox, link those dirs to the real SDK.
58-
Path sdkRoot = findSdkRoot();
59-
linkSandboxDir(sdkRoot, "bin", Path.of(PathManager.getBinPath()));
60-
linkSandboxDir(sdkRoot, "lib", Path.of(PathManager.getLibPath()));
61-
62-
setIfEmpty("idea.classpath.index.enabled", "false");
63-
64-
// Some plugins have a since-build and until-build restriction, so we need
65-
// to update the build number here
66-
String buildNumber = readApiVersionNumber();
67-
if (buildNumber == null) {
68-
buildNumber = BuildNumber.currentVersion().asString();
69-
}
70-
setIfEmpty("idea.plugins.compatible.build", buildNumber);
71-
setIfEmpty(PlatformUtils.PLATFORM_PREFIX_KEY, determinePlatformPrefix(buildNumber));
72-
73-
// b/166052760: Early in the android studio initialization, it accesses the user's home
74-
// directory for retrieving some analytics settings, and to also set up an SDK from
75-
// ~/Android/sdk, both of which it shouldn't be doing during testing. To fix this, we reset home
76-
// directory to point to a temporary directory. (Blaze may be doing this in general, so this
77-
// is more useful for bazel).
64+
// Reset the home directory to a temporary location so tests can neither read nor
65+
// write the real user home (e.g. analytics settings or a local SDK under ~/Android).
7866
System.setProperty("user.home", new File(sandbox, "userhome").getAbsolutePath());
79-
80-
// Tests fail if they access files outside of the project roots and other system directories.
81-
// When the class path contains jars which contain `kotlin` packages the `BuiltinsVirtualFileProviderBaseImpl`
82-
// also access jars from the class path. Therefore, this checks needs either to be disabled or
83-
// the specific jars need to be added to the `VfsRootAccess` allow list.
84-
System.setProperty("NO_FS_ROOTS_ACCESS_CHECK", "true");
85-
86-
setIfEmpty("idea.force.use.core.classloader", "true");
8767
}
8868

89-
@Nullable
90-
private static String determinePlatformPrefix(String buildNumber) {
91-
if (buildNumber.startsWith("IU")) { // IntelliJ Ultimate
92-
return null;
93-
} else if (buildNumber.startsWith("IC")) { // IntelliJ Community
94-
return "Idea";
95-
} else if (buildNumber.startsWith("CL")) { // CLion
96-
return "CLion";
97-
} else {
98-
throw new RuntimeException("Unable to determine platform prefix for build: " + buildNumber);
99-
}
100-
}
101-
102-
@Nullable
103-
private static String readApiVersionNumber() {
104-
String apiVersionFilePath = System.getProperty("blaze.idea.api.version.file");
105-
String runfilesWorkspaceRoot = System.getProperty("user.dir");
106-
if (apiVersionFilePath == null) {
107-
throw new RuntimeException("No api_version_file found in runfiles directory");
108-
}
109-
if (runfilesWorkspaceRoot == null) {
110-
throw new RuntimeException("Runfiles workspace root not found");
111-
}
112-
final var apiVersionFile = new File(runfilesWorkspaceRoot, apiVersionFilePath);
113-
if (!apiVersionFile.canRead()) {
114-
return null;
115-
}
116-
try {
117-
return com.google.common.io.Files.asCharSource(apiVersionFile, StandardCharsets.UTF_8).readFirstLine();
118-
} catch (IOException e) {
119-
throw new RuntimeException(e);
120-
}
69+
/**
70+
* Links the SDK {@code bin} and {@code lib} directories into the sandbox home.
71+
*
72+
* <p>On the radler/Rider code path the platform accesses {@code <idea.home.path>/lib} (and {@code
73+
* /bin}) very early during application load, before any test setUp would run. Since {@code
74+
* idea.home.path} points at the empty sandbox, those directories have to be linked to the real
75+
* SDK beforehand.
76+
*/
77+
private static void linkSdkDirectoriesIntoSandbox() {
78+
final var sdkRoot = findSdkRoot();
79+
linkSandboxDir(sdkRoot, "bin", Path.of(PathManager.getBinPath()));
80+
linkSandboxDir(sdkRoot, "lib", Path.of(PathManager.getLibPath()));
12181
}
12282

12383
private static Path findSdkRoot() {
@@ -150,6 +110,67 @@ private static void linkSandboxDir(Path sdkRoot, String dirName, Path link) {
150110
}
151111
}
152112

113+
/**
114+
* Reports the build number (and matching platform prefix) the tests should use, so plugins with a
115+
* since-build/until-build restriction are considered compatible.
116+
*/
117+
private static void configurePluginCompatibility() {
118+
final var buildNumber = resolveBuildNumber();
119+
setIfEmpty("idea.plugins.compatible.build", buildNumber);
120+
setIfEmpty(PlatformUtils.PLATFORM_PREFIX_KEY, determinePlatformPrefix(buildNumber));
121+
}
122+
123+
private static String resolveBuildNumber() {
124+
final var apiVersion = readApiVersionNumber();
125+
return apiVersion != null ? apiVersion : BuildNumber.currentVersion().asString();
126+
}
127+
128+
@Nullable
129+
private static String readApiVersionNumber() {
130+
final var apiVersionFilePath = System.getProperty("blaze.idea.api.version.file");
131+
if (apiVersionFilePath == null) {
132+
throw new RuntimeException("No api_version_file found in runfiles directory");
133+
}
134+
135+
final var runfilesWorkspaceRoot = System.getProperty("user.dir");
136+
if (runfilesWorkspaceRoot == null) {
137+
throw new RuntimeException("Runfiles workspace root not found");
138+
}
139+
140+
final var apiVersionFile = Path.of(runfilesWorkspaceRoot, apiVersionFilePath);
141+
if (!Files.isReadable(apiVersionFile)) {
142+
return null;
143+
}
144+
145+
try {
146+
return Files.readString(apiVersionFile, StandardCharsets.UTF_8).lines().findFirst().orElse(null);
147+
} catch (IOException e) {
148+
throw new RuntimeException(e);
149+
}
150+
}
151+
152+
@Nullable
153+
private static String determinePlatformPrefix(String buildNumber) {
154+
if (buildNumber.startsWith("IU")) { // IntelliJ Ultimate
155+
return null;
156+
} else if (buildNumber.startsWith("IC")) { // IntelliJ Community
157+
return "Idea";
158+
} else if (buildNumber.startsWith("CL")) { // CLion
159+
return "CLion";
160+
} else {
161+
throw new RuntimeException("Unable to determine platform prefix for build: " + buildNumber);
162+
}
163+
}
164+
165+
/**
166+
* Disables the VfsRootAccess check. Tests otherwise fail when accessing files outside the project
167+
* roots; in particular {@code BuiltinsVirtualFileProviderBaseImpl} accesses class path jars that
168+
* contain {@code kotlin} packages, which would need to be added to the allow list instead.
169+
*/
170+
private static void relaxFileSystemChecks() {
171+
System.setProperty("NO_FS_ROOTS_ACCESS_CHECK", "true");
172+
}
173+
153174
private static void setSandboxPath(String property, File path) {
154175
path.mkdirs();
155176
setIfEmpty(property, path.getPath());

0 commit comments

Comments
 (0)