Skip to content

Commit

Permalink
Adds tests for the pack list generation (#5873)
Browse files Browse the repository at this point in the history
**What's the problem this PR addresses?**

#5872 made me notice that we have few tests covering how the `*` / `**`
patterns are interpreted.

**How did you fix it?**

This diff fixes that by adding a couple of tests, along with comments on
the few places where we seem to diverge from npm. It doesn't try to
change the pack list generation, just to increase its coverage and make
it easier to add more tests in the future.

**Checklist**
<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
  • Loading branch information
arcanis authored Oct 26, 2023
1 parent 2ded191 commit 047bebf
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/5452f3ea.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declined:
- "@yarnpkg/core"
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import {xfs, npath} from '@yarnpkg/fslib';
import {fs as fsUtils} from 'pkg-tests-core';
import tar from 'tar';
import {xfs, npath} from '@yarnpkg/fslib';
import {fs as fsUtils, misc} from 'pkg-tests-core';
import tar from 'tar';

async function genPackList(run) {
const {stdout} = await run(`pack`, `--dry-run`, `--json`);

return misc.parseJsonStream(stdout).map(entry => {
return entry.location;
}).filter(location => !!location);
}

describe(`Commands`, () => {
describe(`pack`, () => {
Expand Down Expand Up @@ -316,6 +324,151 @@ describe(`Commands`, () => {
}),
);

test(
`it should support gitignore patterns (*)`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `x.js\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`foo/y.js`,
`package.json`,
`y.js`,
]);
}),
);

test(
`it should allow to explicitly add back a file excluded by a gitignore`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `*.js\n!x.js\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`foo/x.js`,
`package.json`,
`x.js`,
]);
}),
);

test(
`it should allow to explicitly add back a single file excluded by a gitignore`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `*.js\n!/x.js\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`package.json`,
`x.js`,
]);
}),
);

test(
`it should support gitignore patterns (**)`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `**/x.js\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`foo/bar/y.js`,
`foo/y.js`,
`package.json`,
`x.js`, // TODO: This shouldn't be here; https://github.com/yarnpkg/berry/issues/5872
`y.js`,
]);
}),
);

test(
`it should keep digging inside excluded folders (unlike gitignore!)`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `*\n!x.js\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`foo/bar/x.js`,
`foo/x.js`,
`package.json`,
`x.js`,
]);
}),
);

test(
`it should keep digging inside excluded folders (unlike gitignore!)`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `*\n!x.js\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`foo/bar/x.js`,
`foo/x.js`,
`package.json`,
`x.js`,
]);
}),
);

// TODO: https://github.com/yarnpkg/berry/issues/5872
test.skip(
`it should be possible to re-exclude something which got not-excluded`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await fsUtils.writeFile(`${path}/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/x.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/foo/bar/y.js`, `module.exports = 42;\n`);
await fsUtils.writeFile(`${path}/.gitignore`, `*\n!x.js\nfoo/bar\n`);

await run(`install`);

await expect(genPackList(run)).resolves.toEqual([
`foo/x.js`,
`package.json`,
`x.js`,
]);
}),
);

test(
`it should override fields in the packed manifest`,
makeTemporaryEnv({
Expand Down

0 comments on commit 047bebf

Please sign in to comment.