Skip to content

Commit d74a176

Browse files
authored
feat: Hard fail on invalid bundle names (#136)
Exit bundle process when an invalid bundle name is passed through. GH #124
1 parent 91e933f commit d74a176

File tree

15 files changed

+375
-24
lines changed

15 files changed

+375
-24
lines changed

.changeset/two-spies-talk.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@codecov/bundler-plugin-core": patch
3+
"@codecov/sveltekit-plugin": patch
4+
"@codecov/webpack-plugin": patch
5+
"@codecov/rollup-plugin": patch
6+
"@codecov/nuxt-plugin": patch
7+
"@codecov/vite-plugin": patch
8+
---
9+
10+
When a user submits a invalid bundle name, we will hard fail and exit the bundle process now.

integration-tests/fixtures/generate-bundle-stats/nuxt/nuxt-plugin.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,50 @@ describe("Generating nuxt stats", () => {
147147
{ timeout: 25_000 },
148148
);
149149
});
150+
151+
describe("invalid bundle name is passed", () => {
152+
beforeEach(async () => {
153+
const config = new GenerateConfig({
154+
// nuxt uses vite under the hood
155+
plugin: "nuxt",
156+
configFileName: "nuxt",
157+
format: "esm",
158+
detectFormat: "esm",
159+
version: `v3`,
160+
detectVersion: "v3",
161+
file_format: "ts",
162+
enableSourceMaps: false,
163+
overrideOutputPath: `${nuxtApp}/nuxt.config.ts`,
164+
});
165+
166+
await config.createConfig();
167+
config.removeBundleName(`test-nuxt-v${version}`);
168+
await config.writeConfig();
169+
});
170+
171+
afterEach(async () => {
172+
await $`rm -rf ${nuxtApp}/nuxt.config.ts`;
173+
await $`rm -rf ${nuxtApp}/distV${version}`;
174+
});
175+
176+
it(
177+
"warns users and exits process with a code 1",
178+
async () => {
179+
const id = `nuxt-v${version}-${Date.now()}`;
180+
const API_URL = `http://localhost:8000/test-url/${id}/200/false`;
181+
182+
// prepare and build the app
183+
const { exitCode } =
184+
await $`cd test-apps/nuxt && API_URL=${API_URL} pnpm run build`.nothrow();
185+
186+
expect(exitCode).toBe(1);
187+
// for some reason this isn't being outputted in the test env
188+
// expect(stdout.toString()).toContain(
189+
// "[codecov] bundleName: `` does not match format: `/^[wd_:/@.{}[]$-]+$/`.",
190+
// );
191+
},
192+
{ timeout: 25_000 },
193+
);
194+
});
150195
});
151196
});

integration-tests/fixtures/generate-bundle-stats/rollup/rollup-plugin.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,45 @@ describe("Generating rollup stats", () => {
125125
});
126126
});
127127
});
128+
129+
describe("passing invalid bundle name", () => {
130+
beforeEach(async () => {
131+
const config = new GenerateConfig({
132+
plugin: "rollup",
133+
configFileName: "rollup",
134+
format: "esm",
135+
detectFormat: "esm",
136+
version: `v${version}`,
137+
detectVersion: "v3",
138+
file_format: "cjs",
139+
enableSourceMaps: true,
140+
});
141+
142+
await config.createConfig();
143+
config.removeBundleName(`test-rollup-v${version}`);
144+
await config.writeConfig();
145+
});
146+
147+
afterEach(async () => {
148+
await $`rm -rf ${rollupConfig(version, "esm")}`;
149+
await $`rm -rf ${rollupApp}/distV${version}`;
150+
});
151+
152+
it("warns users and exits process with a code 1", async () => {
153+
const id = `rollup-v${version}-sourcemaps-${Date.now()}`;
154+
const rollup = rollupPath(version);
155+
const configFile = rollupConfig(version, "esm");
156+
const API_URL = `http://localhost:8000/test-url/${id}/200/false`;
157+
158+
// build the app
159+
const { exitCode, stdout } =
160+
await $`API_URL=${API_URL} node ${rollup} -c ${configFile}`.nothrow();
161+
162+
expect(exitCode).toBe(1);
163+
expect(stdout.toString()).toContain(
164+
"[codecov] bundleName: `` does not match format: `/^[wd_:/@.{}[]$-]+$/`.",
165+
);
166+
});
167+
});
128168
});
129169
});

