@@ -4,6 +4,7 @@ import * as path from "path"
44import { generateUuid } from "../../../src/common/util"
55import { tmpdir } from "../../../src/node/constants"
66import * as util from "../../../src/node/util"
7+ import { clean , tmpdir as tempDirHelper } from "../../utils/helpers"
78
89describe ( "getEnvPaths" , ( ) => {
910 describe ( "on darwin" , ( ) => {
@@ -482,3 +483,98 @@ describe("humanPath", () => {
482483 expect ( actual ) . toBe ( expected )
483484 } )
484485} )
486+
487+ describe ( "isWsl" , ( ) => {
488+ const testName = "wsl"
489+
490+ beforeAll ( async ( ) => {
491+ await clean ( testName )
492+ } )
493+
494+ describe ( "on Linux (microsoft)" , ( ) => {
495+ it ( "should return true" , async ( ) => {
496+ const fileName = "proc-version"
497+ const osRelease = "5.4.0-1066-gke"
498+ const pathToFile = path . join ( await tempDirHelper ( testName ) , fileName )
499+ await fs . writeFile (
500+ pathToFile ,
501+ "Linux version 3.4.0-Microsoft ([email protected] ) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014" , 502+ )
503+ expect ( await util . isWsl ( "linux" , osRelease , pathToFile ) ) . toBe ( true )
504+ } )
505+ } )
506+ describe ( "on Linux (non-microsoft)" , ( ) => {
507+ it ( "should return false" , async ( ) => {
508+ const fileName = "proc-version2"
509+ const osRelease = "Linux"
510+ const pathToFile = path . join ( await tempDirHelper ( testName ) , fileName )
511+ await fs . writeFile (
512+ pathToFile ,
513+ "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" ,
514+ )
515+ expect ( await util . isWsl ( "linux" , osRelease , pathToFile ) ) . toBe ( false )
516+ } )
517+ } )
518+ describe ( "on Win32 with microsoft in /proc/version" , ( ) => {
519+ it ( "should return false" , async ( ) => {
520+ const fileName = "proc-version3"
521+ const osRelease = "3.4.0-Microsoft"
522+ const pathToFile = path . join ( await tempDirHelper ( testName ) , fileName )
523+ await fs . writeFile (
524+ pathToFile ,
525+ "Linux version 3.4.0-Microsoft ([email protected] ) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014" , 526+ )
527+ expect ( await util . isWsl ( "win32" , osRelease , pathToFile ) ) . toBe ( false )
528+ } )
529+ } )
530+ describe ( "on Darwin" , ( ) => {
531+ it ( "should return false" , async ( ) => {
532+ const fileName = "proc-version4"
533+ const osRelease =
534+ "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"
535+ const pathToFile = path . join ( await tempDirHelper ( testName ) , fileName )
536+ expect ( await util . isWsl ( "darwin" , osRelease , pathToFile ) ) . toBe ( false )
537+ } )
538+ } )
539+ } )
540+
541+ describe ( "open" , ( ) => {
542+ it ( "should throw an error if address is a string" , async ( ) => {
543+ const address = "localhost:3000"
544+ await expect ( util . open ( address ) ) . rejects . toThrow ( "Cannot open socket paths" )
545+ } )
546+ } )
547+ describe ( "constructOpenOptions" , ( ) => {
548+ it ( "should return options for darwin" , ( ) => {
549+ const platform : NodeJS . Platform | "wsl" = "darwin"
550+ const url = new URL ( "localhost:8080" )
551+ const { args, command, urlSearch } = util . constructOpenOptions ( platform , url . search )
552+ expect ( args ) . toStrictEqual ( [ ] )
553+ expect ( command ) . toBe ( "open" )
554+ expect ( urlSearch ) . toBe ( "" )
555+ } )
556+ it ( "should return options for linux" , ( ) => {
557+ const platform : NodeJS . Platform | "wsl" = "linux"
558+ const url = new URL ( "localhost:8080" )
559+ const { args, command, urlSearch } = util . constructOpenOptions ( platform , url . search )
560+ expect ( args ) . toStrictEqual ( [ ] )
561+ expect ( command ) . toBe ( "xdg-open" )
562+ expect ( urlSearch ) . toBe ( "" )
563+ } )
564+ it ( "should return options for win32" , ( ) => {
565+ const platform : NodeJS . Platform | "wsl" = "win32"
566+ const url = new URL ( "localhost:8080?q=&test" )
567+ const { args, command, urlSearch } = util . constructOpenOptions ( platform , url . search )
568+ expect ( args ) . toStrictEqual ( [ "/c" , "start" , '""' , "/b" ] )
569+ expect ( command ) . toBe ( "cmd" )
570+ expect ( urlSearch ) . toBe ( "?q=^&test" )
571+ } )
572+ it ( "should return options for wsl" , ( ) => {
573+ const platform : NodeJS . Platform | "wsl" = "wsl"
574+ const url = new URL ( "localhost:8080?q=&test" )
575+ const { args, command, urlSearch } = util . constructOpenOptions ( platform , url . search )
576+ expect ( args ) . toStrictEqual ( [ "/c" , "start" , '""' , "/b" ] )
577+ expect ( command ) . toBe ( "cmd.exe" )
578+ expect ( urlSearch ) . toBe ( "?q=^&test" )
579+ } )
580+ } )
0 commit comments