@@ -3,9 +3,18 @@ import { promises as fs } from "fs"
3
3
import * as net from "net"
4
4
import * as os from "os"
5
5
import * as path from "path"
6
- import { Args , parse , setDefaults , shouldOpenInExistingInstance , splitOnFirstEquals } from "../../../src/node/cli"
6
+ import {
7
+ Args ,
8
+ bindAddrFromArgs ,
9
+ parse ,
10
+ setDefaults ,
11
+ shouldOpenInExistingInstance ,
12
+ shouldRunVsCodeCli ,
13
+ splitOnFirstEquals ,
14
+ } from "../../../src/node/cli"
7
15
import { tmpdir } from "../../../src/node/constants"
8
16
import { paths } from "../../../src/node/util"
17
+ import { useEnv } from "../../utils/helpers"
9
18
10
19
type Mutable < T > = {
11
20
- readonly [ P in keyof T ] : T [ P ]
@@ -463,3 +472,173 @@ describe("splitOnFirstEquals", () => {
463
472
expect ( actual ) . toEqual ( expect . arrayContaining ( expected ) )
464
473
} )
465
474
} )
475
+
476
+ describe ( "shouldRunVsCodeCli" , ( ) => {
477
+ it ( "should return false if no 'extension' related args passed in" , ( ) => {
478
+ const args = {
479
+ _ : [ ] ,
480
+ }
481
+ const actual = shouldRunVsCodeCli ( args )
482
+ const expected = false
483
+
484
+ expect ( actual ) . toBe ( expected )
485
+ } )
486
+
487
+ it ( "should return true if 'list-extensions' passed in" , ( ) => {
488
+ const args = {
489
+ _ : [ ] ,
490
+ [ "list-extensions" ] : true ,
491
+ }
492
+ const actual = shouldRunVsCodeCli ( args )
493
+ const expected = true
494
+
495
+ expect ( actual ) . toBe ( expected )
496
+ } )
497
+
498
+ it ( "should return true if 'install-extension' passed in" , ( ) => {
499
+ const args = {
500
+ _ : [ ] ,
501
+ [ "install-extension" ] : [ "hello.world" ] ,
502
+ }
503
+ const actual = shouldRunVsCodeCli ( args )
504
+ const expected = true
505
+
506
+ expect ( actual ) . toBe ( expected )
507
+ } )
508
+
509
+ it ( "should return true if 'uninstall-extension' passed in" , ( ) => {
510
+ const args = {
511
+ _ : [ ] ,
512
+ [ "uninstall-extension" ] : [ "hello.world" ] ,
513
+ }
514
+ const actual = shouldRunVsCodeCli ( args )
515
+ const expected = true
516
+
517
+ expect ( actual ) . toBe ( expected )
518
+ } )
519
+ } )
520
+
521
+ describe ( "bindAddrFromArgs" , ( ) => {
522
+ it ( "should return the bind address" , ( ) => {
523
+ const args = {
524
+ _ : [ ] ,
525
+ }
526
+
527
+ const addr = {
528
+ host : "localhost" ,
529
+ port : 8080 ,
530
+ }
531
+
532
+ const actual = bindAddrFromArgs ( addr , args )
533
+ const expected = addr
534
+
535
+ expect ( actual ) . toStrictEqual ( expected )
536
+ } )
537
+
538
+ it ( "should use the bind-address if set in args" , ( ) => {
539
+ const args = {
540
+ _ : [ ] ,
541
+ [ "bind-addr" ] : "localhost:3000" ,
542
+ }
543
+
544
+ const addr = {
545
+ host : "localhost" ,
546
+ port : 8080 ,
547
+ }
548
+
549
+ const actual = bindAddrFromArgs ( addr , args )
550
+ const expected = {
551
+ host : "localhost" ,
552
+ port : 3000 ,
553
+ }
554
+
555
+ expect ( actual ) . toStrictEqual ( expected )
556
+ } )
557
+
558
+ it ( "should use the host if set in args" , ( ) => {
559
+ const args = {
560
+ _ : [ ] ,
561
+ [ "host" ] : "coder" ,
562
+ }
563
+
564
+ const addr = {
565
+ host : "localhost" ,
566
+ port : 8080 ,
567
+ }
568
+
569
+ const actual = bindAddrFromArgs ( addr , args )
570
+ const expected = {
571
+ host : "coder" ,
572
+ port : 8080 ,
573
+ }
574
+
575
+ expect ( actual ) . toStrictEqual ( expected )
576
+ } )
577
+
578
+ it ( "should use process.env.PORT if set" , ( ) => {
579
+ const [ setValue , resetValue ] = useEnv ( "PORT" )
580
+ setValue ( "8000" )
581
+
582
+ const args = {
583
+ _ : [ ] ,
584
+ }
585
+
586
+ const addr = {
587
+ host : "localhost" ,
588
+ port : 8080 ,
589
+ }
590
+
591
+ const actual = bindAddrFromArgs ( addr , args )
592
+ const expected = {
593
+ host : "localhost" ,
594
+ port : 8000 ,
595
+ }
596
+
597
+ expect ( actual ) . toStrictEqual ( expected )
598
+ resetValue ( )
599
+ } )
600
+
601
+ it ( "should set port if in args" , ( ) => {
602
+ const args = {
603
+ _ : [ ] ,
604
+ port : 3000 ,
605
+ }
606
+
607
+ const addr = {
608
+ host : "localhost" ,
609
+ port : 8080 ,
610
+ }
611
+
612
+ const actual = bindAddrFromArgs ( addr , args )
613
+ const expected = {
614
+ host : "localhost" ,
615
+ port : 3000 ,
616
+ }
617
+
618
+ expect ( actual ) . toStrictEqual ( expected )
619
+ } )
620
+
621
+ it ( "should use the args.port over process.env.PORT if both set" , ( ) => {
622
+ const [ setValue , resetValue ] = useEnv ( "PORT" )
623
+ setValue ( "8000" )
624
+
625
+ const args = {
626
+ _ : [ ] ,
627
+ port : 3000 ,
628
+ }
629
+
630
+ const addr = {
631
+ host : "localhost" ,
632
+ port : 8080 ,
633
+ }
634
+
635
+ const actual = bindAddrFromArgs ( addr , args )
636
+ const expected = {
637
+ host : "localhost" ,
638
+ port : 3000 ,
639
+ }
640
+
641
+ expect ( actual ) . toStrictEqual ( expected )
642
+ resetValue ( )
643
+ } )
644
+ } )
0 commit comments