Skip to content

Commit 5fccb46

Browse files
committed
fixup!: refactor isWsl and tests
1 parent ee64ad9 commit 5fccb46

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed

src/node/util.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,27 @@ export const getMediaMime = (filePath?: string): string => {
381381
* A helper function that checks if the platform is Windows Subsystem for Linux
382382
* (WSL)
383383
*
384+
* @see https://github.com/sindresorhus/is-wsl/blob/main/index.js
384385
* @returns {Boolean} boolean if it is WSL
385386
*/
386-
export const isWsl = async (platform: NodeJS.Platform, procVersionFilePath: string): Promise<boolean> => {
387-
return (
388-
(platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) ||
389-
(await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().indexOf("microsoft") !== -1
390-
)
387+
export const isWsl = async (
388+
platform: NodeJS.Platform,
389+
osRelease: string,
390+
procVersionFilePath: string,
391+
): Promise<boolean> => {
392+
if (platform !== "linux") {
393+
return false
394+
}
395+
396+
if (osRelease.toLowerCase().includes("microsoft")) {
397+
return true
398+
}
399+
400+
try {
401+
return (await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().includes("microsoft")
402+
} catch (_) {
403+
return false
404+
}
391405
}
392406

393407
/**
@@ -404,7 +418,7 @@ export const open = async (address: URL | string): Promise<void> => {
404418
}
405419
const args = [] as string[]
406420
const options = {} as cp.SpawnOptions
407-
const platform = (await isWsl(process.platform, "/proc/version")) ? "wsl" : process.platform
421+
const platform = (await isWsl(process.platform, os.release(), "/proc/version")) ? "wsl" : process.platform
408422
let command = platform === "darwin" ? "open" : "xdg-open"
409423
if (platform === "win32" || platform === "wsl") {
410424
command = platform === "wsl" ? "cmd.exe" : "cmd"

test/unit/node/util.test.ts

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from "path"
44
import { generateUuid } from "../../../src/common/util"
55
import { tmpdir } from "../../../src/node/constants"
66
import * as util from "../../../src/node/util"
7+
import { clean, tmpdir as tempDirHelper } from "../../utils/helpers"
78

89
describe("getEnvPaths", () => {
910
describe("on darwin", () => {
@@ -484,72 +485,46 @@ describe("humanPath", () => {
484485
})
485486

486487
describe("isWsl", () => {
487-
describe("on Linux (microsoft)", () => {
488-
const testDir = path.join(tmpdir, "tests", "isWsl-linux-microsoft")
489-
let pathToFile = ""
488+
const testName = "wsl"
490489

491-
beforeEach(async () => {
492-
pathToFile = path.join(testDir, "/proc-version")
493-
await fs.mkdir(testDir, { recursive: true })
494-
await fs.writeFile(pathToFile, "microsoft")
495-
})
496-
afterEach(async () => {
497-
await fs.rm(testDir, { recursive: true, force: true })
498-
})
490+
beforeAll(async () => {
491+
await clean(testName)
492+
})
499493

494+
describe("on Linux (microsoft)", () => {
500495
it("should return true", async () => {
501-
expect(await util.isWsl("linux", pathToFile)).toBe(true)
496+
const fileName = "proc-version"
497+
const osRelease = "Linux"
498+
const pathToFile = path.join(await tempDirHelper(testName), fileName)
499+
await fs.writeFile(pathToFile, "microsoft")
500+
expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(true)
502501
})
503502
})
504503
describe("on Linux (non-microsoft)", () => {
505-
const testDir = path.join(tmpdir, "tests", "isWsl-linux-non-microsoft")
506-
let pathToFile = ""
507-
508-
beforeEach(async () => {
509-
pathToFile = path.join(testDir, "/proc-version")
510-
await fs.mkdir(testDir, { recursive: true })
511-
await fs.writeFile(pathToFile, "linux")
512-
})
513-
afterEach(async () => {
514-
await fs.rm(testDir, { recursive: true, force: true })
515-
})
516-
517504
it("should return false", async () => {
518-
expect(await util.isWsl("linux", pathToFile)).toBe(false)
505+
const fileName = "proc-version2"
506+
const osRelease = "Linux"
507+
const pathToFile = path.join(await tempDirHelper(testName), fileName)
508+
await fs.writeFile(pathToFile, "linux")
509+
expect(await util.isWsl("linux", osRelease, pathToFile)).toBe(false)
519510
})
520511
})
521512
describe("on Win32 with microsoft in /proc/version", () => {
522-
const testDir = path.join(tmpdir, "tests", "isWsl-win32-microsoft")
523-
let pathToFile = ""
524-
525-
beforeEach(async () => {
526-
pathToFile = path.join(testDir, "/proc-version")
527-
await fs.mkdir(testDir, { recursive: true })
513+
it("should return false", async () => {
514+
const fileName = "proc-version3"
515+
const osRelease = "Microsoft"
516+
const pathToFile = path.join(await tempDirHelper(testName), fileName)
528517
await fs.writeFile(pathToFile, "microsoft")
529-
})
530-
afterEach(async () => {
531-
await fs.rm(testDir, { recursive: true, force: true })
532-
})
533-
534-
it("should return true", async () => {
535-
expect(await util.isWsl("win32", pathToFile)).toBe(true)
518+
expect(await util.isWsl("win32", osRelease, pathToFile)).toBe(false)
536519
})
537520
})
538521
describe("on Darwin", () => {
539-
const testDir = path.join(tmpdir, "tests", "isWsl-darwin")
540-
let pathToFile = ""
541-
542-
beforeEach(async () => {
543-
pathToFile = path.join(testDir, "/proc-version")
544-
await fs.mkdir(testDir, { recursive: true })
545-
await fs.writeFile(pathToFile, "darwin")
546-
})
547-
afterEach(async () => {
548-
await fs.rm(testDir, { recursive: true, force: true })
549-
})
550-
551522
it("should return false", async () => {
552-
expect(await util.isWsl("darwin", pathToFile)).toBe(false)
523+
const fileName = "proc-version4"
524+
const osRelease =
525+
"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"
526+
const pathToFile = path.join(await tempDirHelper(testName), fileName)
527+
expect(await util.isWsl("darwin", osRelease, pathToFile)).toBe(false)
553528
})
554529
})
555530
})

0 commit comments

Comments
 (0)