-
Notifications
You must be signed in to change notification settings - Fork 206
/
kitchen.ts
180 lines (160 loc) · 6.05 KB
/
kitchen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import chalk = require('chalk');
import * as cp from 'child_process';
import * as fs from 'fs-extra';
import * as tmp from 'tmp';
import * as assert from 'assert';
import * as path from 'path';
import {describe, it, before, after} from 'mocha';
import spawn = require('cross-spawn');
import execa = require('execa');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');
const keep = !!process.env.GTS_KEEP_TEMPDIRS;
const stagingDir = tmp.dirSync({keep, unsafeCleanup: true});
const stagingPath = stagingDir.name;
const execOpts = {
cwd: `${stagingPath}${path.sep}kitchen`,
encoding: 'utf8' as BufferEncoding,
};
const action = process.platform !== 'win32' ? describe : describe.skip;
action('🚰 kitchen sink', () => {
const fixturesPath = path.join('test', 'fixtures');
const gtsPath = path.join('node_modules', '.bin', 'gts');
const kitchenPath = path.join(stagingPath, 'kitchen');
// Create a staging directory with temp fixtures used to test on a fresh application.
before(() => {
console.log(`${chalk.blue(`${__filename} staging area: ${stagingPath}`)}`);
cp.execSync('npm pack');
const tarball = `${pkg.name}-${pkg.version}.tgz`;
fs.renameSync(tarball, 'gts.tgz');
const targetPath = path.resolve(stagingPath, 'gts.tgz');
console.log('moving packed tar to ', targetPath);
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) {
stagingDir.removeCallback();
}
});
it('it should run init', () => {
const args = [
'-p',
path.resolve(stagingPath, 'gts.tgz'),
'gts',
'init',
// 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.
'-n',
];
const res = spawn.sync('npx', args, execOpts);
console.log('out: ', res.stdout + '');
console.log('error: ', res.stderr + '');
// Ensure config files got generated.
fs.accessSync(path.join(kitchenPath, 'tsconfig.json'));
fs.accessSync(path.join(kitchenPath, '.eslintrc.json'));
fs.accessSync(path.join(kitchenPath, '.eslintignore'));
fs.accessSync(path.join(kitchenPath, '.prettierrc.js'));
fs.accessSync(path.join(kitchenPath, '.editorconfig'));
console.log('ensured config files existed');
// Compilation shouldn't have happened. Hence no `build` directory.
const dirContents = fs.readdirSync(kitchenPath);
console.log(`read dirContents length = ${dirContents.length}`);
assert.strictEqual(dirContents.indexOf('build'), -1);
});
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 tmpDir = tmp.dirSync({keep, unsafeCleanup: true});
const opts = {cwd: path.join(tmpDir.name, 'kitchen')};
// Copy test files.
fs.copySync(fixturesPath, tmpDir.name);
// Test package.json expects a gts tarball from ../gts.tgz.
fs.copySync(
path.join(stagingPath, 'gts.tgz'),
path.join(tmpDir.name, 'gts.tgz'),
);
// 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);
// The `extends` field must use the local gts path.
const tsconfigJson = fs.readFileSync(
path.join(tmpDir.name, 'kitchen', 'tsconfig.json'),
'utf8',
);
const tsconfig = JSON.parse(tsconfigJson);
assert.deepStrictEqual(
tsconfig.extends,
'./node_modules/gts/tsconfig-google.json',
);
// server.ts has a lint error. Should error.
assert.throws(() => cp.execSync(`${GTS} lint src/server.ts`, opts));
if (!keep) {
tmpDir.removeCallback();
}
});
it('should terminate generated files with newline', () => {
const GTS = path.resolve(stagingPath, gtsPath);
spawn.sync(GTS, ['init', '-y'], execOpts);
assert.ok(
fs
.readFileSync(path.join(kitchenPath, 'package.json'), 'utf8')
.endsWith('\n'),
);
assert.ok(
fs
.readFileSync(path.join(kitchenPath, 'tsconfig.json'), 'utf8')
.endsWith('\n'),
);
assert.ok(
fs
.readFileSync(path.join(kitchenPath, '.eslintrc.json'), 'utf8')
.endsWith('\n'),
);
assert.ok(
fs
.readFileSync(path.join(kitchenPath, '.eslintignore'), 'utf8')
.endsWith('\n'),
);
assert.ok(
fs
.readFileSync(path.join(kitchenPath, '.prettierrc.js'), 'utf8')
.endsWith('\n'),
);
});
it('should lint before fix', async () => {
const res = await execa(
'npm',
['run', 'lint'],
Object.assign({}, {reject: false}, execOpts),
);
assert.strictEqual(res.exitCode, 1);
assert.ok(res.stdout.includes('assigned a value but'));
});
it('should fix', () => {
const preFix = fs
.readFileSync(path.join(kitchenPath, 'src', 'server.ts'), 'utf8')
.split(/[\n\r]+/);
cp.execSync('npm run fix', execOpts);
const postFix = fs
.readFileSync(path.join(kitchenPath, 'src', 'server.ts'), 'utf8')
.split(/[\n\r]+/);
assert.strictEqual(preFix[0].trim() + ';', postFix[0]); // fix should have added a semi-colon
});
it('should lint after fix', () => {
cp.execSync('npm run lint', execOpts);
});
it('should build', () => {
cp.execSync('npm run compile', execOpts);
fs.accessSync(path.join(kitchenPath, 'build', 'src', 'server.js'));
fs.accessSync(path.join(kitchenPath, 'build', 'src', 'server.js.map'));
fs.accessSync(path.join(kitchenPath, 'build', 'src', 'server.d.ts'));
});
// Verify the `gts clean` command actually removes the output dir
it('should clean', () => {
cp.execSync('npm run clean', execOpts);
assert.throws(() => fs.accessSync(path.join(kitchenPath, 'build')));
});
});