From 38e327097873b0bd3a9c3ca5fa14083cf258a5b8 Mon Sep 17 00:00:00 2001 From: Florian Kinder Date: Mon, 2 Nov 2026 15:52:49 +0100 Subject: [PATCH 1/3] Fix mod updater game-branch compatibility --- docker/files/update-mods.sh | 39 +++++----------- tests/test_mod_versions.py | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 tests/test_mod_versions.py diff --git a/docker/files/update-mods.sh b/docker/files/update-mods.sh index 7653b00c..b48d02e9 100755 --- a/docker/files/update-mods.sh +++ b/docker/files/update-mods.sh @@ -24,33 +24,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_required_version="$1" + local current_game_version="$2" + local game_major game_minor remainder + IFS='.' read -r game_major game_minor remainder <<< "$current_game_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) - - # 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 +113,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 diff --git a/tests/test_mod_versions.py b/tests/test_mod_versions.py new file mode 100644 index 00000000..5ebefc55 --- /dev/null +++ b/tests/test_mod_versions.py @@ -0,0 +1,89 @@ +"""Exercise release selection with a local fake mod portal; no credentials/network.""" + +import hashlib +import json +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +SCRIPT = Path(__file__).resolve().parents[1] / "docker/files/update-mods.sh" +PAYLOAD = b"fixture mod archive" + + +def release(version, game, dependencies=None): + info = {} if game is None else {"factorio_version": game} + if dependencies is not None: + info["dependencies"] = dependencies + return {"version": version, "file_name": f"fixture_{version}.zip", + "download_url": f"/download/fixture/{version}", + "sha1": hashlib.sha1(PAYLOAD).hexdigest(), "info_json": info} + + +class ModVersionTests(unittest.TestCase): + def check_selection(self, game, releases, expected): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + mods = root / "mods" + mods.mkdir() + (mods / "mod-list.json").write_text(json.dumps({"mods": [{"name": "fixture", "enabled": True}]})) + old = mods / "fixture_0.0.1.zip" + old.write_bytes(b"keep until a compatible update succeeds") + for index, item in enumerate(releases): + item["released_at"] = f"2026-01-{index + 1:02d}T00:00:00Z" + (root / "metadata.json").write_text(json.dumps({"name": "fixture", "releases": releases})) + curl = root / "curl" + curl.write_text('''#!/usr/bin/env python3 +import json, os, sys +from pathlib import Path +root = Path(os.environ["FIXTURE_ROOT"]) +args = sys.argv[1:] +if "/api/mods/fixture/full" in args[-1]: + print((root / "metadata.json").read_text()) +else: + (root / "download.json").write_text(json.dumps(args)) + Path(args[args.index("-o") + 1]).write_bytes(b"fixture mod archive") + print("200", end="") +''') + curl.chmod(0o755) + env = {**os.environ, "PATH": f"{root}:{os.environ['PATH']}", "FIXTURE_ROOT": str(root)} + result = subprocess.run(["bash", str(SCRIPT), game, str(mods), "test-user", "test-token", ""], + env=env, capture_output=True, text=True, timeout=15) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + if expected is None: + self.assertFalse((root / "download.json").exists(), result.stdout + result.stderr) + self.assertEqual(old.read_bytes(), b"keep until a compatible update succeeds") + self.assertIn("Not compatible", result.stdout) + else: + self.assertEqual(sorted(p.name for p in mods.glob("*.zip")), [f"fixture_{expected}.zip"], + result.stdout + result.stderr) + self.assertEqual((mods / f"fixture_{expected}.zip").read_bytes(), PAYLOAD) + self.assertIn(f"/download/fixture/{expected}?", json.loads((root / "download.json").read_text())[-1]) + + def test_reject_older_and_newer_game_branches(self): + self.check_selection("2.1.24", [release("1.0.0", "2.1"), release("2.0.0", "2.0"), + release("3.0.0", "1.1"), release("4.0.0", "3.0")], "1.0.0") + self.check_selection("2.0.77", [release("1.0.0", "1.1"), release("2.0.0", "2.1")], None) + + def test_same_branch_selects_newest_release(self): + self.check_selection("2.1.24", [release("1.0.0", "2.1"), release("1.1.0", "2.1")], "1.1.0") + + def test_legacy_018_exception_is_only_for_10(self): + self.check_selection("1.0.0", [release("1.0.0", "0.18")], "1.0.0") + for game in ("1.1.110", "2.0.77", "0.17.79"): + with self.subTest(game=game): + self.check_selection(game, [release("1.0.0", "0.18")], None) + + def test_missing_factorio_version_defaults_to_012(self): + self.check_selection("0.12.35", [release("1.0.0", None)], "1.0.0") + self.check_selection("2.1.24", [release("1.0.0", None)], None) + + def test_base_patch_requirement_does_not_download_newer_requirement(self): + self.check_selection("2.0.15", [release("1.0.0", "2.0", ["base >= 2.0.10"]), + release("1.1.0", "2.0", ["base >= 2.0.18"])], "1.0.0") + self.check_selection("2.0.15", [release("1.1.0", "2.0", ["base >= 2.0.18"])], None) + + +if __name__ == "__main__": + unittest.main() From 3bbee8e5f2596cbd5899b72a4cd6bad5dd1d206c Mon Sep 17 00:00:00 2001 From: Florian Kinder Date: Mon, 2 Nov 2026 16:35:19 +0100 Subject: [PATCH 2/3] Discard unused patch component when matching game branches --- docker/files/update-mods.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/files/update-mods.sh b/docker/files/update-mods.sh index b48d02e9..56eb3391 100755 --- a/docker/files/update-mods.sh +++ b/docker/files/update-mods.sh @@ -29,8 +29,8 @@ print_failure() check_game_version() { local mod_required_version="$1" local current_game_version="$2" - local game_major game_minor remainder - IFS='.' read -r game_major game_minor remainder <<< "$current_game_version" + local game_major game_minor + IFS='.' read -r game_major game_minor _ <<< "$current_game_version" if [[ "$mod_required_version" == "$game_major.$game_minor" ]] || [[ "$game_major.$game_minor" == "1.0" && "$mod_required_version" == "0.18" ]]; then From b5e4bfd0194a88eaf2fed66f9e10bcca909693c3 Mon Sep 17 00:00:00 2001 From: Florian Kinder Date: Tue, 3 Nov 2026 15:54:21 +0100 Subject: [PATCH 3/3] Test mod compatibility and release selection with Bats --- .github/workflows/tests.yml | 6 ++++ docker/files/update-mods.sh | 23 +++++++------ tests/update-mods.bats | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 tests/update-mods.bats 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 56eb3391..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" @@ -231,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)' ] +}