From 385d9d05c8abf280c850f8210a616f6775ab6e7b Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Fri, 7 May 2021 12:54:35 -0700 Subject: [PATCH] fix problem with `__dirname` and env token imports --- bin/record.js | 17 ++++++++--------- lib/normalize/index.js | 13 ------------- lib/read.js | 5 +++-- lib/remove-credentials.js | 17 +++++++++-------- lib/write.js | 12 ++++++++---- .../record.js | 19 ++++++++----------- .../api.github.com/release-assets/record.js | 10 ++++++++-- 7 files changed, 44 insertions(+), 49 deletions(-) diff --git a/bin/record.js b/bin/record.js index cb2d25fa5..90d04b564 100755 --- a/bin/record.js +++ b/bin/record.js @@ -17,13 +17,13 @@ import cloneDeep from "lodash/cloneDeep.js"; import { diff, diffString } from "json-diff"; import glob from "glob"; import humanize from "humanize-string"; +import minimist from "minimist"; import normalize from "../lib/normalize/index.js"; import read from "../lib/read.js"; import recordScenario from "../lib/record-scenario.js"; import write from "../lib/write.js"; -import minimist from "minimist"; const argv = minimist(process.argv.slice(2), { boolean: ["update", "test-cached"], }); @@ -31,11 +31,17 @@ const doUpdate = argv.update; const selectedScenarios = argv._; const hasSelectedScenarios = selectedScenarios.length > 0; +console.log(`hasSelectedScenarios`); +console.log(hasSelectedScenarios); + const scenarios = hasSelectedScenarios ? selectedScenarios : glob.sync("scenarios/**/record.js"); const diffs = []; +console.log(`scenarios`); +console.log(scenarios); + // run scenarios one by one scenarios .reduce(async (promise, scenarioPath) => { @@ -73,17 +79,10 @@ scenarios return limiter.schedule(async () => config); }); - // set strict validation header, remove once stricter validations are applied - // to all requests: https://developer.github.com/changes/2018-11-07-strict-validation/ - request.interceptors.request.use((config) => { - config.headers.Accept = `${config.headers.Accept},application/vnd.github.speedy-preview+json`; - return config; - }); - const oldNormalizedFixtures = await read(fixtureName); const newRawFixtures = await recordScenario({ request: request, - scenario: require(`../scenarios/${fixtureName}/record`), + scenario: await import(`../scenarios/${fixtureName}/record.js`), }); const scenarioState = { diff --git a/lib/normalize/index.js b/lib/normalize/index.js index d60dec6d0..9ef50010f 100644 --- a/lib/normalize/index.js +++ b/lib/normalize/index.js @@ -140,22 +140,9 @@ async function normalize(scenarioState, fixture) { } } - // remove strict validation headers - // TODO: remove after Nov 1, see https://blog.github.com/changelog/2018-09-26-new-preview-header-for-strict-validation-in-rest-api/ - fixture.reqheaders.accept = fixture.reqheaders.accept.replace( - ",application/vnd.github.speedy-preview+json", - "" - ); - // remove varnishs header if present setIfExists(fixture.headers, "x-varnish", "1000"); setIfExists(fixture.headers, "age", "0"); - if (fixture.headers["x-github-media-type"]) { - fixture.headers["x-github-media-type"] = fixture.headers[ - "x-github-media-type" - ].replace(" param=speedy-preview;", ""); - } - return fixture; } diff --git a/lib/read.js b/lib/read.js index 7725bc16c..3a86d53ff 100644 --- a/lib/read.js +++ b/lib/read.js @@ -1,10 +1,11 @@ export default read; -import { resolve } from "path"; +import { resolve, dirname } from "path"; +import { fileURLToPath } from "url"; function read(fixturesPath) { const path = resolve( - __dirname, + dirname(fileURLToPath(import.meta.url)), "..", "scenarios", fixturesPath, diff --git a/lib/remove-credentials.js b/lib/remove-credentials.js index 6b208ca67..fc37d116a 100644 --- a/lib/remove-credentials.js +++ b/lib/remove-credentials.js @@ -10,15 +10,16 @@ const tokenToFixture = { }; function removeCredentials(fixture) { + console.log(`fixture.reqheaders`); + console.log(fixture.reqheaders); + // zerofy auth token - fixture.reqheaders.authorization = fixture.reqheaders.authorization.replace( - /^token (\w{40})$/, - (_, token) => { - token = - tokenToFixture[token] || "0000000000000000000000000000000000000000"; - return `token ${token}`; - } - ); + fixture.reqheaders.authorization = ( + fixture.reqheaders.authorization || "" + ).replace(/^token (\w{40})$/, (_, token) => { + token = tokenToFixture[token] || "0000000000000000000000000000000000000000"; + return `token ${token}`; + }); return fixture; } diff --git a/lib/write.js b/lib/write.js index b658e0382..fca391fd8 100644 --- a/lib/write.js +++ b/lib/write.js @@ -1,15 +1,19 @@ export default write; -import { promisify } from "util"; import { resolve, dirname } from "path"; -import { writeFile as _writeFile } from "fs"; -const writeFile = promisify(_writeFile); +import { writeFile } from "fs/promises"; +import { fileURLToPath } from "url"; import prettier from "prettier"; import mkdirp from "mkdirp"; async function write(fixturesPath, fixtures) { - const path = resolve(__dirname, "..", "scenarios", fixturesPath); + const path = resolve( + dirname(fileURLToPath(import.meta.url)), + "..", + "scenarios", + fixturesPath + ); await mkdirp(dirname(path)); return Promise.all([ diff --git a/scenarios/api.github.com/add-and-remove-repository-collaborator/record.js b/scenarios/api.github.com/add-and-remove-repository-collaborator/record.js index b03c4e8e3..5c71b1c3f 100644 --- a/scenarios/api.github.com/add-and-remove-repository-collaborator/record.js +++ b/scenarios/api.github.com/add-and-remove-repository-collaborator/record.js @@ -1,9 +1,6 @@ export default addAndRemoveRepostioryCollaborator; -import { - FIXTURES_USER_A_TOKEN_FULL_ACCESS, - FIXTURES_USER_B_TOKEN_FULL_ACCESS, -} from "../../../lib/env.js"; +import env from "../../../lib/env.js"; import getTemporaryRepository from "../../../lib/temporary-repository.js"; // - As user A, invite user B as collaborator to repository "octokit-fixture-org/hello-world" @@ -16,7 +13,7 @@ async function addAndRemoveRepostioryCollaborator(state) { // create a temporary repository const temporaryRepository = getTemporaryRepository({ request: state.request, - token: FIXTURES_USER_A_TOKEN_FULL_ACCESS, + token: env.FIXTURES_USER_A_TOKEN_FULL_ACCESS, org: "octokit-fixture-org", name: "add-and-remove-repository-collaborator", }); @@ -29,7 +26,7 @@ async function addAndRemoveRepostioryCollaborator(state) { url: `/repos/octokit-fixture-org/${temporaryRepository.name}/collaborators/octokit-fixture-user-b`, headers: { Accept: "application/vnd.github.v3+json", - Authorization: `token ${FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, + Authorization: `token ${env.FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, }, }); @@ -39,7 +36,7 @@ async function addAndRemoveRepostioryCollaborator(state) { url: `/repos/octokit-fixture-org/${temporaryRepository.name}/invitations`, headers: { Accept: "application/vnd.github.v3+json", - Authorization: `token ${FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, + Authorization: `token ${env.FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, }, }); @@ -49,7 +46,7 @@ async function addAndRemoveRepostioryCollaborator(state) { url: `/user/repository_invitations/${invitationsResponse.data[0].id}`, headers: { Accept: "application/vnd.github.v3+json", - Authorization: `token ${FIXTURES_USER_B_TOKEN_FULL_ACCESS}`, + Authorization: `token ${env.FIXTURES_USER_B_TOKEN_FULL_ACCESS}`, }, }); @@ -62,7 +59,7 @@ async function addAndRemoveRepostioryCollaborator(state) { url: `/repos/octokit-fixture-org/${temporaryRepository.name}/collaborators`, headers: { Accept: "application/vnd.github.v3+json", - Authorization: `token ${FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, + Authorization: `token ${env.FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, }, }); @@ -72,7 +69,7 @@ async function addAndRemoveRepostioryCollaborator(state) { url: `/repos/octokit-fixture-org/${temporaryRepository.name}/collaborators/octokit-fixture-user-b`, headers: { Accept: "application/vnd.github.v3+json", - Authorization: `token ${FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, + Authorization: `token ${env.FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, }, }); @@ -82,7 +79,7 @@ async function addAndRemoveRepostioryCollaborator(state) { url: `/repos/octokit-fixture-org/${temporaryRepository.name}/collaborators`, headers: { Accept: "application/vnd.github.v3+json", - Authorization: `token ${FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, + Authorization: `token ${env.FIXTURES_USER_A_TOKEN_FULL_ACCESS}`, }, }); diff --git a/scenarios/api.github.com/release-assets/record.js b/scenarios/api.github.com/release-assets/record.js index 30cea6916..1f0a81ba4 100644 --- a/scenarios/api.github.com/release-assets/record.js +++ b/scenarios/api.github.com/release-assets/record.js @@ -1,6 +1,9 @@ export default releaseAssets; + import fs from "fs"; -import { resolve as pathResolve } from "path"; +import { resolve as pathResolve, dirname } from "path"; +import { fileURLToPath } from "url"; + import urlTemplate from "url-template"; import env from "../../../lib/env.js"; import getTemporaryRepository from "../../../lib/temporary-repository.js"; @@ -72,7 +75,10 @@ async function releaseAssets(state) { // https://developer.github.com/v3/repos/releases/#upload-a-release-asset // upload attachment to release URL returned by create release request const FILE_NAME = "test-upload.txt"; - const filePath = pathResolve(__dirname, FILE_NAME); + const filePath = pathResolve( + dirname(fileURLToPath(import.meta.url)), + FILE_NAME + ); const url = urlTemplate.parse(uploadUrl).expand({ name: FILE_NAME, label: "test",