fix(uti): Fixing broken code part V
[csit.git] / resources / libraries / bash / entry / check / model_version.sh
1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 set -exuo pipefail
15
16 # This file should be executed from tox, as the assumed working directory
17 # is different from where this file is located.
18 # This file does not have executable flag nor shebang,
19 # to dissuade non-tox callers.
20
21 # This script runs a two grep commands as "if" conditions,
22 # using log files to store the data (generated by git commands) to grep,
23 # failing when model implementation edits do not come with model version edit.
24 # The contents of the log files may be useful when fail cause is not obvious.
25
26 # "set -eu" handles failures from the following two lines.
27 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
28 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
29 source "${BASH_FUNCTION_DIR}/common.sh" || {
30     echo "Source failed." >&2
31     exit 1
32 }
33
34 impl_log="edited_files.log"
35 git diff --name-only HEAD~ > "${impl_log}"
36 if ! grep -q '^docs/model/current/schema/test_case*' "${impl_log}"; then
37     # Failing grep means no model edits.
38     warn "No model implementation edits detected."
39     warn
40     warn "CSIT model version checker: PASS"
41     exit 0
42 fi
43 const_log="constants_edits.log"
44 git diff -U0 HEAD~ -- "resources/libraries/python/Constants.py" > "${const_log}"
45 if ! grep -q '^\+    MODEL_VERSION = ' "${const_log}"; then
46     warn "Model implementation edits without version edit detected!"
47     warn "See ${impl_log} and ${const_log} for what was detected."
48     warn
49     warn "CSIT model version checker: FAIL"
50     exit 1
51 fi
52 doc_log="docs_edits.log"
53 git diff -U0 HEAD~ -- "docs/model/current/top.rst" > "${doc_log}"
54 if ! grep -q '^\+This document is valid for CSIT model' "${doc_log}"; then
55     warn "Model implementation edits without documentation update detected!"
56     warn "See ${impl_log}, ${const_log} and ${doc_log} for what was detected."
57     warn
58     warn "CSIT model version checker: FAIL"
59     exit 1
60 fi
61 # TODO: Check constants and docs are specifying the same version.
62 warn "Model version and doc are edited, model implementation edits are allowed."
63 warn
64 warn "CSIT model version checker: PASS"
65 exit 0