summaryrefslogtreecommitdiff
path: root/packages/upgrade/test/utils.js
blob: d57ceacd3ab3eb5e0548db2de6c2f471b0995c7f (plain) (blame)
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
import fs from 'node:fs';
import { setStdout } from '../dist/index.js';
import stripAnsi from 'strip-ansi';

export function setup() {
	const ctx = { messages: [] };
	before(() => {
		setStdout(
			Object.assign({}, process.stdout, {
				write(buf) {
					ctx.messages.push(stripAnsi(String(buf)).trim());
					return true;
				},
			})
		);
	});
	beforeEach(() => {
		ctx.messages = [];
	});

	return {
		messages() {
			return ctx.messages;
		},
		length() {
			return ctx.messages.length;
		},
		hasMessage(content) {
			return !!ctx.messages.find((msg) => msg.includes(content));
		},
	};
}

const resetBasicFixture = async () => {
	const packagePath = new URL('./fixtures/basic/package.json', import.meta.url);
	const packageJsonData = JSON.parse(
		await fs.promises.readFile(packagePath, { encoding: 'utf-8' })
	);
	const overriddenPackageJson = Object.assign(packageJsonData, {
		dependencies: {
			astro: '1.0.0',
		},
	});

	return Promise.all([
		fs.promises.writeFile(packagePath, JSON.stringify(overriddenPackageJson, null, 2), {
			encoding: 'utf-8',
		}),
	]);
};

export const resetFixtures = () => Promise.allSettled([resetBasicFixture()]);