|
| 1 | +import {describe, it, expect, beforeEach, vi} from 'vitest'; |
| 2 | +import * as git from '../src/git.js'; |
| 3 | +import * as github from '@actions/github'; |
| 4 | +import process from 'process'; |
| 5 | +import {fileURLToPath} from 'node:url'; |
| 6 | +import path from 'node:path'; |
| 7 | + |
| 8 | +const currentDir = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const rootDir = path.join(currentDir, '..'); |
| 10 | + |
| 11 | +describe('getBaseRef', () => { |
| 12 | + it('should return input base ref if provided', () => { |
| 13 | + try { |
| 14 | + process.env['INPUT_BASE-REF'] = 'feature-branch'; |
| 15 | + const baseRef = git.getBaseRef(); |
| 16 | + expect(baseRef).toBe('feature-branch'); |
| 17 | + } finally { |
| 18 | + delete process.env['INPUT_BASE-REF']; |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return pull request base ref if in PR context', () => { |
| 23 | + const originalPayload = github.context.payload; |
| 24 | + try { |
| 25 | + github.context.payload = { |
| 26 | + pull_request: { |
| 27 | + number: 303, |
| 28 | + base: { |
| 29 | + ref: 'develop' |
| 30 | + } |
| 31 | + } |
| 32 | + }; |
| 33 | + const baseRef = git.getBaseRef(); |
| 34 | + expect(baseRef).toBe('origin/develop'); |
| 35 | + } finally { |
| 36 | + github.context.payload = originalPayload; |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return default base ref if no input or PR context', () => { |
| 41 | + const originalPayload = github.context.payload; |
| 42 | + try { |
| 43 | + github.context.payload = {}; |
| 44 | + const baseRef = git.getBaseRef(); |
| 45 | + expect(baseRef).toBe('origin/main'); |
| 46 | + } finally { |
| 47 | + github.context.payload = originalPayload; |
| 48 | + } |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('getFileFromRef', () => { |
| 53 | + beforeEach(() => { |
| 54 | + vi.mock(import('@actions/core'), async (importModule) => { |
| 55 | + const mod = await importModule(); |
| 56 | + return { |
| 57 | + ...mod, |
| 58 | + info: vi.fn(), |
| 59 | + error: vi.fn() |
| 60 | + }; |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return file content from a given ref', () => { |
| 65 | + const content = git.getFileFromRef('HEAD', 'package.json', rootDir); |
| 66 | + expect(content).toBeDefined(); |
| 67 | + expect(content).toContain('"name":'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return null if file does not exist in the given ref', () => { |
| 71 | + const content = git.getFileFromRef('HEAD', 'nonexistentfile.txt', rootDir); |
| 72 | + expect(content).toBeNull(); |
| 73 | + }); |
| 74 | +}); |
0 commit comments