From 5293db986ad26cac92ad021c88c9aa213f5648ea Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Mon, 3 Jul 2017 16:21:27 -0500 Subject: [PATCH] Support `YADM_DISTRO` in Jinja templates (#68) --- test/007_unit_query_distro.bats | 49 +++++++++++++++++++++++++++++++++ test/113_accept_jinja_alt.bats | 4 +-- test/common.bash | 4 ++- yadm | 10 +++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 test/007_unit_query_distro.bats diff --git a/test/007_unit_query_distro.bats b/test/007_unit_query_distro.bats new file mode 100644 index 0000000..639ab29 --- /dev/null +++ b/test/007_unit_query_distro.bats @@ -0,0 +1,49 @@ +load common +load_fixtures + +@test "Query distro (lsb_release present)" { + echo " + Use value of lsb_release -si + " + + #shellcheck source=/dev/null + YADM_TEST=1 source "$T_YADM" + status=0 + { output=$( query_distro ); } || { + status=$? + true + } + + expected="${T_DISTRO}" + + echo "output=$output" + echo "expect=$expected" + + [ "$status" == 0 ] + [ "$output" = "$expected" ] +} + +@test "Query distro (lsb_release missing)" { + echo " + Empty value if lsb_release is missing + " + + #shellcheck source=/dev/null + YADM_TEST=1 source "$T_YADM" + LSB_RELEASE_PROGRAM="missing_lsb_release" + echo "Using $LSB_RELEASE_PROGRAM as lsb_release" + + status=0 + { output=$( query_distro ); } || { + status=$? + true + } + + expected="" + + echo "output=$output" + echo "expect=$expected" + + [ "$status" == 0 ] + [ "$output" = "$expected" ] +} diff --git a/test/113_accept_jinja_alt.bats b/test/113_accept_jinja_alt.bats index 6ed6c63..8cfe343 100644 --- a/test/113_accept_jinja_alt.bats +++ b/test/113_accept_jinja_alt.bats @@ -21,11 +21,11 @@ function test_alt() { case $alt_type in base) real_name="alt-jinja" - file_content_match="-${T_SYS}-${T_HOST}-${T_USER}" + file_content_match="-${T_SYS}-${T_HOST}-${T_USER}-${T_DISTRO}" ;; override_all) real_name="alt-jinja" - file_content_match="custom_class-custom_system-custom_host-custom_user" + file_content_match="custom_class-custom_system-custom_host-custom_user-${T_DISTRO}" ;; esac diff --git a/test/common.bash b/test/common.bash index d617db1..46a38b1 100644 --- a/test/common.bash +++ b/test/common.bash @@ -27,6 +27,8 @@ function load_fixtures() { T_HOST=$(hostname -s) export T_USER T_USER=$(id -u -n) + export T_DISTRO + T_DISTRO=$(lsb_release -si 2>/dev/null || true) } function configure_git() { @@ -202,7 +204,7 @@ function create_worktree() { make_parents "$DIR_WORKTREE/$f" echo "$f" > "$DIR_WORKTREE/$f" done - echo "{{ YADM_CLASS }}-{{ YADM_OS }}-{{ YADM_HOSTNAME }}-{{ YADM_USER }}" > "$DIR_WORKTREE/alt-jinja##yadm.j2" + echo "{{ YADM_CLASS }}-{{ YADM_OS }}-{{ YADM_HOSTNAME }}-{{ YADM_USER }}-{{ YADM_DISTRO }}" > "$DIR_WORKTREE/alt-jinja##yadm.j2" fi #; for some cygwin tests diff --git a/yadm b/yadm index 878248e..9938169 100755 --- a/yadm +++ b/yadm @@ -34,6 +34,7 @@ GPG_PROGRAM="gpg" GIT_PROGRAM="git" LS_PROGRAM="/bin/ls" ENVTPL_PROGRAM="envtpl" +LSB_RELEASE_PROGRAM="lsb_release" PROC_VERSION="/proc/version" OPERATING_SYSTEM="Unknown" @@ -230,6 +231,7 @@ function alt() { YADM_OS="$local_system" \ YADM_HOSTNAME="$local_host" \ YADM_USER="$local_user" \ + YADM_DISTRO=$(query_distro) \ "$ENVTPL_PROGRAM" < "$tracked_file" > "$real_file" else debug "envtpl not available, not creating $real_file from template $tracked_file" @@ -698,6 +700,14 @@ function version() { #; ****** Utility Functions ****** +function query_distro() { + distro="" + if command -v "$LSB_RELEASE_PROGRAM" >/dev/null 2>&1; then + distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null) + fi + echo "$distro" +} + function process_global_args() { #; global arguments are removed before the main processing is done