sync.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Sync all saved data by re-running the tests for each version.
  3. *
  4. * This script should be used when the bench program or its tests has changed
  5. * and all data needs to be updated.
  6. */
  7. import path from "path";
  8. import {
  9. ANCHOR_VERSION_ARG,
  10. BenchData,
  11. LockFile,
  12. Toml,
  13. VersionManager,
  14. runAnchorTest,
  15. } from "./utils";
  16. (async () => {
  17. const bench = await BenchData.open();
  18. const cargoToml = await Toml.open(
  19. path.join("..", "programs", "bench", "Cargo.toml")
  20. );
  21. const anchorToml = await Toml.open(path.join("..", "Anchor.toml"));
  22. for (const version of bench.getVersions()) {
  23. console.log(`Updating '${version}'...`);
  24. const isUnreleased = version === "unreleased";
  25. // Use the lock file from cache
  26. await LockFile.replace(version);
  27. // Set active solana version
  28. VersionManager.setSolanaVersion(bench.get(version).solanaVersion);
  29. // Update the anchor dependency versions
  30. for (const dependency of ["lang", "spl"]) {
  31. cargoToml.replaceValue(`anchor-${dependency}`, () => {
  32. return isUnreleased
  33. ? `{ path = "../../../../${dependency}" }`
  34. : `"${version}"`;
  35. });
  36. }
  37. // Save Cargo.toml
  38. await cargoToml.save();
  39. // Update `anchor test` command to pass version in Anchor.toml
  40. anchorToml.replaceValue(
  41. "test",
  42. (cmd) => {
  43. return cmd.includes(ANCHOR_VERSION_ARG)
  44. ? cmd.replace(
  45. new RegExp(`\\s*${ANCHOR_VERSION_ARG}\\s+(.+)`),
  46. (arg, ver) => (isUnreleased ? "" : arg.replace(ver, version))
  47. )
  48. : `${cmd} ${ANCHOR_VERSION_ARG} ${version}`;
  49. },
  50. { insideQuotes: true }
  51. );
  52. // Save Anchor.toml
  53. await anchorToml.save();
  54. // Run the command to update the current version's results
  55. const result = runAnchorTest();
  56. console.log(result.output.toString());
  57. // Check failure
  58. if (result.status !== 0) {
  59. console.error("Please fix the error and re-run this command.");
  60. process.exitCode = 1;
  61. return;
  62. }
  63. }
  64. })();