|
| 1 | +import path from 'node:path'; |
| 2 | + |
| 3 | +import { describe, expect, test, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { fetchGithubActionFixture, root } from '../../test/helper.js'; |
| 6 | + |
| 7 | +const configFile = path.resolve(root, 'cspell.json'); |
| 8 | + |
| 9 | +import { Context } from './context.js'; |
| 10 | + |
| 11 | +describe('Context', () => { |
| 12 | + test('Context', () => { |
| 13 | + createContextEnvFromFile('pull_request.json'); |
| 14 | + const context = new Context(); |
| 15 | + expect(context.eventName).toBe('pull_request'); |
| 16 | + expect(context.sha).toBe('fac78ee45538f198c00ae651db5aedc7336f7ccc'); |
| 17 | + }); |
| 18 | + |
| 19 | + test('Context no event path', () => { |
| 20 | + createContextEnvFromFile('pull_request.json'); |
| 21 | + process.env.GITHUB_EVENT_PATH = ''; |
| 22 | + const context = new Context(); |
| 23 | + expect(context.eventName).toBe('pull_request'); |
| 24 | + expect(context.sha).toBe('fac78ee45538f198c00ae651db5aedc7336f7ccc'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('Context bad event path', () => { |
| 28 | + process.env.GITHUB_EVENT_PATH = './non/existent/path.json'; |
| 29 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 30 | + expect(() => new Context()).not.toThrow(); |
| 31 | + expect(stdoutSpy).toHaveBeenCalledWith('GITHUB_EVENT_PATH ./non/existent/path.json does not exist\n'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('Context.issue', () => { |
| 35 | + createContextEnvFromFile('pull_request.json'); |
| 36 | + const context = new Context(); |
| 37 | + expect(context.issue).toEqual({ number: 3, owner: 'streetsidesoftware', repo: 'cspell-action' }); |
| 38 | + }); |
| 39 | + |
| 40 | + test('Context.issue with non-issue', () => { |
| 41 | + process.env.GITHUB_EVENT_PATH = ''; |
| 42 | + process.env.GITHUB_REPOSITORY = 'some-owner/some-repo'; |
| 43 | + const context = new Context(); |
| 44 | + expect(context.issue).toEqual({ number: undefined, owner: 'some-owner', repo: 'some-repo' }); |
| 45 | + }); |
| 46 | + |
| 47 | + test('Context.repo', () => { |
| 48 | + createContextEnvFromFile('pull_request.json'); |
| 49 | + process.env.GITHUB_REPOSITORY = ''; |
| 50 | + const context = new Context(); |
| 51 | + expect(context.repo).toEqual({ owner: 'streetsidesoftware', repo: 'cspell-action' }); |
| 52 | + |
| 53 | + process.env.GITHUB_REPOSITORY = 'some-owner/some-repo'; |
| 54 | + expect(context.repo).toEqual({ owner: 'some-owner', repo: 'some-repo' }); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Context.repo error', () => { |
| 58 | + process.env.GITHUB_EVENT_PATH = ''; |
| 59 | + process.env.GITHUB_REPOSITORY = ''; |
| 60 | + const context = new Context(); |
| 61 | + expect(() => context.repo).toThrow( |
| 62 | + "context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'", |
| 63 | + ); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +function createContextEnvFromFile(filename: string, ...params: Record<string, string>[]): void { |
| 68 | + return createContext(fetchGithubActionFixture(filename), ...params); |
| 69 | +} |
| 70 | + |
| 71 | +function createContext(...params: Record<string, string>[]): void { |
| 72 | + Object.assign(process.env, ...params); |
| 73 | + setEnvIfNotExist('INPUT_ROOT', root); |
| 74 | + setEnvIfNotExist('INPUT_CONFIG', configFile); |
| 75 | + process.env.INPUT_CONFIG = path.resolve(root, process.env.INPUT_CONFIG || configFile); |
| 76 | +} |
| 77 | + |
| 78 | +function setEnvIfNotExist(key: string, value: string) { |
| 79 | + if (process.env[key] === undefined) { |
| 80 | + process.env[key] = value; |
| 81 | + } |
| 82 | +} |
0 commit comments