def testFileSystemAction(self): CASES = [ # Dirs and files ('c', ['configure', 'core', 'cpp']), ('opy/doc', ['opy/doc']), ] a = completion.FileSystemAction() for prefix, expected in CASES: log('') log('-- PREFIX %r', prefix) comp = self._CompApi([], 0, prefix) self.assertEqual(expected, sorted(a.Matches(comp))) os.system('mkdir -p /tmp/oil_comp_test') os.system('bash -c "touch /tmp/oil_comp_test/{one,two,three}"') # This test depends on actual file system content. But we choose things # that shouldn't go away. ADD_SLASH_CASES = [ # Dirs and files ('c', ['configure', 'core/', 'cpp/']), ('nonexistent/', []), ('README.', ['README.md']), # Directory should be completed to core/ ? ('core', ['core/']), ('asdl/R', ['asdl/README.md']), ('opy/doc', ['opy/doc/']), ('opy/doc/', ['opy/doc/opcodes.md']), ('/bi', ['/bin/']), ('/tmp/oil_comp_test/', [ '/tmp/oil_comp_test/one', '/tmp/oil_comp_test/three', '/tmp/oil_comp_test/two', ]), ('./b', ['./benchmarks/', './bin/', './build/']), ] a = completion.FileSystemAction(add_slash=True) for prefix, expected in ADD_SLASH_CASES: log('') log('-- PREFIX %s', prefix) comp = self._CompApi([], 0, prefix) self.assertEqual(expected, sorted(a.Matches(comp))) # A bunch of repos in oilshell comp = completion.Api() comp.Update(partial_argv=[], index=0, to_complete='../o') print(list(a.Matches(comp))) EXEC_ONLY_CASES = [ ('i', ['install']) ] a = completion.FileSystemAction(exec_only=True) for prefix, expected in EXEC_ONLY_CASES: log('') log('-- PREFIX %s', prefix) comp = self._CompApi([], 0, prefix) self.assertEqual(expected, sorted(a.Matches(comp)))
def MockApi(line): """Match readline's get_begidx() / get_endidx().""" end = len(line) i = end - 1 while i > 0: if line[i] in util.READLINE_DELIMS: break i -= 1 return completion.Api(line=line, begin=i + 1, end=end)
def Run(self, cmd_val): argv = cmd_val.argv[1:] arg_r = args.Reader(argv) arg = COMPGEN_SPEC.Parse(arg_r) if arg_r.AtEnd(): to_complete = '' else: to_complete = arg_r.Peek() arg_r.Next() # bash allows extra arguments here. #if not arg_r.AtEnd(): # raise error.Usage('Extra arguments') matched = False base_opts = dict(arg.opt_changes) try: user_spec = self.spec_builder.Build(argv, arg, base_opts) except error.Parse as e: # error printed above return 2 # NOTE: Matching bash in passing dummy values for COMP_WORDS and COMP_CWORD, # and also showing ALL COMPREPLY reuslts, not just the ones that start with # the word to complete. matched = False comp = completion.Api() comp.Update(first='compgen', to_complete=to_complete, prev='', index=-1) try: for m, _ in user_spec.Matches(comp): matched = True print(m) except error.FatalRuntime: # - DynamicWordsAction: We already printed an error, so return failure. return 1 # - ShellFuncAction: We do NOT get FatalRuntimeError. We printed an error # in the executor, but RunFuncForCompletion swallows failures. See test # case in builtin-completion.test.sh. # TODO: # - need to dedupe results. return 0 if matched else 1
def __call__(self, argv): arg_r = args.Reader(argv) arg = COMPGEN_SPEC.Parse(arg_r) if arg_r.AtEnd(): to_complete = '' else: to_complete = arg_r.Peek() arg_r.Next() # bash allows extra arguments here. #if not arg_r.AtEnd(): # raise args.UsageError('Extra arguments') matched = False comp_opts = completion.Options(arg.opt_changes) user_spec = _BuildUserSpec(argv, arg, comp_opts, self.ex, respect_x=False) # NOTE: Matching bash in passing dummy values for COMP_WORDS and COMP_CWORD, # and also showing ALL COMPREPLY reuslts, not just the ones that start with # the word to complete. matched = False comp = completion.Api() comp.Update(first='compgen', to_complete=to_complete, prev='', index=-1) for m, _ in user_spec.Matches(comp): matched = True print(m) # TODO: # - need to dedupe results. return 0 if matched else 1
def _CompApi(self, partial_argv, index, to_complete): comp = completion.Api() comp.Update(partial_argv=partial_argv, index=index, to_complete=to_complete) return comp
def MockApi(line): """Match readline's get_begidx() / get_endidx().""" return completion.Api(line=line, begin=0, end=len(line))
def _MakeComp(self, words, index, to_complete): comp = completion.Api() comp.Update(partial_argv=['f'], index=0, to_complete='f') return comp