test: move github repoData api test to integration
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
41
__tests__/github/github.test.itg.ts
Normal file
41
__tests__/github/github.test.itg.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2026 actions-toolkit authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {describe, expect, it} from 'vitest';
|
||||||
|
|
||||||
|
import {GitHub} from '../../src/github/github.js';
|
||||||
|
|
||||||
|
describe('repoData', () => {
|
||||||
|
it('returns docker/actions-toolkit', async () => {
|
||||||
|
if (!process.env.GITHUB_TOKEN) {
|
||||||
|
console.log(`GitHub token not available, skipping test`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const originalEnv = process.env;
|
||||||
|
process.env = {
|
||||||
|
...originalEnv,
|
||||||
|
GITHUB_REPOSITORY: 'docker/actions-toolkit'
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const github = new GitHub({token: process.env.GITHUB_TOKEN});
|
||||||
|
const repo = await github.repoData();
|
||||||
|
const fullName = repo.full_name ?? `${repo.owner?.login}/${repo.name}`;
|
||||||
|
expect(fullName).toEqual('docker/actions-toolkit');
|
||||||
|
} finally {
|
||||||
|
process.env = originalEnv;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -26,6 +26,14 @@ import repoFixture from '../.fixtures/github-repo.json' with {type: 'json'};
|
|||||||
|
|
||||||
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
||||||
|
|
||||||
|
vi.mock('@actions/core', async () => {
|
||||||
|
const actual = await vi.importActual<typeof import('@actions/core')>('@actions/core');
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
info: vi.fn()
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe('repoData', () => {
|
describe('repoData', () => {
|
||||||
it('returns GitHub repo data', async () => {
|
it('returns GitHub repo data', async () => {
|
||||||
vi.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
|
vi.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
|
||||||
@@ -36,32 +44,6 @@ describe('repoData', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('repoData (api)', () => {
|
|
||||||
it('returns docker/actions-toolkit', async () => {
|
|
||||||
if (!process.env.GITHUB_TOKEN) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalEnv = process.env;
|
|
||||||
process.env = {
|
|
||||||
...originalEnv,
|
|
||||||
GITHUB_REPOSITORY: 'docker/actions-toolkit'
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
vi.resetModules();
|
|
||||||
vi.unmock('@actions/github');
|
|
||||||
const {GitHub} = await import('../../src/github/github.js');
|
|
||||||
const github = new GitHub({token: process.env.GITHUB_TOKEN});
|
|
||||||
const repo = await github.repoData();
|
|
||||||
const fullName = repo.full_name ?? `${repo.owner?.login}/${repo.name}`;
|
|
||||||
expect(fullName).toEqual('docker/actions-toolkit');
|
|
||||||
} finally {
|
|
||||||
process.env = originalEnv;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('context', () => {
|
describe('context', () => {
|
||||||
it('returns repository name from payload', async () => {
|
it('returns repository name from payload', async () => {
|
||||||
expect(GitHub.context.payload.repository?.name).toEqual('test-docker-action');
|
expect(GitHub.context.payload.repository?.name).toEqual('test-docker-action');
|
||||||
@@ -201,7 +183,7 @@ describe('printActionsRuntimeTokenACs', () => {
|
|||||||
await expect(GitHub.printActionsRuntimeTokenACs()).rejects.toThrow(new Error('Cannot parse GitHub Actions Runtime Token: Invalid token specified: missing part #2'));
|
await expect(GitHub.printActionsRuntimeTokenACs()).rejects.toThrow(new Error('Cannot parse GitHub Actions Runtime Token: Invalid token specified: missing part #2'));
|
||||||
});
|
});
|
||||||
it('refs/heads/master', async () => {
|
it('refs/heads/master', async () => {
|
||||||
const infoSpy = vi.spyOn(core, 'info');
|
const infoSpy = vi.mocked(core.info);
|
||||||
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(fixturesDir, 'runtimeToken.txt')).toString().trim();
|
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(fixturesDir, 'runtimeToken.txt')).toString().trim();
|
||||||
await GitHub.printActionsRuntimeTokenACs();
|
await GitHub.printActionsRuntimeTokenACs();
|
||||||
expect(infoSpy).toHaveBeenCalledTimes(1);
|
expect(infoSpy).toHaveBeenCalledTimes(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user