Merge pull request #56 from rykov/fix-shebang-env

Fixed GetLanguagesByShebang for paths with “env”
This commit is contained in:
Alex 2021-06-25 17:46:15 +02:00 committed by GitHub
commit 7c24e3d5d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package enry
import (
"bufio"
"bytes"
"path"
"path/filepath"
"strings"
@ -314,13 +315,17 @@ func getInterpreter(data []byte) (interpreter string) {
return ""
}
if bytes.Contains(splitted[0], []byte("env")) {
if len(splitted) > 1 {
interpreter = string(splitted[1])
// Extract interpreter name from path. Use path.Base because
// shebang on Cygwin/Windows still use a forward slash
interpreter = path.Base(string(splitted[0]))
// #!/usr/bin/env [...]
if interpreter == "env" {
if len(splitted) == 1 {
// /usr/bin/env with no arguments
return ""
}
} else {
splittedPath := bytes.Split(splitted[0], []byte{'/'})
interpreter = string(splittedPath[len(splittedPath)-1])
interpreter = path.Base(string(splitted[1]))
}
if interpreter == "sh" {

View File

@ -296,7 +296,8 @@ println("The shell script says ",vm.arglist.concat(" "));`
{name: "TestGetLanguagesByShebang_8", content: []byte(`#!bash`), expected: []string{"Shell"}},
{name: "TestGetLanguagesByShebang_9", content: []byte(multilineExecHack), expected: []string{"Tcl"}},
{name: "TestGetLanguagesByShebang_10", content: []byte(multilineNoExecHack), expected: []string{"Shell"}},
{name: "TestGetLanguagesByShebang_11", content: []byte(`#!`), expected: nil},
{name: "TestGetLanguagesByShebang_11", content: []byte(`#!/envinpath/python`), expected: []string{"Python"}},
{name: "TestGetLanguagesByShebang_12", content: []byte(`#!`), expected: nil},
}
for _, test := range tests {