Merge pull request #101 from github/joshmgross/fetch-mocking
Replace `nock` and add tests for action input descriptions
This commit is contained in:
@@ -53,8 +53,8 @@
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.0.3",
|
||||
"fetch-mock": "^9.11.0",
|
||||
"jest": "^29.0.3",
|
||||
"nock": "^13.2.9",
|
||||
"prettier": "^2.7.1",
|
||||
"rimraf": "^3.0.2",
|
||||
"ts-jest": "^29.0.3",
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import {data, DescriptionDictionary} from "@github/actions-expressions";
|
||||
import {getStepsContext as getDefaultStepsContext} from "@github/actions-languageservice/context-providers/steps";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import nock from "nock";
|
||||
import fetchMock from "fetch-mock";
|
||||
|
||||
import {createWorkflowContext} from "../test-utils/workflow-context";
|
||||
import {TTLCache} from "../utils/cache";
|
||||
import {getStepsContext} from "./steps";
|
||||
@@ -54,14 +55,6 @@ const actionMetadata = {
|
||||
}
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
nock.disableNetConnect();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
it("returns default context when job is undefined", async () => {
|
||||
const workflowContext = createWorkflowContext(workflow, undefined);
|
||||
const defaultContext = getDefaultStepsContext(workflowContext);
|
||||
@@ -71,12 +64,23 @@ it("returns default context when job is undefined", async () => {
|
||||
});
|
||||
|
||||
it("adds action outputs", async () => {
|
||||
nock("https://api.github.com").get("/repos/actions/cache/contents/action.yml?ref=v3").reply(200, actionMetadata);
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/cache/contents/action.yml?ref=v3", actionMetadata);
|
||||
|
||||
const workflowContext = createWorkflowContext(workflow, "build");
|
||||
const defaultContext = getDefaultStepsContext(workflowContext);
|
||||
|
||||
const stepsContext = await getStepsContext(new Octokit(), new TTLCache(), defaultContext, workflowContext);
|
||||
const stepsContext = await getStepsContext(
|
||||
new Octokit({
|
||||
request: {
|
||||
fetch: mock
|
||||
}
|
||||
}),
|
||||
new TTLCache(),
|
||||
defaultContext,
|
||||
workflowContext
|
||||
);
|
||||
expect(stepsContext).toBeDefined();
|
||||
|
||||
expect(stepsContext).toEqual(
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
import {StringToken} from "@github/actions-workflow-parser/templates/tokens/string-token";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import fetchMock from "fetch-mock";
|
||||
|
||||
import {createWorkflowContext} from "../test-utils/workflow-context";
|
||||
import {TTLCache} from "../utils/cache";
|
||||
import {getActionInputDescription} from "./action-input";
|
||||
|
||||
const workflow = `
|
||||
name: Hello World
|
||||
on: workflow_dispatch
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
`;
|
||||
|
||||
// A simplified version of the action.yml file from actions/checkout
|
||||
const actionMetadataContent = `
|
||||
name: 'Checkout'
|
||||
description: 'Checkout a Git repository at a particular version'
|
||||
inputs:
|
||||
repository:
|
||||
description: Repository name with owner. For example, actions/checkout
|
||||
default: \${{ github.repository }}
|
||||
ref:
|
||||
description: The branch, tag or SHA to checkout.
|
||||
required: true
|
||||
token:
|
||||
description: Personal access token (PAT) used to fetch the repository.
|
||||
default: \${{ github.token }}
|
||||
repo:
|
||||
description: 'Repository name with owner. For example, actions/checkout'
|
||||
deprecationMessage: 'Use repository instead'
|
||||
runs:
|
||||
using: node16
|
||||
main: dist/index.js
|
||||
post: dist/index.js
|
||||
`;
|
||||
|
||||
// Based on https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3
|
||||
const actionMetadata = {
|
||||
name: "action.yml",
|
||||
path: "action.yml",
|
||||
sha: "cab09ebd3a964aba67b57f9727f5f6fff1372b04",
|
||||
size: 3649,
|
||||
url: "https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3",
|
||||
html_url: "https://github.com/actions/checkout/blob/v3/action.yml",
|
||||
git_url: "https://api.github.com/repos/actions/checkout/git/blobs/cab09ebd3a964aba67b57f9727f5f6fff1372b04",
|
||||
download_url: "https://raw.githubusercontent.com/actions/checkout/v3/action.yml",
|
||||
type: "file",
|
||||
content: Buffer.from(actionMetadataContent).toString("base64"),
|
||||
encoding: "base64",
|
||||
_links: {
|
||||
self: "https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3",
|
||||
git: "https://api.github.com/repos/actions/checkout/git/blobs/cab09ebd3a964aba67b57f9727f5f6fff1372b04",
|
||||
html: "https://github.com/actions/checkout/blob/v3/action.yml"
|
||||
}
|
||||
};
|
||||
|
||||
async function getDescription(input: string, mock: fetchMock.FetchMockSandbox) {
|
||||
const workflowContext = createWorkflowContext(workflow, "build", 0);
|
||||
|
||||
return await getActionInputDescription(
|
||||
new Octokit({
|
||||
request: {
|
||||
fetch: mock
|
||||
}
|
||||
}),
|
||||
new TTLCache(),
|
||||
workflowContext.step!,
|
||||
new StringToken(undefined, undefined, input, undefined)
|
||||
);
|
||||
}
|
||||
|
||||
describe("action descriptions", () => {
|
||||
it("optional input", async () => {
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3", actionMetadata);
|
||||
|
||||
expect(await getDescription("repository", mock)).toEqual(
|
||||
"Repository name with owner. For example, actions/checkout"
|
||||
);
|
||||
});
|
||||
|
||||
it("required input", async () => {
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3", actionMetadata);
|
||||
|
||||
expect(await getDescription("ref", mock)).toEqual("The branch, tag or SHA to checkout.\n\n**Required**");
|
||||
});
|
||||
|
||||
it("deprecated input", async () => {
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3", actionMetadata);
|
||||
|
||||
expect(await getDescription("repo", mock)).toEqual(
|
||||
"Repository name with owner. For example, actions/checkout\n\n**Deprecated**"
|
||||
);
|
||||
});
|
||||
|
||||
it("invalid input", async () => {
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3", actionMetadata);
|
||||
|
||||
expect(await getDescription("typo", mock)).toBeUndefined();
|
||||
});
|
||||
|
||||
// TODO: https://github.com/github/c2c-actions-experience/issues/7056
|
||||
it.failing("action does not exist", async () => {
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3", 404)
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yaml?ref=v3", 404);
|
||||
|
||||
expect(await getDescription("repository", mock)).toBeUndefined();
|
||||
});
|
||||
|
||||
// TODO: https://github.com/github/c2c-actions-experience/issues/7056
|
||||
it.failing("invalid permissions", async () => {
|
||||
const mock = fetchMock
|
||||
.sandbox()
|
||||
.getOnce("https://api.github.com/repos/actions/checkout/contents/action.yml?ref=v3", 403);
|
||||
|
||||
expect(await getDescription("repository", mock)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -19,7 +19,7 @@ export function createWorkflowContext(workflow: string, job?: string, stepIndex?
|
||||
context.job = template.jobs.find(j => j.id.value === job);
|
||||
}
|
||||
|
||||
if (stepIndex) {
|
||||
if (stepIndex !== undefined) {
|
||||
context.step = context.job?.steps[stepIndex];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user