pisg-autoalias-test.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. """Tests for pisg-autoalias.py: python3 scripts/pisg-autoalias-test.py
  3. Builds small logs in a temporary folder and checks each rule; touches nothing else."""
  4. import os
  5. import subprocess
  6. import sys
  7. import tempfile
  8. HERE = os.path.dirname(os.path.abspath(__file__))
  9. SCRIPT = os.path.join(HERE, "pisg-autoalias.py")
  10. passed = failed = 0
  11. def ok(cond, msg):
  12. global passed, failed
  13. if cond:
  14. passed += 1
  15. else:
  16. failed += 1
  17. print(" FAIL:", msg)
  18. with tempfile.TemporaryDirectory() as tmp:
  19. os.mkdir(os.path.join(tmp, "logs"))
  20. L = []
  21. join = lambda n, h: L.append(f"[10:00:00] {n} (~u@{h}) joined #Test.")
  22. part = lambda n, h: L.append(f"[11:00:00] {n} (~u@{h}) left #Test.")
  23. say = lambda n: L.append(f"[10:30:00] <{n}> hello there")
  24. join("Alice", "alice.users.undernet.org"); join("Alice_", "alice.users.undernet.org"); part("+Al", "alice.users.undernet.org")
  25. for _ in range(5): say("Alice")
  26. say("Alice_")
  27. join("Gina", "irccloud.example.net"); join("Hank", "irccloud.example.net")
  28. join("Bob", "bob.users.undernet.org"); join("Bobby", "bob.users.undernet.org")
  29. join("Carl", "shared.users.undernet.org"); join("Carla", "shared.users.undernet.org")
  30. for i in range(31): join(f"Crowd{i}", "crowd.users.undernet.org")
  31. join("Solo", "solo.users.undernet.org")
  32. join("%Dan", "DAN.users.undernet.org"); join("Dan2", "dan.users.undernet.org"); say("Dan2"); say("Dan2")
  33. open(os.path.join(tmp, "logs", "t.log.20260101"), "w").write("\n".join(L) + "\n")
  34. open(os.path.join(tmp, "logs", ".hidden.tmp"), "w").write(
  35. "[10:00:00] Zed (~u@zed.users.undernet.org) joined #Test.\n[10:00:00] Zed2 (~u@zed.users.undernet.org) joined #Test.\n")
  36. open(os.path.join(tmp, "manual.cfg"), "w").write('# mine\n<user nick="Bob" alias="B2" sex="m">\n<user nick="Carl">\n<user nick="Carla" sex="f">\n')
  37. out = os.path.join(tmp, "out.cfg")
  38. run = lambda: subprocess.run([sys.executable, SCRIPT, "--logdir", os.path.join(tmp, "logs"), "--prefix", "t.log.",
  39. "--manual", os.path.join(tmp, "manual.cfg"), "--out", out], capture_output=True, text=True)
  40. r = run()
  41. ok(r.returncode == 0, "runs: " + r.stderr[-200:])
  42. t = open(out).read()
  43. ok('nick="Alice" alias="Al Alice_"' in t, "busiest nick names the group; the +Al prefix is stripped and merged")
  44. ok("Gina" not in t and "Hank" not in t, "a shared non-X host is never merged")
  45. ok('nick="Bob" alias="Bobby"' in t and "B2" not in t, "a group touching your entry joins YOUR entry")
  46. ok("Carl" not in t and "Carla" not in t, "a group touching two of your entries is skipped")
  47. ok("Crowd" not in t, "an oversized group is skipped")
  48. ok("Solo" not in t, "a single-nick account produces nothing")
  49. ok('nick="Dan2" alias="Dan"' in t, "host match ignores case; %-prefix stripped; busiest chatter named")
  50. ok("Zed" not in t, "hidden and temporary files in the log folder are ignored")
  51. first = [l for l in t.splitlines() if "Generated by" not in l]
  52. run()
  53. second = [l for l in open(out).read().splitlines() if "Generated by" not in l]
  54. ok(first == second, "a second run gives the same result")
  55. ok(not [f for f in os.listdir(tmp) if f.startswith(".autoalias")], "no temporary file left behind")
  56. print(f"{passed} passed, {failed} failed")
  57. sys.exit(1 if failed else 0)