Class: Bashcov::Detective

Inherits:
Object
  • Object
show all
Defined in:
lib/bashcov/detective.rb

Overview

Detect shell scripts

Constant Summary collapse

SHELL_BASENAMES =
Set<String>

Basenames of shell executables

Set.new(%w[bash sh ash dash]).freeze
OTHER_BASENAMES =
Set<String>

Basenames of executables commonly used to exec other

processes, including shells
Set.new(%w[env]).freeze
SHELLSCRIPT_EXTENSIONS =
Set<String>

Filename extensions commonly used for shell scripts

Set.new(%w[.bash .sh]).freeze

Instance Method Summary collapse

Constructor Details

#initialize(bash_path) ⇒ Detective

Create an object that can be used for inferring whether a file is or is not a shell script.

Parameters:

  • bash_path (String)

    path to a Bash interpreter



21
22
23
# File 'lib/bashcov/detective.rb', line 21

def initialize(bash_path)
  @bash_path = bash_path
end

Instance Method Details

#shellscript?(filename) ⇒ Boolean

Note:

returns false when filename is not readable, even if filename indeed refers to a shell script.

Checks whether the provided file refers to a shell script by determining whether the first line is a shebang that refers to a shell executable, or whether the file has a shellscript extension and contains valid shell syntax.

Parameters:

  • filename (String, Pathname)

    the name of the file to be checked

Returns:

  • (Boolean)

    whether filename refers to a shell script



33
34
35
36
37
38
39
# File 'lib/bashcov/detective.rb', line 33

def shellscript?(filename)
  return false unless File.exist?(filename) && File.readable?(filename) \
    && File.file?(File.realpath(filename))

  shellscript_shebang?(filename) || \
    (shellscript_extension?(filename) && shellscript_syntax?(filename))
end