Skip to content

Commit

Permalink
Set distro family from ID line if no ID_LIKE line is found
Browse files Browse the repository at this point in the history
See #456.
  • Loading branch information
erijo committed Dec 9, 2024
1 parent 18d5f66 commit 6c1970f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
12 changes: 7 additions & 5 deletions test/test_unit_query_distro_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
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):
"""Match ID_LIKE when present"""
test_family = "testfamily"
os_release = tmp_path.joinpath("os-release")
if "os-release" in condition:
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"""
YADM_TEST=1 source {yadm}
OS_RELEASE="{os_release}"
Expand All @@ -19,7 +21,7 @@ def test_query_distro_family(runner, yadm, tmp_path, condition):
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
if "os-release" in condition:
assert run.out.rstrip() == test_family
else:
if condition == "missing":
assert run.out.rstrip() == ""
else:
assert run.out.rstrip() == test_family
10 changes: 6 additions & 4 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ function exclude_encrypted() {
}

function query_distro() {
distro=""
local distro=""
if command -v "$LSB_RELEASE_PROGRAM" &> /dev/null; then
distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null)
elif [ -f "$OS_RELEASE" ]; then
Expand All @@ -1515,17 +1515,19 @@ function query_distro() {
}

function query_distro_family() {
family=""
local family=""
if [ -f "$OS_RELEASE" ]; then
while IFS='' read -r line || [ -n "$line" ]; do
if [[ "$line" = ID_LIKE=* ]]; then
family="${line#ID_LIKE=}"
family="${family//\"}"
break
elif [[ "$line" = ID=* ]]; then
family="${line#ID=}"
# No break, only used as fallback in case ID_LIKE isn't found
fi
done < "$OS_RELEASE"
fi
echo "$family"
echo "${family//\"}"
}

function process_global_args() {
Expand Down
3 changes: 2 additions & 1 deletion yadm.1
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ or by inspecting the ID from
.BR distro_family , " f
Valid if the value matches the distro family (ignoring case).
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
.BR os , " o
Valid if the value matches the OS.
Expand Down

0 comments on commit 6c1970f

Please sign in to comment.