sync.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. spawn,
  16. } from "./utils";
  17. (async () => {
  18. const bench = await BenchData.open();
  19. const cargoToml = await Toml.open(
  20. path.join("..", "programs", "bench", "Cargo.toml")
  21. );
  22. const anchorToml = await Toml.open(path.join("..", "Anchor.toml"));
  23. for (const version of bench.getVersions()) {
  24. console.log(`Updating '${version}'...`);
  25. const isUnreleased = version === "unreleased";
  26. // Use the lock file from cache
  27. await LockFile.replace(version);
  28. // Set active solana version
  29. VersionManager.setSolanaVersion(bench.get(version).solanaVersion);
  30. // Update the anchor dependency versions
  31. for (const dependency of ["lang", "spl"]) {
  32. cargoToml.replaceValue(`anchor-${dependency}`, () => {
  33. return isUnreleased
  34. ? `{ path = "../../../../${dependency}" }`
  35. : `"${version}"`;
  36. });
  37. }
  38. // Save Cargo.toml
  39. await cargoToml.save();
  40. // Update `anchor test` command to pass version in Anchor.toml
  41. anchorToml.replaceValue(
  42. "test",
  43. (cmd) => {
  44. return cmd.includes(ANCHOR_VERSION_ARG)
  45. ? cmd.replace(
  46. new RegExp(`\\s*${ANCHOR_VERSION_ARG}\\s+(.+)`),
  47. (arg, ver) => (isUnreleased ? "" : arg.replace(ver, version))
  48. )
  49. : `${cmd} ${ANCHOR_VERSION_ARG} ${version}`;
  50. },
  51. { insideQuotes: true }
  52. );
  53. // Save Anchor.toml
  54. await anchorToml.save();
  55. // Run the command to update the current version's results
  56. const result = runAnchorTest();
  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. // Sync markdown files
  65. spawn("anchor", ["run", "sync-markdown"]);
  66. })();