Skip to content

Commit 9c7f723

Browse files
committed
Add tests for reading shfmt config options from .editorconfig
1 parent 0c7dd3c commit 9c7f723

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-1
lines changed

server/src/shfmt/__tests__/index.test.ts

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
/Shfmt: exited with status 1: <standard input>:10:1: > must be followed by a word/,
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
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 3
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 3
4+
5+
switch_case_indent = false
6+
space_redirects = false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 3
4+
5+
switch_case_indent = true
6+
space_redirects = true
7+
shell_variant = 'mksh'

0 commit comments

Comments
 (0)