github.com/Percona-Lab/go-swagger@v0.19.0/hack/codegen-nonreg.sh (about) 1 #! /bin/bash 2 # A small utility to build fixture servers 3 # for non regression testing of codegen 4 shopt -s extglob 5 6 # function initColors() { 7 if [[ -z "${MONO}" ]]; then 8 normal=$(tput sgr0) 9 red=$(tput setaf 1) 10 green=$(tput setaf 2) 11 orange=$(tput setaf 3) 12 bold=$(tput bold) 13 blue=$(tput setaf 4) 14 else 15 normal="" 16 red="" 17 green="" 18 orange="" 19 blue="" 20 bold="" 21 fi 22 # } 23 24 function okcr() { 25 ok "$*"; printf "\n" 26 } 27 function ok() { 28 printf "${green}%s${normal}" "$*" 29 } 30 function errcr() { 31 err "$*" ; printf "\n" 32 } 33 function err() { 34 printf "${red}${bold}%s${normal}" "$*" 35 } 36 function successcr() { 37 success "$*" ; printf "\n" 38 } 39 function success() { 40 printf "${green}${bold}%s${normal}" "$*" 41 } 42 function warncr() { 43 warn "$*" ; printf "\n" 44 } 45 function warn() { 46 printf "${orange}%s${normal}" "$*" 47 } 48 function infocr() { 49 info "$*" ; printf "\n" 50 } 51 function info() { 52 printf "${blue}%s${normal}" "$*" 53 } 54 55 if [ ! -f `which swagger` ]; then 56 echo "can't find swagger in the PATH" 57 exit 1 58 fi 59 60 # NOTE(fredbi): 61 # The following ones fail generation: 62 # - existing-model.yml requires pregeneration (not supported yet by this script) 63 # - issue72: model works with --skip-validation. Invalid spec (duplicate operationID) 64 # - todolist.simple.yml: invalid default values put on purpose for UT (provided fixed version for testing) 65 # - fixture-basetypes.yaml: exhibits some edge case failures with discriminator (e.g. using a base type in tuple...) 66 # - fixture-polymorphism.yaml: idem 67 # 68 # The following ones requires some checks to be skipped: 69 # - azure-text-analyis.json: works with --skip-validation. Invalid specification with duplicate property in allOf construct (provided fixed version for testing) 70 # - swagger-gsma.json: idem 71 # 72 # The following ones used to fail and are ok: 73 # - todolist.discriminators.yml: works (not with expand) 74 # - bitbucket.json: works nows (not with expand: too many files for ordinary ulimit) 75 # - cloudbreak.json : now works 76 known_failed="@(\ 77 existing-model.yml|\ 78 issue72.json|\ 79 todolist.simple.yml|\ 80 fixture-basetypes.yaml|\ 81 fixture-polymorphism.yaml|\ 82 )" 83 # The following ones should fail validation, but produce correct generated code (at least it builds) 84 known_skip_validation="@(\ 85 todolist.enums.yml|\ 86 todolist.enums.flattened.json|\ 87 todolist.models.yml|\ 88 todolist.schemavalidation.yml|\ 89 azure-text-analyis.json|\ 90 swagger-gsma.json|\ 91 fixture-844-variations.yaml|\ 92 fixture-allOf.yaml|\ 93 fixture-errors.yaml|\ 94 fixture-itching.yaml|\ 95 fixture-tuple.yaml|\ 96 fixture-simple-tuple.yaml|\ 97 )" 98 99 # A list of known client build failures 100 known_client_failure="@(\ 101 todolist.arrayform.yml|\ 102 todolist.arrayquery.yml|\ 103 todolist.url.basepath.yml|\ 104 todolist.url.simple.yml|\ 105 swagger-codegen-tests.json|\ 106 fixture-1414.json|\ 107 fixture-909-3.yaml|\ 108 fixture-909-4.yaml|\ 109 fixture-909-5.yaml|\ 110 fixture-909-6.yaml|\ 111 gentest2.yaml|\ 112 gentest3.yaml|\ 113 gentest.yaml|\ 114 fixture-1437-4.yaml|\ 115 fixture-1392-2.yaml|\ 116 fixture-1392-3.yaml|\ 117 )" 118 119 # A list of known fixtures not supporting expand mode (not including the discriminator case). 120 # Normally, this is because of duplicate names constructed during codegen of anonymous structures. 121 # This should be solved with proper analysis of names before codegen. 122 known_expand_failure="@(\ 123 todolist.enums.flattened.json|\ 124 fixture-1479.yaml|\ 125 bitbucket.json|\ 126 )" 127 128 # A list of known failures with minimal spec flattening. For those special cases, a full 129 # flattening is required 130 known_failed_minimal="@(\ 131 fixture-1767.yaml|\ 132 )" 133 134 if [[ "$1" == "--circleci" ]] ; then 135 # Coloured output not supported by default on CircleCI. 136 # Forcing term to xterm is not enough: tput not available with minimalist env. 137 MONO=1 138 # In CI run, test builds with default option (minimal flatten) 139 OPTS="--with-flatten=minimal" 140 #export TERM=xterm 141 #MONO="" 142 else 143 # In manual run, enable coloured output 144 MONO="" 145 # In manual run, test the full range of available options for spec preprocessing 146 OPTS="--with-flatten=full --with-flatten=minimal --with-flatten=expand" 147 fi 148 149 # A little chrome does not hurt... 150 # initColors 151 152 # All fixtures in ./fixtures/codegen + some others 153 cd $(git rev-parse --show-toplevel) 154 specdir="fixtures/codegen fixtures/bugs/909 fixtures/bugs/1437 fixtures/bugs/1314 fixtures/bugs/1062/eve-online-esi.json" 155 specdir=${specdir}" fixtures/bugs/1392" 156 specdir=${specdir}" fixtures/bugs/1277" 157 specdir=${specdir}" fixtures/bugs/1536" 158 specdir=${specdir}" fixtures/bugs/1487" 159 specdir=${specdir}" fixtures/bugs/1571" 160 specdir=${specdir}" fixtures/bugs/957" 161 specdir=${specdir}" fixtures/bugs/1614" 162 specdir=${specdir}" fixtures/bugs/931" 163 specdir=${specdir}" fixtures/bugs/1683" 164 specdir=${specdir}" fixtures/bugs/1796" 165 specdir=${specdir}" fixtures/bugs/1839" 166 specdir=${specdir}" fixtures/bugs/1719" 167 specdir=${specdir}" fixtures/bugs/1859" 168 gendir=./tmp-gen 169 rm -rf ${gendir} 170 171 check_list=`for d in ${specdir}; do ls $d/*.yml;ls $d/*.json;ls $d/*.yaml;done 2>/dev/null` 172 # there are several subspecs there: we just want the master 173 check_list=${check_list}" fixtures/bugs/1621/fixture-1621.yaml" 174 check_list=${check_list}" fixtures/bugs/1774/def_api.yaml" 175 check_list=${check_list}" fixtures/bugs/1767/fixture-1767.yaml" 176 check_list=${check_list}" fixtures/bugs/1260/fixture-realiased-types.yaml" 177 check_list=${check_list}" fixtures/bugs/1260/test3-swagger.yaml fixtures/bugs/1260/test3-bis-swagger.yaml" 178 check_list=${check_list}" fixtures/bugs/1260/test3-ter-swagger.yaml fixtures/bugs/1260/test3-ter-swagger-flat.json" 179 180 list=( $check_list ) 181 fixtures_count=${#list[@]} 182 okcr "Running codegen for ${fixtures_count} specs" 183 184 for spec in ${check_list}; do 185 testcase=${spec##*/} 186 case ${testcase} in 187 ${known_failed}) 188 warncr "[`date +%T`]${spec}: not tested against full build because of known issues." 189 run="false" 190 opts="" 191 buildClient="false" 192 noexpand="false" 193 ;; 194 ${known_skip_validation}) 195 infocr "[`date +%T`]${spec}: assumed spec is invalid but tested against build nonetheless..." 196 run="true" 197 opts="--skip-validation" 198 buildClient="true" 199 noexpand="false" 200 ;; 201 ${known_client_failure}) 202 warncr "[`date +%T`]${spec}: will not attempt to build the client because of known issues..." 203 run="true" 204 opts="" 205 buildClient="false" 206 noexpand="false" 207 ;; 208 ${known_expand_failure}) 209 warncr "[`date +%T`]${spec}: will not attempt to build with expand mode because of known issues..." 210 run="true" 211 opts="" 212 buildClient="true" 213 noexpand="true" 214 ;; 215 ${known_failed_minimal}) 216 warncr "[`date +%T`]${spec}: will not attempt to build with flatten minimal mode because of known issues..." 217 run="true" 218 opts="" 219 buildClient="true" 220 noflatten="true" 221 ;; 222 *) 223 infocr "[`date +%T`]${spec}: assumed valid and tested against build." 224 run="true" 225 opts="" 226 buildClient="true" 227 noexpand="false" 228 ;; 229 esac 230 231 if [[ ${run} == "true" ]]; then 232 declare -i index=0 233 for preprocessingOpts in ${OPTS} ; do 234 index+=1 235 infocr "Generation with options: ${preprocessingOpts} ${opts}" 236 237 # Do not attempt to generate on expanded specs when there is a discriminator specified 238 grep -q discriminator ${spec} 239 discriminated=$? 240 if [[ ${discriminated} -eq 0 && ${preprocessingOpts} == "--with-flatten=expand" ]] ; then 241 warncr "Skipped ${testcase} with ${preprocessingOpts}: discriminator not supported in this mode" 242 continue 243 fi 244 if [[ ${noexpand} != "true" && ${preprocessingOpts} == "--with-flatten=expand" ]] ; then 245 continue 246 fi 247 if [[ ${noflatten} == "true" && ( ${preprocessingOpts} == "--with-flatten=minimal" || ${preprocessingOpts} == "" ) ]] ; then 248 if [[ ${OPTS} == "--with-flatten=minimal" ]] ; then 249 preprocessingOpts="--with-flatten=full" 250 else 251 continue 252 fi 253 fi 254 255 target=${gendir}/gen-${testcase%.*}${index} 256 target_client=${gendir}/gen-${testcase%.*}${index}"-client" 257 258 server_name="nrcodegen" 259 client_name="nrcodegen" 260 errlog=${gendir}/stderr.log 261 262 rm -rf ${target} ${target_client} 263 mkdir -p ${target} ${target_client} 264 rm -f ${errlog} 265 266 # Gen server 267 if ! swagger generate server --spec ${spec} --target ${target} --name=${server_name} --quiet ${opts} ${preprocessingOpts} 2>${errlog} ; then 268 errcr "Generation Failed" 269 if [[ -f ${errlog} ]] ; then errcr "$(cat ${errlog})" ; rm ${errlog};fi 270 exit 1 271 fi 272 okcr "Generation server OK" 273 rm -f ${errlog} 274 # Gen client 275 276 if ! swagger generate client --spec "${spec}" --target "${target_client}" --name "${client_name}" --quiet ${opts} "${preprocessingOpts}" 2>${errlog}; then 277 errcr "Generation Failed" 278 if [[ -f ${errlog} ]] ; then errcr `cat ${errlog}` ; rm ${errlog};fi 279 exit 1 280 fi 281 okcr "Generation client OK" 282 283 # 284 # Check build on generated artifacts 285 # 286 287 # Build server 288 if ! (cd ${target}/cmd/${server_name}"-server"; go build) 2>${errlog} ; then 289 errcr "Server build Failed" 290 if [[ -f ${errlog} ]] ; then errcr `cat ${errlog}` ; rm ${errlog};fi 291 exit 1 292 fi 293 okcr "Server build OK" 294 # Build models if any produced 295 if [[ -d ${target}/models ]] ; then 296 297 if ! (cd ${target}/models ; go build) 2>${errlog}; then 298 errcr "Model build Failed" 299 if [[ -f ${errlog} ]] ; then errcr `cat ${errlog}` ; rm ${errlog};fi 300 exit 1 301 fi 302 fi 303 okcr "Models build OK" 304 # Build client 305 if [[ ${buildClient} == "false" ]] ; then 306 warncr "(no client built)" 307 else 308 if ! (cd ${target_client}/client ; go build) 2>${errlog} ; then 309 errcr "Client build Failed" 310 if [[ -f ${errlog} ]] ; then errcr `cat ${errlog}` ; rm ${errlog};fi 311 exit 1 312 fi 313 okcr "Client build OK" 314 fi 315 successcr " [All builds for ${spec}: OK]" 316 rm -f ${errlog} 317 rm -rf ${target} ${target_client} 318 done 319 fi 320 done 321 rm -rf ${gendir} 322 exit 0