From 435b5876123f7c36fd3ca3f91bbe8ac4d13b407f Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 21 Dec 2020 09:51:08 +0100 Subject: [PATCH] [tests] WSL lookup once per test class GitOrigin-RevId: b411fd9d42b123b1174de26ef14603d2dfb1a471 --- .../execution/wsl/WSLCommandEscapingTest.java | 18 ++++--- .../intellij/execution/wsl/WSLUtilTest.java | 54 ++++++++++--------- .../openapi/vfs/local/WslFileWatcherTest.kt | 11 ++-- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLCommandEscapingTest.java b/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLCommandEscapingTest.java index dd81166176fe..88aedbc34a73 100644 --- a/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLCommandEscapingTest.java +++ b/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLCommandEscapingTest.java @@ -6,6 +6,7 @@ import com.intellij.execution.ExecutionException; import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.execution.process.CapturingProcessHandler; import com.intellij.execution.process.ProcessOutput; +import com.intellij.openapi.util.NullableLazyValue; import com.intellij.testFramework.fixtures.BareTestFixtureTestCase; import com.intellij.testFramework.rules.TempDirectory; import org.junit.Before; @@ -28,7 +29,12 @@ import static org.junit.Assume.assumeTrue; public class WSLCommandEscapingTest extends BareTestFixtureTestCase { @Rule public final TempDirectory tempDir = new TempDirectory(); - private WSLDistribution myWSL; + private static final NullableLazyValue WSL = NullableLazyValue.createValue(() -> { + List distributions = WSLUtil.getAvailableDistributions(); + return distributions.isEmpty() ? null : distributions.get(0); + }); + + private WSLDistribution wsl; @BeforeClass public static void checkEnvironment() { @@ -38,9 +44,7 @@ public class WSLCommandEscapingTest extends BareTestFixtureTestCase { @Before public void setUp() { - List distributions = WSLUtil.getAvailableDistributions(); - assumeTrue("No WSL distributions available", distributions.size() > 0); - myWSL = distributions.get(0); + assumeTrue("No WSL distributions available", (wsl = WSL.getValue()) != null); } @Test @@ -211,7 +215,7 @@ public class WSLCommandEscapingTest extends BareTestFixtureTestCase { } private void assertPwdOutputInDirectory(String directoryName) throws ExecutionException { - String path = myWSL.getWslPath(tempDir.newDirectory(directoryName).getPath()); + String path = wsl.getWslPath(tempDir.newDirectory(directoryName).getPath()); assertWslCommandOutput(path + "\n", path, Collections.emptyMap(), List.of("pwd")); } @@ -250,7 +254,7 @@ public class WSLCommandEscapingTest extends BareTestFixtureTestCase { private String createEchoScriptAndGetLinuxPath(String executableName) { File file = tempDir.newFile(executableName + ".sh", "#!/bin/sh\necho \"$@\"".getBytes(StandardCharsets.UTF_8)); - String wslPath = myWSL.getWslPath(file.getPath()); + String wslPath = wsl.getWslPath(file.getPath()); assertNotNull("local path: " + file, wslPath); return wslPath; } @@ -275,7 +279,7 @@ public class WSLCommandEscapingTest extends BareTestFixtureTestCase { } private void assertWslCommandOutput(String expectedOut, WSLCommandLineOptions options, Map envs, List command) throws ExecutionException { - GeneralCommandLine cmd = myWSL.patchCommandLine(new GeneralCommandLine(command).withEnvironment(envs), null, options); + GeneralCommandLine cmd = wsl.patchCommandLine(new GeneralCommandLine(command).withEnvironment(envs), null, options); ProcessOutput output = new CapturingProcessHandler(cmd).runProcess(10_000); String expected = stringify(false, "", 0, expectedOut); diff --git a/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLUtilTest.java b/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLUtilTest.java index 9d943b55b116..3b623903a40c 100644 --- a/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLUtilTest.java +++ b/platform/platform-tests/testSrc/com/intellij/execution/wsl/WSLUtilTest.java @@ -4,6 +4,7 @@ package com.intellij.execution.wsl; import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.execution.process.CapturingProcessHandler; import com.intellij.execution.process.ProcessOutput; +import com.intellij.openapi.util.NullableLazyValue; import com.intellij.testFramework.fixtures.BareTestFixtureTestCase; import com.intellij.testFramework.rules.TempDirectory; import org.junit.Before; @@ -22,7 +23,12 @@ import static org.junit.Assume.assumeTrue; public class WSLUtilTest extends BareTestFixtureTestCase { @Rule public TempDirectory tempDirectory = new TempDirectory(); - private WSLDistribution myWSL; + private static final NullableLazyValue WSL = NullableLazyValue.createValue(() -> { + List distributions = WSLUtil.getAvailableDistributions(); + return distributions.isEmpty() ? null : distributions.get(0); + }); + + private WSLDistribution wsl; @BeforeClass public static void checkEnvironment() { @@ -32,43 +38,41 @@ public class WSLUtilTest extends BareTestFixtureTestCase { @Before public void setUp() { - List distributions = WSLUtil.getAvailableDistributions(); - assumeTrue("No WSL distributions available", distributions.size() > 0); - myWSL = distributions.get(0); + assumeTrue("No WSL distributions available", (wsl = WSL.getValue()) != null); } @Test public void testWslToWinPath() { - assertNull(myWSL.getWindowsPath("/mnt/cd")); - assertNull(myWSL.getWindowsPath("/mnt")); - assertNull(myWSL.getWindowsPath("")); - assertNull(myWSL.getWindowsPath("/mnt//test")); - assertNull(myWSL.getWindowsPath("/mnt/1/test")); + assertNull(wsl.getWindowsPath("/mnt/cd")); + assertNull(wsl.getWindowsPath("/mnt")); + assertNull(wsl.getWindowsPath("")); + assertNull(wsl.getWindowsPath("/mnt//test")); + assertNull(wsl.getWindowsPath("/mnt/1/test")); - assertEquals("C:", myWSL.getWindowsPath("/mnt/c")); - assertEquals("X:\\", myWSL.getWindowsPath("/mnt/x/")); - assertEquals("C:\\temp\\foo", myWSL.getWindowsPath("/mnt/c/temp/foo")); - assertEquals("C:\\temp\\KeepCase", myWSL.getWindowsPath("/mnt/c/temp/KeepCase")); - assertEquals("C:\\name with spaces\\another name with spaces", myWSL.getWindowsPath("/mnt/c/name with spaces/another name with spaces")); + assertEquals("C:", wsl.getWindowsPath("/mnt/c")); + assertEquals("X:\\", wsl.getWindowsPath("/mnt/x/")); + assertEquals("C:\\temp\\foo", wsl.getWindowsPath("/mnt/c/temp/foo")); + assertEquals("C:\\temp\\KeepCase", wsl.getWindowsPath("/mnt/c/temp/KeepCase")); + assertEquals("C:\\name with spaces\\another name with spaces", wsl.getWindowsPath("/mnt/c/name with spaces/another name with spaces")); //noinspection NonAsciiCharacters - assertEquals("C:\\юникод", myWSL.getWindowsPath("/mnt/c/юникод")); + assertEquals("C:\\юникод", wsl.getWindowsPath("/mnt/c/юникод")); } @Test public void testWinToWslPath() { - assertEquals("/mnt/c/foo", myWSL.getWslPath("C:\\foo")); - assertEquals("/mnt/c/temp/KeepCase", myWSL.getWslPath("C:\\temp\\KeepCase")); - assertNull(myWSL.getWslPath("?:\\temp\\KeepCase")); - assertNull(myWSL.getWslPath("c:c")); + assertEquals("/mnt/c/foo", wsl.getWslPath("C:\\foo")); + assertEquals("/mnt/c/temp/KeepCase", wsl.getWslPath("C:\\temp\\KeepCase")); + assertNull(wsl.getWslPath("?:\\temp\\KeepCase")); + assertNull(wsl.getWslPath("c:c")); } @Test public void testPaths() { String originalWinPath = "C:\\usr\\something\\bin\\gcc"; - assertEquals(originalWinPath, myWSL.getWindowsPath(myWSL.getWslPath(originalWinPath))); + assertEquals(originalWinPath, wsl.getWindowsPath(wsl.getWslPath(originalWinPath))); String originalWslPath = "/mnt/c/usr/bin/gcc"; - assertEquals(originalWslPath, myWSL.getWslPath(myWSL.getWindowsPath(originalWslPath))); + assertEquals(originalWslPath, wsl.getWslPath(wsl.getWindowsPath(originalWslPath))); } @Test @@ -76,18 +80,18 @@ public class WSLUtilTest extends BareTestFixtureTestCase { File winFile = tempDirectory.newFile("the_file.txt"); File winSymlink = new File(tempDirectory.getRoot(), "sym_link"); - String file = myWSL.getWslPath(winFile.getPath()); - String symlink = myWSL.getWslPath(winSymlink.getPath()); + String file = wsl.getWslPath(winFile.getPath()); + String symlink = wsl.getWslPath(winSymlink.getPath()); mkSymlink(file, symlink); - String resolved = myWSL.getWindowsPath(myWSL.resolveSymlink(symlink)); + String resolved = wsl.getWindowsPath(wsl.resolveSymlink(symlink)); assertNotNull(resolved); assertTrue(new File(resolved).exists()); assertTrue(winFile.getPath().equalsIgnoreCase(resolved)); } private void mkSymlink(String file, String symlink) throws Exception { - GeneralCommandLine cmd = myWSL.patchCommandLine(new GeneralCommandLine("ln", "-s", file, symlink), null, new WSLCommandLineOptions()); + GeneralCommandLine cmd = wsl.patchCommandLine(new GeneralCommandLine("ln", "-s", file, symlink), null, new WSLCommandLineOptions()); @SuppressWarnings("deprecation") ProcessOutput output = WSLUtil.addInputCloseListener(new CapturingProcessHandler(cmd)).runProcess(10_000); assertFalse(output.isTimeout()); assertEquals(0, output.getExitCode()); diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/WslFileWatcherTest.kt b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/WslFileWatcherTest.kt index 009af4e3d0d1..4d833150ddd7 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/WslFileWatcherTest.kt +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/WslFileWatcherTest.kt @@ -47,7 +47,10 @@ import java.util.concurrent.atomic.AtomicBoolean //todo[r.sh] fix ignored tests class WslFileWatcherTest : BareTestFixtureTestCase() { // - private val LOG: Logger by lazy { Logger.getInstance(WslFileWatcher::class.java) } + companion object { + private val LOG: Logger by lazy { Logger.getInstance(WslFileWatcher::class.java) } + private val WSL: String? by lazy { enumerateWslDistributions().firstOrNull() } + } private lateinit var tempDir: Path private lateinit var wsl: String @@ -64,10 +67,8 @@ class WslFileWatcherTest : BareTestFixtureTestCase() { assumeTrue(SystemInfo.isWin10OrNewer) assumeWslPresence() - val distributions = enumerateWslDistributions() - assumeTrue("No WSL distributions found", distributions.isNotEmpty()) - - wsl = distributions[0] + assumeTrue("No WSL distributions found", WSL != null) + wsl = WSL!! assumeTrue("WSL distribution ${wsl} doesn't seem to be alive", reanimateWslDistribution(wsl)) LOG.debug("================== setting up " + getTestName(false) + " ==================")