From 4e78a763ca6a9a6f0f369e873780b98fe4965fd3 Mon Sep 17 00:00:00 2001 From: Rezart Qelibari Date: Fri, 11 Feb 2022 16:05:02 +0100 Subject: [PATCH] Install selected modules --- install.sh | 27 +++++++++- spec/generateModuleOptions_spec.sh | 82 ++++++++++++++++++++++++++++++ spec/installModules_spec.sh | 36 +++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 spec/generateModuleOptions_spec.sh create mode 100644 spec/installModules_spec.sh diff --git a/install.sh b/install.sh index f266a1c..fa99754 100755 --- a/install.sh +++ b/install.sh @@ -149,7 +149,6 @@ function answerQuestionsFromConfigOrAskUser() { function askNecessaryQuestions() { local mod - local -A answers for mod in "${modulesToInstall[@]}"; do local -A questions=() populateQuestionsWithModuleRequiredInformation @@ -172,6 +171,30 @@ function loadModules() { [ "${list}" = true ] && printModulesToInstall } +function generateModuleOptions() { + local value answerKey optionKey + for answerKey in ${(k)answers}; do + [[ ${answerKey} = ${mod}_* ]] || continue + optionKey="${answerKey#${mod}_}" + value="${answers[${answerKey}]}" + if [[ "${optionKey}" =~ ^[[:alpha:]]$ ]]; then + moduleOptions+=("-${optionKey}" "${value}") + elif [[ "${optionKey}" =~ ^[[:alpha:]][-[:alpha:]]+$ ]]; then + moduleOptions+=("--${optionKey}" "${value}") + else + moduleOptions+=("${optionKey}" "${value}") + fi + done +} + +function installModules() { + local mod moduleOptions + for mod in "${modulesToInstall[@]}"; do + generateModuleOptions + runModule "${mod}" ${moduleOptions} + done +} + function main() { eval "`docopts -f -V - -h - : "$@" <<- USAGE Usage: $0 [options] [-m PATH]... [...] @@ -192,10 +215,12 @@ function main() { License EUPL-1.2. There is NO WARRANTY, to the extent permitted by law. USAGE`" local allModules=() modulesToInstall=() + local -A answers ensureDocopts autoloadZShLib loadModules askNecessaryQuestions + installModules lop debug "Current working dir is: `pwd`" } diff --git a/spec/generateModuleOptions_spec.sh b/spec/generateModuleOptions_spec.sh new file mode 100644 index 0000000..8de1c9a --- /dev/null +++ b/spec/generateModuleOptions_spec.sh @@ -0,0 +1,82 @@ +Describe 'generateModuleOptions' + Include ./install.sh + + It 'does nothing if answers array is empty' + declare -A answers=() + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '' + The status should be success + End + + It 'does nothing if answers does not contain module answers' + declare -A answers=('some-module_name' 'answer') + mod='module/my-module' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '' + The status should be success + End + + It 'prefixes single char option names with a dash' + declare -A answers=('mymodule_n' 'my name') + mod='mymodule' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '-n my name' + The status should be success + End + + It 'does not prefix single char option name with dash if it is already a dash' + declare -A answers=('mymodule_-' 'my name') + mod='mymodule' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '- my name' + The status should be success + End + + It 'prefixes multi char option names with double dash' + declare -A answers=('mymodule_your-name' 'my name') + mod='mymodule' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '--your-name my name' + The status should be success + End + + It 'does not prefix multi char option names with double dash if it starts with a dash' + declare -A answers=('mymodule_-your-name' 'my name') + mod='mymodule' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '-your-name my name' + The status should be success + End + + It 'works with modules that contains slashes' + declare -A answers=('/some/dir/mymodule_your-name' 'my name') + mod='/some/dir/mymodule' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '--your-name my name' + The status should be success + End + + It 'works with modules that contains spaces' + declare -A answers=('/some/dir with spaces/mymodule with spaces_your-name' 'my name') + mod='/some/dir with spaces/mymodule with spaces' + moduleOptions=() + When call generateModuleOptions + The output should eq '' + The variable 'moduleOptions' should eq '--your-name my name' + The status should be success + End +End diff --git a/spec/installModules_spec.sh b/spec/installModules_spec.sh new file mode 100644 index 0000000..6ced6c4 --- /dev/null +++ b/spec/installModules_spec.sh @@ -0,0 +1,36 @@ +Describe 'installModules' + Include ./install.sh + output=() + runModule() { output=("$@") } + + It 'does nothing if modules array is empty' + declare -A modulesToInstall=() answers=() + called=false + generateModuleOptions() { called=true } + When call installModules + The output should eq '' + The variable 'called' should eq 'false' + The status should be success + End + + It 'calls the module without options if answers is empty' + declare -A answers=() + modulesToInstall=('/modules/my module') + When call installModules + The output should eq '' + The variable 'output[1]' should eq '/modules/my module' + The status should be success + End + + It 'calls the module with given answers as options' + declare -A answers=('/modules/my module_name' 'hercules') + modulesToInstall=('/modules/my module') + When call installModules + The output should eq '' + The variable 'output' should eq '/modules/my module --name hercules' + The variable 'output[1]' should eq '/modules/my module' + The variable 'output[2]' should eq '--name' + The variable 'output[3]' should eq 'hercules' + The status should be success + End +End