| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Test our _init_completion builtin against the bash_completion implementation.
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   testdata/completion/bash_oracle.sh <function name>
 | 
| 7 | 
 | 
| 8 | SH=${SH:-bash}
 | 
| 9 | 
 | 
| 10 | argv1() {
 | 
| 11 |   python -c 'import sys; print(repr(sys.argv[1]))' "$@"
 | 
| 12 | }
 | 
| 13 | 
 | 
| 14 | tab-complete() {
 | 
| 15 |   local code=$1
 | 
| 16 | 
 | 
| 17 |   echo
 | 
| 18 |   echo 'case = {}'
 | 
| 19 |   echo 'CASES.append(case)'
 | 
| 20 |   echo -n 'case["code"] = '; argv1 "$code"
 | 
| 21 | 
 | 
| 22 |   local pat='^case'
 | 
| 23 |   { cat testdata/completion/bash_oracle_plugins.sh; echo "$code"; } |
 | 
| 24 |     $SH --rcfile /dev/null -i 2>&1 |
 | 
| 25 |     egrep "$pat" || echo "ERROR: output didn't match $pat"
 | 
| 26 | }
 | 
| 27 | 
 | 
| 28 | # _init_completion flags used:
 | 
| 29 | # -s
 | 
| 30 | # -n :
 | 
| 31 | #
 | 
| 32 | # Do NOT need to implement:
 | 
| 33 | #
 | 
| 34 | # -o '@(diff|patch)' is used once.  But it's for redirect args, which we parse
 | 
| 35 | # in OSH itself.
 | 
| 36 | #
 | 
| 37 | # NOTE: I see _init_completion -s -n : , but I believe that's identical to
 | 
| 38 | # _init_completion -s.
 | 
| 39 | # Also I see '-n =+!' once, but that may be a mistake.  The most common cases
 | 
| 40 | # are : and =.
 | 
| 41 | 
 | 
| 42 | codegen-header() {
 | 
| 43 |   # Make everything here into a Python comment.
 | 
| 44 |   awk '{ print "# " $0 }' << EOF
 | 
| 45 | 
 | 
| 46 | DO NOT EDIT -- Generated by $0
 | 
| 47 | 
 | 
| 48 | bash --version:
 | 
| 49 | $(bash --version)
 | 
| 50 | 
 | 
| 51 | bash_completion:'
 | 
| 52 | 
 | 
| 53 | $(md5sum testdata/completion/bash_completion)
 | 
| 54 | $(ls -l testdata/completion/bash_completion)
 | 
| 55 | 
 | 
| 56 | EOF
 | 
| 57 | }
 | 
| 58 | 
 | 
| 59 | init-cases() {
 | 
| 60 |   codegen-header
 | 
| 61 |   echo 'CASES = []'
 | 
| 62 | 
 | 
| 63 |   tab-complete $'echo foo:bar --color=auto\t'
 | 
| 64 |   # Hm := are stuck together!  That is weird.
 | 
| 65 |   tab-complete $'echo foo=one:two:=three --color=auto\t'
 | 
| 66 | 
 | 
| 67 |   # readline includes quotes, and _init_completion doesn't do anything about this.
 | 
| 68 |   # I think that is a mistake and I will get rid of it?
 | 
| 69 |   #
 | 
| 70 |   # ls "--ver<TAB> or '--ver<TAB>' does NOT complete.
 | 
| 71 |   # But echo 'fro<TAB> DOES!  So that is a mistake.
 | 
| 72 | 
 | 
| 73 |   tab-complete $'echo "foo:bar|" --color=auto\t'
 | 
| 74 | 
 | 
| 75 |   # scrape tab completion
 | 
| 76 |   echo
 | 
| 77 |   tab-complete $'noflags foo:bar --color=auto\t'
 | 
| 78 |   tab-complete $'noflags "foo:bar|" --color=auto\t'
 | 
| 79 |   tab-complete $'noflags "foo:bar|\t'
 | 
| 80 | 
 | 
| 81 |   echo
 | 
| 82 |   tab-complete $'s foo:bar --color=auto\t'
 | 
| 83 |   tab-complete $'s foo:bar --color auto\t'
 | 
| 84 | 
 | 
| 85 |   echo
 | 
| 86 |   tab-complete $'n foo:bar --color=auto\t'
 | 
| 87 | 
 | 
| 88 |   echo
 | 
| 89 |   tab-complete $'n2 foo:bar --color=auto\t'
 | 
| 90 | }
 | 
| 91 | 
 | 
| 92 | # Write a file that's committed
 | 
| 93 | write-init-cases() {
 | 
| 94 |   local out=testdata/completion/bash_oracle.py
 | 
| 95 |   init-cases > $out
 | 
| 96 |   wc -l $out
 | 
| 97 |   echo "Wrote $out"
 | 
| 98 | }
 | 
| 99 | 
 | 
| 100 | other-cases() {
 | 
| 101 |   codegen-header
 | 
| 102 |   echo 'CASES = []'
 | 
| 103 | 
 | 
| 104 |   tab-complete $'reassemble foo:bar --color=auto\t'
 | 
| 105 |   tab-complete $'words foo:bar --color=auto\t'
 | 
| 106 | }
 | 
| 107 | 
 | 
| 108 | # NOTE: This was for __reassemble_comp_words_by_ref and _get_comp_words_by_ref,
 | 
| 109 | # but they turned out to trivial to implement with 'compadjust'.  I'm keeping
 | 
| 110 | # this for now in case we need another example of a bash oracle.
 | 
| 111 | 
 | 
| 112 | write-other-cases() {
 | 
| 113 |   local out=testdata/completion/bash_oracle_other.py
 | 
| 114 |   other-cases > $out
 | 
| 115 |   wc -l $out
 | 
| 116 |   echo "Wrote $out"
 | 
| 117 | }
 | 
| 118 | 
 | 
| 119 | # TODO: osh -i isn't easily scraped, for some reason.  Does it have something
 | 
| 120 | # to do with terminal modes?
 | 
| 121 | compare() {
 | 
| 122 |   SH=bash tab-complete $'echo f\t'
 | 
| 123 |   SH=bin/osh tab-complete $'echo f\t'
 | 
| 124 | }
 | 
| 125 | 
 | 
| 126 | "$@"
 |