Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Loop around tests? #136

@Thubo

Description

@Thubo

I would like to create the following scenario: (Note the for loop in the following code)

#!/usr/bin/env bats                                                                                          

set -o pipefail                                                                                              

###############################################################################                              
# Functions                                                                                                  
###############################################################################                              
free_space_in_gb() {                                                                                               
  echo $(df -P $1 | tail -n 1 | awk '{print int($4/1024/1024)}')                                             
}                                                                                                            

check_partition() {                                                                                          
  mount | grep " on $1 "                                                                                     
}                                                                                                            
###############################################################################                              

for i in /tmp /var; do                                                                                   
  @test "$i: exists" {                                                                                       
    stat $i  2>/dev/null
  }                                                                                                          
  @test "$i: separate partition" {                                                                           
    mount | grep " on $i "                                                                                   
  }                                                                                                          
done                                                                                                         

@test "/var: free space" {                                                                                   
  [[ $(free_space_in_gb /var) -gt 100 ]]                                                                           
}

Is there a simple way to achieve this?

The above example runs the tests only for the last variable in the for loop (here /var).

Maybe I'm on the wrong track here, but I have may very similar tests, which force me to create copy&paste code - which is something I would like to avoid....

On a side note: Is there a way to pass a parameter to a @test? I'm thinking about something like:

@test "run stat on $1" /var {
  stat $1 # Check if  /var is there
}

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions