Skip to content

Commit

Permalink
Fix shell scripts missing tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Enzime committed May 30, 2023
1 parent 655155a commit 12cd96c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
19 changes: 8 additions & 11 deletions identify/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NON_EXECUTABLE = 'non-executable'
TEXT = 'text'
BINARY = 'binary'
SHELL = 'shell'

TYPE_TAGS = frozenset((DIRECTORY, FILE, SYMLINK, SOCKET))
MODE_TAGS = frozenset((EXECUTABLE, NON_EXECUTABLE))
Expand Down Expand Up @@ -59,16 +60,14 @@ def tags_from_path(path: str) -> set[str]:
else:
tags.add(NON_EXECUTABLE)

# As an optimization, if we're able to read tags from the filename, then we
# don't peek at the file contents.
t = tags_from_filename(os.path.basename(path))
if len(t) > 0:
tags.update(t)
else:
if executable:
shebang = parse_shebang_from_file(path)
if len(shebang) > 0:
tags.update(tags_from_interpreter(shebang[0]))
tags.update(t)

# SHELL will get added by the filename
if executable or SHELL in tags:
shebang = parse_shebang_from_file(path)
if len(shebang) > 0:
tags.update(tags_from_interpreter(shebang[0]))

# some extensions can be both binary and text
# see EXTENSIONS_NEED_BINARY_CHECK
Expand Down Expand Up @@ -206,8 +205,6 @@ def parse_shebang_from_file(path: str) -> tuple[str, ...]:
"""Parse the shebang given a file path."""
if not os.path.lexists(path):
raise ValueError(f'{path} does not exist.')
if not os.access(path, os.X_OK):
return ()

try:
with open(path, 'rb') as f:
Expand Down
17 changes: 16 additions & 1 deletion tests/identify_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ def test_tags_from_path_file_with_shebang_executable(tmpdir):
'file', 'text', 'executable', 'python',
}

def test_tags_from_path_executable_shell_script(tmpdir):
x = tmpdir.join('test')
x.write_text('#!/bin/bash', encoding='UTF-8')
make_executable(x.strpath)
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'executable', 'shell', 'bash'
}

def test_tags_from_path_non_executable_shell_script(tmpdir):
x = tmpdir.join('test.sh')
x.write_text('#!/bin/bash', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable', 'shell', 'bash',
}


def test_tags_from_path_binary(tmpdir):
x = tmpdir.join('test')
Expand Down Expand Up @@ -336,7 +351,7 @@ def test_parse_shebang_from_file_does_not_exist():
def test_parse_shebang_from_file_nonexecutable(tmpdir):
x = tmpdir.join('f')
x.write_text('#!/usr/bin/env python', encoding='UTF-8')
assert identify.parse_shebang_from_file(x.strpath) == ()
assert identify.parse_shebang_from_file(x.strpath) == ('python',)


def test_parse_shebang_from_file_simple(tmpdir):
Expand Down

0 comments on commit 12cd96c

Please sign in to comment.