From c67bd58e7937afa0cff3e9ce94e4d5d3a3a14a3d Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Sat, 24 Sep 2022 16:37:59 -0500 Subject: [PATCH] %ansible_collection_url: Don't require control macros Reimplement %ansible_collection_url to accept the collection namespace and name as arguments instead of requiring oblique control macros. This also adds some basic tests to ensure that the macro behaves properly. --- ansible-packaging.spec | 51 ++++++++++++++++++++++++++++++++++++++++++ macros.ansible-srpm | 26 ++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/ansible-packaging.spec b/ansible-packaging.spec index 4e2478c..26b648f 100644 --- a/ansible-packaging.spec +++ b/ansible-packaging.spec @@ -77,6 +77,57 @@ install -Dpm0644 -t %{buildroot}%{_rpmmacrodir} macros.ansible-srpm install -Dpm0755 -t %{buildroot}%{_rpmconfigdir} ansible-generator install -Dpm0755 -t %{buildroot}%{_rpmconfigdir} ansible_collection.py +%check +# TODO: Currently, this only tests %%{ansible_collection_url}. + +rpm_eval() { + default_macros_path="$(rpm --showrc | grep 'Macro path' | awk -F ': ' '{print $2}')" + rpm --macros="${default_macros_path}:%{buildroot}%{_rpmmacrodir}/macros.*" "$@" +} + +errors() { + error="error: %%ansible_collection_url: You must pass the collection namespace as the first arg and the collection name as the second" + "$@" && exit 1 + "$@" |& grep -q "${error}" +} + +echo "Ensure macro fails when only collection_namespace macro is defined" +errors rpm_eval -D 'collection_namespace cc' -E '%%ansible_collection_url' + +echo +echo "Ensure macro fails when only collection_name macro is defined" +errors rpm_eval -D 'collection_name cc' -E '%%ansible_collection_url' + +echo +echo "Ensure macro fails when second argument is missing" +errors rpm_eval -E '%%ansible_collection_url a' + +echo +echo "Ensure macro fails when second argument is missing" +errors rpm_eval -D 'collection_name b' -E '%%ansible_collection_url a' + +echo +echo "Ensure macro fails when neither the control macros nor macro arguments are passed" +errors rpm_eval -E '%%ansible_collection_url' + + +echo +echo +echo "Ensure macro works when both arguments are passed and no control macros are set" +[[ $(rpm_eval -E '%%ansible_collection_url community general') == \ + "https://galaxy.ansible.com/community/general" ]] + +echo +echo "Ensure macro works with the control macros" +[[ $(rpm_eval -D 'collection_namespace ansible' -D 'collection_name posix' \ + -E '%%ansible_collection_url') == "https://galaxy.ansible.com/ansible/posix" ]] + +echo +echo "Ensure macro prefers the collection namespace and name passed as an argument over the control macros" +[[ $(rpm_eval -D 'collection_namespace ansible' -D 'collection_name posix' \ + -E '%%ansible_collection_url community general') == "https://galaxy.ansible.com/community/general" ]] + + %files %license COPYING diff --git a/macros.ansible-srpm b/macros.ansible-srpm index 9b208b4..0adacd7 100644 --- a/macros.ansible-srpm +++ b/macros.ansible-srpm @@ -1 +1,25 @@ -%ansible_collection_url() https://galaxy.ansible.com/%{collection_namespace}/%{collection_name} +# Note(gotmax23): I'm trying to get rid of the need for control macros in favor +# of a metadata based approach. %%ansible_collection_url is the only macro that +# requires manually specifying the collection namespace and name, as it is used +# at the SRPM build stage. +# +# Currently, this macro supports either passing this information as arguments +# or defining the control macros. In order to reduce confusion, this is not an +# either or approach. Both arguments must be passed OR both control macros must +# be defined. + +%ansible_collection_url() %{lua: + local namespace_name = nil + if rpm.expand("%collection_namespace") ~= "%collection_namespace" + and rpm.expand("%collection_name") ~= "%collection_name" then + namespace_name = rpm.expand("%collection_namespace") .. "/" .. rpm.expand("%collection_name") + end + if rpm.expand("%1") ~= "%1" and rpm.expand("%2") ~= "%2" then + namespace_name = rpm.expand("%1") .. "/" .. rpm.expand("%2") + end + if not namespace_name then + rpm.expand("%{error:%%ansible_collection_url: You must pass the collection " .. + "namespace as the first arg and the collection name as the second}") + end + print("https://galaxy.ansible.com/" .. namespace_name) +}