test: integration tests (#40)

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
This commit is contained in:
Parker Brown
2023-09-05 08:32:55 -07:00
committed by GitHub
parent 49ce228ea7
commit 10f155294b
10 changed files with 2021 additions and 8 deletions
+32 -2
View File
@@ -1,5 +1,8 @@
name: test
on:
push:
branches:
- main
pull_request:
concurrency:
@@ -7,13 +10,40 @@ concurrency:
cancel-in-progress: true
jobs:
test:
integration:
name: Integration
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.16"
node-version: 20
cache: "npm"
- run: npm ci
- run: npm run build
- uses: ./ # Uses the action in the root directory
id: test
with:
app_id: ${{ vars.TEST_APP_ID }}
private_key: ${{ secrets.TEST_APP_PRIVATE_KEY }}
- uses: octokit/request-action@v2.x
id: get-repository
env:
GITHUB_TOKEN: ${{ steps.test.outputs.token }}
with:
route: GET /installation/repositories
- run: echo '${{ steps.get-repository.outputs.data }}'
end-to-end:
name: End-to-End
runs-on: ubuntu-latest
# do not run from forks, as forks dont have access to repository secrets
if: github.repository_owner == 'actions'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
cache: "npm"
- run: npm ci
- run: npm run build
+4 -1
View File
@@ -7,7 +7,10 @@
export async function post(core, request) {
const token = core.getState("token");
if (!token) return;
if (!token) {
core.info("Token is not set");
return;
}
await request("DELETE /installation/token", {
headers: {
+1892 -3
View File
File diff suppressed because it is too large Load Diff
+5 -2
View File
@@ -6,7 +6,7 @@
"description": "GitHub Action for creating a GitHub App Installation Access Token",
"scripts": {
"build": "esbuild main.js post.js --bundle --outdir=dist --out-extension:.js=.cjs --platform=node --target=node16.16",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava tests/index.js"
},
"license": "MIT",
"dependencies": {
@@ -15,8 +15,11 @@
"@octokit/request": "^8.1.1"
},
"devDependencies": {
"ava": "^5.3.1",
"dotenv": "^16.3.1",
"esbuild": "^0.19.2"
"esbuild": "^0.19.2",
"execa": "^8.0.1",
"undici": "^5.23.0"
},
"release": {
"branches": [
+19
View File
@@ -0,0 +1,19 @@
# Tests
Add one test file per scenario. You can run them in isolation with:
```bash
node tests/post-token-set.test.js
```
All tests are run together in [tests/index.js](index.js), which can be execauted with ava
```
npx ava tests/index.js
```
or with npm
```
npm test
```
+14
View File
@@ -0,0 +1,14 @@
import { readdirSync } from "node:fs";
import { execa } from "execa";
import test from "ava";
const tests = readdirSync("tests").filter((file) => file.endsWith(".test.js"));
for (const file of tests) {
test(file, async (t) => {
const { stderr, stdout } = await execa("node", [`tests/${file}`]);
t.snapshot(stderr, "stderr");
t.snapshot(stdout, "stdout");
});
}
+25
View File
@@ -0,0 +1,25 @@
import { MockAgent, setGlobalDispatcher } from "undici";
// state variables are set as environment variables with the prefix STATE_
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#sending-values-to-the-pre-and-post-actions
process.env.STATE_token = "secret123";
const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
// Provide the base url to the request
const mockPool = mockAgent.get("https://api.github.com");
// intercept the request
mockPool
.intercept({
path: "/installation/token",
method: "DELETE",
headers: {
authorization: "token secret123",
},
})
.reply(204);
await import("../post.js");
+5
View File
@@ -0,0 +1,5 @@
// state variables are set as environment variables with the prefix STATE_
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#sending-values-to-the-pre-and-post-actions
delete process.env.STATE_token;
await import("../post.js");
+25
View File
@@ -0,0 +1,25 @@
# Snapshot report for `tests/index.js`
The actual snapshot is saved in `index.js.snap`.
Generated by [AVA](https://avajs.dev).
## post-token-set.test.js
> stderr
''
> stdout
'Token revoked'
## post-token-unset.test.js
> stderr
''
> stdout
'Token is not set'
Binary file not shown.