Skip to content

Commit

Permalink
feat: Tolerate string literals w/ ppath.join / ppath.resolve (#5250)
Browse files Browse the repository at this point in the history
* Allows generic as parameters for ppath.join / ppath.resolve

* Removes "as" from callsites

* Versions

* Fixes lint

* Replaces "(join\(.*`) as Filename"

* Fixes lint
  • Loading branch information
arcanis authored Feb 8, 2023
1 parent cd262cd commit ca47575
Show file tree
Hide file tree
Showing 68 changed files with 564 additions and 508 deletions.
36 changes: 36 additions & 0 deletions .yarn/versions/90a7a735.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
releases:
"@yarnpkg/fslib": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/cli"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/libzip"
- "@yarnpkg/nm"
- "@yarnpkg/pnp"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
40 changes: 20 additions & 20 deletions packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import {PortablePath, npath, toFilename, xfs, ppath, Filename} from '@yarnpkg/fslib';
import assert from 'assert';
import crypto from 'crypto';
import finalhandler from 'finalhandler';
import https from 'https';
import {IncomingMessage, ServerResponse} from 'http';
import http from 'http';
import invariant from 'invariant';
import {AddressInfo} from 'net';
import os from 'os';
import pem from 'pem';
import semver from 'semver';
import serveStatic from 'serve-static';
import {promisify} from 'util';
import {v5 as uuidv5} from 'uuid';
import {Gzip} from 'zlib';

import {ExecResult} from './exec';
import * as fsUtils from './fs';
import {PortablePath, npath, toFilename, xfs, ppath} from '@yarnpkg/fslib';
import assert from 'assert';
import crypto from 'crypto';
import finalhandler from 'finalhandler';
import https from 'https';
import {IncomingMessage, ServerResponse} from 'http';
import http from 'http';
import invariant from 'invariant';
import {AddressInfo} from 'net';
import os from 'os';
import pem from 'pem';
import semver from 'semver';
import serveStatic from 'serve-static';
import {promisify} from 'util';
import {v5 as uuidv5} from 'uuid';
import {Gzip} from 'zlib';

import {ExecResult} from './exec';
import * as fsUtils from './fs';

const deepResolve = require(`super-resolve`);
const staticServer = serveStatic(npath.fromPortablePath(require(`pkg-tests-fixtures`)));
Expand Down Expand Up @@ -675,7 +675,7 @@ export const generatePkgDriver = ({
return Object.assign(async (): Promise<void> => {
const homePath = await xfs.mktempPromise();

const path = ppath.join(homePath, `test` as Filename);
const path = ppath.join(homePath, `test`);
await xfs.mkdirPromise(path);

const registryUrl = await startPackageServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export async function writePackage(dir: PortablePath, manifest: {[key: string]:
}

export function getPluginPath(dir: PortablePath, name: string) {
return ppath.join(dir, `.yarn/plugins/${name}.cjs` as PortablePath);
return ppath.join(dir, `.yarn/plugins/${name}.cjs`);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {xfs, ppath, Filename} from '@yarnpkg/fslib';
import {xfs, ppath} from '@yarnpkg/fslib';

describe(`Commands`, () => {
describe(`create`, () => {
test(
`it should generate \`hello.txt\` correctly using a starter-kit-package`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`create`, `-q`, `test-app`);
expect(xfs.readFileSync(ppath.join(path, `hello.txt` as Filename), `utf8`)).toEqual(`Hello World`);
expect(xfs.readFileSync(ppath.join(path, `hello.txt`), `utf8`)).toEqual(`Hello World`);
}),
);

test(
`it should generate \`hello.txt\` correctly using a scoped starter-kit-package`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`create`, `-q`, `@scoped/test-app`);
expect(xfs.readFileSync(ppath.join(path, `hello.txt` as Filename), `utf8`)).toEqual(`Hello World`);
expect(xfs.readFileSync(ppath.join(path, `hello.txt`), `utf8`)).toEqual(`Hello World`);
}),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Filename, npath, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';

describe(`Commands`, () => {
describe(`exec`, () => {
Expand Down Expand Up @@ -27,9 +27,9 @@ describe(`Commands`, () => {
test(
`it should inject binaries the workspace has access to`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await xfs.mkdirPromise(ppath.join(path, `bin` as Filename));
await xfs.writeFilePromise(ppath.join(path, `bin/index.js` as PortablePath), `console.log(42)`);
await xfs.writeJsonPromise(ppath.join(path, `bin` as Filename, Filename.manifest), {
await xfs.mkdirPromise(ppath.join(path, `bin`));
await xfs.writeFilePromise(ppath.join(path, `bin/index.js`), `console.log(42)`);
await xfs.writeJsonPromise(ppath.join(path, `bin`, Filename.manifest), {
name: `bin`,
bin: `./index.js`,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {ppath, xfs} from '@yarnpkg/fslib';

describe(`Commands`, () => {
describe(`info`, () => {
Expand Down Expand Up @@ -68,8 +68,8 @@ describe(`Commands`, () => {
`workspace`,
],
}, async ({path, run, source}) => {
await xfs.mkdirpPromise(ppath.join(path, `workspace` as PortablePath));
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json` as PortablePath), {
await xfs.mkdirpPromise(ppath.join(path, `workspace`));
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json`), {
dependencies: {
[`no-deps`]: `1.0.0`,
},
Expand All @@ -88,8 +88,8 @@ describe(`Commands`, () => {
`workspace`,
],
}, async ({path, run, source}) => {
await xfs.mkdirpPromise(ppath.join(path, `workspace` as PortablePath));
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json` as PortablePath), {
await xfs.mkdirpPromise(ppath.join(path, `workspace`));
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json`), {
dependencies: {
[`no-deps`]: `1.0.0`,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Filename, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {Filename, ppath, xfs} from '@yarnpkg/fslib';

describe(`Commands`, () => {
describe(`init`, () => {
test(
`it should create a new package.json in the local directory if it doesn't exist`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await xfs.mktempPromise(async tmpDir => {
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
const pkgDir = ppath.join(tmpDir, `my-package`);
await xfs.mkdirpPromise(pkgDir);

await run(`init`, {
Expand All @@ -24,7 +24,7 @@ describe(`Commands`, () => {
`it should create a new package.json in the specified directory if it doesn't exist`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await xfs.mktempPromise(async tmpDir => {
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
const pkgDir = ppath.join(tmpDir, `my-package`);
await xfs.mkdirpPromise(pkgDir);

await run(`./my-package`, `init`, {
Expand All @@ -42,7 +42,7 @@ describe(`Commands`, () => {
`it should create a new package.json in the specified directory even if said directory doesn't exist`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await xfs.mktempPromise(async tmpDir => {
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
const pkgDir = ppath.join(tmpDir, `my-package`);

await run(`./my-package`, `init`, {
cwd: tmpDir,
Expand All @@ -59,14 +59,14 @@ describe(`Commands`, () => {
`it should copy the currently running bundle when using --install`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await xfs.mktempPromise(async tmpDir => {
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
const pkgDir = ppath.join(tmpDir, `my-package`);
await xfs.mkdirpPromise(pkgDir);

await run(`init`, `--install=self`, {
cwd: pkgDir,
});

await expect(xfs.existsPromise(ppath.join(pkgDir, `.yarn/releases` as PortablePath))).resolves.toEqual(true);
await expect(xfs.existsPromise(ppath.join(pkgDir, `.yarn/releases`))).resolves.toEqual(true);
});
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
import {npath, ppath, xfs} from '@yarnpkg/fslib';

describe(`Commands`, () => {
describe(`patch`, () => {
Expand All @@ -25,7 +25,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `foo.js` as Filename);
const updateFile = ppath.join(updateFolder, `foo.js`);

const fileUser = `module.exports = 'foo';\n`;
await xfs.writeFilePromise(updateFile, fileUser);
Expand All @@ -42,7 +42,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `bar.js` as Filename);
const updateFile = ppath.join(updateFolder, `bar.js`);

const fileUser = `module.exports = 'bar';\n`;
await xfs.writeFilePromise(updateFile, fileUser);
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `foo.js` as Filename);
const updateFile = ppath.join(updateFolder, `foo.js`);

const fileUser = `module.exports = 'foo';\n`;
await xfs.writeFilePromise(updateFile, fileUser);
Expand All @@ -96,7 +96,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `bar.js` as Filename);
const updateFile = ppath.join(updateFolder, `bar.js`);

const fileUser = `module.exports = 'bar';\n`;
await xfs.writeFilePromise(updateFile, fileUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
const updateFile = ppath.join(updateFolder, `index.js`);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
Expand All @@ -38,7 +38,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
const updateFile = ppath.join(updateFolder, `index.js`);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
Expand Down Expand Up @@ -68,7 +68,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
const updateFile = ppath.join(updateFolder, `index.js`);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
Expand Down Expand Up @@ -99,7 +99,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
const updateFile = ppath.join(updateFolder, `index.js`);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
Expand Down Expand Up @@ -133,7 +133,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `new.js` as Filename);
const updateFile = ppath.join(updateFolder, `new.js`);

const fileUser = `module.exports = 42;\n`;
await xfs.writeFilePromise(updateFile, fileUser);
Expand All @@ -152,7 +152,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `new.js` as Filename);
const updateFile = ppath.join(updateFolder, `new.js`);

const fileUser = `module.exports = 21;\n`;
await xfs.writeFilePromise(updateFile, fileUser);
Expand Down Expand Up @@ -181,7 +181,7 @@ describe(`Commands`, () => {
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
const updateFile = ppath.join(updateFolder, `index.js`);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe(`Commands`, () => {
makeTemporaryEnv({}, {
env: {COREPACK_ROOT: undefined},
}, async ({path, run, source}) => {
const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename);
const yarnIndirection = ppath.join(path, `custom-yarn.cjs`);
await xfs.writeFilePromise(yarnIndirection, ``);

await expect(run(`set`, `version`, yarnIndirection, `--no-yarn-path`)).rejects.toThrow();
Expand All @@ -78,7 +78,7 @@ describe(`Commands`, () => {
makeTemporaryEnv({}, {
env: {COREPACK_ROOT: undefined},
}, async ({path, run, source}) => {
const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename);
const yarnIndirection = ppath.join(path, `custom-yarn.cjs`);
await xfs.writeFilePromise(yarnIndirection, ``);

await run(`set`, `version`, yarnIndirection);
Expand All @@ -94,7 +94,7 @@ describe(`Commands`, () => {
await run(`set`, `version`, `self`);
await check(path, {corepackVersion: /[0-9]+\./, usePath: true});

const projectDir = ppath.join(path, `project` as Filename);
const projectDir = ppath.join(path, `project`);
await xfs.mkdirPromise(projectDir);
await xfs.writeJsonPromise(ppath.join(projectDir, Filename.manifest), {});
await xfs.writeFilePromise(ppath.join(projectDir, Filename.lockfile), ``);
Expand Down Expand Up @@ -129,7 +129,7 @@ describe(`Commands`, () => {
await run(`set`, `version`, `self`);
await check(path, {corepackVersion: /[0-9]+\./, usePath: true});

const projectDir = ppath.join(path, `project` as Filename);
const projectDir = ppath.join(path, `project`);
await xfs.mkdirPromise(projectDir);
await xfs.writeJsonPromise(ppath.join(projectDir, Filename.manifest), {});
await xfs.writeFilePromise(ppath.join(projectDir, Filename.lockfile), ``);
Expand All @@ -142,7 +142,7 @@ describe(`Commands`, () => {
});

async function check(path: PortablePath, checks: {corepackVersion: string | RegExp, usePath: boolean}) {
const releasesPath = ppath.join(path, `.yarn/releases` as PortablePath);
const releasesPath = ppath.join(path, `.yarn/releases`);
const yarnrcPath = ppath.join(path, Filename.rc);
const manifestPath = ppath.join(path, Filename.manifest);

Expand Down
Loading

0 comments on commit ca47575

Please sign in to comment.