@@ -596,7 +596,7 @@ describe('formatter', () => {
596596 ` )
597597 } )
598598
599- it ( 'should omit filename from the shfmt comment when it cannot be determined' , async ( ) => {
599+ it ( 'should omit filename from the shfmt command when it cannot be determined' , async ( ) => {
600600 // There's no easy way to see what filename has been passed to shfmt without inspecting the
601601 // contents of the logs. As a workaround, we set a non-file:// URI on a dodgy document to
602602 // trigger an exception and inspect the error message.
@@ -613,4 +613,152 @@ describe('formatter', () => {
613613 / S h f m t : e x i t e d w i t h s t a t u s 1 : < s t a n d a r d i n p u t > : 1 0 : 1 : > m u s t b e f o l l o w e d b y a w o r d / ,
614614 )
615615 } )
616+
617+ describe ( 'getShfmtArguments()' , ( ) => {
618+ const lspShfmtConfig = {
619+ binaryNextLine : true ,
620+ funcNextLine : true ,
621+ simplifyCode : true ,
622+ }
623+ const lspShfmtArgs = [ '-bn' , '-fn' , '-s' ]
624+ const formatOptions = { tabSize : 2 , insertSpaces : true }
625+
626+ const formatter = new Formatter ( {
627+ executablePath : 'shfmt' ,
628+ } )
629+
630+ describe ( 'when the document URI is not a filepath' , ( ) => {
631+ let shfmtArgs : string [ ]
632+ const filepath = `${ FIXTURE_FOLDER } /shfmt.sh`
633+
634+ beforeAll ( async ( ) => {
635+ // @ts -expect-error Testing a private method
636+ shfmtArgs = await formatter . getShfmtArguments (
637+ `test://${ filepath } ` ,
638+ formatOptions ,
639+ lspShfmtConfig ,
640+ )
641+ } )
642+
643+ it ( 'should use language server config' , async ( ) => {
644+ expect ( shfmtArgs ) . toEqual ( expect . arrayContaining ( lspShfmtArgs ) )
645+ expect ( shfmtArgs . length ) . toEqual ( 4 ) // indentation
646+ } )
647+
648+ it ( 'should use indentation config from the editor' , ( ) => {
649+ expect ( shfmtArgs ) . toContain ( '-i=2' )
650+ } )
651+
652+ it ( 'should not include the filename argument' , async ( ) => {
653+ expect ( shfmtArgs ) . not . toContain ( `--filename=${ filepath } ` )
654+ } )
655+ } )
656+
657+ describe ( 'when no .editorconfig exists' , ( ) => {
658+ let shfmtArgs : string [ ]
659+ const filepath = `${ FIXTURE_FOLDER } /shfmt.sh`
660+
661+ beforeAll ( async ( ) => {
662+ // @ts -expect-error Testing a private method
663+ shfmtArgs = await formatter . getShfmtArguments (
664+ `file://${ filepath } ` ,
665+ formatOptions ,
666+ lspShfmtConfig ,
667+ )
668+ } )
669+
670+ it ( 'should use language server config' , ( ) => {
671+ expect ( shfmtArgs ) . toEqual ( expect . arrayContaining ( lspShfmtArgs ) )
672+ expect ( shfmtArgs . length ) . toEqual ( 5 ) // indentation + filename
673+ } )
674+
675+ it ( 'should use indentation config from the editor' , ( ) => {
676+ expect ( shfmtArgs ) . toContain ( '-i=2' )
677+ } )
678+
679+ it ( 'should include the filename argument' , ( ) => {
680+ expect ( shfmtArgs ) . toContain ( `--filename=${ filepath } ` )
681+ } )
682+ } )
683+
684+ describe ( 'when an .editorconfig exists without shfmt options' , ( ) => {
685+ let shfmtArgs : string [ ]
686+ const filepath = `${ FIXTURE_FOLDER } /shfmt-editorconfig/no-shfmt-properties/foo.sh`
687+
688+ beforeAll ( async ( ) => {
689+ // @ts -expect-error Testing a private method
690+ shfmtArgs = await formatter . getShfmtArguments (
691+ `file://${ filepath } ` ,
692+ formatOptions ,
693+ lspShfmtConfig ,
694+ )
695+ } )
696+
697+ it ( 'should use language server config' , ( ) => {
698+ expect ( shfmtArgs ) . toEqual ( expect . arrayContaining ( lspShfmtArgs ) )
699+ expect ( shfmtArgs . length ) . toEqual ( 5 ) // indentation + filename
700+ } )
701+
702+ it ( 'should use indentation config from the editor' , ( ) => {
703+ expect ( shfmtArgs ) . toContain ( '-i=2' )
704+ } )
705+
706+ it ( 'should include the filename argument' , ( ) => {
707+ expect ( shfmtArgs ) . toContain ( `--filename=${ filepath } ` )
708+ } )
709+ } )
710+
711+ describe ( 'when an .editorconfig exists and contains only false shfmt options' , ( ) => {
712+ let shfmtArgs : string [ ]
713+ const filepath = `${ FIXTURE_FOLDER } /shfmt-editorconfig/shfmt-properties-false/foo.sh`
714+
715+ beforeAll ( async ( ) => {
716+ // @ts -expect-error Testing a private method
717+ shfmtArgs = await formatter . getShfmtArguments (
718+ `file://${ filepath } ` ,
719+ formatOptions ,
720+ lspShfmtConfig ,
721+ )
722+ } )
723+
724+ it ( 'should use .editorconfig config (even though no options are enabled)' , ( ) => {
725+ expect ( shfmtArgs . length ) . toEqual ( 2 ) // indentation + filename
726+ } )
727+
728+ it ( 'should use indentation config from the editor' , ( ) => {
729+ expect ( shfmtArgs ) . toContain ( '-i=2' )
730+ } )
731+
732+ it ( 'should include the filename argument' , ( ) => {
733+ expect ( shfmtArgs ) . toContain ( `--filename=${ filepath } ` )
734+ } )
735+ } )
736+
737+ describe ( 'when an .editorconfig exists and contains one or more shfmt options' , ( ) => {
738+ let shfmtArgs : string [ ]
739+ const filepath = `${ FIXTURE_FOLDER } /shfmt-editorconfig/shfmt-properties/foo.sh`
740+
741+ beforeAll ( async ( ) => {
742+ // @ts -expect-error Testing a private method
743+ shfmtArgs = await formatter . getShfmtArguments (
744+ `file://${ filepath } ` ,
745+ formatOptions ,
746+ lspShfmtConfig ,
747+ )
748+ } )
749+
750+ it ( 'should use .editorconfig config' , ( ) => {
751+ expect ( shfmtArgs ) . toEqual ( expect . arrayContaining ( [ '-ci' , '-sr' , "-ln='mksh'" ] ) )
752+ expect ( shfmtArgs . length ) . toEqual ( 5 ) // indentation + filename
753+ } )
754+
755+ it ( 'should use indentation config from the editor' , ( ) => {
756+ expect ( shfmtArgs ) . toContain ( '-i=2' )
757+ } )
758+
759+ it ( 'should include the filename argument' , ( ) => {
760+ expect ( shfmtArgs ) . toContain ( `--filename=${ filepath } ` )
761+ } )
762+ } )
763+ } )
616764} )
0 commit comments