I would like to write a Unit Test for a (rather complex) Bash completion script, preferrably with Python - just something that gets the values of a Bash completion programmatically. The test should look like this:
def test_completion():
# trigger_completion should return what a user should get on triggering
# Bash completion like this: 'pbt createkvm<TAB>'
assert trigger_completion('pbt createkvm') == "module1 module2 module3"
How can I simulate Bash completion programmatically to check the completion values inside a testsuite for my tool?
Say you have a bash-completion script in a file called
asdf-completion, containing:This uses the shell function
_asdfto provide completions for the fictionalasdfcommand. If we set the right environment variables (from the bash man page), then we can get the same result, which is the placement of the potential expansions into theCOMPREPLYvariable. Here's an example of doing that in a unittest:This should work for any completions that use
-F, but may work for others as well.je4d's comment to use
expectis a good one for a more complete test.