|
1 | | -import { hasApprovedPullRuns } from "lib/bot/utils"; |
| 1 | +import { |
| 2 | + clearFilesChangedCache, |
| 3 | + getFilesChangedByPrCached, |
| 4 | + hasApprovedPullRuns, |
| 5 | +} from "lib/bot/utils"; |
2 | 6 | import nock from "nock"; |
3 | 7 | import { Probot } from "probot"; |
4 | 8 | import * as utils from "./utils"; |
@@ -89,3 +93,82 @@ describe("utils: hasApprovedPullRuns", () => { |
89 | 93 | await checkhasApprovedPullRunsReturns(false); |
90 | 94 | }); |
91 | 95 | }); |
| 96 | + |
| 97 | +describe("utils: getFilesChangedByPrCached", () => { |
| 98 | + beforeEach(() => clearFilesChangedCache()); |
| 99 | + afterEach(() => clearFilesChangedCache()); |
| 100 | + |
| 101 | + test("dedupes by head sha; re-fetches on sha change or after clear", async () => { |
| 102 | + const octokit = { |
| 103 | + paginate: jest |
| 104 | + .fn() |
| 105 | + .mockResolvedValue([{ filename: "a.py" }, { filename: "b.py" }]), |
| 106 | + } as any; |
| 107 | + const call = (sha: string) => |
| 108 | + getFilesChangedByPrCached( |
| 109 | + octokit, |
| 110 | + "delivery1", |
| 111 | + "pytorch", |
| 112 | + "pytorch", |
| 113 | + 1, |
| 114 | + sha |
| 115 | + ); |
| 116 | + |
| 117 | + expect(await call("sha1")).toEqual(["a.py", "b.py"]); |
| 118 | + expect(await call("sha1")).toEqual(["a.py", "b.py"]); |
| 119 | + expect(octokit.paginate).toHaveBeenCalledTimes(1); |
| 120 | + |
| 121 | + await call("sha2"); |
| 122 | + expect(octokit.paginate).toHaveBeenCalledTimes(2); |
| 123 | + |
| 124 | + clearFilesChangedCache(); |
| 125 | + await call("sha1"); |
| 126 | + expect(octokit.paginate).toHaveBeenCalledTimes(3); |
| 127 | + }); |
| 128 | + |
| 129 | + test("dedupes within one delivery; re-fetches across deliveries", async () => { |
| 130 | + const octokit = { |
| 131 | + paginate: jest.fn().mockResolvedValue([{ filename: "a.py" }]), |
| 132 | + } as any; |
| 133 | + const call = (deliveryId: string) => |
| 134 | + getFilesChangedByPrCached( |
| 135 | + octokit, |
| 136 | + deliveryId, |
| 137 | + "pytorch", |
| 138 | + "pytorch", |
| 139 | + 1, |
| 140 | + "sha1" |
| 141 | + ); |
| 142 | + |
| 143 | + await call("delivery1"); |
| 144 | + await call("delivery1"); |
| 145 | + expect(octokit.paginate).toHaveBeenCalledTimes(1); |
| 146 | + |
| 147 | + await call("delivery2"); |
| 148 | + expect(octokit.paginate).toHaveBeenCalledTimes(2); |
| 149 | + }); |
| 150 | + |
| 151 | + test("a later delivery sees its own file list after a base retarget", async () => { |
| 152 | + const octokit = { |
| 153 | + paginate: jest |
| 154 | + .fn() |
| 155 | + .mockResolvedValueOnce([{ filename: "torch/csrc/foo.cpp" }]) |
| 156 | + .mockResolvedValueOnce([{ filename: "docs/readme.md" }]), |
| 157 | + } as any; |
| 158 | + // Retargeting a PR's base fires pull_request.edited without moving |
| 159 | + // head.sha, so every key component except the delivery id is identical. |
| 160 | + const call = (deliveryId: string) => |
| 161 | + getFilesChangedByPrCached( |
| 162 | + octokit, |
| 163 | + deliveryId, |
| 164 | + "pytorch", |
| 165 | + "pytorch", |
| 166 | + 1, |
| 167 | + "sha1" |
| 168 | + ); |
| 169 | + |
| 170 | + expect(await call("delivery1")).toEqual(["torch/csrc/foo.cpp"]); |
| 171 | + expect(await call("delivery2")).toEqual(["docs/readme.md"]); |
| 172 | + expect(octokit.paginate).toHaveBeenCalledTimes(2); |
| 173 | + }); |
| 174 | +}); |
0 commit comments