Skip to content

Commit

Permalink
Updated to use tilde. Cleaned up negation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronYoung5 committed Feb 15, 2025
1 parent 6051d67 commit fb6478e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 49 deletions.
25 changes: 14 additions & 11 deletions test/test_unit_score_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,24 +329,27 @@ 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)
assert run.success
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."""
Expand All @@ -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
Expand All @@ -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)
Expand Down
51 changes: 13 additions & 38 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -264,16 +237,18 @@ function score_file() {
esac
shopt -u nocasematch

(( negate )) && delta=$(( -delta ))
if ((delta < 0)); then
score=0
return
fi
score=$((score + 1000 + delta))
score=$(( score + 1000 + delta ))
done

record_score "$score" "$target" "$source" "$template_processor"
}


function record_score() {
local score="$1"
local target="$2"
Expand Down

0 comments on commit fb6478e

Please sign in to comment.