def run(args, input): """ Runs the grep command. The usage for the command should be grep [-iw] [-A N] PATTERN [FILE...]. Supported flags are: -A <NUM>, --after-context=<NUM> Print NUM lines of trailing context after matching lines. -w, --word-regexp Select only those lines containing matches that form whole words. -i, --ignore-case Ignore case distinctions, so that characters differing case-only match. :param args: A list of arguments matching the usage pattern. :param input: An input string to search on if no files are passed to grep. :return: A string containing a list of matches for the pattern. :raise: CommandException if the command's arguments are invalid. """ try: parsed_args = GrepArguments(args) except ParsingException as exception: raise CommandException('grep: {}'.format(exception)) output = GrepOutputFormatter(len(parsed_args.files), parsed_args.was_context_set) if not parsed_args.files and input: Grep._get_matches(output, '', parsed_args, input.splitlines(True)) for file in parsed_args.files: Grep._process_file(output, parsed_args, file) return output.format()
def testFormat_filesWithLinesep(self): expected = [ 'first:one line', 'first-not a match', '--', 'second:another line', 'second-also no match', 'second:another matching line', '' ] output = GrepOutputFormatter(file_amount=3, needs_splitter=True) output.add_line('first', 1, 'one line' + os.linesep, True) output.add_line('first', 2, 'not a match' + os.linesep, False) output.add_line('second', 2, 'another line' + os.linesep, True) output.add_line('second', 3, 'also no match' + os.linesep, False) output.add_line('second', 4, 'another matching line' + os.linesep, True) self.assertEqual(output.format(), os.linesep.join(expected))
def testAddFile_fileNotNeeded(self): output = GrepOutputFormatter(file_amount=1, needs_splitter=True) output.add_line('first', 1, 'one line' + os.linesep, True) self.assertEqual(output.format(), 'one line' + os.linesep)
def testAddSplitter_adjacentMatches(self): expected = ['first:one line', 'first:another line', ''] output = GrepOutputFormatter(file_amount=7, needs_splitter=True) output.add_line('first', 1, 'one line' + os.linesep, True) output.add_line('first', 2, 'another line' + os.linesep, True) self.assertEqual(output.format(), os.linesep.join(expected))
def testAddSplitter_splitterNotSet(self): expected = ['first:one line', 'first:another line', ''] output = GrepOutputFormatter(file_amount=4, needs_splitter=False) output.add_line('first', 1, 'one line' + os.linesep, True) output.add_line('first', 4, 'another line' + os.linesep, True) self.assertEqual(output.format(), os.linesep.join(expected))
def testAddSplitter_sameFileNewSegment(self): expected = ['first:one line', '--', 'first:another line', ''] output = GrepOutputFormatter(file_amount=2, needs_splitter=True) output.add_line('first', 1, 'one line' + os.linesep, True) output.add_line('first', 3, 'another line' + os.linesep, True) self.assertEqual(output.format(), os.linesep.join(expected))
def testAddSplitter_differentFile(self): expected = ['first:one line', '--', 'second:another line', ''] output = GrepOutputFormatter(file_amount=3, needs_splitter=True) output.add_line('first', 1, 'one line' + os.linesep, True) output.add_line('second', 2, 'another line' + os.linesep, True) self.assertEqual(output.format(), os.linesep.join(expected))