test_install_env_version.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """Regression tests for VERSION updates performed by install.sh."""
  2. import shutil
  3. import stat
  4. import subprocess
  5. from pathlib import Path
  6. import pytest
  7. PROJECT_ROOT = Path(__file__).resolve().parent.parent
  8. INSTALL_SCRIPT = PROJECT_ROOT / "install.sh"
  9. UPDATED_VERSION = "9.8.7"
  10. def _write_version_update_harness(tmp_path: Path) -> Path:
  11. """Create a shell harness that loads installer functions without running main."""
  12. installer_source = INSTALL_SCRIPT.read_text(encoding="utf-8")
  13. functions_source, marker, _ = installer_source.partition(
  14. "\n# Procesar argumentos\n"
  15. )
  16. assert marker, "No se encontró el bloque principal de install.sh"
  17. (tmp_path / "install.sh").write_text(
  18. f'CURRENT_VERSION="{UPDATED_VERSION}"\n', encoding="utf-8"
  19. )
  20. harness = tmp_path / "run-version-update.sh"
  21. harness.write_text(
  22. f"{functions_source}\n"
  23. 'LOG_FILE="$1/install-test.log"\n'
  24. 'loadCurrentVersionFromInstalledScript "$1" || exit $?\n'
  25. 'updateEnvVersion "$1"\n',
  26. encoding="utf-8",
  27. )
  28. return harness
  29. def _run_version_update(tmp_path: Path) -> subprocess.CompletedProcess[str]:
  30. harness = _write_version_update_harness(tmp_path)
  31. shell_bin = shutil.which("dash") or "/bin/sh"
  32. return subprocess.run( # noqa: S603
  33. [shell_bin, str(harness), str(tmp_path)],
  34. capture_output=True,
  35. text=True,
  36. check=False,
  37. )
  38. @pytest.mark.parametrize(
  39. "contents",
  40. [
  41. "VERSION=1.0.0\nSQUID_HOST=127.0.0.1\n",
  42. "export VERSION = 1.0.0\n VERSION=0.9.0\nSQUID_HOST=127.0.0.1\n",
  43. "SQUID_HOST=127.0.0.1\n",
  44. "",
  45. ],
  46. )
  47. def test_update_env_version_normalizes_to_installed_current_version(tmp_path, contents):
  48. """The updater writes exactly one VERSION, including for an empty .env."""
  49. env_file = tmp_path / ".env"
  50. env_file.write_text(contents, encoding="utf-8")
  51. env_file.chmod(0o640)
  52. result = _run_version_update(tmp_path)
  53. assert result.returncode == 0, result.stderr
  54. lines = env_file.read_text(encoding="utf-8").splitlines()
  55. assert lines.count(f"VERSION={UPDATED_VERSION}") == 1
  56. assert not any(
  57. line.startswith(("export VERSION", " VERSION", "VERSION =")) for line in lines
  58. )
  59. assert stat.S_IMODE(env_file.stat().st_mode) == 0o640
  60. if "SQUID_HOST" in contents:
  61. assert "SQUID_HOST=127.0.0.1" in lines
  62. def test_update_env_version_fails_when_env_file_is_missing(tmp_path):
  63. """A missing .env is an update failure instead of a false success."""
  64. result = _run_version_update(tmp_path)
  65. assert result.returncode != 0
  66. assert not (tmp_path / ".env").exists()