-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: increase test coverage * ci: add a test ci
- Loading branch information
Showing
12 changed files
with
420 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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,32 @@ | ||
name: Test jobs | ||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened, ready_for_review] | ||
jobs: | ||
test: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version-file: ./.node-version | ||
- name: Enable corepack | ||
run: corepack enable | ||
- name: Get pnpm store directory | ||
id: pnpm-cache | ||
shell: bash | ||
run: | | ||
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT | ||
- uses: actions/cache@v3 | ||
name: Setup pnpm cache | ||
with: | ||
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} | ||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm-store- | ||
- name: Install dependencies | ||
run: pnpm i --frozen-lockfile | ||
- name: Test | ||
run: pnpm run test |
This file contains 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,38 @@ | ||
import type { ParseArgsConfig } from 'node:util'; | ||
|
||
export const argsOptions = { | ||
output: { | ||
type: 'string', | ||
multiple: false, | ||
short: 'o', | ||
default: './licenses.json', | ||
}, | ||
pretty: { | ||
type: 'boolean', | ||
multiple: false, | ||
short: 'p', | ||
default: false, | ||
}, | ||
recursive: { | ||
type: 'boolean', | ||
multiple: false, | ||
short: 'r', | ||
default: false, | ||
}, | ||
dev: { | ||
type: 'boolean', | ||
multiple: false, | ||
short: 'd', | ||
default: false, | ||
}, | ||
'no-prod': { | ||
type: 'boolean', | ||
multiple: false, | ||
default: false, | ||
}, | ||
'no-optional': { | ||
type: 'boolean', | ||
multiple: false, | ||
default: false, | ||
}, | ||
} satisfies ParseArgsConfig['options']; |
This file contains 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
41 changes: 41 additions & 0 deletions
41
src/pnpm/export-license/utils/build-exported-pnpm-package-info.test.ts
This file contains 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,41 @@ | ||
import { nextLicense } from '../../../license-txt'; | ||
import { buildExportedPnpmPackageInfo } from './build-exported-pnpm-package-info'; | ||
|
||
test('should return package info without licenseTxt', () => { | ||
expect(buildExportedPnpmPackageInfo({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
path: '/Users/luco/ghq/sucomado-frontend/node_modules/.pnpm/@ampproject+remapping@2.2.0/node_modules/@ampproject/remapping', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
}, undefined)).toStrictEqual({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
}); | ||
}); | ||
|
||
test('should return package info with licenseTxt', () => { | ||
expect(buildExportedPnpmPackageInfo({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
path: '/Users/luco/ghq/sucomado-frontend/node_modules/.pnpm/@ampproject+remapping@2.2.0/node_modules/@ampproject/remapping', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
}, nextLicense)).toStrictEqual({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
licenseTxt: nextLicense, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
src/pnpm/export-license/utils/build-exported-pnpm-package-info.ts
This file contains 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,9 @@ | ||
import type { PnpmPackageInfo, ExportedPnpmPackageInfo } from '../types/export-licenses.types'; | ||
|
||
export function buildExportedPnpmPackageInfo(pnpmPackage: PnpmPackageInfo, licenseTxt: undefined | string): ExportedPnpmPackageInfo { | ||
// remove path property | ||
const { path, ...PnpmPackageInfo } = pnpmPackage; | ||
return licenseTxt === undefined | ||
? PnpmPackageInfo | ||
: { ...PnpmPackageInfo, licenseTxt }; | ||
} |
This file contains 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,35 @@ | ||
import { Mock } from 'vitest'; | ||
import { findLicenseDirent } from './find-license-dirent'; | ||
import { readdir } from 'node:fs/promises'; | ||
|
||
vitest.mock('node:fs/promises', () => ({ | ||
readdir: vitest.fn().mockResolvedValue([ | ||
{ | ||
name: 'LICENSE.md', | ||
parentPath: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
path: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
}, | ||
{ | ||
name: 'README.md', | ||
parentPath: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
path: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
} | ||
]) | ||
})); | ||
|
||
test('should return license dirent', async () => { | ||
expect(await findLicenseDirent('/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest')).toStrictEqual({ | ||
name: 'LICENSE.md', | ||
parentPath: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
path: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/' | ||
}); | ||
}); | ||
|
||
test('should return undefined', async () => { | ||
(readdir as Mock).mockResolvedValue([{ | ||
name: 'README.md', | ||
parentPath: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
path: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
}]); | ||
expect(await findLicenseDirent('/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest')).toBe(undefined); | ||
}); |
This file contains 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,7 @@ | ||
import type { Dirent } from 'node:fs'; | ||
import { readdir } from 'node:fs/promises'; | ||
|
||
export async function findLicenseDirent(pnpmPackagePath: string): Promise<Dirent | undefined> { | ||
const dirents = await readdir(pnpmPackagePath, { withFileTypes: true }); | ||
return dirents.find((dirent) => dirent.name.toLowerCase().includes('license')); | ||
} |
108 changes: 108 additions & 0 deletions
108
src/pnpm/export-license/utils/search-license.util.test.ts
This file contains 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,108 @@ | ||
import { mockPnpmPackages } from '../mocks/mock-pnpm-packages'; | ||
import { searchLicense } from './search-license.util'; | ||
import { findLicenseDirent } from './find-license-dirent'; | ||
import { buildExportedPnpmPackageInfo } from './build-exported-pnpm-package-info'; | ||
import { readLicense } from './read-license.util'; | ||
import { Mock } from 'vitest'; | ||
import { nextLicense } from '../../../license-txt'; | ||
|
||
vitest.mock('./find-license-dirent', () => ({ | ||
findLicenseDirent: vitest.fn().mockResolvedValue({ | ||
name: 'LICENSE.md', | ||
parentPath: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/', | ||
path: '/Users/mock/luco-inc/pnpm-license-exporter/node_modules/.pnpm/vitest@1.5.0_@types+node@20.12.7/node_modules/vitest/' | ||
}) | ||
})); | ||
|
||
vitest.mock('./build-exported-pnpm-package-info', () => ({ | ||
buildExportedPnpmPackageInfo: vitest.fn().mockImplementation(()=> ({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
})) | ||
})); | ||
|
||
vitest.mock('./read-license.util', () => ({ | ||
readLicense: vitest.fn().mockResolvedValue(undefined) | ||
})); | ||
|
||
test('should return licenses', async () => { | ||
expect(await searchLicense([ | ||
mockPnpmPackages['Apache-2.0'][0], | ||
mockPnpmPackages.MIT[0], | ||
mockPnpmPackages.MIT[1], | ||
])).toStrictEqual([ | ||
{ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
}, | ||
{ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
}, | ||
{ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
}, | ||
]) | ||
expect(findLicenseDirent).toHaveBeenCalledTimes(3); | ||
expect(readLicense).toHaveBeenCalledTimes(3); | ||
expect(buildExportedPnpmPackageInfo).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
test('should using pnpm package in args', async () => { | ||
(readLicense as Mock).mockResolvedValue(nextLicense); | ||
(buildExportedPnpmPackageInfo as Mock).mockResolvedValue({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
licenseTxt: nextLicense, | ||
}); | ||
|
||
expect(await searchLicense([mockPnpmPackages['Apache-2.0'][0]])).toStrictEqual([ | ||
{ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
licenseTxt: nextLicense, | ||
} | ||
]); | ||
expect(findLicenseDirent).toHaveBeenCalledTimes(1); | ||
expect(findLicenseDirent).toHaveBeenCalledWith('/Users/luco/ghq/sucomado-frontend/node_modules/.pnpm/@ampproject+remapping@2.2.0/node_modules/@ampproject/remapping'); | ||
expect(readLicense).toHaveBeenCalledTimes(1); | ||
expect(readLicense).toHaveBeenCalledWith({ | ||
name: '@ampproject/remapping', | ||
licensePath: '/Users/luco/ghq/sucomado-frontend/node_modules/.pnpm/@ampproject+remapping@2.2.0/node_modules/@ampproject/remapping/LICENSE.md', | ||
}); | ||
expect(buildExportedPnpmPackageInfo).toHaveBeenCalledTimes(1); | ||
expect(buildExportedPnpmPackageInfo).toHaveBeenCalledWith({ | ||
name: '@ampproject/remapping', | ||
version: '2.2.0', | ||
license: 'Apache-2.0', | ||
author: 'Justin Ridgewell', | ||
homepage: 'https://github.com/ampproject/remapping#readme', | ||
description: 'Remap sequential sourcemaps through transformations to point at the original source code', | ||
path: "/Users/luco/ghq/sucomado-frontend/node_modules/.pnpm/@ampproject+remapping@2.2.0/node_modules/@ampproject/remapping" | ||
}, nextLicense); | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import { readdir } from 'node:fs/promises'; | ||
import type { PnpmPackageInfo, ExportedPnpmPackageInfo } from '../types/export-licenses.types'; | ||
import { readLicense } from './read-license.util'; | ||
import { findLicenseDirent } from './find-license-dirent'; | ||
import { buildExportedPnpmPackageInfo } from './build-exported-pnpm-package-info'; | ||
|
||
export function searchLicense(packages: PnpmPackageInfo[]) { | ||
export function searchLicense(packages: PnpmPackageInfo[]): Promise<(ExportedPnpmPackageInfo|undefined)[]> { | ||
return Promise.all( | ||
packages.map(async (pnpmPackage): Promise<ExportedPnpmPackageInfo | undefined> => { | ||
const dirents = await readdir(pnpmPackage.path, { withFileTypes: true }); | ||
const licenseDirent = dirents.find((dirent) => dirent.name.toLowerCase().includes('license')); | ||
const licenseDirent = await findLicenseDirent(pnpmPackage.path); | ||
if (licenseDirent === undefined) { | ||
return undefined; | ||
} | ||
const licenseTxt = await readLicense({ | ||
name: pnpmPackage.name, | ||
licensePath: `${pnpmPackage.path}/${licenseDirent.name}`, | ||
}); | ||
// remove path property | ||
const { path, ...PnpmPackageInfo } = pnpmPackage; | ||
return licenseTxt === undefined ? PnpmPackageInfo : { ...PnpmPackageInfo, licenseTxt }; | ||
return buildExportedPnpmPackageInfo(pnpmPackage, licenseTxt); | ||
}), | ||
).then((licenses) => licenses.filter(Boolean)); | ||
} |
Oops, something went wrong.