mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-23 23:29:17 -05:00
156 lines
5.2 KiB
TypeScript
156 lines
5.2 KiB
TypeScript
/// <reference path="../../../__typings__/index.d.ts"/>
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
import { fixtures } from '@pnpm/test-fixtures'
|
|
import { sync as rimraf } from '@zkochan/rimraf'
|
|
import { jest } from '@jest/globals'
|
|
|
|
const debug = jest.fn()
|
|
jest.unstable_mockModule('@pnpm/logger', () => {
|
|
return ({ globalWarn: jest.fn(), debug, logger: () => ({ debug }) })
|
|
})
|
|
const { createDirectoryFetcher } = await import('@pnpm/directory-fetcher')
|
|
|
|
const f = fixtures(import.meta.dirname)
|
|
|
|
test('fetch including only package files', async () => {
|
|
process.chdir(f.find('simple-pkg'))
|
|
const fetcher = createDirectoryFetcher({ includeOnlyPackageFiles: true })
|
|
|
|
// eslint-disable-next-line
|
|
const fetchResult = await fetcher.directory({} as any, {
|
|
directory: '.',
|
|
type: 'directory',
|
|
}, {
|
|
lockfileDir: process.cwd(),
|
|
})
|
|
|
|
expect(fetchResult.local).toBe(true)
|
|
expect(fetchResult.packageImportMethod).toBe('hardlink')
|
|
expect(fetchResult.filesIndex.get('package.json')).toBe(path.resolve('package.json'))
|
|
|
|
// Only those files are included which would get published
|
|
expect(Array.from(fetchResult.filesIndex.keys()).sort()).toStrictEqual([
|
|
'index.js',
|
|
'package.json',
|
|
])
|
|
})
|
|
|
|
test('fetch including all files', async () => {
|
|
process.chdir(f.find('simple-pkg'))
|
|
const fetcher = createDirectoryFetcher()
|
|
|
|
// eslint-disable-next-line
|
|
const fetchResult = await fetcher.directory({} as any, {
|
|
directory: '.',
|
|
type: 'directory',
|
|
}, {
|
|
lockfileDir: process.cwd(),
|
|
})
|
|
|
|
expect(fetchResult.local).toBe(true)
|
|
expect(fetchResult.packageImportMethod).toBe('hardlink')
|
|
expect(fetchResult.filesIndex.get('package.json')).toBe(path.resolve('package.json'))
|
|
|
|
// Only those files are included which would get published
|
|
expect(Array.from(fetchResult.filesIndex.keys()).sort()).toStrictEqual([
|
|
'index.js',
|
|
'package.json',
|
|
'test.js',
|
|
])
|
|
})
|
|
|
|
test('fetch a directory that has no package.json', async () => {
|
|
process.chdir(f.find('no-manifest'))
|
|
const fetcher = createDirectoryFetcher()
|
|
|
|
// eslint-disable-next-line
|
|
const fetchResult = await fetcher.directory({} as any, {
|
|
directory: '.',
|
|
type: 'directory',
|
|
}, {
|
|
lockfileDir: process.cwd(),
|
|
readManifest: true,
|
|
})
|
|
|
|
expect(fetchResult.manifest).toBeUndefined()
|
|
expect(fetchResult.local).toBe(true)
|
|
expect(fetchResult.packageImportMethod).toBe('hardlink')
|
|
expect(fetchResult.filesIndex.get('index.js')).toBe(path.resolve('index.js'))
|
|
|
|
// Only those files are included which would get published
|
|
expect(Array.from(fetchResult.filesIndex.keys()).sort()).toStrictEqual([
|
|
'index.js',
|
|
])
|
|
})
|
|
|
|
test('fetch does not fail on package with broken symlink', async () => {
|
|
jest.mocked(debug).mockClear()
|
|
process.chdir(f.find('pkg-with-broken-symlink'))
|
|
const fetcher = createDirectoryFetcher()
|
|
|
|
// eslint-disable-next-line
|
|
const fetchResult = await fetcher.directory({} as any, {
|
|
directory: '.',
|
|
type: 'directory',
|
|
}, {
|
|
lockfileDir: process.cwd(),
|
|
})
|
|
|
|
expect(fetchResult.local).toBe(true)
|
|
expect(fetchResult.packageImportMethod).toBe('hardlink')
|
|
expect(fetchResult.filesIndex.get('package.json')).toBe(path.resolve('package.json'))
|
|
|
|
// Only those files are included which would get published
|
|
expect(Array.from(fetchResult.filesIndex.keys()).sort()).toStrictEqual([
|
|
'index.js',
|
|
'package.json',
|
|
])
|
|
expect(debug).toHaveBeenCalledWith({ brokenSymlink: path.resolve('not-exists') })
|
|
})
|
|
|
|
describe('fetch resolves symlinked files to their real locations', () => {
|
|
const indexJsPath = path.join(f.find('no-manifest'), 'index.js')
|
|
const srcPath = f.find('simple-pkg')
|
|
beforeAll(async () => {
|
|
process.chdir(f.find('pkg-with-symlinked-dir-and-files'))
|
|
rimraf('index.js')
|
|
fs.symlinkSync(indexJsPath, path.resolve('index.js'), 'file')
|
|
rimraf('src')
|
|
fs.symlinkSync(srcPath, path.resolve('src'), 'dir')
|
|
})
|
|
test('fetch resolves symlinked files to their real locations', async () => {
|
|
const fetcher = createDirectoryFetcher({ resolveSymlinks: true })
|
|
// eslint-disable-next-line
|
|
const fetchResult = await fetcher.directory({} as any, {
|
|
directory: '.',
|
|
type: 'directory',
|
|
}, {
|
|
lockfileDir: process.cwd(),
|
|
})
|
|
|
|
expect(fetchResult.local).toBe(true)
|
|
expect(fetchResult.packageImportMethod).toBe('hardlink')
|
|
expect(fetchResult.filesIndex.get('package.json')).toBe(path.resolve('package.json'))
|
|
expect(fetchResult.filesIndex.get('index.js')).toBe(indexJsPath)
|
|
expect(fetchResult.filesIndex.get('src/index.js')).toBe(path.join(srcPath, 'index.js'))
|
|
})
|
|
test('fetch does not resolve symlinked files to their real locations by default', async () => {
|
|
const fetcher = createDirectoryFetcher()
|
|
|
|
// eslint-disable-next-line
|
|
const fetchResult = await fetcher.directory({} as any, {
|
|
directory: '.',
|
|
type: 'directory',
|
|
}, {
|
|
lockfileDir: process.cwd(),
|
|
})
|
|
|
|
expect(fetchResult.local).toBe(true)
|
|
expect(fetchResult.packageImportMethod).toBe('hardlink')
|
|
expect(fetchResult.filesIndex.get('package.json')).toBe(path.resolve('package.json'))
|
|
expect(fetchResult.filesIndex.get('index.js')).toBe(path.resolve('index.js'))
|
|
expect(fetchResult.filesIndex.get('src/index.js')).toBe(path.resolve('src/index.js'))
|
|
})
|
|
})
|