def TestValidInterpreter(self): """Fail if a script's interpreter is not found, or not executable. A script interpreter is anything after the #! sign, up to the end of line or the first space. """ failures = [] for root, _, file_names in os.walk(image_test_lib.ROOT_A): for file_name in file_names: full_name = os.path.join(root, file_name) file_stat = os.lstat(full_name) if (not stat.S_ISREG(file_stat.st_mode) or (file_stat.st_mode & 0o0111) == 0): continue with open(full_name, 'rb') as f: if f.read(2) != '#!': continue line = '#!' + f.readline().strip() try: # Ignore arguments to the interpreter. interp, _ = filetype.SplitShebang(line) except ValueError: failures.append( 'File %s has an invalid interpreter path: "%s".' % (full_name, line)) # Absolute path to the interpreter. interp = os.path.join(image_test_lib.ROOT_A, interp.lstrip('/')) # Interpreter could be a symlink. Resolve it. interp = osutils.ResolveSymlinkInRoot(interp, image_test_lib.ROOT_A) if not os.path.isfile(interp): failures.append( 'File %s uses non-existing interpreter %s.' % (full_name, interp)) elif (os.stat(interp).st_mode & 0o111) == 0: failures.append('Interpreter %s is not executable.' % interp) self.assertFalse(failures, '\n'.join(failures))
def testValidBytes(self): """Test bytes inputs.""" self.assertEqual(('/foo', '-v'), filetype.SplitShebang(b'#!/foo -v'))
def testCaseWithSpaces(self): """Test a case with several spaces in the line.""" self.assertEqual(('/bin/sh', '-i'), filetype.SplitShebang('#! /bin/sh -i \n'))
def testCaseWithEndline(self): """Test a case finished with a newline char.""" self.assertEqual(('/bin/sh', '-i'), filetype.SplitShebang('#!/bin/sh -i\n'))
def testCaseWithArguments(self): """Test a case with arguments.""" self.assertEqual(('/bin/sh', '-i -c "ls"'), filetype.SplitShebang('#!/bin/sh -i -c "ls"'))
def testSimpleCase(self): """Test a simple case.""" self.assertEqual(('/bin/sh', ''), filetype.SplitShebang('#!/bin/sh'))