From bc70c90b8d6bb6c9acca6445e56f1dd4c820eea9 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Mon, 11 Mar 2019 19:04:47 -0400 Subject: ob_parse_changelog(): Pass args, not global vars, to cb And update test file. --- diff --git a/lib/changelog.sh b/lib/changelog.sh index 0fc07b5..bced08d 100644 --- a/lib/changelog.sh +++ b/lib/changelog.sh @@ -1,6 +1,6 @@ # Functions for parsing changelogs # -# Copyright (C) 2012, 2018 Patrick McDermott +# Copyright (C) 2012, 2018, 2019 Patrick McDermott # # This file is part of opkbuild. # @@ -17,13 +17,6 @@ # You should have received a copy of the GNU General Public License # along with opkbuild. If not, see . -OB_CHANGELOG_SOURCE= -OB_CHANGELOG_VERSION= -OB_CHANGELOG_DISTRIBUTION= -OB_CHANGELOG_CHANGES= -OB_CHANGELOG_MAINTAINER= -OB_CHANGELOG_DATE= - _ob_parse_changelog_error() { local file= @@ -68,24 +61,20 @@ _ob_get_changelog_expect_str() ## specified at ## . For ## each version entry, \fBob_parse_changelog\fP calls \fIentry_cb\fP -## after setting \fIOB_CHANGELOG_SOURCE\fP to the source package name, -## \fIOB_CHANGELOG_VERSION\fP to the source package version, -## \fIOB_CHANGELOG_DISTRIBUTION\fP to the list of distributions into -## which the package should be installed, \fIOB_CHANGELOG_CHANGES\fP to -## the formatted heading (source package name, source package version, -## and distributions list) and change details, -## \fIOB_CHANGELOG_MAINTAINER\fP to the name and e-mail address of the -## package maintainer, and \fPOB_CHANGELOG_DATE\fP to the date of -## packaging. \fBob_parse_changelog\fP stops parsing either at the end -## of the \fIchangelog\fP file or when \fIentry_cb\fP returns non-zero. +## with the source package name, the source package version, the list +## of distributions into which the package should be installed, the +## formatted heading (source package name, source package version, and +## distributions list) and change details, the name and e-mail address +## of the package maintainer, and the date of packaging as arguments. +## \fBob_parse_changelog\fP stops parsing either at the end of the +## \fIchangelog\fP file or when \fIentry_cb\fP returns non-zero. ## @operand file req The file to parse, or "-" for standard input. ## @operand entry_cb req Callback to run for each entry. ## @return Returns 0 after parsing, or 125 on missing arguments. ## @stderr Prints error messages on parse errors. -## @pure maybe This function sets global variables, but they're only intended -## for use by \fIentry_cb\fP, which is called during the lifetime of -## this function. Whether this function is subshell-safe in -## practice depends on whether \fIentry_cb\fP is subshell-safe. +## @pure maybe This function has no side effects. Whether this function is +## subshell-safe in practice depends on whether \fIentry_cb\fP is +## subshell-safe. ob_parse_changelog() { local file= @@ -94,6 +83,7 @@ ob_parse_changelog() local line= local line_= local expect= + local changes= local blank_lines= local source= local distribution= @@ -118,7 +108,7 @@ ob_parse_changelog() case "${line}" in '') if [ "${expect}" = 'start_changes' ]; then - OB_CHANGELOG_CHANGES="${OB_CHANGELOG_CHANGES}${_OB_LF}${line}" + changes="${changes}${_OB_LF}${line}" elif [ "${expect}" = 'next_or_eof' ]; then : elif [ "${expect}" != 'changes_or_trailer' ]; then @@ -146,10 +136,10 @@ ob_parse_changelog() "${version% *}" != "${version}" ]; then _ob_parse_changelog_error "${file}" "${line_nr}" \ 'changelog_bad_heading' - OB_CHANGELOG_SOURCE= - OB_CHANGELOG_VERSION= - OB_CHANGELOG_DISTRIBUTION= - OB_CHANGELOG_CHANGES= + source='' + version='' + distribution='' + changes='' else if ! ob_validate_source_name "${source}"; then _ob_parse_changelog_error \ @@ -161,13 +151,10 @@ ob_parse_changelog() "${file}" "${line_nr}" \ 'changelog_bad_source_version' "${version}" fi - OB_CHANGELOG_SOURCE="${source}" - OB_CHANGELOG_VERSION="${version}" - OB_CHANGELOG_DISTRIBUTION="${distribution}" - OB_CHANGELOG_CHANGES="${line}" + changes="${line}" fi expect='start_changes' - blank_lines= + blank_lines='' ;; ' -- '*) if [ "${expect}" != 'changes_or_trailer' ]; then @@ -182,18 +169,18 @@ ob_parse_changelog() "${maintainer}" = "${date}" ]; then _ob_parse_changelog_error "${file}" "${line_nr}" \ 'changelog_bad_trailer' - OB_CHANGELOG_MAINTAINER= - OB_CHANGELOG_DATE= - elif [ -n "${OB_CHANGELOG_SOURCE}" ]; then - OB_CHANGELOG_MAINTAINER="${maintainer}" - OB_CHANGELOG_DATE="${date}" - "${entry_cb}" + maintainer='' + date='' + elif [ -n "${source}" ]; then + "${entry_cb}" "${source}" "${version}" \ + "${distribution}" "${changes}" \ + "${maintainer}" "${date}" if [ ${?} -ne 0 ]; then return 0 fi fi expect='next_or_eof' - blank_lines= + blank_lines='' ;; ' --'*) _ob_parse_changelog_error "${file}" "${line_nr}" \ @@ -206,14 +193,14 @@ ob_parse_changelog() 'changelog_found_change' \ "$(_ob_get_changelog_expect_str "${expect}")" fi - OB_CHANGELOG_CHANGES="${OB_CHANGELOG_CHANGES}${_OB_LF}${blank_lines}${line}" + changes="${changes}${_OB_LF}${blank_lines}${line}" expect='changes_or_trailer' - blank_lines= + blank_lines='' ;; *) _ob_parse_changelog_error "${file}" "${line_nr}" \ 'changelog_bad_line' - blank_lines= + blank_lines='' ;; esac done <<-EOF diff --git a/tests/ob_parse_changelog.sh b/tests/ob_parse_changelog.sh index 8c29042..d406d6c 100755 --- a/tests/ob_parse_changelog.sh +++ b/tests/ob_parse_changelog.sh @@ -1,6 +1,6 @@ # Tests for ob_parse_changelog() # -# Copyright (C) 2018 Patrick McDermott +# Copyright (C) 2018, 2019 Patrick McDermott # # This file is part of opkbuild. # @@ -68,28 +68,44 @@ is() entry_1() { - is 'source' "${OB_CHANGELOG_SOURCE}" 'bar' - is 'version' "${OB_CHANGELOG_VERSION}" '4' - is 'distribution' "${OB_CHANGELOG_DISTRIBUTION}" 'dev' - is 'changes' "${OB_CHANGELOG_CHANGES}" "$(cat <<-EOF + local source="${1}" + local version="${2}" + local distribution="${3}" + local changes="${4}" + local maintainer="${5}" + local date="${6}" + shift 6 + + is 'source' "${source}" 'bar' + is 'version' "${version}" '4' + is 'distribution' "${distribution}" 'dev' + is 'changes' "${changes}" "$(cat <<-EOF bar (4) dev * Non-maintainer upload. Rename package and upload elsewhere. EOF )" - is 'maintainer' "${OB_CHANGELOG_MAINTAINER}" \ + is 'maintainer' "${maintainer}" \ '"K. Random Hacker" ' - is 'date' "${OB_CHANGELOG_DATE}" 'Sun, 04 Jan 1970 00:00:00 +0000' + is 'date' "${date}" 'Sun, 04 Jan 1970 00:00:00 +0000' return 0 } entry_2() { - is 'source' "${OB_CHANGELOG_SOURCE}" 'foo' - is 'version' "${OB_CHANGELOG_VERSION}" '3' - is 'distribution' "${OB_CHANGELOG_DISTRIBUTION}" 'trunk' - is 'changes' "${OB_CHANGELOG_CHANGES}" "$(cat <<-EOF + local source="${1}" + local version="${2}" + local distribution="${3}" + local changes="${4}" + local maintainer="${5}" + local date="${6}" + shift 6 + + is 'source' "${source}" 'foo' + is 'version' "${version}" '3' + is 'distribution' "${distribution}" 'trunk' + is 'changes' "${changes}" "$(cat <<-EOF foo (3) trunk @@ -97,26 +113,34 @@ entry_2() * Break all the things. EOF )" - is 'maintainer' "${OB_CHANGELOG_MAINTAINER}" \ + is 'maintainer' "${maintainer}" \ '"J. Random Hacker" ' - is 'date' "${OB_CHANGELOG_DATE}" 'Sat, 03 Jan 1970 00:00:00 +0000' + is 'date' "${date}" 'Sat, 03 Jan 1970 00:00:00 +0000' return 0 } entry_3() { - is 'source' "${OB_CHANGELOG_SOURCE}" 'foo' - is 'version' "${OB_CHANGELOG_VERSION}" '2' - is 'distribution' "${OB_CHANGELOG_DISTRIBUTION}" 'trunk' - is 'changes' "${OB_CHANGELOG_CHANGES}" "$(cat <<-EOF + local source="${1}" + local version="${2}" + local distribution="${3}" + local changes="${4}" + local maintainer="${5}" + local date="${6}" + shift 6 + + is 'source' "${source}" 'foo' + is 'version' "${version}" '2' + is 'distribution' "${distribution}" 'trunk' + is 'changes' "${changes}" "$(cat <<-EOF foo (2) trunk * Reticulated splines. EOF )" - is 'maintainer' "${OB_CHANGELOG_MAINTAINER}" \ + is 'maintainer' "${maintainer}" \ '"J. Random Hacker" ' - is 'date' "${OB_CHANGELOG_DATE}" 'Fri, 02 Jan 1970 00:00:00 +0000' + is 'date' "${date}" 'Fri, 02 Jan 1970 00:00:00 +0000' return 1 } @@ -129,7 +153,7 @@ entry_4() entry_cb() { entry=$((${entry} + 1)) - "entry_${entry}" + "entry_${entry}" "${@}" } ob_parse_changelog - entry_cb <<-EOF -- cgit v0.9.1