Skip to content

Commit

Permalink
Support overriding distro and distro family
Browse files Browse the repository at this point in the history
Fixes #430.
  • Loading branch information
erijo committed Dec 10, 2024
1 parent c092b7c commit 8c2f833
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
2 changes: 2 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def supported_configs():
return [
"local.arch",
"local.class",
"local.distro",
"local.distro-family",
"local.hostname",
"local.os",
"local.user",
Expand Down
70 changes: 25 additions & 45 deletions test/test_unit_set_local_alt_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"os",
"hostname",
"user",
"distro",
"distro-family",
],
ids=[
"no-override",
Expand All @@ -21,11 +23,15 @@
"override-os",
"override-hostname",
"override-user",
"override-distro",
"override-distro-family",
],
)
@pytest.mark.usefixtures("ds1_copy")
def test_set_local_alt_values(runner, yadm, paths, tst_arch, tst_sys, tst_host, tst_user, override):
"""Use issue_legacy_path_warning"""
def test_set_local_alt_values(
runner, yadm, paths, tst_arch, tst_sys, tst_host, tst_user, tst_distro, tst_distro_family, override
):
"""Test handling of local alt values"""
script = f"""
YADM_TEST=1 source {yadm} &&
set_operating_system &&
Expand All @@ -34,8 +40,10 @@ def test_set_local_alt_values(runner, yadm, paths, tst_arch, tst_sys, tst_host,
echo "class='$local_class'"
echo "arch='$local_arch'"
echo "os='$local_system'"
echo "host='$local_host'"
echo "hostname='$local_host'"
echo "user='$local_user'"
echo "distro='$local_distro'"
echo "distro-family='$local_distro_family'"
"""

if override == "class":
Expand All @@ -48,46 +56,18 @@ def test_set_local_alt_values(runner, yadm, paths, tst_arch, tst_sys, tst_host,
assert run.success
assert run.err == ""

if override == "class":
assert "class='override'" in run.out
else:
assert "class=''" in run.out

if override == "arch":
assert "arch='override'" in run.out
else:
assert f"arch='{tst_arch}'" in run.out

if override == "os":
assert "os='override'" in run.out
else:
assert f"os='{tst_sys}'" in run.out

if override == "hostname":
assert "host='override'" in run.out
else:
assert f"host='{tst_host}'" in run.out

if override == "user":
assert "user='override'" in run.out
else:
assert f"user='{tst_user}'" in run.out


def test_distro_and_family(runner, yadm):
"""Assert that local_distro/local_distro_family are set"""
default_values = {
"class": "",
"arch": tst_arch,
"os": tst_sys,
"hostname": tst_host,
"user": tst_user,
"distro": tst_distro,
"distro-family": tst_distro_family,
}

script = f"""
YADM_TEST=1 source {yadm}
function config() {{ echo "$1"; }}
function query_distro() {{ echo "testdistro"; }}
function query_distro_family() {{ echo "testfamily"; }}
set_local_alt_values
echo "distro='$local_distro'"
echo "distro_family='$local_distro_family'"
"""
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert "distro='testdistro'" in run.out
assert "distro_family='testfamily'" in run.out
for key, value in default_values.items():
if key == override:
assert f"{key}='override'" in run.out
else:
assert f"{key}='{value}'" in run.out
23 changes: 16 additions & 7 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -649,28 +649,35 @@ function set_local_alt_values() {
done <<< "$all_classes"

local_arch="$(config local.arch)"
if [ -z "$local_arch" ] ; then
if [[ -z "$local_arch" ]]; then
local_arch=$(uname -m)
fi

local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
if [[ -z "$local_system" ]]; then
local_system="$OPERATING_SYSTEM"
fi

local_host="$(config local.hostname)"
if [ -z "$local_host" ] ; then
if [[ -z "$local_host" ]]; then
local_host=$(uname -n)
local_host=${local_host%%.*} # trim any domain from hostname
fi

local_user="$(config local.user)"
if [ -z "$local_user" ] ; then
if [[ -z "$local_user" ]]; then
local_user=$(id -u -n)
fi

local_distro="$(query_distro)"
local_distro_family="$(query_distro_family)"
local_distro="$(config local.distro)"
if [[ -z "$local_distro" ]]; then
local_distro="$(query_distro)"
fi

local_distro_family="$(config local.distro-family)"
if [[ -z "$local_distro_family" ]]; then
local_distro_family="$(query_distro_family)"
fi

}

Expand Down Expand Up @@ -849,7 +856,7 @@ EOF
function config() {

use_repo_config=0
local_options="^local\.(class|arch|os|hostname|user)$"
local_options="^local\.(class|arch|os|hostname|user|distro|distro-family)$"
for option in "$@"; do
[[ "$option" =~ $local_options ]] && use_repo_config=1
done
Expand Down Expand Up @@ -1230,6 +1237,8 @@ function introspect_configs() {
read -r -d '' msg <<-EOF
local.arch
local.class
local.distro
local.distro-family
local.hostname
local.os
local.user
Expand Down
16 changes: 12 additions & 4 deletions yadm.1
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Disable the permission changes to
This feature is enabled by default.

.RE
The following five "local" configurations are not stored in the
The following "local" configurations are not stored in the
.IR $HOME/.config/yadm/config,
they are stored in the local repository.

Expand All @@ -452,6 +452,12 @@ Override the OS for the purpose of symlinking alternate files.
.TP
.B local.user
Override the user for the purpose of symlinking alternate files.
.TP
.B local.distro
Override the distro for the purpose of symlinking alternate files.
.TP
.B local.distro-family
Override the distro family for the purpose of symlinking alternate files.

.SH ALTERNATES

Expand Down Expand Up @@ -618,13 +624,15 @@ command. The following sets the class to be "Work".

yadm config local.class Work

Similarly, the values of architecture, os, hostname, and user can be manually
overridden using the configuration options
Similarly, the values of architecture, os, hostname, user, distro, and
distro_family can be manually overridden using the configuration options
.BR local.arch ,
.BR local.os ,
.BR local.hostname ,
.BR local.user ,
.BR local.distro ,
and
.BR local.user .
.BR local.distro-family .

.SH TEMPLATES

Expand Down

0 comments on commit 8c2f833

Please sign in to comment.