Skip to content

Commit

Permalink
fix problem with __dirname and env token imports
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored and wolfy1339 committed Jun 11, 2021
1 parent 4dd61b7 commit 385d9d0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 49 deletions.
17 changes: 8 additions & 9 deletions bin/record.js
Expand Up @@ -17,25 +17,31 @@ 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"],
});
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) => {
Expand Down Expand Up @@ -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 = {
Expand Down
13 changes: 0 additions & 13 deletions lib/normalize/index.js
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions 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,
Expand Down
17 changes: 9 additions & 8 deletions lib/remove-credentials.js
Expand Up @@ -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;
}
12 changes: 8 additions & 4 deletions 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([
Expand Down
@@ -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"
Expand All @@ -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",
});
Expand All @@ -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}`,
},
});

Expand All @@ -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}`,
},
});

Expand All @@ -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}`,
},
});

Expand All @@ -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}`,
},
});

Expand All @@ -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}`,
},
});

Expand All @@ -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}`,
},
});

Expand Down
10 changes: 8 additions & 2 deletions 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";
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 385d9d0

Please sign in to comment.