github.com/rancher/elemental/tests@v0.0.0-20240517125144-ae048c615b3f/scripts/generate-tests-description (about)

     1  #!/bin/bash
     2  
     3  # Small script to generated README.sh file to add a
     4  # basic test documentation based on the code.
     5  # It uses Describe/It/By syntax to extract kindof
     6  # useful informations.
     7  #
     8  # It currently supports these frameworks/languages:
     9  # - Ginkgo(Gomega)/Go
    10  # - Cypress/Typescript
    11  
    12  function abort() {
    13    echo "$1" >&2
    14    exit 1
    15  }
    16  
    17  # Variables
    18  DIR=$1
    19  EXT="*_test.go *.spec.ts"
    20  
    21  # Go to directory if it exists, otherwise exit
    22  [[ ! -d ${DIR:=$PWD} ]] && abort "Directory '${DIR}' does not exist!"
    23  pushd ${DIR} >/dev/null
    24  
    25  # Loop on each test file
    26  for TST_FILE in $(ls ${EXT} 2>/dev/null); do
    27    # Set regex/fields depending of language
    28    if [[ "${TST_FILE##*.}" == "go" ]]; then
    29      REGEX="^[[:space:]]*(Context|It|By)|^.*=.*(Describe)"
    30      FIELDS="\1\2"
    31    elif [[ "${TST_FILE##*.}" == "ts" ]]; then
    32      REGEX="^[[:space:]]*(describe|it|by)"
    33      FIELDS="\1"
    34    fi
    35  
    36    # Extact the informations
    37    TXT=$(
    38      grep -h -E "${REGEX}" ${TST_FILE} \
    39        | tr -d "('\"" \
    40        | sed -E \
    41              -e "s/${REGEX}/${FIELDS}: /" \
    42              -e 's/,[[:space:]]*(func|Label).*//' \
    43              -e 's/,[[:space:]]*\)[[:space:]]*=>[[:space:]]*\{//' \
    44              -e 's/\{[[:space:]]*tags:.*\}//' \
    45              -e 's/\)$//' \
    46              -e 's/,[[:space:]]*$//' \
    47              -e 's/^[Dd]escribe:/- **Describe:**/' \
    48              -e 's/^[Cc]ontext:/\t- **Context:**/' \
    49              -e 's/^[Ii]t:/\t\t- **It:**/' \
    50              -e 's/^[Bb]y:/\t\t\t-  **By:**/'
    51    )
    52  
    53    # If empty
    54    [[ -z "${TXT}" ]] && TXT="*No test defined!*"
    55  
    56    # Show all informations (with tabs size set to 2)
    57    echo -e "## \`${TST_FILE}\`\n\n${TXT}\n" | expand -t2
    58  done
    59  
    60  # Go back to the previous directory
    61  popd >/dev/null
    62  
    63  # Done!
    64  exit 0