-
Notifications
You must be signed in to change notification settings - Fork 147
releaser: Add Vitest coverage for utility functions #801
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
Open
skools-here
wants to merge
15
commits into
headlamp-k8s:main
Choose a base branch
from
skools-here:releaser-utils-vitest-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,079
−52
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4fefe26
releaser: Add Vitest coverage for utility functions
skools-here a110afb
releaser: Configure Vitest test infrastructure
skools-here 123b25a
releaser: Fix Vitest coverage configuration and test isolation
skools-here bec55b4
releaser: Improve test assertions and formatting
skools-here 70526be
releaser: Address ESM import review comments
skools-here bdc7f65
releaser: Align tests with ESM import conventions
skools-here 52c86e2
releaser: Improve test typing and mocking
skools-here 5789bfc
releaser: Improve Vitest test isolation and type checking
skools-here 2651e5d
releaser: Replace automocks with explicit mock factories
skools-here 7a6f199
releaser: Refine Vitest coverage and mock handling
skools-here ed7d1fa
releaser: Addressing restore all mocks related comment
skools-here d849e08
releaser: Adding start script back
skools-here 29243ab
releaser: Declare specific node version
skools-here 2c1930a
releaser: Add vitest for createtag for github.ts
skools-here 5d8e7a7
releaser: Update package-lock.json with engines constraint
skools-here File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import * as crypto from 'crypto'; | ||
| import * as yaml from 'js-yaml'; | ||
| import { | ||
| hasArtifactHubFile, | ||
| getArtifactHubPath, | ||
| readArtifactHubConfig, | ||
| updateArtifactHubConfig, | ||
| createArtifactHubTemplate | ||
| } from './artifacthub.js'; | ||
| import * as pluginUtils from './plugin.js'; | ||
| import * as githubUtils from './github.js'; | ||
|
skools-here marked this conversation as resolved.
|
||
|
|
||
| vi.mock('fs'); | ||
| vi.mock('crypto', () => { | ||
| const mock = { | ||
| createHash: vi.fn(), | ||
| }; | ||
| return { | ||
| ...mock, | ||
| default: mock, | ||
| }; | ||
| }); | ||
| vi.mock('js-yaml', () => ({ | ||
| load: vi.fn(), | ||
| dump: vi.fn(), | ||
| })); | ||
| vi.mock('./plugin.js'); | ||
| vi.mock('./github.js'); | ||
|
|
||
| describe('artifacthub utilities', () => { | ||
| const mockPluginPath = '/mock/plugin/path'; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(pluginUtils.getPluginPath).mockReturnValue(mockPluginPath); | ||
| vi.mocked(pluginUtils.getPluginInfo).mockReturnValue({ name: 'Mock Plugin', version: '1.2.3' }); | ||
| vi.mocked(githubUtils.getOwnerAndRepo).mockReturnValue({ owner: 'owner', repo: 'repo' }); | ||
| }); | ||
|
|
||
| describe('hasArtifactHubFile', () => { | ||
| it('should return true if file exists', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||
| expect(hasArtifactHubFile('my-plugin')).toBe(true); | ||
| expect(fs.existsSync).toHaveBeenCalledWith(path.join(mockPluginPath, 'artifacthub-pkg.yml')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getArtifactHubPath', () => { | ||
| it('should return correct path', () => { | ||
| expect(getArtifactHubPath('my-plugin')).toBe(path.join(mockPluginPath, 'artifacthub-pkg.yml')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('readArtifactHubConfig', () => { | ||
| it('should read and parse yaml content', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||
| vi.mocked(fs.readFileSync).mockReturnValue('yaml content'); | ||
| vi.mocked(yaml.load).mockReturnValue({ version: '1.0.0' }); | ||
|
|
||
| const config = readArtifactHubConfig('my-plugin'); | ||
| expect(config).toEqual({ version: '1.0.0' }); | ||
| expect(yaml.load).toHaveBeenCalledWith('yaml content'); | ||
| }); | ||
|
|
||
| it('should return null if file missing', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(false); | ||
| expect(readArtifactHubConfig('my-plugin')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateArtifactHubConfig', () => { | ||
| it('should create new config if none exists', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(false); | ||
| vi.mocked(fs.readFileSync).mockReturnValue(Buffer.from('mock buffer')); | ||
| const mockHash = { update: vi.fn().mockReturnThis(), digest: vi.fn().mockReturnValue('checksum123') }; | ||
| vi.mocked(crypto.createHash).mockReturnValue(mockHash as any); | ||
| vi.mocked(yaml.dump).mockReturnValue('new yaml content'); | ||
|
|
||
| updateArtifactHubConfig('my-plugin', '1.2.3', '/path/to/tarball.tgz'); | ||
|
|
||
| expect(fs.writeFileSync).toHaveBeenCalledWith(expect.anything(), 'new yaml content'); | ||
| expect(yaml.dump).toHaveBeenCalledWith(expect.objectContaining({ | ||
| version: '1.2.3', | ||
| name: 'headlamp_my_plugin' | ||
| }), expect.anything()); | ||
| }); | ||
|
|
||
| it('should update existing config if it exists', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||
| vi.mocked(fs.readFileSync).mockReturnValue('existing content'); | ||
| vi.mocked(yaml.load).mockReturnValue({ | ||
| version: '1.0.0', | ||
| annotations: { 'headlamp/plugin/archive-url': 'old' } | ||
| }); | ||
| vi.mocked(yaml.dump).mockReturnValue('updated yaml content'); | ||
|
|
||
| const mockHash = { update: vi.fn().mockReturnThis(), digest: vi.fn().mockReturnValue('checksum456') }; | ||
| vi.mocked(crypto.createHash).mockReturnValue(mockHash as any); | ||
|
|
||
| updateArtifactHubConfig('my-plugin', '1.2.3', '/path/to/tarball.tgz'); | ||
|
|
||
| expect(yaml.dump).toHaveBeenCalledWith(expect.objectContaining({ | ||
| version: '1.2.3', | ||
| annotations: expect.objectContaining({ | ||
| 'headlamp/plugin/archive-url': expect.stringContaining('my-plugin-1.2.3') | ||
| }) | ||
| }), expect.anything()); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createArtifactHubTemplate', () => { | ||
| it('should create a template file', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(false); | ||
| vi.mocked(yaml.dump).mockReturnValue('template yaml'); | ||
|
|
||
| createArtifactHubTemplate('my-plugin'); | ||
|
|
||
| expect(fs.writeFileSync).toHaveBeenCalledWith(expect.anything(), 'template yaml'); | ||
| }); | ||
|
|
||
| it('should throw if file already exists', () => { | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||
| expect(() => createArtifactHubTemplate('my-plugin')).toThrow('artifacthub-pkg.yml already exists'); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import * as child_process from 'child_process'; | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { | ||
| getRepoRoot, | ||
| getPluginVersion, | ||
| commitPluginVersionChange, | ||
| getVersionBumpCommit, | ||
| validateCommitSha, | ||
| isCommitPushedToRemote, | ||
| createReleaseTag, | ||
| pushTag, | ||
| checkGitStatus, | ||
| getLatestTagForPlugin, | ||
| hasChangesInPluginSinceTag, | ||
| getCommitsSinceTag, | ||
| getChangelogForPlugin, | ||
| tagExists | ||
| } from './git.js'; | ||
|
skools-here marked this conversation as resolved.
|
||
|
|
||
| vi.mock('child_process', () => ({ | ||
| execFileSync: vi.fn(), | ||
| execSync: vi.fn(), | ||
| })); | ||
| vi.mock('fs'); | ||
|
|
||
| describe('git utilities', () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getRepoRoot', () => { | ||
| it('should return repo root from git command', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue('/repo/root\n'); | ||
| expect(getRepoRoot()).toBe('/repo/root'); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['rev-parse', '--show-toplevel'], expect.anything()); | ||
| }); | ||
|
|
||
| it('should exit if not in a git repo', () => { | ||
| const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit'); }); | ||
| vi.mocked(child_process.execFileSync).mockImplementation(() => { throw new Error('not a git repo'); }); | ||
| expect(() => getRepoRoot()).toThrow('exit'); | ||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getPluginVersion', () => { | ||
| it('should read version from package.json', () => { | ||
| vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({ version: '1.2.3' })); | ||
| expect(getPluginVersion('/plugin/path')).toBe('1.2.3'); | ||
| }); | ||
|
|
||
| it('should exit on error', () => { | ||
| const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit'); }); | ||
| vi.mocked(fs.readFileSync).mockImplementation(() => { throw new Error('read error'); }); | ||
| expect(() => getPluginVersion('/plugin/path')).toThrow('exit'); | ||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('commitPluginVersionChange', () => { | ||
| it('should add files and commit', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); // package-lock.json exists | ||
|
|
||
| commitPluginVersionChange('my-plugin', '1.2.3'); | ||
|
|
||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['add', expect.stringContaining('package.json')], expect.anything()); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['add', expect.stringContaining('package-lock.json')], expect.anything()); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['commit', '--signoff', '-m', 'my-plugin: Bump version to 1.2.3'], expect.anything()); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getVersionBumpCommit', () => { | ||
| it('should find commit SHA for version bump', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue('abc123commitsha\n'); | ||
| const sha = getVersionBumpCommit('my-plugin', '1.2.3'); | ||
| expect(sha).toBe('abc123commitsha'); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith( | ||
| 'git', | ||
| ['log', '--format=%H', '-S', '"version": "1.2.3"', '--', path.join('my-plugin', 'package.json')], | ||
| expect.anything() | ||
| ); | ||
| }); | ||
|
|
||
| it('should fallback to latest package.json change if exact match not found', () => { | ||
| vi.mocked(child_process.execFileSync) | ||
| .mockReturnValueOnce('/repo/root\n') // repo root | ||
| .mockReturnValueOnce('') // No -S match | ||
| .mockReturnValueOnce('fallbacksha\n'); // Latest change | ||
|
|
||
| const sha = getVersionBumpCommit('my-plugin', '1.2.3'); | ||
| expect(sha).toBe('fallbacksha'); | ||
| }); | ||
|
|
||
| it('should return null if no commit found', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(getVersionBumpCommit('my-plugin', '1.2.3')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('validateCommitSha', () => { | ||
| it('should return true for valid SHA', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(validateCommitSha('abc123sha')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false for invalid SHA', () => { | ||
| vi.mocked(child_process.execFileSync).mockImplementation(() => { throw new Error('invalid sha'); }); | ||
| expect(validateCommitSha('invalid')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isCommitPushedToRemote', () => { | ||
| it('should return true if commit is ancestor of remote HEAD', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(isCommitPushedToRemote('sha')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false if merge-base fails', () => { | ||
| vi.mocked(child_process.execFileSync) | ||
| .mockReturnValueOnce('/repo/root\n') // repo root | ||
| .mockReturnValueOnce('') // cat-file success | ||
| .mockImplementationOnce(() => { throw new Error('not an ancestor'); }); // merge-base | ||
| expect(isCommitPushedToRemote('sha')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createReleaseTag', () => { | ||
| it('should create an annotated tag', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| const tag = createReleaseTag('my-plugin', '1.2.3'); | ||
| expect(tag).toBe('my-plugin-1.2.3'); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['tag', '-a', 'my-plugin-1.2.3', '-m', 'my-plugin 1.2.3'], expect.anything()); | ||
| }); | ||
| }); | ||
|
|
||
| describe('pushTag', () => { | ||
| it('should push tag to origin', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| pushTag('my-plugin-1.2.3'); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['push', 'origin', 'my-plugin-1.2.3'], expect.anything()); | ||
| }); | ||
| }); | ||
|
|
||
| describe('checkGitStatus', () => { | ||
| it('should return true if status is empty', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(checkGitStatus()).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false if status is not empty', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue('M file.txt'); | ||
| expect(checkGitStatus()).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getLatestTagForPlugin', () => { | ||
| it('should return the newest tag', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue('my-plugin-1.1.0\nmy-plugin-1.0.0\n'); | ||
| expect(getLatestTagForPlugin('my-plugin')).toBe('my-plugin-1.1.0'); | ||
| }); | ||
|
|
||
| it('should return null if no tags found', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(getLatestTagForPlugin('my-plugin')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('hasChangesInPluginSinceTag', () => { | ||
| it('should return true if diff has entries', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue('modified-file.ts\n'); | ||
| expect(hasChangesInPluginSinceTag('my-plugin', 'v1.0.0')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false if diff is empty', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(hasChangesInPluginSinceTag('my-plugin', 'v1.0.0')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getCommitsSinceTag', () => { | ||
| it('should return array of commit lines', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue('hash1 commit message 1\nhash2 commit message 2\n'); | ||
| const commits = getCommitsSinceTag('my-plugin', 'v1.0.0'); | ||
| expect(commits).toHaveLength(2); | ||
| expect(commits[0]).toBe('hash1 commit message 1'); | ||
| }); | ||
|
|
||
| it('should return empty array if no commits', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(getCommitsSinceTag('my-plugin', 'v1.0.0')).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getChangelogForPlugin', () => { | ||
| it('should generate changelog based on commits since tag', () => { | ||
| vi.mocked(child_process.execFileSync) | ||
| .mockReturnValueOnce('/repo/root\n') // rev-parse for latest tag | ||
| .mockReturnValueOnce('my-plugin-1.0.0\n') // latest tag | ||
| .mockReturnValueOnce('/repo/root\n') // rev-parse for commits since tag | ||
| .mockReturnValueOnce('hash1 message\n'); // commits since tag | ||
|
|
||
| const changelog = getChangelogForPlugin('my-plugin'); | ||
| expect(changelog).toBe('hash1 message'); | ||
| }); | ||
|
|
||
| it('should handle case with no previous tag', () => { | ||
| vi.mocked(child_process.execFileSync) | ||
| .mockReturnValueOnce('/repo/root\n') // rev-parse for latest tag | ||
| .mockReturnValueOnce('') // no latest tag | ||
| .mockReturnValueOnce('/repo/root\n') // rev-parse for all commits | ||
| .mockReturnValueOnce('all commits message\n'); // all commits | ||
|
|
||
| const changelog = getChangelogForPlugin('my-plugin'); | ||
| expect(changelog).toBe('all commits message'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('tagExists', () => { | ||
| it('should check local tag existence', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(tagExists('my-tag')).toBe(true); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['rev-parse', '--verify', 'my-tag'], expect.anything()); | ||
| }); | ||
|
|
||
| it('should check remote tag existence', () => { | ||
| vi.mocked(child_process.execFileSync).mockReturnValue(''); | ||
| expect(tagExists('my-tag', true)).toBe(true); | ||
| expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['ls-remote', '--tags', 'origin', 'my-tag'], expect.anything()); | ||
| }); | ||
|
|
||
| it('should return false if command fails', () => { | ||
| vi.mocked(child_process.execFileSync).mockImplementation(() => { throw new Error('not found'); }); | ||
| expect(tagExists('non-existent')).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.