summaryrefslogtreecommitdiff
path: root/share/spack/qa/completion-test.sh
blob: 1071ed36dbeac255642ea7c9fa75268d07d9f912 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/sh
#
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

#
# This script tests that Spack's tab completion scripts work.
#
# The tests are portable to bash, zsh, and bourne shell, and can be run
# in any of these shells.
#

export QA_DIR=$(dirname "$0")
export SHARE_DIR=$(cd "$QA_DIR/.." && pwd)
export SPACK_ROOT=$(cd "$QA_DIR/../../.." && pwd)

. "$QA_DIR/test-framework.sh"

# Fail on undefined variables
set -u

# Source setup-env.sh before tests
. "$SHARE_DIR/setup-env.sh"
. "$SHARE_DIR/spack-completion.$_sp_shell"

title "Testing spack-completion.$_sp_shell with $_sp_shell"

# Spack command is now available
succeeds which spack

title 'Testing all subcommands'
# read line into an array portably
READ="read -ra line"
if [ -n "${ZSH_VERSION:-}" ]; then
  READ=(read -rA line)
fi
while IFS=' ' $READ
do
    # Test that completion with no args works
    succeeds _spack_completions "${line[@]}" ''

    # Test that completion with flags works
    # all commands but spack pkg grep have -h; all have --help
    contains '--help' _spack_completions "${line[@]}" -
done <<- EOF
    $(spack commands --aliases --format=subcommands)
EOF

title 'Testing for correct output'
contains 'compiler' _spack_completions spack ''
contains 'install' _spack_completions spack inst
contains 'find' _spack_completions spack help ''
contains 'hdf5' _spack_completions spack list ''
contains 'py-numpy' _spack_completions spack list py-
contains 'mpi' _spack_completions spack providers ''
contains 'builtin' _spack_completions spack repo remove ''
contains 'packages' _spack_completions spack config edit ''
contains 'python' _spack_completions spack extensions ''
contains 'hdf5' _spack_completions spack -d install --jobs 8 ''
contains 'hdf5' _spack_completions spack install -v ''

title 'Testing alias handling'
contains 'concretize' _spack_completions spack c
contains 'concretise' _spack_completions spack c
contains 'concretize' _spack_completions spack conc
does_not_contain 'concretise' _spack_completions spack conc

does_not_contain 'concretize' _spack_completions spack isnotacommand
does_not_contain 'concretize' _spack_completions spack env isnotacommand

# XFAIL: Fails for Python 2.6 because pkg_resources not found?
#contains 'compilers.py' _spack_completions spack unit-test ''

_test_debug_functions() {
    title 'Testing debugging functions'

    if [ -n "${ZSH_VERSION:-}" ]; then
        emulate -L sh
    fi

    # Test whether `spack install --verb[] spec` completes to `spack install --verbose spec`
    COMP_LINE='spack install --verb spec'
    COMP_POINT=20
    COMP_WORDS=(spack install --verb spec)
    COMP_CWORD=2
    COMP_KEY=9
    COMP_TYPE=64
    _bash_completion_spack
    contains "--verbose" echo "${COMPREPLY[@]}"

    # This is a particularly tricky case that involves the following situation:
    #     `spack -d [] install `
    # Here, [] represents the cursor, which is in the middle of the line.
    # We should tab-complete optional flags for `spack`, not optional flags for
    # `spack install` or package names.
    COMP_LINE='spack -d  install '
    COMP_POINT=9
    COMP_WORDS=(spack -d install)
    COMP_CWORD=2
    COMP_KEY=9
    COMP_TYPE=64

    _bash_completion_spack
    contains "--all-help" echo "${COMPREPLY[@]}"

    contains "['spack', '-d', 'install', '']" _pretty_print COMP_WORDS[@]

    # Set the rest of the intermediate variables manually
    COMP_WORDS_NO_FLAGS=(spack install)
    COMP_CWORD_NO_FLAGS=1
    subfunction=_spack
    cur=

    list_options=true
    contains "'True'" _test_vars
    list_options=false
    contains "'False'" _test_vars
}
_test_debug_functions