sync.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 { spawnSync } from "child_process";
  9. import { ANCHOR_VERSION_ARG, BenchData, Toml } from "./utils";
  10. (async () => {
  11. const bench = await BenchData.open();
  12. const cargoToml = await Toml.open(
  13. path.join("..", "programs", "bench", "Cargo.toml")
  14. );
  15. const anchorToml = await Toml.open(path.join("..", "Anchor.toml"));
  16. for (const version of bench.getVersions()) {
  17. console.log(`Updating '${version}'...`);
  18. const isUnreleased = version === "unreleased";
  19. // Update the anchor dependency versions
  20. for (const dependency of ["lang", "spl"]) {
  21. cargoToml.replaceValue(`anchor-${dependency}`, () => {
  22. return isUnreleased
  23. ? `{ path = "../../../../${dependency}" }`
  24. : `"${version}"`;
  25. });
  26. }
  27. // Save Cargo.toml
  28. await cargoToml.save();
  29. // Update `anchor test` command to pass version in Anchor.toml
  30. anchorToml.replaceValue(
  31. "test",
  32. (cmd) => {
  33. return cmd.includes(ANCHOR_VERSION_ARG)
  34. ? cmd.replace(
  35. new RegExp(`\\s*${ANCHOR_VERSION_ARG}\\s+(.+)`),
  36. (arg, ver) => (isUnreleased ? "" : arg.replace(ver, version))
  37. )
  38. : `${cmd} ${ANCHOR_VERSION_ARG} ${version}`;
  39. },
  40. { insideQuotes: true }
  41. );
  42. // Save Anchor.toml
  43. await anchorToml.save();
  44. // Run the command to update the current version's results
  45. const result = spawnSync("anchor", ["test", "--skip-lint"]);
  46. console.log(result.output.toString());
  47. // Check for failure
  48. if (result.status !== 0) {
  49. console.error("Please fix the error and re-run this command.");
  50. process.exitCode = 1;
  51. return;
  52. }
  53. }
  54. })();