|  | 
|  | 1 | +#!/bin/bash | 
|  | 2 | +# Software Name: floss-toolbox | 
|  | 3 | +# SPDX-FileCopyrightText: Copyright (c) 2020-2023 Orange | 
|  | 4 | +# SPDX-License-Identifier: Apache-2.0 | 
|  | 5 | +# | 
|  | 6 | +# This software is distributed under the Apache 2.0 license. | 
|  | 7 | +# | 
|  | 8 | +# Author: Pierre-Yves LAPERSONNE <pierreyves(dot)lapersonne(at)orange(dot)com> et al. | 
|  | 9 | + | 
|  | 10 | +# Since...............: 10/03/2023 | 
|  | 11 | +# Description.........: Make a dry-run of the LicensesIventory module to check if everything is ready to use | 
|  | 12 | +# Version.............: 1.0.0 | 
|  | 13 | + | 
|  | 14 | +set -eu | 
|  | 15 | + | 
|  | 16 | +# Couts | 
|  | 17 | +# ----- | 
|  | 18 | + | 
|  | 19 | +NUMBER_OF_CHECKS=0 | 
|  | 20 | +NUMBER_OF_SUCCESS=0 | 
|  | 21 | +NUMBER_OF_WARNINGS=0 | 
|  | 22 | +NUMBER_OF_ERRORS=0 | 
|  | 23 | + | 
|  | 24 | +# Utils | 
|  | 25 | +# ----- | 
|  | 26 | + | 
|  | 27 | +# $1 - File name to test | 
|  | 28 | +CheckIfFileExists(){ | 
|  | 29 | +    if [ ! -f "$1" ]; then | 
|  | 30 | +        echo "⛔  ERROR: The file '$1' does not exist" | 
|  | 31 | +        NUMBER_OF_ERRORS=$((NUMBER_OF_ERRORS+1)) | 
|  | 32 | +    else | 
|  | 33 | +        echo "✅  Cool! The file '$1' exists" | 
|  | 34 | +        NUMBER_OF_SUCCESS=$((NUMBER_OF_SUCCESS+1)) | 
|  | 35 | +    fi | 
|  | 36 | +    NUMBER_OF_CHECKS=$((NUMBER_OF_CHECKS+1)) | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +# $1 - FiDirectory name to test | 
|  | 40 | +CheckIfDirectoryExists(){ | 
|  | 41 | +    if [ ! -d "$1" ]; then | 
|  | 42 | +        echo "⛔  ERROR: The directory '$1' does not exist" | 
|  | 43 | +        NUMBER_OF_ERRORS=$((NUMBER_OF_ERRORS+1))         | 
|  | 44 | +    else | 
|  | 45 | +        echo "✅  Cool! The directory '$1' exists"     | 
|  | 46 | +        NUMBER_OF_SUCCESS=$((NUMBER_OF_SUCCESS+1))   | 
|  | 47 | +    fi | 
|  | 48 | +    NUMBER_OF_CHECKS=$((NUMBER_OF_CHECKS+1)) | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +# $1 - Runtime name | 
|  | 52 | +# $2 - Command to check runtime | 
|  | 53 | +# $3 - Expected / suggested version | 
|  | 54 | +CheckIfRuntimeExists(){ | 
|  | 55 | +    runtime_version=`$2` | 
|  | 56 | +    if [ $? != "0" ]; then # Exist status != 0 so runtime version check failed ; we assume the runtime is missing | 
|  | 57 | +        echo "❌  WARNING: It seems '$1' is not ready" | 
|  | 58 | +        NUMBER_OF_ERRORS=$((NUMBER_OF_ERRORS+1))         | 
|  | 59 | +    else  | 
|  | 60 | +        echo "✅  Cool! '$1' is available" | 
|  | 61 | +        echo -e "\t🔎  You should check if the version is at least '$3'" | 
|  | 62 | +        NUMBER_OF_SUCCESS=$((NUMBER_OF_SUCCESS+1)) | 
|  | 63 | +    fi | 
|  | 64 | +    NUMBER_OF_CHECKS=$((NUMBER_OF_CHECKS+1)) | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +# $1 - Ruby configuration file | 
|  | 68 | +# $2 - Key to test | 
|  | 69 | +CheckIfConfigurationKeyDefined(){ | 
|  | 70 | +    CONFIG_KEY=`cat $1 | grep $2 | cut -d= -f2 | tr -d '"' | sed 's/ //g'` | 
|  | 71 | +    if [ "$CONFIG_KEY" == "" ]; then | 
|  | 72 | +        echo "❔  WARNING: It seems '$2' is not defined in $1" | 
|  | 73 | +        NUMBER_OF_WARNINGS=$((NUMBER_OF_WARNINGS+1)) | 
|  | 74 | +    else | 
|  | 75 | +        echo "✅  Cool! '$2' is defined" | 
|  | 76 | +        NUMBER_OF_SUCCESS=$((NUMBER_OF_SUCCESS+1)) | 
|  | 77 | +    fi | 
|  | 78 | +    NUMBER_OF_CHECKS=$((NUMBER_OF_CHECKS+1)) | 
|  | 79 | +} | 
|  | 80 | + | 
|  | 81 | +# $1 - The name of the Python module to test | 
|  | 82 | +CheckIfPythonModuleInstalled(){ | 
|  | 83 | +    if [ "$#" -ne 1 ]; then | 
|  | 84 | +        echo "❌  WARNING: It seems '$1' is not ready" | 
|  | 85 | +        NUMBER_OF_ERRORS=$((NUMBER_OF_ERRORS+1))   | 
|  | 86 | +    else | 
|  | 87 | +        python3 -c "import $1" | 
|  | 88 | +        result=$? | 
|  | 89 | +        if [ $result == "0" ]; then | 
|  | 90 | +            echo "✅  Cool! Python module '$1' is available" | 
|  | 91 | +            NUMBER_OF_SUCCESS=$((NUMBER_OF_SUCCESS+1)) | 
|  | 92 | +        else | 
|  | 93 | +            echo "❌  WARNING: It seems Python modyle '$1' is not installed" | 
|  | 94 | +            NUMBER_OF_ERRORS=$((NUMBER_OF_ERRORS+1))   | 
|  | 95 | +        fi | 
|  | 96 | +    fi | 
|  | 97 | +    NUMBER_OF_CHECKS=$((NUMBER_OF_CHECKS+1)) | 
|  | 98 | +} | 
|  | 99 | +# Licenses Inventory tool | 
|  | 100 | +# ----------------------- | 
|  | 101 | + | 
|  | 102 | +echo -e "\n------------------------------------------" | 
|  | 103 | +echo "Assertions for the Licenses Inventory tool" | 
|  | 104 | +echo "------------------------------------------" | 
|  | 105 | + | 
|  | 106 | +echo -e "\nCheck if main folders exist..." | 
|  | 107 | +CheckIfDirectoryExists "./sources" | 
|  | 108 | +CheckIfDirectoryExists "./tests" | 
|  | 109 | + | 
|  | 110 | +echo -e "\nCheck sources files..." | 
|  | 111 | +CheckIfFileExists "./sources/common/__init__.py" | 
|  | 112 | +CheckIfFileExists "./sources/common/datas.py" | 
|  | 113 | +CheckIfFileExists "./sources/common/files.py" | 
|  | 114 | +CheckIfFileExists "./sources/common/filters.py" | 
|  | 115 | +CheckIfFileExists "./sources/common/names.py" | 
|  | 116 | +CheckIfFileExists "./sources/configuration/__init__.py" | 
|  | 117 | +CheckIfFileExists "./sources/configuration/config.py" | 
|  | 118 | +CheckIfFileExists "./sources/dependencies/__init__.py" | 
|  | 119 | +CheckIfFileExists "./sources/dependencies/dependencies.py" | 
|  | 120 | +CheckIfFileExists "./sources/dependencies/parsings.py" | 
|  | 121 | +CheckIfFileExists "./sources/search/__init__.py" | 
|  | 122 | +CheckIfFileExists "./sources/search/downloads.py" | 
|  | 123 | +CheckIfFileExists "./sources/search/parsings.py" | 
|  | 124 | +CheckIfFileExists "./sources/search/search.py" | 
|  | 125 | +CheckIfFileExists "./sources/__init__.py" | 
|  | 126 | +CheckIfFileExists "./sources/main.py" | 
|  | 127 | +CheckIfFileExists "./config.ini" | 
|  | 128 | + | 
|  | 129 | +echo -e "\nCheck integration test files..." | 
|  | 130 | +CheckIfFileExists "./tests/integrationtests/data/gradle/dependency_github.gradle" | 
|  | 131 | +CheckIfFileExists "./tests/integrationtests/data/gradle/dependency_maven_central.gradle" | 
|  | 132 | +CheckIfFileExists "./tests/integrationtests/data/gradle/license_github.json" | 
|  | 133 | +CheckIfFileExists "./tests/integrationtests/data/gradle/license_maven_central.pom" | 
|  | 134 | +CheckIfFileExists "./tests/integrationtests/data/gradle/version_maven_central.json" | 
|  | 135 | +CheckIfFileExists "./tests/integrationtests/data/package_json/license_package_json.html" | 
|  | 136 | +CheckIfFileExists "./tests/integrationtests/data/package_json/package.json" | 
|  | 137 | +CheckIfFileExists "./tests/integrationtests/data/roast/Cargo.lock" | 
|  | 138 | +CheckIfFileExists "./tests/integrationtests/data/config.ini" | 
|  | 139 | +CheckIfFileExists "./tests/integrationtests/test_search.py" | 
|  | 140 | + | 
|  | 141 | +echo -e "\nCheck unit test files..." | 
|  | 142 | +CheckIfFileExists "./tests/unittests/data/config/config_no_data.ini" | 
|  | 143 | +CheckIfFileExists "./tests/unittests/data/config/config.ini" | 
|  | 144 | +CheckIfFileExists "./tests/unittests/data/get_content_by_name/my_gradle_file.txt" | 
|  | 145 | +CheckIfFileExists "./tests/unittests/data/get_content_by_name/package.json" | 
|  | 146 | +CheckIfFileExists "./tests/unittests/data/gradle/license_github.json" | 
|  | 147 | +CheckIfFileExists "./tests/unittests/data/gradle/license_maven_central.pom" | 
|  | 148 | +CheckIfFileExists "./tests/unittests/data/gradle/version.json" | 
|  | 149 | +CheckIfFileExists "./tests/unittests/data/package_json/license_package_json.html" | 
|  | 150 | +CheckIfFileExists "./tests/unittests/data/roast/license_roast.json" | 
|  | 151 | +CheckIfFileExists "./tests/unittests/data/dependency_a.txt" | 
|  | 152 | +CheckIfFileExists "./tests/unittests/data/dependency_b.txt" | 
|  | 153 | +CheckIfFileExists "./tests/unittests/data/filename_by_name.test" | 
|  | 154 | +CheckIfFileExists "./tests/unittests/data/files_read.txt" | 
|  | 155 | +CheckIfFileExists "./tests/unittests/test_config.py" | 
|  | 156 | +CheckIfFileExists "./tests/unittests/test_dependency.py" | 
|  | 157 | +CheckIfFileExists "./tests/unittests/test_files_check_the_directory.py" | 
|  | 158 | +CheckIfFileExists "./tests/unittests/test_files_get_the_filenames_by_name.py" | 
|  | 159 | +CheckIfFileExists "./tests/unittests/test_files_write_and_read.py" | 
|  | 160 | +CheckIfFileExists "./tests/unittests/test_filter.py" | 
|  | 161 | +CheckIfFileExists "./tests/unittests/test_parsing_download.py" | 
|  | 162 | +CheckIfFileExists "./tests/unittests/test_parsing.py" | 
|  | 163 | + | 
|  | 164 | +# Runtimes and tools | 
|  | 165 | +# ------------------ | 
|  | 166 | + | 
|  | 167 | +echo -e "\n-----------------------" | 
|  | 168 | +echo "Assertions for runtimes" | 
|  | 169 | +echo "-----------------------" | 
|  | 170 | + | 
|  | 171 | +echo -e "\nCheck for Python3..." | 
|  | 172 | +CheckIfRuntimeExists "Python3" "python3 --version" "3.8.5" | 
|  | 173 | + | 
|  | 174 | +echo -e "\nCheck for Python modules" | 
|  | 175 | +CheckIfPythonModuleInstalled "requests" | 
|  | 176 | +CheckIfPythonModuleInstalled "xmltodict" | 
|  | 177 | +CheckIfPythonModuleInstalled "pytest" | 
|  | 178 | + | 
|  | 179 | +# Units tests | 
|  | 180 | +# ----------- | 
|  | 181 | + | 
|  | 182 | +echo -e "\n----------------------------------" | 
|  | 183 | +echo "Run of LicensesInventory unit test" | 
|  | 184 | +echo "----------------------------------" | 
|  | 185 | + | 
|  | 186 | +echo -e "\nRunning integration tests..." | 
|  | 187 | +python3 -m pytest ./tests/integrationtests/*.py | 
|  | 188 | + | 
|  | 189 | +echo -e "\nRunning unit tests..." | 
|  | 190 | +python3 -m pytest ./tests/unittests/*.py | 
|  | 191 | + | 
|  | 192 | +# Conclusion | 
|  | 193 | +# ---------- | 
|  | 194 | + | 
|  | 195 | +echo -e "\n----------" | 
|  | 196 | +echo "Conclusion" | 
|  | 197 | +echo "----------" | 
|  | 198 | + | 
|  | 199 | +echo -e "\nDry-run done! See the logs above to check all points controls." | 
|  | 200 | +echo -e "\tNumber of controls.......: $NUMBER_OF_CHECKS" | 
|  | 201 | +echo -e "\tNumber of success........: $NUMBER_OF_SUCCESS" | 
|  | 202 | +echo -e "\tNumber of warnings.......: $NUMBER_OF_WARNINGS" | 
|  | 203 | +echo -e "\tNumber of errors.........: $NUMBER_OF_ERRORS" | 
0 commit comments