Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:56:48+00:00 2026-05-30T13:56:48+00:00

I would like to write a Unit Test for a (rather complex) Bash completion

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:56:49+00:00Added an answer on May 30, 2026 at 1:56 pm

    Say you have a bash-completion script in a file called asdf-completion, containing:

    _asdf() {
    COMPREPLY=()
    local cur prev
    cur=$(_get_cword)
    COMPREPLY=( $( compgen -W "one two three four five six" -- "$cur") )
    return 0
    }    
    complete -F _asdf asdf
    

    This uses the shell function _asdf to provide completions for the fictional asdf command. 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 the COMPREPLY variable. Here’s an example of doing that in a unittest:

    import subprocess
    import unittest
    
    class BashTestCase(unittest.TestCase):
        def test_complete(self):
            completion_file="asdf-completion"
            partial_word="f"
            cmd=["asdf", "other", "arguments", partial_word]
            cmdline = ' '.join(cmd)
    
            out = subprocess.Popen(['bash', '-i', '-c',
                r'source {compfile}; COMP_LINE="{cmdline}" COMP_WORDS=({cmdline}) COMP_CWORD={cword} COMP_POINT={cmdlen} $(complete -p {cmd} | sed "s/.*-F \\([^ ]*\\) .*/\\1/") && echo ${{COMPREPLY[*]}}'.format(
                    compfile=completion_file, cmdline=cmdline, cmdlen=len(cmdline), cmd=cmd[0], cword=cmd.index(partial_word)
                    )],
                stdout=subprocess.PIPE)
            stdout, stderr = out.communicate()
            self.assertEqual(stdout, "four five\n")
    
    if (__name__=='__main__'):
        unittest.main()
    

    This should work for any completions that use -F, but may work for others as well.

    je4d’s comment to use expect is a good one for a more complete test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write the unit test for a text parser class but
I would like to write some scripts in python that do some automated changes
I'm writing a python module and I would like to unit test it. I
I would like to write unit tests to test whether a dependency exists between
I have an openGL display that I would like to write unit-tests for. However,
I'd like to write an iOS unit test for a dealloc method that (basically)
I would like to know how to write unit tests for a module that
I would like to write a plug-in that will allow a custom written CRM
I would like to write a program that will identify a machine( for licensing
I would like to write a utility that will provide me with a relatively

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.