|
| 1 | +import { join } from 'path' |
| 2 | +import { |
| 3 | + describe, |
| 4 | + expect, |
| 5 | + it |
| 6 | +} from 'vitest' |
| 7 | +import { |
| 8 | + createDirectory, |
| 9 | + forkProject, |
| 10 | + packageJsonProject |
| 11 | +} from 'test' |
| 12 | +import { NodeGhaProject } from './project.js' |
| 13 | + |
| 14 | +function refHash(output: string) { |
| 15 | + return output.split(/\s+/)[0] |
| 16 | +} |
| 17 | + |
| 18 | +describe('node-gha', () => { |
| 19 | + describe('publish', () => { |
| 20 | + it('should publish GitHub Action refs from the built latest branch', async () => { |
| 21 | + const { cwd } = await forkProject('node-gha-publish', packageJsonProject({ |
| 22 | + version: '2.5.0', |
| 23 | + files: [ |
| 24 | + 'dist' |
| 25 | + ] |
| 26 | + })) |
| 27 | + const remote = await createDirectory('node-gha-publish-remote') |
| 28 | + const project = new NodeGhaProject({ |
| 29 | + path: join(cwd, 'package.json') |
| 30 | + }) |
| 31 | + |
| 32 | + await project.gitClient.exec('-C', remote, 'init', '--bare') |
| 33 | + await project.gitClient.exec('remote', 'set-url', 'origin', remote) |
| 34 | + |
| 35 | + await project.publish({ |
| 36 | + build: 'node -e "const fs=require(\'fs\');fs.mkdirSync(\'dist\',{recursive:true});fs.writeFileSync(\'dist/index.js\',\'built\\n\')"' |
| 37 | + }) |
| 38 | + |
| 39 | + const latestRef = await project.gitClient.exec('ls-remote', '--heads', 'origin', 'latest') |
| 40 | + const majorRef = await project.gitClient.exec('ls-remote', '--heads', 'origin', 'v2') |
| 41 | + const tagRef = await project.gitClient.exec('ls-remote', '--tags', 'origin', 'v2.5.0') |
| 42 | + |
| 43 | + expect(latestRef).toContain('refs/heads/latest') |
| 44 | + expect(majorRef).toContain('refs/heads/v2') |
| 45 | + expect(tagRef).toContain('refs/tags/v2.5.0') |
| 46 | + expect(refHash(latestRef)).toBe(refHash(majorRef)) |
| 47 | + expect(refHash(latestRef)).toBe(refHash(tagRef)) |
| 48 | + expect(await project.gitClient.exec('show', 'v2.5.0:dist/index.js')).toBe('built') |
| 49 | + expect(await project.gitClient.getCurrentBranch()).toBe('master') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should use custom ref names and explicit files', async () => { |
| 53 | + const { cwd } = await forkProject('node-gha-publish-custom-refs', packageJsonProject({ |
| 54 | + version: '3.0.0' |
| 55 | + })) |
| 56 | + const remote = await createDirectory('node-gha-publish-custom-refs-remote') |
| 57 | + const project = new NodeGhaProject({ |
| 58 | + path: join(cwd, 'package.json') |
| 59 | + }) |
| 60 | + |
| 61 | + await project.gitClient.exec('-C', remote, 'init', '--bare') |
| 62 | + await project.gitClient.exec('remote', 'set-url', 'origin', remote) |
| 63 | + |
| 64 | + await project.publish({ |
| 65 | + latestBranch: 'stable', |
| 66 | + majorBranchPrefix: 'release-', |
| 67 | + tagPrefix: 'action-v', |
| 68 | + build: 'node -e "const fs=require(\'fs\');fs.mkdirSync(\'lib\',{recursive:true});fs.writeFileSync(\'lib/action.js\',\'custom\\n\')"', |
| 69 | + files: [ |
| 70 | + 'lib/action.js' |
| 71 | + ] |
| 72 | + }) |
| 73 | + |
| 74 | + const latestRef = await project.gitClient.exec('ls-remote', '--heads', 'origin', 'stable') |
| 75 | + const majorRef = await project.gitClient.exec('ls-remote', '--heads', 'origin', 'release-3') |
| 76 | + const tagRef = await project.gitClient.exec('ls-remote', '--tags', 'origin', 'action-v3.0.0') |
| 77 | + |
| 78 | + expect(latestRef).toContain('refs/heads/stable') |
| 79 | + expect(majorRef).toContain('refs/heads/release-3') |
| 80 | + expect(tagRef).toContain('refs/tags/action-v3.0.0') |
| 81 | + expect(refHash(latestRef)).toBe(refHash(majorRef)) |
| 82 | + expect(refHash(latestRef)).toBe(refHash(tagRef)) |
| 83 | + expect(await project.gitClient.exec('show', 'action-v3.0.0:lib/action.js')).toBe('custom') |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
0 commit comments