Skip to content

Commit d2af4d7

Browse files
committed
Fix the shebang parser
1 parent 9cfe6c5 commit d2af4d7

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

server/src/util/__tests__/shebang.test.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@ describe('hasBashShebang', () => {
99
expect(hasBashShebang(`#!/usr/bin/env python2.7\n# set -x`)).toBe(false)
1010
})
1111

12-
it('returns true for "#!/bin/sh -"', () => {
13-
expect(hasBashShebang('#!/bin/sh -')).toBe(true)
14-
expect(hasBashShebang('#!/bin/sh - ')).toBe(true)
12+
it('returns false for "#!/usr/bin/pwsh"', () => {
13+
expect(hasBashShebang('#!/usr/bin/pwsh')).toBe(false)
1514
})
1615

17-
it('returns true for "#!/usr/bin/env bash"', () => {
18-
expect(hasBashShebang('#!/usr/bin/env bash')).toBe(true)
19-
expect(hasBashShebang('#!/usr/bin/env bash ')).toBe(true)
20-
})
21-
22-
it('returns true for "#!/bin/sh"', () => {
23-
expect(hasBashShebang('#!/bin/sh')).toBe(true)
24-
expect(hasBashShebang('#!/bin/sh ')).toBe(true)
25-
})
26-
27-
it('returns true for "#!/bin/bash"', () => {
28-
expect(hasBashShebang('#!/bin/bash')).toBe(true)
29-
expect(hasBashShebang('#!/bin/bash ')).toBe(true)
16+
test.each([
17+
['#!/bin/sh -'],
18+
['#!/usr/bin/env bash'],
19+
['#!/bin/sh'],
20+
['#!/bin/bash'],
21+
['#!/bin/bash -u'],
22+
])('returns true for %p', (command) => {
23+
expect(hasBashShebang(command)).toBe(true)
24+
expect(hasBashShebang(`${command} `)).toBe(true)
3025
})
3126
})

server/src/util/shebang.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ export function getShebang(fileContent: string): string | null {
66
return null
77
}
88

9-
return match[1].replace('-', '').trim()
9+
return match[1]
1010
}
1111

12+
/**
13+
* Check if the given shebang is a bash shebang.
14+
*/
1215
export function isBashShebang(shebang: string): boolean {
13-
return shebang.endsWith('bash') || shebang.endsWith('sh')
16+
return (
17+
shebang.startsWith('/bin/bash') ||
18+
shebang.startsWith('/bin/sh') ||
19+
shebang.startsWith('/usr/bin/env bash') ||
20+
shebang.startsWith('/usr/bin/env sh')
21+
)
1422
}
1523

1624
export function hasBashShebang(fileContent: string): boolean {

0 commit comments

Comments
 (0)