-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathauto-install.test.ts
More file actions
146 lines (120 loc) · 4.54 KB
/
auto-install.test.ts
File metadata and controls
146 lines (120 loc) · 4.54 KB
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
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { describe, it, expect } from 'vitest';
import { rollup, type Plugin } from 'rollup';
import nodeResolve from '@rollup/plugin-node-resolve';
import autoInstall from '~package';
const PACKAGE_ROOT = process.cwd();
async function withFixture(
fixtureName: string,
fn: (ctx: { cwd: string; input: string; outputFile: string }) => Promise<void>
) {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'rollup-plugin-auto-install-'));
const cwd = path.join(tmpDir, 'cwd');
await fs.mkdir(cwd, { recursive: true });
const fixturesDir = path.join(PACKAGE_ROOT, 'test', 'fixtures');
await fs.cp(path.join(fixturesDir, fixtureName), cwd, { recursive: true });
const input = path.join(cwd, 'input.js');
await fs.copyFile(path.join(fixturesDir, 'input.js'), input);
const outputFile = path.join(cwd, 'output', 'bundle.js');
await fs.mkdir(path.dirname(outputFile), { recursive: true });
const previousCwd = process.cwd();
process.chdir(cwd);
try {
await fn({ cwd, input, outputFile });
} finally {
process.chdir(previousCwd);
await fs.rm(tmpDir, { recursive: true, force: true });
}
}
async function bundleWithPlugins(input: string, outputFile: string, plugins: Plugin[]) {
const bundle = await rollup({
input,
plugins
});
try {
await bundle.write({
file: outputFile,
format: 'cjs'
});
} finally {
await bundle.close();
}
}
const noopResolvePlugin: Plugin = {
name: 'noop-resolver',
resolveId(id) {
if (id === 'node-noop') return id;
return null;
},
load(id) {
if (id === 'node-noop') {
return 'export default {}';
}
return null;
}
};
describe('@rollup/plugin-auto-install', () => {
it('throws on invalid manager', () => {
expect(() => autoInstall({ manager: 'foo' as any })).toThrowError(RangeError);
expect(() => autoInstall({ manager: 'foo' as any })).toThrowError(
/is not a valid package manager/
);
});
it('npm', async () => {
await withFixture('npm', async ({ cwd, input, outputFile }) => {
await bundleWithPlugins(input, outputFile, [
autoInstall({ pkgFile: path.join(cwd, 'package.json'), manager: 'npm' }),
nodeResolve()
]);
const json = JSON.parse(await fs.readFile(path.join(cwd, 'package.json'), 'utf-8'));
expect(json.dependencies?.['node-noop']).toBeDefined();
});
}, 50_000);
it('npm, bare', async () => {
await withFixture('npm-bare', async ({ cwd, input, outputFile }) => {
await bundleWithPlugins(input, outputFile, [autoInstall(), nodeResolve()]);
const json = JSON.parse(await fs.readFile(path.join(cwd, 'package.json'), 'utf-8'));
expect(json.dependencies?.['node-noop']).toBeDefined();
const lockFile = await fs.readFile(path.join(cwd, 'package-lock.json'), 'utf-8');
expect(lockFile).toContain('"node-noop"');
});
}, 50_000);
it('pnpm', async () => {
await withFixture('pnpm', async ({ cwd, input, outputFile }) => {
await bundleWithPlugins(input, outputFile, [autoInstall(), nodeResolve()]);
const json = JSON.parse(await fs.readFile(path.join(cwd, 'package.json'), 'utf-8'));
expect(json.dependencies?.['node-noop']).toBeDefined();
});
}, 50_000);
it('pnpm, bare', async () => {
await withFixture('pnpm-bare', async ({ cwd, input, outputFile }) => {
await bundleWithPlugins(input, outputFile, [autoInstall({ manager: 'pnpm' }), nodeResolve()]);
const json = JSON.parse(await fs.readFile(path.join(cwd, 'package.json'), 'utf-8'));
expect(json.dependencies?.['node-noop']).toBeDefined();
});
}, 50_000);
it('yarn', async () => {
await withFixture('yarn', async ({ cwd, input, outputFile }) => {
await bundleWithPlugins(input, outputFile, [
autoInstall({ commands: { yarn: 'echo yarn > yarn.lock' } }),
noopResolvePlugin,
nodeResolve()
]);
const lockFile = await fs.readFile(path.join(cwd, 'yarn.lock'), 'utf-8');
expect(lockFile).toMatch(/yarn\s+node-noop/);
});
}, 50_000);
it('yarn, bare', async () => {
await withFixture('yarn-bare', async ({ cwd, input, outputFile }) => {
await bundleWithPlugins(input, outputFile, [
autoInstall({ manager: 'yarn', commands: { yarn: 'echo yarn.bare > yarn.lock' } }),
noopResolvePlugin,
nodeResolve()
]);
const lockFile = await fs.readFile(path.join(cwd, 'yarn.lock'), 'utf-8');
expect(lockFile).toMatch(/yarn\.bare\s+node-noop/);
});
}, 50_000);
});