Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to cover basic functionality #21

Merged
merged 8 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build
on:
push:
branches:
- master
- main
pull_request:
branches: '*'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Linter
on:
push:
branches:
- master
- main
pull_request:
branches: '*'

Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on:
push:
branches:
- main
pull_request:
branches: '*'

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install node
uses: actions/setup-node@v4
with:
node-version: 18
- name: Cache
uses: actions/cache@v4
id: cache
with:
path: '**/node_modules'
key: yarn-v1-${{ hashFiles('**/yarn.lock') }}
- name: Install
run: yarn --immutable
if: steps.cache.outputs.cache-hit != 'true'
- name: Test
run: yarn test:ci
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"scripts": {
"build": "tsc",
"start": "probot run ./lib/index.js",
"test": "vitest",
"test": "vitest --coverage --run",
"test:watch": "vitest --coverage --watch",
"test:ci": "vitest --run",
"lint": "eslint .",
"lint:fix": "eslint --fix ."
},
Expand All @@ -30,14 +32,15 @@
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"@vitest/coverage-v8": "^2.0.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"nock": "^14.0.0-beta.5",
"prettier": "^3.3.2",
"smee-client": "^2.0.0",
"typescript": "^5.3.3",
"vitest": "^1.3.1"
"vitest": "^2.0.1"
},
"engines": {
"node": ">= 18"
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/installation.created.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"action": "created",
"installation": {
"id": 2,
"account": {
"login": "github",
"id": 1
},
"repository_selection": "selected",
"access_tokens_url": "https://api.github.com/app/installations/2/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/settings/installations/2"
},
"repositories": [
{
"id": 1296269,
"name": "Hello-World",
"full_name": "octocat/Hello-World"
}
],
"sender": {
"login": "octocat",
"id": 1
}
}
18 changes: 0 additions & 18 deletions test/fixtures/issues.opened.json

This file was deleted.

24 changes: 24 additions & 0 deletions test/fixtures/pull_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"pull_request": {
"title": "Test Pull Request",
"number": 1,
"state": "open",
"draft": false,
"head": {
"sha": "abc123"
},
"base": {
"ref": "master"
},
"labels": []
},
"repository": {
"name": "your-repo-name",
"owner": {
"login": "your-repo"
}
},
"installation": {
"id": 2
}
}
123 changes: 123 additions & 0 deletions test/general/installation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { describe, beforeEach, afterEach, test, expect } from 'vitest';
import fs from 'fs';
import nock from 'nock';
import path from 'path';

import { setupProbot, teardownProbot } from '../testHelpers';
import { labels } from '../../src/labels.js';

const installationPayload = JSON.parse(
fs.readFileSync(
path.join(__dirname, '../fixtures', 'installation.created.json'),
'utf-8',
),
);

const reposResponse = {
repositories: [
{ owner: { login: 'owner1' }, name: 'repo1' },
{ owner: { login: 'owner2' }, name: 'repo2' },
],
};

describe('Probot installation Handlers', () => {
let probot: any;

beforeEach(() => {
probot = setupProbot();
});

afterEach(() => {
teardownProbot();
});

test('make sure labels exist', async () => {
const mock = nock('https://api.github.com')
.post('/app/installations/2/access_tokens')
.reply(200, {
token: 'test',
permissions: {
issues: 'write',
},
})
.get('/installation/repositories')
.query({ installation_id: 2 })
.reply(200, reposResponse);

for (const repo of reposResponse.repositories) {
mock.get(`/repos/${repo.owner.login}/${repo.name}/labels`).reply(200, []);

for (const label of Object.values(labels)) {
const color = label.color.substring(1).toUpperCase();

mock
.post(
`/repos/${repo.owner.login}/${repo.name}/labels`,
(body: any) => {
expect(body).toMatchObject({
name: label.name,
color: color,
});
return true;
},
)
.reply(201);
}
}

await probot.receive({
name: 'installation',
payload: installationPayload,
});

expect(mock.pendingMocks()).toStrictEqual([]);
});

test('updates label color if it does not match', async () => {
const mock = nock('https://api.github.com')
.post('/app/installations/2/access_tokens')
.reply(200, {
token: 'test',
permissions: {
issues: 'write',
},
})
.get('/installation/repositories')
.query({ installation_id: 2 })
.reply(200, reposResponse);

for (const repo of reposResponse.repositories) {
const existingLabels = Object.values(labels).map(label => ({
name: label.name,
color: '000000',
}));

mock
.get(`/repos/${repo.owner.login}/${repo.name}/labels`)
.reply(200, existingLabels);

for (const label of Object.values(labels)) {
const color = label.color.substring(1).toUpperCase();

mock
.patch(
`/repos/${repo.owner.login}/${repo.name}/labels/${encodeURIComponent(label.name)}`,
(body: any) => {
expect(body).toMatchObject({
color,
});
return true;
},
)
.reply(200);
}
}

await probot.receive({
name: 'installation',
payload: installationPayload,
});

expect(mock.pendingMocks()).toStrictEqual([]);
});
});
83 changes: 0 additions & 83 deletions test/index.test.ts

This file was deleted.

Loading