integration-tests/fixtures/generate-bundle-stats/sveltekit/sveltekit-plugin.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,50 @@ describe("Generating sveltekit stats", () => {
139139
{ timeout: 25_000 },
140140
);
141141
});
142+
143+
describe("invalid bundle name is passed", () => {
144+
beforeEach(async () => {
145+
const config = new GenerateConfig({
146+
plugin: "sveltekit",
147+
configFileName: "vite",
148+
format: "esm",
149+
detectFormat: "esm",
150+
version: `v2`,
151+
detectVersion: "v2",
152+
file_format: "ts",
153+
enableSourceMaps: false,
154+
overrideOutputPath: `${sveltekitApp}/vite.config.ts`,
155+
});
156+
157+
await config.createConfig();
158+
config.removeBundleName(`test-sveltekit-v${version}`);
159+
await config.writeConfig();
160+
});
161+
162+
afterEach(async () => {
163+
await $`rm -rf ${sveltekitApp}/vite.config.ts`;
164+
await $`rm -rf ${sveltekitApp}/.svelte-kit`;
165+
await $`rm -rf ./fixtures/generate-bundle-stats/sveltekit/.svelte-kit`;
166+
});
167+
168+
it(
169+
"warns users and exits process with a code 1",
170+
async () => {
171+
const id = `sveltekit-v${version}-${Date.now()}`;
172+
const API_URL = `http://localhost:8000/test-url/${id}/200/false`;
173+
174+
// prepare and build the app
175+
const { exitCode, stdout } =
176+
await $`cd test-apps/sveltekit && API_URL=${API_URL} pnpm run build`.nothrow();
177+
178+
expect(exitCode).toBe(1);
179+
// for some reason this isn't being outputted in the test env
180+
expect(stdout.toString()).toContain(
181+
"[codecov] bundleName: `` does not match format: `/^[wd_:/@.{}[]$-]+$/`.",
182+
);
183+
},
184+
{ timeout: 25_000 },
185+
);
186+
});
142187
});
143188
});

integration-tests/fixtures/generate-bundle-stats/vite/vite-plugin.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GenerateConfig } from "../../../scripts/gen-config";
66
const vitePath = (version: number) =>
77
`node_modules/viteV${version}/bin/vite.js`;
88
const viteConfig = (version: number, format: string) =>
9-
`fixtures/generate-bundle-stats/vite/vite-v${version}-${format}.config.ts`;
9+
`fixtures/generate-bundle-stats/vite/vite-v${version}-${format}.config.*`;
1010
const viteApp = "test-apps/vite";
1111

1212
const VERSIONS = [4, 5];
@@ -124,5 +124,45 @@ describe("Generating vite stats", () => {
124124
});
125125
});
126126
});
127+
128+
describe("invalid bundle name is passed", () => {
129+
beforeEach(async () => {
130+
const config = new GenerateConfig({
131+
plugin: "vite",
132+
configFileName: "vite",
133+
format: "esm",
134+
detectFormat: "esm",
135+
version: `v${version}`,
136+
detectVersion: "v5",
137+
file_format: "ts",
138+
enableSourceMaps: true,
139+
});
140+
141+
await config.createConfig();
142+
config.removeBundleName(`test-vite-v${version}`);
143+
await config.writeConfig();
144+
});
145+
146+
afterEach(async () => {
147+
await $`rm -rf ${viteConfig(version, "esm")}`;
148+
await $`rm -rf ${viteApp}/distV${version}`;
149+
});
150+
151+
it("warns users and exits process with a code 1", async () => {
152+
const id = `vite-v${version}-sourcemaps-${Date.now()}`;
153+
const vite = vitePath(version);
154+
const configFile = viteConfig(version, "esm");
155+
const API_URL = `http://localhost:8000/test-url/${id}/200/false`;
156+
157+
// build the app
158+
const { exitCode, stdout } =
159+
await $`API_URL=${API_URL} node ${vite} build -c ${configFile}`.nothrow();
160+
161+
expect(exitCode).toBe(1);
162+
expect(stdout.toString()).toContain(
163+
"[codecov] bundleName: `` does not match format: `/^[wd_:/@.{}[]$-]+$/`.",
164+
);
165+
});
166+
});
127167
});
128168
});

