diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7f847da6..472d2961 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,6 +16,12 @@ jobs: - name: Run regression tests run: python3 -m unittest discover -s tests -v + - name: Install shell test dependencies + run: sudo apt-get update && sudo apt-get install -y bats jq + + - name: Run shell tests + run: bats tests/*.bats + azure-template: runs-on: ubuntu-latest permissions: diff --git a/docker/files/update-mods.sh b/docker/files/update-mods.sh index 7653b00c..28da9451 100755 --- a/docker/files/update-mods.sh +++ b/docker/files/update-mods.sh @@ -1,14 +1,4 @@ #!/bin/bash -set -eou pipefail - -FACTORIO_VERSION=$1 -MOD_DIR=$2 -USERNAME=$3 -TOKEN=$4 -UPDATE_IGNORE=$5 - -MOD_BASE_URL="https://mods.factorio.com" - print_step() { echo "$1" @@ -24,33 +14,16 @@ print_failure() echo "$1" } -# Checks if the current game version satisfies the mod's minimum required version. -# Returns 1 if the game version is compatible with the mod, 0 if not +# factorio_version selects one major.minor, not a minimum supported version. +# Print 1 for compatibility, 0 otherwise (the caller consumes this as data). check_game_version() { - local mod_required_version="$1" # The minimum Factorio version required by the mod - local current_game_version="$2" # The current Factorio version - - local mod_major mod_minor game_major game_minor - mod_major=$(echo "$mod_required_version" | cut -d '.' -f1) - mod_minor=$(echo "$mod_required_version" | cut -d '.' -f2) - game_major=$(echo "$current_game_version" | cut -d '.' -f1) - game_minor=$(echo "$current_game_version" | cut -d '.' -f2) + local mod_required_version="$1" + local current_game_version="$2" + local game_major game_minor + IFS='.' read -r game_major game_minor _ <<< "$current_game_version" - # If game major version is greater than mod's required major version, it's compatible - if [[ "$game_major" -gt "$mod_major" ]]; then - echo 1 - return - fi - - # If game major version is less than mod's required major version, it's not compatible - if [[ "$game_major" -lt "$mod_major" ]]; then - echo 0 - return - fi - - # Major versions are equal, check minor versions - # Game minor version must be >= mod's required minor version - if [[ "$game_minor" -ge "$mod_minor" ]]; then + if [[ "$mod_required_version" == "$game_major.$game_minor" ]] || + [[ "$game_major.$game_minor" == "1.0" && "$mod_required_version" == "0.18" ]]; then echo 1 else echo 0 @@ -130,11 +103,9 @@ get_mod_info() while IFS= read -r mod_release_info; do local mod_version mod_factorio_version mod_version=$(echo "$mod_release_info" | jq -r ".version") - mod_factorio_version=$(echo "$mod_release_info" | jq -r ".info_json.factorio_version") + mod_factorio_version=$(echo "$mod_release_info" | jq -r '.info_json.factorio_version // "0.12"') - # Check if this mod version is compatible with our Factorio version - # This prevents downloading mods that require a newer Factorio version (fixes #468) - # and ensures backward compatibility (e.g., Factorio 2.0 can use 1.x mods) (fixes #517) + # Older and newer game branches are both incompatible, except 0.18 mods on 1.0. if [[ $(check_game_version "$mod_factorio_version" "$FACTORIO_VERSION") == 0 ]]; then echo " Skipping mod version $mod_version because of factorio version mismatch" >&2 continue @@ -250,6 +221,19 @@ update_mod() return 0 } +# Sourcing exposes the functions without running updates or changing shell options. +if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then + return +fi + +set -eou pipefail +FACTORIO_VERSION=$1 +MOD_DIR=$2 +USERNAME=$3 +TOKEN=$4 +UPDATE_IGNORE=$5 +MOD_BASE_URL="https://mods.factorio.com" + # Process all enabled mods from mod-list.json, but skip built-in mods # The Space Age DLC includes built-in mods (elevated-rails, quality, space-age) that should not be downloaded if [[ -f $MOD_DIR/mod-list.json ]]; then diff --git a/tests/update-mods.bats b/tests/update-mods.bats new file mode 100644 index 00000000..06cd40bd --- /dev/null +++ b/tests/update-mods.bats @@ -0,0 +1,69 @@ +#!/usr/bin/env bats + +setup() { + bats_require_minimum_version 1.5.0 + source "$BATS_TEST_DIRNAME/../docker/files/update-mods.sh" +} + +@test "game compatibility matches the branch, including the 1.0 exception" { + while read -r mod game expected; do + run check_game_version "$mod" "$game" + [ "$status" -eq 0 ] + [ "$output" = "$expected" ] + done <<'CASES' +2.0 2.0.77 1 +1.1 2.0.77 0 +2.0 2.1.24 0 +2.1 2.0.77 0 +0.18 1.0.0 1 +1.0 1.0.0 1 +0.17 1.0.0 0 +0.18 1.1.110 0 +0.12 0.12.35 1 +CASES +} + +@test "release selection skips wrong branches and unmet base patch requirements" { + export FACTORIO_VERSION=2.0.77 + run --separate-stderr get_mod_info '{"releases":[ + {"version":"1.0.0","released_at":"2025-01-01","info_json":{"factorio_version":"2.0"},"file_name":"old.zip","download_url":"/old","sha1":"old"}, + {"version":"2.0.0","released_at":"2025-01-02","info_json":{"factorio_version":"2.0","dependencies":["base >= 2.0.77"]},"file_name":"selected.zip","download_url":"/selected","sha1":"selected"}, + {"version":"3.0.0","released_at":"2025-01-03","info_json":{"factorio_version":"2.0","dependencies":["base >= 2.0.78"]}}, + {"version":"4.0.0","released_at":"2025-01-04","info_json":{"factorio_version":"1.1"}}, + {"version":"5.0.0","released_at":"2025-01-05","info_json":{"factorio_version":"2.1"}} + ]}' + [ "$status" -eq 0 ] + [ "$output" = 'selected.zip;/selected;selected' ] +} + +@test "missing factorio_version defaults to 0.12, not a modern branch" { + local releases='{"releases":[{"version":"1.0.0","released_at":"2015-01-01","info_json":{},"file_name":"legacy.zip","download_url":"/legacy","sha1":"legacy"}]}' + export FACTORIO_VERSION=0.12.35 + run --separate-stderr get_mod_info "$releases" + [ "$status" -eq 0 ] + [ "$output" = 'legacy.zip;/legacy;legacy' ] + + export FACTORIO_VERSION=2.0.77 + run --separate-stderr get_mod_info "$releases" + [ "$status" -eq 0 ] + [ -z "$output" ] +} + +@test "sourcing without arguments preserves shell options and does not run updates" { + run bash -c ' + script=$1 + set -- + before=$(set +o) + source "$script" + [[ $(set +o) == "$before" ]] + ' bash "$BATS_TEST_DIRNAME/../docker/files/update-mods.sh" + [ "$status" -eq 0 ] + [ -z "$output" ] +} + +@test "executing the updater still processes enabled mods" { + printf '%s\n' '{"mods":[{"name":"base","enabled":true},{"name":"example","enabled":true}]}' > "$BATS_TEST_TMPDIR/mod-list.json" + run bash "$BATS_TEST_DIRNAME/../docker/files/update-mods.sh" 2.0.77 "$BATS_TEST_TMPDIR" unused unused example + [ "$status" -eq 0 ] + [ "$output" = 'Skipping mod example (listed in UPDATE_IGNORE)' ] +}