dimanche 7 mars 2021

How to test a bash function that prompts user input using bats?

Context

After manually testing a method named prompt_user_choice that takes in an array of installation_types and prompts for user input, I would like to automate this testing using bats files. I intend to do this by probing the log file that that function should create based on the user choices. I am able to test the log file, but I do not yet know how to simulate the user input consisting of multiple (y/n) answers.

Initially I thought I could include an argument that simulates the answers within the prompt_user_choice method by prepending an integer n that indicates how many of the first array elements are user question answers, followed by n yes or no answers, followed by the actual array. But that is poor testing as it modifies the behavior of the method when it's testing (VW-testing). Therefore, I thought it would be better to run the method and try to pipe the simulated user input into it somehow.

Description of code

This is the command that runs the method:

prompt_user_choice $(read_categories)

Where read_categories is another method that "returns" a string of space-separated strings that represent installation types. And this is the behaviour of the method:

Do you wish to install: git?n
Do you wish to install: somefiller?y
Do you wish to install: anotherfiller?n
The selected software packages are:
somefiller

Question

How would I pipe/simulate a sequence user answers of for example n y n into running the method:prompt_user_choice $(read_categories) ?

Aucun commentaire:

Enregistrer un commentaire