summaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/test/functions/include-files.test.js
blob: e54e116a78c3cda69a1bf5eef0f65c7f875ca5cf (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
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
181
182
183
184
import * as assert from 'node:assert/strict';
import { existsSync } from 'node:fs';
import { after, before, describe, it } from 'node:test';
import netlify from '@astrojs/netlify';
import * as cheerio from 'cheerio';
import { globSync } from 'tinyglobby';
import { loadFixture } from '../../../../astro/test/test-utils.js';

describe(
	'Included vite assets files',
	() => {
		let fixture;

		const root = new URL('./fixtures/includes/', import.meta.url);
		const expectedCwd = new URL('.netlify/v1/functions/ssr/packages/integrations/netlify/', root);

		const expectedAssetsInclude = ['./*.json'];
		const excludedAssets = ['./files/exclude-asset.json'];

		before(async () => {
			fixture = await loadFixture({
				root,
				vite: {
					assetsInclude: expectedAssetsInclude,
				},
				adapter: netlify({
					excludeFiles: excludedAssets,
				}),
			});
			await fixture.build();
		});

		it('Emits vite assets files', async () => {
			for (const pattern of expectedAssetsInclude) {
				const files = globSync(pattern);
				for (const file of files) {
					assert.ok(
						existsSync(new URL(file, expectedCwd)),
						`Expected file ${pattern} to exist in build`,
					);
				}
			}
		});

		it('Does not include vite assets files when excluded', async () => {
			for (const file of excludedAssets) {
				assert.ok(
					!existsSync(new URL(file, expectedCwd)),
					`Expected file ${file} to not exist in build`,
				);
			}
		});

		after(async () => {
			await fixture.clean();
		});
	},
	{
		timeout: 120000,
	},
);

describe(
	'Included files',
	() => {
		let fixture;

		const root = new URL('./fixtures/includes/', import.meta.url);
		const expectedCwd = new URL(
			'.netlify/v1/functions/ssr/packages/integrations/netlify/test/functions/fixtures/includes/',
			root,
		);

		const expectedFiles = [
			'./files/include-this.txt',
			'./files/also-this.csv',
			'./files/subdirectory/and-this.csv',
		];

		before(async () => {
			fixture = await loadFixture({
				root,
				adapter: netlify({
					includeFiles: expectedFiles,
				}),
			});
			await fixture.build();
		});

		it('Emits include files', async () => {
			for (const file of expectedFiles) {
				assert.ok(existsSync(new URL(file, expectedCwd)), `Expected file ${file} to exist`);
			}
		});

		it('Can load included files correctly', async () => {
			const entryURL = new URL(
				'./fixtures/includes/.netlify/v1/functions/ssr/ssr.mjs',
				import.meta.url,
			);
			const { default: handler } = await import(entryURL);
			const resp = await handler(new Request('http://example.com/?file=include-this.txt'), {});
			const html = await resp.text();
			const $ = cheerio.load(html);
			assert.equal($('h1').text(), 'hello');
		});

		it('Includes traced node modules with symlinks', async () => {
			const expected = new URL(
				'.netlify/v1/functions/ssr/node_modules/.pnpm/cowsay@1.6.0/node_modules/cowsay/cows/happy-whale.cow',
				root,
			);
			assert.ok(existsSync(expected, 'Expected excluded file to exist in default build'));
		});

		after(async () => {
			await fixture.clean();
		});
	},
	{
		timeout: 120000,
	},
);

describe(
	'Excluded files',
	() => {
		let fixture;

		const root = new URL('./fixtures/includes/', import.meta.url);
		const expectedCwd = new URL(
			'.netlify/v1/functions/ssr/packages/integrations/netlify/test/functions/fixtures/includes/',
			root,
		);

		const includeFiles = ['./files/**/*.txt'];
		const excludedTxt = ['./files/subdirectory/not-this.txt', './files/subdirectory/or-this.txt'];
		const excludeFiles = [...excludedTxt, '../../../../../../../node_modules/.pnpm/cowsay@*/**'];

		before(async () => {
			fixture = await loadFixture({
				root,
				adapter: netlify({
					includeFiles: includeFiles,
					excludeFiles: excludeFiles,
				}),
			});
			await fixture.build();
		});

		it('Excludes traced node modules', async () => {
			const expected = new URL(
				'.netlify/v1/functions/ssr/node_modules/.pnpm/cowsay@1.6.0/node_modules/cowsay/cows/happy-whale.cow',
				root,
			);
			assert.ok(!existsSync(expected), 'Expected excluded file to not exist in build');
		});

		it('Does not include files when excluded', async () => {
			for (const pattern of includeFiles) {
				const files = globSync(pattern, { ignore: excludedTxt });
				for (const file of files) {
					assert.ok(
						existsSync(new URL(file, expectedCwd)),
						`Expected file ${pattern} to exist in build`,
					);
				}
			}
			for (const file of excludedTxt) {
				assert.ok(
					!existsSync(new URL(file, expectedCwd)),
					`Expected file ${file} to not exist in build`,
				);
			}
		});

		after(async () => {
			await fixture.clean();
		});
	},
	{
		timeout: 120000,
	},
);