mirror of
https://github.com/TheLocehiliosan/yadm
synced 2025-06-04 00:23:58 +00:00
Updated to use tilde. Cleaned up negation logic.
This commit is contained in:
parent
6051d67e1d
commit
c74faa5301
@ -329,16 +329,19 @@ def test_negative_class_condition(runner, yadm):
|
||||
score=0
|
||||
local_class="testclass"
|
||||
local_classes=("testclass")
|
||||
# Negative condition with matching value should yield delta -1 and zero the score.
|
||||
score_file "filename##!class.testclass" "dest"
|
||||
|
||||
# 0
|
||||
score_file "filename##~class.testclass" "dest"
|
||||
echo "score: $score"
|
||||
# Negative condition with non-matching value should yield delta 16.
|
||||
|
||||
# 1000 + 16
|
||||
score=0
|
||||
score_file "filename##!class.badclass" "dest"
|
||||
score_file "filename##~class.badclass" "dest"
|
||||
echo "score2: $score"
|
||||
# Check class shorthand (c) as well.
|
||||
|
||||
# 0
|
||||
score=0
|
||||
score_file "filename##!c.testclass" "dest"
|
||||
score_file "filename##~c.badclass" "dest"
|
||||
echo "score3: $score"
|
||||
"""
|
||||
run = runner(command=["bash"], inp=script)
|
||||
@ -346,7 +349,7 @@ def test_negative_class_condition(runner, yadm):
|
||||
output = run.out.strip().splitlines()
|
||||
assert output[0] == "score: 0"
|
||||
assert output[1] == "score2: 1016"
|
||||
assert output[2] == "score3: 0"
|
||||
assert output[2] == "score3: 1016"
|
||||
|
||||
def test_negative_combined_conditions(runner, yadm):
|
||||
"""Test negative conditions for multiple alt types: returns 0 when matching and proper score when not matching."""
|
||||
@ -359,7 +362,7 @@ def test_negative_combined_conditions(runner, yadm):
|
||||
|
||||
# 0 + 0 = 0
|
||||
score=0
|
||||
score_file "filename##!class.testclass,!distro.testdistro" "dest"
|
||||
score_file "filename##~class.testclass,~distro.testdistro" "dest"
|
||||
echo "score: $score"
|
||||
|
||||
# 1000 * 2 + 16 + 4 = 2020
|
||||
@ -369,17 +372,17 @@ def test_negative_combined_conditions(runner, yadm):
|
||||
|
||||
# 0 (negated class condition)
|
||||
score=0
|
||||
score_file "filename##!class.badclass,!distro.testdistro" "dest"
|
||||
score_file "filename##~class.badclass,~distro.testdistro" "dest"
|
||||
echo "score3: $score"
|
||||
|
||||
# 1000 + 16 + 1000 + 4 = 2020
|
||||
score=0
|
||||
score_file "filename##class.testclass,!distro.baddistro" "dest"
|
||||
score_file "filename##class.testclass,~distro.baddistro" "dest"
|
||||
echo "score4: $score"
|
||||
|
||||
# 1000 + 16 + 1000 + 16 = 2032
|
||||
score=0
|
||||
score_file "filename##class.testclass,!class.badclass" "dest"
|
||||
score_file "filename##class.testclass,~class.badclass" "dest"
|
||||
echo "score5: $score"
|
||||
"""
|
||||
run = runner(command=["bash"], inp=script)
|
||||
|
48
yadm
48
yadm
@ -179,9 +179,9 @@ function score_file() {
|
||||
local value=${field#*.}
|
||||
[ "$field" = "$label" ] && value="" # when .value is omitted
|
||||
|
||||
# Check for negative condition prefix (e.g., "!<label>")
|
||||
# Check for negative condition prefix (e.g., "~<label>")
|
||||
local negate=0
|
||||
if [[ "$label" == !* ]]; then
|
||||
if [[ "$label" == ~* ]]; then
|
||||
negate=1
|
||||
label="${label:1}"
|
||||
fi
|
||||
@ -193,55 +193,28 @@ function score_file() {
|
||||
delta=0
|
||||
;;
|
||||
a | arch)
|
||||
if [[ "$value" = "$local_arch" ]]; then
|
||||
(( negate )) && delta=-1 || delta=1
|
||||
else
|
||||
(( negate )) && delta=1
|
||||
fi
|
||||
[[ "$value" = "$local_arch" ]] && delta=1 || delta=-1
|
||||
;;
|
||||
o | os)
|
||||
if [[ "$value" = "$local_system" ]]; then
|
||||
(( negate )) && delta=-1 || delta=2
|
||||
else
|
||||
(( negate )) && delta=2
|
||||
fi
|
||||
[[ "$value" = "$local_system" ]] && delta=2 || delta=-2
|
||||
;;
|
||||
d | distro)
|
||||
if [[ "${value// /_}" = "${local_distro// /_}" ]]; then
|
||||
(( negate )) && delta=-1 || delta=4
|
||||
else
|
||||
(( negate )) && delta=4
|
||||
fi
|
||||
[[ "${value// /_}" = "${local_distro// /_}" ]] && delta=4 || delta=-4
|
||||
;;
|
||||
f | distro_family)
|
||||
if [[ "${value// /_}" = "${local_distro_family// /_}" ]]; then
|
||||
(( negate )) && delta=-1 || delta=8
|
||||
else
|
||||
(( negate )) && delta=8
|
||||
fi
|
||||
[[ "${value// /_}" = "${local_distro_family// /_}" ]] && delta=8 || delta=-8
|
||||
;;
|
||||
c | class)
|
||||
if in_list "$value" "${local_classes[@]}"; then
|
||||
(( negate )) && delta=-1 || delta=16
|
||||
else
|
||||
(( negate )) && delta=16
|
||||
fi
|
||||
in_list "$value" "${local_classes[@]}" && delta=16 || delta=-16
|
||||
;;
|
||||
h | hostname)
|
||||
if [[ "$value" = "$local_host" ]]; then
|
||||
(( negate )) && delta=-1 || delta=32
|
||||
else
|
||||
(( negate )) && delta=32
|
||||
fi
|
||||
[[ "$value" = "$local_host" ]] && delta=32 || delta=-32
|
||||
;;
|
||||
u | user)
|
||||
if [[ "$value" = "$local_user" ]]; then
|
||||
(( negate )) && delta=-1 || delta=64
|
||||
else
|
||||
(( negate )) && delta=64
|
||||
fi
|
||||
[[ "$value" = "$local_user" ]] && delta=64 || delta=-64
|
||||
;;
|
||||
e | extension)
|
||||
# extension isn't a condition and doesn't affect the score
|
||||
continue
|
||||
;;
|
||||
t | template | yadm)
|
||||
@ -264,6 +237,7 @@ function score_file() {
|
||||
esac
|
||||
shopt -u nocasematch
|
||||
|
||||
(( negate )) && delta=$((-delta))
|
||||
if ((delta < 0)); then
|
||||
score=0
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user