Skip to content

Commit

Permalink
feat: PnP package resolution support
Browse files Browse the repository at this point in the history
Resolves #272
  • Loading branch information
adalinesimonian committed Oct 22, 2021
1 parent eadb801 commit c35b018
Show file tree
Hide file tree
Showing 18 changed files with 12,178 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
coverage
.pnp.*
.yarn
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ coverage
/.vscode-test/**
/test/**/*.css
/package.json
.pnp.*
.yarn
# Unignore config files like .prettierrc.js, because they're ignored by default
!.*rc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`findPackageRoot should throw when encountering a file system error other than ENOENT 1`] = `"EACCES: permission denied, open 'foo/bar/baz'"`;
exports[`findPackageRoot with custom root file should throw when encountering a file system error other than ENOENT 1`] = `"EACCES: permission denied, open 'foo/bar/baz'"`;

exports[`findPackageRoot with defaults should throw when encountering a file system error other than ENOENT 1`] = `"EACCES: permission denied, open 'foo/bar/baz'"`;
175 changes: 127 additions & 48 deletions src/utils/packages/__tests__/find-package-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,155 @@ const mockedFS = /** @type {tests.mocks.FSPromisesModule} */ (require('fs/promis
const findPackageRoot = require('../find-package-root').findPackageRoot;

describe('findPackageRoot', () => {
it('should resolve the package directory when package.json is present', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'package.json': '{}',
baz: {},
describe('with defaults', () => {
it('should resolve the package directory when package.json is present', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'package.json': '{}',
baz: {},
},
},
},
});
});

expect(await findPackageRoot('foo/bar/baz')).toBe('foo/bar');
});
expect(await findPackageRoot('foo/bar/baz')).toBe('foo/bar');
});

it("should not resolve when package.json isn't in the tree", async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: {},
it("should not resolve when package.json isn't in the tree", async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});
it('should not resolve folders with a directory named package.json', async () => {
mockedFS.__mockFileSystem({
foo: {
'package.json': '{}',
bar: {
'package.json': {},
baz: {},
},
},
});

it('should not resolve folders with a directory named package.json', async () => {
mockedFS.__mockFileSystem({
foo: {
'package.json': '{}',
bar: {
expect(await findPackageRoot('foo/bar/baz')).toBe('foo');

mockedFS.__mockFileSystem({
foo: {
'package.json': {},
baz: {},
bar: {
baz: {},
},
},
},
});
});

expect(await findPackageRoot('foo/bar/baz')).toBe('foo');
expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();

mockedFS.__mockFileSystem({
foo: {
mockedFS.__mockFileSystem({
'package.json': {},
bar: {
baz: {},
foo: {
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});

it('should throw when encountering a file system error other than ENOENT', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: createError('EACCES', 'foo/bar/baz', -13, 'stat'),
},
},
});

await expect(findPackageRoot('foo/bar/baz')).rejects.toThrowErrorMatchingSnapshot();
});
});

describe('with custom root file', () => {
it('should resolve the package directory when the root file is present', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'yarn.lock': '',
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBe('foo/bar');
});

mockedFS.__mockFileSystem({
'package.json': {},
foo: {
bar: {
baz: {},
it("should not resolve when the root file isn't in the tree", async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBeUndefined();
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});
it('should not resolve folders with a directory with the same name as the root file', async () => {
mockedFS.__mockFileSystem({
foo: {
'yarn.lock': '',
bar: {
'yarn.lock': {},
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBe('foo');

it('should throw when encountering a file system error other than ENOENT', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: createError('EACCES', 'foo/bar/baz', -13, 'stat'),
mockedFS.__mockFileSystem({
foo: {
'yarn.lock': {},
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBeUndefined();

mockedFS.__mockFileSystem({
'yarn.lock': {},
foo: {
bar: {
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBeUndefined();
});

await expect(findPackageRoot('foo/bar/baz')).rejects.toThrowErrorMatchingSnapshot();
it('should throw when encountering a file system error other than ENOENT', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: createError('EACCES', 'foo/bar/baz', -13, 'stat'),
},
},
});

await expect(
findPackageRoot('foo/bar/baz', 'yarn.lock'),
).rejects.toThrowErrorMatchingSnapshot();
});
});
});

0 comments on commit c35b018

Please sign in to comment.