From 58f8dccbcf676b3a70e53f19022e53a1947a3457 Mon Sep 17 00:00:00 2001 From: Michael Rykov Date: Sat, 19 Jun 2021 00:35:49 +0800 Subject: [PATCH] =?UTF-8?q?Fixed=20GetLanguagesByShebang=20for=20paths=20w?= =?UTF-8?q?ith=20=E2=80=9Cenv=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 17 +++++++++++------ common_test.go | 3 ++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/common.go b/common.go index a31f308..e70c2b0 100644 --- a/common.go +++ b/common.go @@ -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" { diff --git a/common_test.go b/common_test.go index f88b274..e8e5fb9 100644 --- a/common_test.go +++ b/common_test.go @@ -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 {