Skip to content

Commit

Permalink
test(lockfile-file): add read & write test for useInlineSpecifiersFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
gluxon committed Jul 25, 2022
1 parent 015376f commit e81b281
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/lockfile-file/src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ async function _readGitBranchLockfiles (
function convertFromLockfileFileMutable (lockfileFile: LockfileFile): Lockfile {
const specifiersFieldIsOnRoot = typeof lockfileFile?.['specifiers'] !== 'undefined'
// When inlineSpecifiersFormat is true, the specifiers field may not exist.
// Check for one of the dep type fields that were moed to the root instead.
// Check for one of the dep type fields that were moved to the root instead.
const someDepFieldIsOnRoot = DEPENDENCIES_FIELDS.some(depType => lockfileFile[depType] != null)

if (specifiersFieldIsOnRoot || someDepFieldIsOnRoot) {
Expand Down
17 changes: 17 additions & 0 deletions packages/lockfile-file/test/__snapshots__/write.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ packages:
resolution: {integrity: sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=}
"
`;

exports[`writeLockfiles() when useInlineSpecifiersFormat 1`] = `
"lockfileVersion: 5.4
inlineSpecifiersFormat: true
dependencies:
foo:
specifier: ^1.0.0
version: 1.0.0
packages:
/foo/1.0.0:
resolution: {integrity: sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=}
"
`;
14 changes: 14 additions & 0 deletions packages/lockfile-file/test/fixtures/7/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions packages/lockfile-file/test/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,30 @@ test('readWantedLockfile() when useGitBranchLockfile and mergeGitBranchLockfiles
},
})
})

test('readWantedLockfile() with inlineSpecifiersFormat', async () => {
const wantedLockfile = {
importers: {
'.': {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
packages: {
'/is-positive/1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
registry: 'https://registry.npmjs.org',
}

const lockfile = await readWantedLockfile(path.join('fixtures', '7'), { ignoreIncompatible: false })
expect(lockfile?.importers).toEqual(wantedLockfile.importers)
expect(lockfile?.packages).toEqual(wantedLockfile.packages)
})
36 changes: 36 additions & 0 deletions packages/lockfile-file/test/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,39 @@ test('writeLockfiles() when useGitBranchLockfile', async () => {
expect(fs.existsSync(path.join(projectPath, WANTED_LOCKFILE))).toBeFalsy()
expect(fs.existsSync(path.join(projectPath, `pnpm-lock.${branchName}.yaml`))).toBeTruthy()
})

test('writeLockfiles() when useInlineSpecifiersFormat', async () => {
const projectPath = tempy.directory()
const wantedLockfile = {
importers: {
'.': {
dependencies: {
foo: '1.0.0',
},
specifiers: {
foo: '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/foo/1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}

await writeLockfiles({
currentLockfile: wantedLockfile,
currentLockfileDir: projectPath,
wantedLockfile,
wantedLockfileDir: projectPath,
useInlineSpecifiersFormat: true,
})

expect(await readCurrentLockfile(projectPath, { ignoreIncompatible: false })).toEqual(wantedLockfile)
expect(await readWantedLockfile(projectPath, { ignoreIncompatible: false })).toEqual(wantedLockfile)
expect(fs.readFileSync(path.join(projectPath, WANTED_LOCKFILE), 'utf8')).toMatchSnapshot()
})

0 comments on commit e81b281

Please sign in to comment.