Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 3a073c5

Browse files
jablkoelibarzilay
authored andcommitted
Add "now" to bot error fixtures
To avoid `RangeError: Invalid time value` when testing changes that make an existing fixture no longer error. RangeError is due to `getNow()` calling `new Date(readJSON(derivedPath).now)`, and `now` being `undefined` in `derived.json`. Changes the `getNow` argument of `deriveStateForPR` to just `now`, passing the value or defaulting to the time the function was called (rather than a short time later, when `getNow` was called). Update snapshots.
1 parent 57cb3a6 commit 3a073c5

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

src/_tests/fixturedActions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function testFixture(dir: string) {
3838
response,
3939
(expr: string) => Promise.resolve(files[expr] as string),
4040
(name: string, _until?: Date) => name in downloads ? downloads[name] : 0,
41-
() => new Date(readJSON(derivedPath).now)
41+
readJSON(derivedPath).now
4242
);
4343

4444
if (derived.type === "fail") throw new Error("Should never happen");

src/_tests/fixtures/45982/derived.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"type": "error",
3+
"now": "2020-07-11T20:41:49.000Z",
34
"message": "error parsing owners: At 1:1 : Expected /\\/\\/ Type definitions for (non-npm package )?/",
45
"pr_number": 45982,
56
"author": "dasa"

src/commands/create-fixture.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default async function main(directory: string, overwriteInfo: boolean) {
3535
response,
3636
shouldOverwrite(filesJSONPath) ? initFetchFilesAndWriteToFile() : getFilesFromFile,
3737
shouldOverwrite(downloadsJSONPath) ? initGetDownloadsAndWriteToFile() : getDownloadsFromFile,
38-
shouldOverwrite(derivedFixturePath) ? undefined : getTimeFromFile,
38+
shouldOverwrite(derivedFixturePath) ? undefined : getTimeFromFile(),
3939
);
4040

4141
writeFileSync(derivedFixturePath, scrubDiagnosticDetails(JSON.stringify(derivedInfo, null, " ")));
@@ -79,7 +79,7 @@ export default async function main(directory: string, overwriteInfo: boolean) {
7979
}
8080

8181
function getTimeFromFile() {
82-
return new Date(JSON.parse(readFileSync(derivedFixturePath, "utf8")).now);
82+
return JSON.parse(readFileSync(derivedFixturePath, "utf8")).now;
8383
}
8484
}
8585

src/pr-info.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface BotFail {
3939
// Some error found, will be passed to `process` to report in a comment
4040
export interface BotError {
4141
readonly type: "error";
42+
readonly now: string;
4243
readonly pr_number: number;
4344
readonly message: string;
4445
readonly author: string | undefined;
@@ -193,7 +194,7 @@ export async function deriveStateForPR(
193194
info: ApolloQueryResult<PRQueryResult>,
194195
fetchFile = defaultFetchFile,
195196
getDownloads = getMonthlyDownloadCount,
196-
getNow = () => new Date(),
197+
now = new Date().toISOString(),
197198
): Promise<PrInfo | BotFail | BotError | BotEnsureRemovedFromProject | BotNoPackages> {
198199
const prInfo = info.data.repository?.pullRequest;
199200

@@ -224,7 +225,6 @@ export async function deriveStateForPR(
224225
const { pkgInfo, popularityLevel } = pkgInfoEtc;
225226
if (!pkgInfo.some(p => p.name)) return botNoPackages(prInfo.number);
226227

227-
const now = getNow().toISOString();
228228
const reviews = getReviews(prInfo);
229229
const latestReview = latestDate(...reviews.map(r => r.date));
230230
const comments = noNulls(prInfo.comments.nodes || []);
@@ -257,7 +257,7 @@ export async function deriveStateForPR(
257257
}
258258

259259
function botError(pr_number: number, message: string): BotError {
260-
return { type: "error", message, pr_number, author: prInfo?.author?.login };
260+
return { type: "error", now, message, pr_number, author: prInfo?.author?.login };
261261
}
262262

263263
function botEnsureRemovedFromProject(pr_number: number, message: string, isDraft: boolean): BotEnsureRemovedFromProject {

0 commit comments

Comments
 (0)