1
0
Fork 0
mirror of synced 2024-12-22 06:31:07 -05:00

Set distro family from ID line if no ID_LIKE line is found

See #456.
This commit is contained in:
Erik Flodin 2024-12-09 23:10:35 +01:00
parent 18d5f66542
commit 6c1970fb41
No known key found for this signature in database
GPG key ID: 420A7C865EE3F85F
3 changed files with 15 additions and 10 deletions

View file

@ -3,14 +3,16 @@
import pytest import pytest
@pytest.mark.parametrize("condition", ["os-release", "os-release-quotes", "missing"]) @pytest.mark.parametrize("condition", ["os-release", "os-release-quotes", "missing", "fallback"])
def test_query_distro_family(runner, yadm, tmp_path, condition): def test_query_distro_family(runner, yadm, tmp_path, condition):
"""Match ID_LIKE when present""" """Match ID_LIKE when present"""
test_family = "testfamily" test_family = "testfamily"
os_release = tmp_path.joinpath("os-release") os_release = tmp_path.joinpath("os-release")
if "os-release" in condition: if "os-release" in condition:
quotes = '"' if "quotes" in condition else "" quotes = '"' if "quotes" in condition else ""
os_release.write_text(f"testing\nID_LIKE={quotes}{test_family}{quotes}\nfamily") os_release.write_text(f"testing\nID=test\nID_LIKE={quotes}{test_family}{quotes}\nfamily")
elif condition == "fallback":
os_release.write_text(f'testing\nID="{test_family}"\nfamily')
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
OS_RELEASE="{os_release}" OS_RELEASE="{os_release}"
@ -19,7 +21,7 @@ def test_query_distro_family(runner, yadm, tmp_path, condition):
run = runner(command=["bash"], inp=script) run = runner(command=["bash"], inp=script)
assert run.success assert run.success
assert run.err == "" assert run.err == ""
if "os-release" in condition: if condition == "missing":
assert run.out.rstrip() == test_family
else:
assert run.out.rstrip() == "" assert run.out.rstrip() == ""
else:
assert run.out.rstrip() == test_family

10
yadm
View file

@ -1499,7 +1499,7 @@ function exclude_encrypted() {
} }
function query_distro() { function query_distro() {
distro="" local distro=""
if command -v "$LSB_RELEASE_PROGRAM" &> /dev/null; then if command -v "$LSB_RELEASE_PROGRAM" &> /dev/null; then
distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null) distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null)
elif [ -f "$OS_RELEASE" ]; then elif [ -f "$OS_RELEASE" ]; then
@ -1515,17 +1515,19 @@ function query_distro() {
} }
function query_distro_family() { function query_distro_family() {
family="" local family=""
if [ -f "$OS_RELEASE" ]; then if [ -f "$OS_RELEASE" ]; then
while IFS='' read -r line || [ -n "$line" ]; do while IFS='' read -r line || [ -n "$line" ]; do
if [[ "$line" = ID_LIKE=* ]]; then if [[ "$line" = ID_LIKE=* ]]; then
family="${line#ID_LIKE=}" family="${line#ID_LIKE=}"
family="${family//\"}"
break break
elif [[ "$line" = ID=* ]]; then
family="${line#ID=}"
# No break, only used as fallback in case ID_LIKE isn't found
fi fi
done < "$OS_RELEASE" done < "$OS_RELEASE"
fi fi
echo "$family" echo "${family//\"}"
} }
function process_global_args() { function process_global_args() {

3
yadm.1
View file

@ -511,7 +511,8 @@ or by inspecting the ID from
.BR distro_family , " f .BR distro_family , " f
Valid if the value matches the distro family (ignoring case). Valid if the value matches the distro family (ignoring case).
Distro family is calculated by inspecting the ID_LIKE line from Distro family is calculated by inspecting the ID_LIKE line from
.BR "/etc/os-release" . .B "/etc/os-release"
(or ID if no ID_LIKE line is found).
.TP .TP
.BR os , " o .BR os , " o
Valid if the value matches the OS. Valid if the value matches the OS.