blob: cbbc043e0efb2eaae186c404abd1ccf9744c9a9e [file] [log] [blame]
Fatih Degirmenci44122b32020-03-11 13:03:25 +01001#!/bin/bash
2
3# ============LICENSE_START=======================================================
4# Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5# ================================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# SPDX-License-Identifier: Apache-2.0
19# ============LICENSE_END=========================================================
20
21set -o nounset
22set -o errexit
23set -o pipefail
24
25#-------------------------------------------------------------------------------
26# Various tests are run using tox such as docs, yamllint, ansible-lint, and
27# shellcheck. Some repos do not have certain types of files and those repos
28# are ignored using REPOS_TO_IGNORE variable which is controlled within the
29# jjb per lint type.
30#-------------------------------------------------------------------------------
31run_tox() {
32
33 echo "Info : Preparing to run tox for the repo $GERRIT_PROJECT"
34
Fatih Degirmenci072f94d2020-03-12 09:21:37 +000035 # set and export defaults
Fatih Degirmenci44122b32020-03-11 13:03:25 +010036 LINT_TYPE="${LINT_TYPE:-ansible-lint}"
Fatih Degirmenci072f94d2020-03-12 09:21:37 +000037 REPOS_TO_IGNORE="${REPOS_TO_IGNORE:-none}"
38 VERBOSITY=${VERBOSITY:-false}
39 export LINT_TYPE REPOS_TO_IGNORE VERBOSITY
Fatih Degirmenci44122b32020-03-11 13:03:25 +010040
41 # check if the change is for a repo we should ignore
42 if [[ "$REPOS_TO_IGNORE" =~ "$GERRIT_PROJECT" ]]; then
43 echo "Info : Ignoring change as $GERRIT_PROJECT is in ignore list!"
44 echo "Info : Done!"
45 exit 0
46 fi
47
48 # ensure we are in job build WORKSPACE
49 cd "$WORKSPACE"
50
51 # set DEBIAN_FRONTEND to run apt non-interactively
52 DEBIAN_FRONTEND=noninteractive
53 export DEBIAN_FRONTEND
54
55 # install dependencies
Fatih Degirmenci82107d22020-06-09 15:59:24 +020056 echo "Info : Install python3.7-minimal python3-distutils virtualenv using apt"
Fatih Degirmenci44122b32020-03-11 13:03:25 +010057 redirect_cmd sudo apt update
Fatih Degirmenci82107d22020-06-09 15:59:24 +020058 redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y python3.7-minimal python3-distutils virtualenv
Fatih Degirmenci44122b32020-03-11 13:03:25 +010059
60 # create and activate virtualenv
61 echo "Info : Create and activate python virtualenv"
Fatih Degirmenci82107d22020-06-09 15:59:24 +020062 redirect_cmd virtualenv -p python3 .venv
Fatih Degirmenci44122b32020-03-11 13:03:25 +010063 set +u
64 redirect_cmd source .venv/bin/activate
65 set -u
66
Fatih Degirmenci9714e3e2020-03-12 13:09:19 +000067 # install only tox since the rest of the requirements are installed by tox itself
68 TOX_PACKAGE=$(grep "^tox==" test-requirements.txt)
69 echo "Info : Install $TOX_PACKAGE"
70 redirect_cmd pip install --force-reinstall "$TOX_PACKAGE"
Fatih Degirmenci44122b32020-03-11 13:03:25 +010071
72 # run tox
73 echo "Info : Run $LINT_TYPE using tox"
74 echo "----------------------------------------------------"
75 tox -e "$LINT_TYPE"
76 echo "----------------------------------------------------"
77 echo "Info : Done!"
78
79}
80#-------------------------------------------------------------------------------
81# In some cases, it is useful to see all the output generated by commands so
82# this function makes it possible for users to achieve that by not redirecting
83# output to /dev/null when verbosity is enabled
84#-------------------------------------------------------------------------------
85redirect_cmd() {
86
87 if [[ "$VERBOSITY" == "false" ]]; then
88 "$@" > /dev/null 2>&1
89 else
90 "$@"
91 fi
92
93}
94#-------------------------------------------------------------------------------
95# run tox
96#-------------------------------------------------------------------------------
97
98run_tox "$@"
99
100# vim: set ts=2 sw=2 expandtab: