diff --git a/test/test_unit_query_distro.py b/test/test_unit_query_distro.py index 3c53c54..e83ff4e 100644 --- a/test/test_unit_query_distro.py +++ b/test/test_unit_query_distro.py @@ -1,26 +1,30 @@ """Unit tests: query_distro""" +import pytest -def test_lsb_release_present(runner, yadm, tst_distro): +@pytest.mark.parametrize( + 'condition', ['lsb_release', 'os-release', 'missing']) +def test_query_distro(runner, yadm, tst_distro, tmp_path, condition): """Match lsb_release -si when present""" + test_release = 'testrelease' + lsb_release = '' + os_release = tmp_path.joinpath('os-release') + if condition == 'os-release': + os_release.write_text(f"testing\nID={test_release}\nrelease") + if condition != 'lsb_release': + lsb_release = 'LSB_RELEASE_PROGRAM="missing_lsb_release"' script = f""" YADM_TEST=1 source {yadm} + {lsb_release} + OS_RELEASE="{os_release}" query_distro """ run = runner(command=['bash'], inp=script) assert run.success assert run.err == '' - assert run.out.rstrip() == tst_distro - - -def test_lsb_release_missing(runner, yadm): - """Empty value when missing""" - script = f""" - YADM_TEST=1 source {yadm} - LSB_RELEASE_PROGRAM="missing_lsb_release" - query_distro - """ - run = runner(command=['bash'], inp=script) - assert run.success - assert run.err == '' - assert run.out.rstrip() == '' + if condition == 'lsb_release': + assert run.out.rstrip() == tst_distro + elif condition == 'os-release': + assert run.out.rstrip() == test_release + else: + assert run.out.rstrip() == '' diff --git a/yadm b/yadm index a4ff860..650816f 100755 --- a/yadm +++ b/yadm @@ -45,6 +45,7 @@ J2CLI_PROGRAM="j2" ENVTPL_PROGRAM="envtpl" LSB_RELEASE_PROGRAM="lsb_release" +OS_RELEASE="/etc/os-release" PROC_VERSION="/proc/version" OPERATING_SYSTEM="Unknown" @@ -1219,6 +1220,13 @@ function query_distro() { distro="" if command -v "$LSB_RELEASE_PROGRAM" >/dev/null 2>&1; then distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null) + elif [ -f "$OS_RELEASE" ]; then + while IFS='' read -r line || [ -n "$line" ]; do + if [[ "$line" = ID=* ]]; then + distro="${line#ID=}" + break + fi + done < "$OS_RELEASE" fi echo "$distro" } diff --git a/yadm.1 b/yadm.1 index 0e9c49c..5dabce8 100644 --- a/yadm.1 +++ b/yadm.1 @@ -477,7 +477,9 @@ Current user is calculated by running .BR distro , " d Valid if the value matches the distro. Distro is calculated by running -.BR "lsb_release -si" . +.B "lsb_release -si" +or by inspecting the ID from +.BR "/etc/os-release" . .TP .BR os , " o Valid if the value matches the OS. @@ -638,6 +640,10 @@ During processing, the following variables are available in the template: The OS for "Windows Subsystem for Linux" is reported as "WSL", even though uname identifies as "Linux". +.BR NOTE : +If lsb_release is not available, DISTRO will be the ID specified in +/etc/os-release. + Examples: .I whatever##template