-
-
Notifications
You must be signed in to change notification settings - Fork 458
fix: ignore license change for initial version #2816
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
charpeni
wants to merge
4
commits into
npmx-dev:main
Choose a base branch
from
charpeni:fix/license-change-first-version
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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
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
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
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,61 @@ | ||
| import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { H3Event } from 'h3' | ||
| import type { Packument } from '#shared/types/npm-registry' | ||
| import { parsePackageParams } from '#server/utils/parse-package-params' | ||
|
|
||
| const fetchNpmPackageMock = vi.hoisted(() => vi.fn()) | ||
| const getRouterParamMock = vi.hoisted(() => vi.fn()) | ||
| const getQueryMock = vi.hoisted(() => vi.fn()) | ||
| const setHeaderMock = vi.hoisted(() => vi.fn()) | ||
|
|
||
| vi.mock('#server/utils/npm', () => ({ | ||
| fetchNpmPackage: fetchNpmPackageMock, | ||
| })) | ||
|
|
||
| vi.mock('h3', () => ({ | ||
| createError: vi.fn((options: unknown) => options), | ||
| getRouterParam: getRouterParamMock, | ||
| getQuery: getQueryMock, | ||
| setHeader: setHeaderMock, | ||
| })) | ||
|
|
||
| vi.stubGlobal('defineCachedEventHandler', (fn: Function) => fn) | ||
| vi.stubGlobal('parsePackageParams', parsePackageParams) | ||
|
|
||
| const handler = (await import('#server/api/registry/badge/[type]/[...pkg].get')).default | ||
|
|
||
| const fakeEvent = {} as H3Event | ||
|
|
||
| afterAll(() => { | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| describe('badge API', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| getQueryMock.mockReturnValue({}) | ||
| getRouterParamMock.mockImplementation((_event: unknown, name: string) => { | ||
| if (name === 'type') return 'license' | ||
| if (name === 'pkg') return 'my-pkg' | ||
| return undefined | ||
| }) | ||
| }) | ||
|
|
||
| it('normalizes object-shaped licenses before rendering SVG', async () => { | ||
| fetchNpmPackageMock.mockResolvedValue({ | ||
| '_id': 'my-pkg', | ||
| '_rev': '1', | ||
| 'name': 'my-pkg', | ||
| 'dist-tags': { latest: '1.0.0' }, | ||
| 'versions': { | ||
| '1.0.0': { version: '1.0.0', license: { type: 'MIT' } }, | ||
| }, | ||
| 'time': {}, | ||
| } as unknown as Packument) | ||
|
|
||
| const svg = await handler(fakeEvent) | ||
|
|
||
| expect(svg).toContain('>MIT<') | ||
| expect(svg).not.toContain('[object Object]') | ||
| }) | ||
| }) |
102 changes: 102 additions & 0 deletions
102
test/unit/server/api/registry/license-change/pkg.get.spec.ts
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,102 @@ | ||
| import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { createError, type H3Event } from 'h3' | ||
| import type { Packument, PackumentVersion } from '#shared/types/npm-registry' | ||
|
|
||
| const fetchNpmPackageMock = vi.fn() | ||
| vi.stubGlobal('fetchNpmPackage', fetchNpmPackageMock) | ||
| vi.stubGlobal('defineCachedEventHandler', (fn: Function) => fn) | ||
| vi.stubGlobal('createError', createError) | ||
|
|
||
| let routerParam: string | undefined | ||
| let queryParams: Record<string, string | number> = {} | ||
|
|
||
| vi.stubGlobal('getRouterParam', (_event: unknown, _name: string) => routerParam) | ||
| vi.stubGlobal('getQuery', () => queryParams) | ||
|
|
||
| const handler = (await import('#server/api/registry/license-change/[...pkg].get')).default | ||
|
|
||
| function makePackument(opts: { | ||
| versions: Record<string, Partial<PackumentVersion>> | ||
| time: Record<string, string> | ||
| }): Packument { | ||
| return { | ||
| 'dist-tags': {}, | ||
| 'versions': Object.fromEntries( | ||
| Object.entries(opts.versions).map(([v, data]) => [v, { version: v, ...data }]), | ||
| ), | ||
| 'time': opts.time, | ||
| } as Packument | ||
| } | ||
|
|
||
| const fakeEvent = {} as H3Event | ||
|
|
||
| afterAll(() => { | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| describe('license-change API', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| routerParam = 'my-pkg' | ||
| queryParams = {} | ||
| }) | ||
|
|
||
| it('does not report a license change for the first package version', async () => { | ||
| queryParams = { version: '1.0.0' } | ||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'ISC' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| await expect(handler(fakeEvent)).resolves.toEqual({ change: null }) | ||
| }) | ||
|
|
||
| it('reports a license change when a previous version exists', async () => { | ||
| queryParams = { version: '2.0.0' } | ||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'ISC' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| await expect(handler(fakeEvent)).resolves.toEqual({ | ||
| change: { | ||
| from: 'MIT', | ||
| to: 'ISC', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('normalizes license object types before comparing versions', async () => { | ||
| queryParams = { version: '2.0.0' } | ||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: { type: 'MIT' } as never }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| await expect(handler(fakeEvent)).resolves.toEqual({ change: null }) | ||
| }) | ||
| }) | ||
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,25 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { normalizePackageLicense } from '#shared/utils/npm' | ||
| import type { PackumentLicense } from '#shared/types/npm-registry' | ||
|
|
||
| describe('normalizePackageLicense', () => { | ||
| it('returns string licenses unchanged', () => { | ||
| expect(normalizePackageLicense('MIT')).toBe('MIT') | ||
| }) | ||
|
|
||
| it('extracts type from legacy object licenses', () => { | ||
| expect(normalizePackageLicense({ type: 'Apache-2.0', url: 'https://example.com' })).toBe( | ||
| 'Apache-2.0', | ||
| ) | ||
| }) | ||
|
|
||
| it('returns undefined for empty licenses', () => { | ||
| expect(normalizePackageLicense(undefined)).toBeUndefined() | ||
| expect(normalizePackageLicense('')).toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns undefined for malformed object licenses', () => { | ||
| expect(normalizePackageLicense({} as PackumentLicense)).toBeUndefined() | ||
| expect(normalizePackageLicense({ type: 123 } as unknown as PackumentLicense)).toBeUndefined() | ||
| }) | ||
| }) |
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.