Use /etc/os-release if lsb_release is missing (#175)

This commit is contained in:
Tim Byrne 2019-11-30 10:52:18 -06:00
parent 96839a5743
commit 6bf0852609
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
3 changed files with 34 additions and 16 deletions

View File

@ -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() == ''

8
yadm
View File

@ -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"
}

8
yadm.1
View File

@ -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