From a02480eb3b6b8a52084ef6ed633cdc4586947f2f Mon Sep 17 00:00:00 2001 From: Olivier Dugas Date: Fri, 29 Sep 2017 09:49:56 -0400 Subject: [PATCH] cqfd: config_load: avoid a for loop in flavor detection It is much simpler to grep the flavor option in the list of flavors instead of comparing value by value in a for loop. A test is added to make sure partial flavors won't be interpreted as existing flavors. --- cqfd | 18 +++++------------- tests/05-cqfd_init_flavor | 11 +++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cqfd b/cqfd index d8565a5..b43e797 100755 --- a/cqfd +++ b/cqfd @@ -233,8 +233,6 @@ make_archive() { # config_load() - load build settings from cqfdrc # $1: optional "flavor" of the build, is a suffix of command. config_load() { - local p_flavor="$1" - IFS="$IFS" parse_ini_config_file "$cqfdrc" cfg.section.project # load the [project] section @@ -245,18 +243,12 @@ config_load() { # build parameters may be overriden by a flavor defined in the # build section's 'flavors' parameter. - if [ -n "$p_flavor" ]; then - for flavor in $flavors; do - if [ "$flavor" = "$p_flavor" ]; then - local _found=1 - break - fi - done - - if [ -n "$_found" ]; then - cfg.section."$p_flavor" # load the [$p_flavor] section + local flavor="$1" + if [ -n "$flavor" ]; then + if grep -qw "$flavor" <<< "$flavors"; then + cfg.section."$flavor" # load the [$flavor] section else - die "flavor \"$p_flavor\" not found in flavors list" + die "flavor \"$flavor\" not found in flavors list" fi fi diff --git a/tests/05-cqfd_init_flavor b/tests/05-cqfd_init_flavor index 1eb864e..213cff7 100755 --- a/tests/05-cqfd_init_flavor +++ b/tests/05-cqfd_init_flavor @@ -21,6 +21,17 @@ else jtest_result fail fi +################################################################################ +# 'cqfd init' with invalid flavor should fail +################################################################################ +flavorPart="${flavor:0:2}" +jtest_prepare "cqfd init using part of '$flavor' flavor should fail" +if $cqfd -b $flavorPart init; then + jtest_result fail +else + jtest_result pass +fi + ################################################################################ # 'cqfd init' without flavor generates our regular container ################################################################################