integration-tests/fixtures/generate-bundle-stats/webpack/webpack-plugin.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GenerateConfig } from "../../../scripts/gen-config";
66
const webpackPath = (version: number) =>
77
`node_modules/webpackV${version}/bin/webpack.js`;
88
const webpackConfig = (version: number, format: string) =>
9-
`fixtures/generate-bundle-stats/webpack/webpack-v${version}-${format}.config.cjs`;
9+
`fixtures/generate-bundle-stats/webpack/webpack-v${version}-${format}.config.*`;
1010
const webpackApp = "test-apps/webpack";
1111

1212
const VERSIONS = [5];
@@ -116,5 +116,45 @@ describe("Generating webpack stats", () => {
116116
});
117117
});
118118
});
119+
120+
describe("invalid bundle name is passed", () => {
121+
beforeEach(async () => {
122+
const config = new GenerateConfig({
123+
plugin: "webpack",
124+
configFileName: "webpack",
125+
format: "module",
126+
detectFormat: "commonjs",
127+
version: `v${version}`,
128+
detectVersion: "v5",
129+
file_format: "cjs",
130+
enableSourceMaps: false,
131+
});
132+
133+
await config.createConfig();
134+
config.removeBundleName(`test-webpack-v${version}`);
135+
await config.writeConfig();
136+
});
137+
138+
afterEach(async () => {
139+
await $`rm -rf ${webpackConfig(version, "module")}`;
140+
await $`rm -rf ${webpackApp}/distV${version}`;
141+
});
142+
143+
it("warns users and exits process with a code 1", async () => {
144+
const id = `webpack-v${version}-sourcemaps-${Date.now()}`;
145+
const webpack = webpackPath(version);
146+
const configFile = webpackConfig(version, "module");
147+
const API_URL = `http://localhost:8000/test-url/${id}/200/false`;
148+
149+
// build the app
150+
const { exitCode, stdout } =
151+
await $`API_URL=${API_URL} node ${webpack} --config ${configFile}`.nothrow();
152+
153+
expect(exitCode).toBe(1);
154+
expect(stdout.toString()).toContain(
155+
"[codecov] bundleName: `` does not match format: `/^[wd_:/@.{}[]$-]+$/`.",
156+
);
157+
});
158+
});
119159
});
120160
});

integration-tests/scripts/gen-config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ export class GenerateConfig {
114114
return this.newConfigContents;
115115
}
116116

117+
removeBundleName(bundleName: string) {
118+
if (typeof this.newConfigContents === "string") {
119+
this.newConfigContents = this.newConfigContents.replaceAll(
120+
bundleName,
121+
"",
122+
);
123+
}
124+
}
125+
117126
async writeConfig() {
118127
if (typeof this.newConfigContents === "string") {
119128
await Bun.write(this.outFilePath, this.newConfigContents);
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import {
22
type Asset,
3+
type BundleAnalysisUploadPlugin,
34
type Chunk,
45
type Module,
56
type Options,
67
type ProviderUtilInputs,
78
type UploadOverrides,
8-
type BundleAnalysisUploadPlugin,
99
} from "./types.ts";
1010
import { checkNodeVersion } from "./utils/checkNodeVersion.ts";
1111
import { red } from "./utils/logging.ts";
12-
import { normalizeOptions } from "./utils/normalizeOptions.ts";
12+
import { handleErrors, normalizeOptions } from "./utils/normalizeOptions.ts";
1313
import { normalizePath } from "./utils/normalizePath.ts";
1414
import { Output } from "./utils/Output.ts";
1515

1616
export type {
1717
Asset,
18+
BundleAnalysisUploadPlugin,
1819
Chunk,
1920
Module,
2021
Options,
2122
ProviderUtilInputs,
2223
UploadOverrides,
23-
BundleAnalysisUploadPlugin,
2424
};
2525

26-
export { checkNodeVersion, normalizeOptions, normalizePath, red, Output };
26+
export {
27+
checkNodeVersion,
28+
handleErrors,
29+
normalizeOptions,
30+
normalizePath,
31+
Output,
32+
red,
33+
};

0 commit comments

Comments
 (0)