[grep.sh] - Question around multiple-files tests

I was doing the grep.sh exercise and noticed that (some of?) the tests for multiple files actually test against a single file. To confirm that, I created some dummy code to see the number of files in the files array.

By the way, this is how I derived the parameters as per guidance on the exercise:

    while getopts livxn flag; do
        case $flag in
            n) flags+=(-n) ;;
            l) flags+=(-l) ;;
            i) flags+=(-i) ;;
            v) flags+=(-v) ;;
            x) flags+=(-x) ;;
            ?) echo "Usage: PROGRAM [-a ARG1] [-b]"; exit 1 ;;
        esac
    done
    shift $((OPTIND - 1))
    pattern="$1"
    files=( $2 )
 ✗ Multiple files, one match, no flags                                                                                                                                                                
   (from function `assert_output' in file bats-extra.bash, line 394,                                                                                                                                  
    in test file grep.bats, line 226)                                                              
     `assert_output "$expected"' failed                                                            
                                                                                                   
   -- output differs --                                                                            
   expected (1 lines):                                                                             
     iliad.txt:Of Atreus, Agamemnon, King of men.                                                  
   actual (5 lines):                                                                               
     A pattern: Agamemnon                                                                          
     files: iliad.txt                                                                              
     filecount: 1                                                                                  
     flags:                                                                                        
     argc: 4                                                                                                                                                                                                                                       
   --          

Can anyone please have look if I missed something? Cheers.

The unit tests have this:

run bash grep.sh "${flags[@]}" "$pattern" "${files[@]}"

You’re using $2 which contains a single argument (or, a single filename) and ignore any further arguments after that.

You can capture the pattern, shift then use $@ to get the remaining arguments.

This indeed resolved the issue. Thanks @IsaacG