sync.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. console.log(result.output.toString());
  58. // Check failure
  59. if (result.status !== 0) {
  60. console.error("Please fix the error and re-run this command.");
  61. process.exitCode = 1;
  62. return;
  63. }
  64. }
  65. // Sync markdown files
  66. spawn("anchor", ["run", "sync-markdown"]);
  67. })();