Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fix Windows Kitchen Sink #897

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@
"meow": "^9.0.0",
"ncp": "^2.0.0",
"prettier": "3.2.5",
"rimraf": "3.0.2",
"write-file-atomic": "^4.0.0"
"rimraf": "3.0.2"
},
"devDependencies": {
"@npm/types": "^1.0.1",
Expand All @@ -72,7 +71,6 @@
"@types/rimraf": "^3.0.0",
"@types/sinon": "^17.0.0",
"@types/tmp": "^0.2.0",
"@types/write-file-atomic": "^4.0.0",
"c8": "^9.0.0",
"cross-spawn": "^7.0.3",
"fs-extra": "^11.0.0",
Expand Down
5 changes: 2 additions & 3 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as inquirer from 'inquirer';
import * as path from 'path';
import {ncp} from 'ncp';
import * as util from 'util';
import * as writeFileAtomic from 'write-file-atomic';

import {
getPkgManagerCommand,
Expand Down Expand Up @@ -168,7 +167,7 @@ async function writePackageJson(
): Promise<void> {
options.logger.log('Writing package.json...');
if (!options.dryRun) {
await writeFileAtomic('./package.json', formatJson(packageJson));
await fs.promises.writeFile('./package.json', formatJson(packageJson));
}
const preview = {
scripts: packageJson.scripts,
Expand Down Expand Up @@ -216,7 +215,7 @@ async function generateConfigFile(
if (writeFile) {
options.logger.log(`Writing ${filename}...`);
if (!options.dryRun) {
await writeFileAtomic(filename, contents);
await fs.promises.writeFile(filename, contents);
}
options.logger.log(contents);
}
Expand Down
41 changes: 35 additions & 6 deletions test/kitchen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
const stagingDir = tmp.dirSync({keep, unsafeCleanup: true});
const stagingPath = stagingDir.name;
const execOpts = {
cwd: `${stagingPath}${path.sep}kitchen`,
encoding: 'utf8' as BufferEncoding,
};
cwd: path.join(stagingPath, 'kitchen'),
encoding: 'utf8',
} as const;

describe('🚰 kitchen sink', () => {
const fixturesPath = path.join('test', 'fixtures');
const gtsPath = path.join('node_modules', '.bin', 'gts');
const kitchenPath = path.join(stagingPath, 'kitchen');
const kitchenPath = execOpts.cwd;

// Create a staging directory with temp fixtures used to test on a fresh application.
before(() => {
Expand All @@ -34,6 +34,7 @@
fs.moveSync('gts.tgz', targetPath);
fs.copySync(fixturesPath, path.join(stagingPath, path.sep));
});

// CLEAN UP - remove the staging directory when done.
after('cleanup staging', () => {
if (!keep) {
Expand Down Expand Up @@ -73,9 +74,15 @@
it('should use as a non-locally installed module', () => {
// Use from a directory different from where we have locally installed. This
// simulates use as a globally installed module.
const GTS = path.resolve(stagingPath, 'kitchen/node_modules/.bin/gts');
const GTS = path.resolve(
stagingPath,
'kitchen',
'node_modules',
'.bin',
'gts'
);
const tmpDir = tmp.dirSync({keep, unsafeCleanup: true});
const opts = {cwd: path.join(tmpDir.name, 'kitchen')};
const opts = {cwd: path.join(tmpDir.name, 'kitchen')} as const;

// Copy test files.
fs.copySync(fixturesPath, tmpDir.name);
Expand All @@ -84,6 +91,28 @@
path.join(stagingPath, 'gts.tgz'),
path.join(tmpDir.name, 'gts.tgz')
);

try {
console.log(
path.resolve(stagingPath, 'kitchen'),
fs.readdirSync(path.resolve(stagingPath, 'kitchen'))
);

console.log(
path.resolve(stagingPath, 'kitchen', 'node_modules'),
fs.readdirSync(path.resolve(stagingPath, 'kitchen', 'node_modules'))
);

console.log(
path.resolve(stagingPath, 'kitchen', 'node_modules', '.bin'),
fs.readdirSync(
path.resolve(stagingPath, 'kitchen', 'node_modules', '.bin')
)
);
} catch (e) {
console.dir({e});
}

// It's important to use `-n` here because we don't want to overwrite
// the version of gts installed, as it will trigger the npm install.
spawn.sync(GTS, ['init', '-n'], opts);
Expand All @@ -100,7 +129,7 @@
);

// server.ts has a lint error. Should error.
assert.throws(() => cp.execSync(`${GTS} lint src/server.ts`, opts));

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium test

This shell command depends on an uncontrolled
absolute path
.

if (!keep) {
tmpDir.removeCallback();
Expand Down
Loading