From cf62c4a7f8e183daf3aca55a66b487523f598883 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Wed, 8 Jun 2022 23:20:21 +0000 Subject: [PATCH 1/7] refactor: add docs to isWsl and make easier to test --- src/node/util.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/node/util.ts b/src/node/util.ts index e33ed4585779..9c858c3b4432 100644 --- a/src/node/util.ts +++ b/src/node/util.ts @@ -377,10 +377,16 @@ export const getMediaMime = (filePath?: string): string => { return (filePath && mimeTypes[path.extname(filePath)]) || "text/plain" } -export const isWsl = async (): Promise => { +/** + * A helper function that checks if the platform is Windows Subsystem for Linux + * (WSL) + * + * @returns {Boolean} boolean if it is WSL + */ +export const isWsl = async (platform: NodeJS.Platform, procVersionFilePath: string): Promise => { return ( - (process.platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) || - (await fs.readFile("/proc/version", "utf8")).toLowerCase().indexOf("microsoft") !== -1 + (platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) || + (await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().indexOf("microsoft") !== -1 ) } @@ -398,7 +404,7 @@ export const open = async (address: URL | string): Promise => { } const args = [] as string[] const options = {} as cp.SpawnOptions - const platform = (await isWsl()) ? "wsl" : process.platform + const platform = (await isWsl(process.platform, "/proc/version")) ? "wsl" : process.platform let command = platform === "darwin" ? "open" : "xdg-open" if (platform === "win32" || platform === "wsl") { command = platform === "wsl" ? "cmd.exe" : "cmd" From ee64ad99c66481ef27c29f5ccb35195fa38e1c56 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Wed, 8 Jun 2022 23:20:28 +0000 Subject: [PATCH 2/7] feat: add tests for isWsl --- test/unit/node/util.test.ts | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index e37aeac9208c..dc689ae30b69 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -482,3 +482,74 @@ describe("humanPath", () => { expect(actual).toBe(expected) }) }) + +describe("isWsl", () => { + describe("on Linux (microsoft)", () => { + const testDir = path.join(tmpdir, "tests", "isWsl-linux-microsoft") + let pathToFile = "" + + beforeEach(async () => { + pathToFile = path.join(testDir, "/proc-version") + await fs.mkdir(testDir, { recursive: true }) + await fs.writeFile(pathToFile, "microsoft") + }) + afterEach(async () => { + await fs.rm(testDir, { recursive: true, force: true }) + }) + + it("should return true", async () => { + expect(await util.isWsl("linux", pathToFile)).toBe(true) + }) + }) + describe("on Linux (non-microsoft)", () => { + const testDir = path.join(tmpdir, "tests", "isWsl-linux-non-microsoft") + let pathToFile = "" + + beforeEach(async () => { + pathToFile = path.join(testDir, "/proc-version") + await fs.mkdir(testDir, { recursive: true }) + await fs.writeFile(pathToFile, "linux") + }) + afterEach(async () => { + await fs.rm(testDir, { recursive: true, force: true }) + }) + + it("should return false", async () => { + expect(await util.isWsl("linux", pathToFile)).toBe(false) + }) + }) + describe("on Win32 with microsoft in /proc/version", () => { + const testDir = path.join(tmpdir, "tests", "isWsl-win32-microsoft") + let pathToFile = "" + + beforeEach(async () => { + pathToFile = path.join(testDir, "/proc-version") + await fs.mkdir(testDir, { recursive: true }) + await fs.writeFile(pathToFile, "microsoft") + }) + afterEach(async () => { + await fs.rm(testDir, { recursive: true, force: true }) + }) + + it("should return true", async () => { + expect(await util.isWsl("win32", pathToFile)).toBe(true) + }) + }) + describe("on Darwin", () => { + const testDir = path.join(tmpdir, "tests", "isWsl-darwin") + let pathToFile = "" + + beforeEach(async () => { + pathToFile = path.join(testDir, "/proc-version") + await fs.mkdir(testDir, { recursive: true }) + await fs.writeFile(pathToFile, "darwin") + }) + afterEach(async () => { + await fs.rm(testDir, { recursive: true, force: true }) + }) + + it("should return false", async () => { + expect(await util.isWsl("darwin", pathToFile)).toBe(false) + }) + }) +}) From 5fccb461ba9b9b278085c248bd4e399400a2403d Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 9 Jun 2022 18:11:50 +0000 Subject: [PATCH 3/7] fixup!: refactor isWsl and tests --- src/node/util.ts | 26 ++++++++++--- test/unit/node/util.test.ts | 77 +++++++++++++------------------------ 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/node/util.ts b/src/node/util.ts index 9c858c3b4432..37eefc70787b 100644 --- a/src/node/util.ts +++ b/src/node/util.ts @@ -381,13 +381,27 @@ export const getMediaMime = (filePath?: string): string => { * A helper function that checks if the platform is Windows Subsystem for Linux * (WSL) * + * @see https://github.com/sindresorhus/is-wsl/blob/main/index.js * @returns {Boolean} boolean if it is WSL */ -export const isWsl = async (platform: NodeJS.Platform, procVersionFilePath: string): Promise => { - return ( - (platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) || - (await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().indexOf("microsoft") !== -1 - ) +export const isWsl = async ( + platform: NodeJS.Platform, + osRelease: string, + procVersionFilePath: string, +): Promise => { + if (platform !== "linux") { + return false + } + + if (osRelease.toLowerCase().includes("microsoft")) { + return true + } + + try { + return (await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().includes("microsoft") + } catch (_) { + return false + } } /** @@ -404,7 +418,7 @@ export const open = async (address: URL | string): Promise => { } const args = [] as string[] const options = {} as cp.SpawnOptions - const platform = (await isWsl(process.platform, "/proc/version")) ? "wsl" : process.platform + const platform = (await isWsl(process.platform, os.release(), "/proc/version")) ? "wsl" : process.platform let command = platform === "darwin" ? "open" : "xdg-open" if (platform === "win32" || platform === "wsl") { command = platform === "wsl" ? "cmd.exe" : "cmd" diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index dc689ae30b69..e91d9c9bf5e9 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -4,6 +4,7 @@ import * as path from "path" import { generateUuid } from "../../../src/common/util" import { tmpdir } from "../../../src/node/constants" import * as util from "../../../src/node/util" +import { clean, tmpdir as tempDirHelper } from "../../utils/helpers" describe("getEnvPaths", () => { describe("on darwin", () => { @@ -484,72 +485,46 @@ describe("humanPath", () => { }) describe("isWsl", () => { - describe("on Linux (microsoft)", () => { - const testDir = path.join(tmpdir, "tests", "isWsl-linux-microsoft") - let pathToFile = "" + const testName = "wsl" - beforeEach(async () => { - pathToFile = path.join(testDir, "/proc-version") - await fs.mkdir(testDir, { recursive: true }) - await fs.writeFile(pathToFile, "microsoft") - }) - afterEach(async () => { - await fs.rm(testDir, { recursive: true, force: true }) - }) + beforeAll(async () => { + await clean(testName) + }) + describe("on Linux (microsoft)", () => { it("should return true", async () => { - expect(await util.isWsl("linux", pathToFile)).toBe(true) + const fileName = "proc-version" + const osRelease = "Linux" + const pathToFile = path.join(await tempDirHelper(testName), fileName) + await fs.writeFile(pathToFile, "microsoft") + expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(true) }) }) describe("on Linux (non-microsoft)", () => { - const testDir = path.join(tmpdir, "tests", "isWsl-linux-non-microsoft") - let pathToFile = "" - - beforeEach(async () => { - pathToFile = path.join(testDir, "/proc-version") - await fs.mkdir(testDir, { recursive: true }) - await fs.writeFile(pathToFile, "linux") - }) - afterEach(async () => { - await fs.rm(testDir, { recursive: true, force: true }) - }) - it("should return false", async () => { - expect(await util.isWsl("linux", pathToFile)).toBe(false) + const fileName = "proc-version2" + const osRelease = "Linux" + const pathToFile = path.join(await tempDirHelper(testName), fileName) + await fs.writeFile(pathToFile, "linux") + expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(false) }) }) describe("on Win32 with microsoft in /proc/version", () => { - const testDir = path.join(tmpdir, "tests", "isWsl-win32-microsoft") - let pathToFile = "" - - beforeEach(async () => { - pathToFile = path.join(testDir, "/proc-version") - await fs.mkdir(testDir, { recursive: true }) + it("should return false", async () => { + const fileName = "proc-version3" + const osRelease = "Microsoft" + const pathToFile = path.join(await tempDirHelper(testName), fileName) await fs.writeFile(pathToFile, "microsoft") - }) - afterEach(async () => { - await fs.rm(testDir, { recursive: true, force: true }) - }) - - it("should return true", async () => { - expect(await util.isWsl("win32", pathToFile)).toBe(true) + expect(await util.isWsl("win32", osRelease, pathToFile)).toBe(false) }) }) describe("on Darwin", () => { - const testDir = path.join(tmpdir, "tests", "isWsl-darwin") - let pathToFile = "" - - beforeEach(async () => { - pathToFile = path.join(testDir, "/proc-version") - await fs.mkdir(testDir, { recursive: true }) - await fs.writeFile(pathToFile, "darwin") - }) - afterEach(async () => { - await fs.rm(testDir, { recursive: true, force: true }) - }) - it("should return false", async () => { - expect(await util.isWsl("darwin", pathToFile)).toBe(false) + const fileName = "proc-version4" + const osRelease = + "Darwin Roadrunner.local 10.3.0 Darwin Kernel Version 10.3.0: Fri Feb 26 11:58:09 PST 2010; root:xnu-1504.3.12~1/RELEASE_I386 i386" + const pathToFile = path.join(await tempDirHelper(testName), fileName) + expect(await util.isWsl("darwin", osRelease, pathToFile)).toBe(false) }) }) }) From c13b4d5f6c68f10814b199bed7556d2f671eadc9 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 9 Jun 2022 12:26:22 -0700 Subject: [PATCH 4/7] Update test/unit/node/util.test.ts Co-authored-by: Asher --- test/unit/node/util.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index e91d9c9bf5e9..2c5803de2c9c 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -494,9 +494,9 @@ describe("isWsl", () => { describe("on Linux (microsoft)", () => { it("should return true", async () => { const fileName = "proc-version" - const osRelease = "Linux" + const osRelease = "5.4.0-1066-gke" const pathToFile = path.join(await tempDirHelper(testName), fileName) - await fs.writeFile(pathToFile, "microsoft") + await fs.writeFile(pathToFile, "Linux version 5.4.0-1066-gke (buildd@lcy02-amd64-039) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)) #69-Ubuntu SMP Fri Mar 11 13:52:45 UTC 202") expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(true) }) }) From 3869622129a8507cbc22594eda90bf6cc8ca198c Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 9 Jun 2022 12:26:27 -0700 Subject: [PATCH 5/7] Update test/unit/node/util.test.ts Co-authored-by: Asher --- test/unit/node/util.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index 2c5803de2c9c..61ec5eb3bb32 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -512,9 +512,9 @@ describe("isWsl", () => { describe("on Win32 with microsoft in /proc/version", () => { it("should return false", async () => { const fileName = "proc-version3" - const osRelease = "Microsoft" + const osRelease = "3.4.0-Microsoft" const pathToFile = path.join(await tempDirHelper(testName), fileName) - await fs.writeFile(pathToFile, "microsoft") + await fs.writeFile(pathToFile, "Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014") expect(await util.isWsl("win32", osRelease, pathToFile)).toBe(false) }) }) From 7f5deb2c06c380b764f2dead74b89ae4bf0b23a1 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 10 Jun 2022 19:07:29 +0000 Subject: [PATCH 6/7] fixup: fix formatting --- test/unit/node/util.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index 61ec5eb3bb32..91cdd41b2479 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -496,7 +496,10 @@ describe("isWsl", () => { const fileName = "proc-version" const osRelease = "5.4.0-1066-gke" const pathToFile = path.join(await tempDirHelper(testName), fileName) - await fs.writeFile(pathToFile, "Linux version 5.4.0-1066-gke (buildd@lcy02-amd64-039) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)) #69-Ubuntu SMP Fri Mar 11 13:52:45 UTC 202") + await fs.writeFile( + pathToFile, + "Linux version 5.4.0-1066-gke (buildd@lcy02-amd64-039) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)) #69-Ubuntu SMP Fri Mar 11 13:52:45 UTC 202", + ) expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(true) }) }) @@ -514,7 +517,10 @@ describe("isWsl", () => { const fileName = "proc-version3" const osRelease = "3.4.0-Microsoft" const pathToFile = path.join(await tempDirHelper(testName), fileName) - await fs.writeFile(pathToFile, "Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014") + await fs.writeFile( + pathToFile, + "Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014", + ) expect(await util.isWsl("win32", osRelease, pathToFile)).toBe(false) }) }) From 5bc97ee706154b5d74a373d66991785064f52944 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 10 Jun 2022 19:36:38 +0000 Subject: [PATCH 7/7] fixup: use correct proc version strings --- test/unit/node/util.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index 91cdd41b2479..0237296e52af 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -498,7 +498,7 @@ describe("isWsl", () => { const pathToFile = path.join(await tempDirHelper(testName), fileName) await fs.writeFile( pathToFile, - "Linux version 5.4.0-1066-gke (buildd@lcy02-amd64-039) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)) #69-Ubuntu SMP Fri Mar 11 13:52:45 UTC 202", + "Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014", ) expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(true) }) @@ -508,7 +508,10 @@ describe("isWsl", () => { const fileName = "proc-version2" const osRelease = "Linux" const pathToFile = path.join(await tempDirHelper(testName), fileName) - await fs.writeFile(pathToFile, "linux") + await fs.writeFile( + pathToFile, + "Linux version 5.4.0-1066-gke (buildd@lcy02-amd64-039) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)) #69-Ubuntu SMP Fri Mar 11 13:52:45 UTC 202", + ) expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(false) }) })