Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[testing] More testing #104

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
94d9a95
Eslint tweaks
yoannmoinet Oct 8, 2024
0bfbd18
Re-use existing loop script
yoannmoinet Oct 8, 2024
ebe254f
Strongify injection plugin detection
yoannmoinet Oct 8, 2024
4fe9556
Re-use doRequest to submit metrics
yoannmoinet Oct 8, 2024
2f00687
Add new mocks
yoannmoinet Oct 8, 2024
1ee1d52
Incorporate unplugin loaders in bundles
yoannmoinet Oct 8, 2024
5bce0e0
Better cleanup and error handling in runBundlers
yoannmoinet Oct 8, 2024
8abaf14
Add tests for tool helpers
yoannmoinet Oct 8, 2024
8bd0302
Add tests for bundles
yoannmoinet Oct 8, 2024
4d43918
Factorise injection detection
yoannmoinet Oct 8, 2024
b11061c
Add back skipped test
yoannmoinet Oct 9, 2024
0714640
Factories webpack entries
yoannmoinet Oct 9, 2024
96fe4c0
Add missing webpack-sources dependency
yoannmoinet Oct 9, 2024
16207ab
Re-order injection plugin definition
yoannmoinet Oct 9, 2024
3a6b86d
Remove logs from bundling test
yoannmoinet Oct 9, 2024
122005b
Fix missing protocol in telemetry endPoint
yoannmoinet Oct 9, 2024
33c90fb
Ensure we start heavy tests first
yoannmoinet Oct 9, 2024
9d10e17
Integrity
yoannmoinet Oct 9, 2024
e8e7682
Update package.json
yoannmoinet Oct 9, 2024
35aac43
Add plugin names into the global context
yoannmoinet Oct 10, 2024
7ecdcf6
Patch other plugins to avoid initialOptions leakage
yoannmoinet Oct 10, 2024
1a11e48
Fix helpers
yoannmoinet Oct 10, 2024
3ab48c8
Fix esbuild's injection plugin
yoannmoinet Oct 11, 2024
053d529
Run build before tests
yoannmoinet Oct 11, 2024
6b3199a
Fix esbuild build-report after injection update
yoannmoinet Oct 11, 2024
1db7009
Add clean:all command
yoannmoinet Oct 11, 2024
bc31c35
Remove custom sequencer
yoannmoinet Oct 11, 2024
fb8520b
Trim ms from duration helper
yoannmoinet Oct 11, 2024
b2402c9
Add a note about sourcemaps
yoannmoinet Oct 11, 2024
38157b7
Warn about old bundles in tests
yoannmoinet Oct 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,5 +384,11 @@ module.exports = {
'no-console': ['error', { allow: ['time', 'timeEnd'] }],
},
},
{
files: ['packages/core/src/log.ts'],
rules: {
'no-console': 'off',
},
},
],
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"yarn": "1.22.19"
},
"scripts": {
"build:all": "yarn workspaces foreach -Apti --exclude \"@dd/*\" run build",
"build:all": "yarn loop run build",
"clean:all": "yarn loop run clean",
"cli": "yarn workspace @dd/tools cli",
"format": "yarn lint --fix",
"lint": "eslint ./packages/**/*.{ts,js} --quiet",
"loop": "yarn workspaces foreach -Apti --include \"@datadog/*\" --exclude \"@datadog/build-plugins\"",
"oss": "yarn cli oss -d packages -l mit",
"publish:all": "yarn loop --no-private npm publish",
"test": "yarn workspace @dd/tests test",
"test": "yarn build:all && yarn workspace @dd/tests test",
"test:noisy": "yarn workspace @dd/tests test:noisy",
"typecheck:all": "yarn workspaces foreach -Apti run typecheck",
"version:all": "yarn loop version --deferred ${0} && yarn version apply --all",
Expand Down
21 changes: 19 additions & 2 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import retry from 'async-retry';
import type { RequestInit } from 'undici-types';

import type { RequestOpts } from './types';
import { INJECTED_FILE, INJECTION_SUFFIX } from './plugins/injection/constants';
import type { GlobalContext, RequestOpts } from './types';

// Format a duration 0h 0m 0s 0ms
export const formatDuration = (duration: number) => {
Expand All @@ -18,7 +19,7 @@ export const formatDuration = (duration: number) => {
const milliseconds = d.getUTCMilliseconds();
return `${days ? `${days}d ` : ''}${hours ? `${hours}h ` : ''}${minutes ? `${minutes}m ` : ''}${
seconds ? `${seconds}s ` : ''
}${milliseconds}ms`.trim();
}${milliseconds ? `${milliseconds}ms` : ''}`.trim();
};

export const getResolvedPath = (filepath: string) => {
Expand Down Expand Up @@ -117,3 +118,19 @@ export const truncateString = (

return `${str.slice(0, leftStop)}${placeholder}${str.slice(-rightStop)}`;
};

// Is the file coming from the injection plugin?
export const isInjectionFile = (filename: string) => filename.includes(INJECTED_FILE);
export const isInjectionProxy = (filename: string) => filename.endsWith(INJECTION_SUFFIX);
export const isFromInjection = (filename: string) =>
isInjectionFile(filename) || isInjectionProxy(filename);

// Is the given plugin name is from our internal plugins?
export const isInternalPlugin = (pluginName: string, context: GlobalContext) => {
for (const internalPluginName of context.pluginNames) {
if (pluginName.includes(internalPluginName)) {
return true;
}
}
return false;
};
4 changes: 0 additions & 4 deletions packages/core/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ export type Logger = (text: any, type?: LogLevel) => void;
const log = (text: any, level: LogLevel, type: LogLevel = 'debug', name?: string) => {
// By default (debug) we print dimmed.
let color = c.dim;
// eslint-disable-next-line no-console
let logFn = console.log;

if (type === 'error') {
color = c.red;
// eslint-disable-next-line no-console
logFn = console.error;
} else if (type === 'warn') {
color = c.yellow;
// eslint-disable-next-line no-console
logFn = console.warn;
} else if (type === 'info') {
color = c.cyan;
// eslint-disable-next-line no-console
logFn = console.log;
}

Expand Down
43 changes: 37 additions & 6 deletions packages/core/src/plugins/build-report/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { getResolvedPath } from '@dd/core/helpers';
import {
getResolvedPath,
isFromInjection,
isInjectionFile,
isInjectionProxy,
} from '@dd/core/helpers';
import type { Logger } from '@dd/core/log';
import type { Entry, GlobalContext, Input, Output, PluginOptions } from '@dd/core/types';
import { glob } from 'glob';

import { cleanName, getAbsolutePath, getType, isInjection } from './helpers';
import { cleanName, getAbsolutePath, getType } from './helpers';

// Re-index metafile data for easier access.
const reIndexMeta = <T>(obj: Record<string, T>, cwd: string) =>
Expand Down Expand Up @@ -93,9 +98,31 @@ export const getEsbuildPlugin = (context: GlobalContext, log: Logger): PluginOpt
const metaInputsIndexed = reIndexMeta(result.metafile.inputs, cwd);
const metaOutputsIndexed = reIndexMeta(result.metafile.outputs, cwd);

// From a proxy entry point, created by our injection plugin, get the real path.
const getRealPathFromInjectionProxy = (entryPoint: string): string => {
if (!isInjectionProxy(entryPoint)) {
return entryPoint;
}

const metaInput = metaInputsIndexed[getAbsolutePath(cwd, entryPoint)];
if (!metaInput) {
return entryPoint;
}

// Get the first non-injection import.
const actualImport = metaInput.imports.find(
(imp) => !isFromInjection(imp.path),
);
if (!actualImport) {
return entryPoint;
}

return actualImport.path;
};

// Loop through inputs.
for (const [filename, input] of Object.entries(result.metafile.inputs)) {
if (isInjection(filename)) {
if (isFromInjection(filename)) {
continue;
}

Expand All @@ -121,7 +148,7 @@ export const getEsbuildPlugin = (context: GlobalContext, log: Logger): PluginOpt
// Get inputs of this output.
const inputFiles: Input[] = [];
for (const inputName of Object.keys(output.inputs)) {
if (isInjection(inputName)) {
if (isFromInjection(inputName)) {
continue;
}

Expand Down Expand Up @@ -167,7 +194,11 @@ export const getEsbuildPlugin = (context: GlobalContext, log: Logger): PluginOpt
continue;
}

const inputFile = reportInputsIndexed[getAbsolutePath(cwd, output.entryPoint!)];
// The entryPoint may have been altered by our injection plugin.
const inputFile =
reportInputsIndexed[
getAbsolutePath(cwd, getRealPathFromInjectionProxy(output.entryPoint))
];

if (inputFile) {
// In the case of "splitting: true", all the files are considered entries to esbuild.
Expand Down Expand Up @@ -216,7 +247,7 @@ export const getEsbuildPlugin = (context: GlobalContext, log: Logger): PluginOpt
// There are some exceptions we want to ignore.
const FILE_EXCEPTIONS_RX = /(<runtime>|https:|file:|data:|#)/g;
const isFileSupported = (filePath: string) => {
if (isInjection(filePath) || filePath.match(FILE_EXCEPTIONS_RX)) {
if (isInjectionFile(filePath) || filePath.match(FILE_EXCEPTIONS_RX)) {
return false;
}
return true;
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/plugins/build-report/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { isInjectionFile } from '@dd/core/helpers';
import { INJECTED_FILE } from '@dd/core/plugins/injection/constants';
import type {
BuildReport,
Expand Down Expand Up @@ -189,9 +190,6 @@ export const unserializeBuildReport = (report: SerializedBuildReport): BuildRepo
};
};

// Is the file coming from the injection plugin?
export const isInjection = (filename: string) => filename.includes(INJECTED_FILE);

const BUNDLER_SPECIFICS = ['unknown', 'commonjsHelpers.js', 'vite/preload-helper.js'];
// Make list of paths unique, remove the current file and particularities.
export const cleanReport = <T = string>(
Expand All @@ -204,7 +202,7 @@ export const cleanReport = <T = string>(
const cleanedPath = cleanPath(reportFilepath);
if (
// Don't add injections.
isInjection(reportFilepath) ||
isInjectionFile(reportFilepath) ||
// Don't add itself into it.
cleanedPath === filepath ||
// Remove common specific files injected by bundlers.
Expand Down Expand Up @@ -243,7 +241,7 @@ export const cleanPath = (filepath: string) => {

// Will only prepend the cwd if not already there.
export const getAbsolutePath = (cwd: string, filepath: string) => {
if (isInjection(filepath)) {
if (isInjectionFile(filepath)) {
return INJECTED_FILE;
}

Expand All @@ -255,7 +253,7 @@ export const getAbsolutePath = (cwd: string, filepath: string) => {

// Extract a name from a path based on the context (out dir and cwd).
export const cleanName = (context: GlobalContext, filepath: string) => {
if (isInjection(filepath)) {
if (isInjectionFile(filepath)) {
return INJECTED_FILE;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/plugins/build-report/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { isInjectionFile } from '@dd/core/helpers';
import type { Logger } from '@dd/core/log';
import type { Entry, GlobalContext, Input, Output, PluginOptions } from '@dd/core/types';

import { cleanName, cleanReport, getAbsolutePath, getType, isInjection } from './helpers';
import { cleanName, cleanReport, getAbsolutePath, getType } from './helpers';

export const getWebpackPlugin =
(context: GlobalContext, PLUGIN_NAME: string, log: Logger): PluginOptions['webpack'] =>
Expand Down Expand Up @@ -138,7 +139,7 @@ export const getWebpackPlugin =

const isModuleSupported = (module: (typeof modules)[number]) => {
if (
isInjection(getModulePath(module)) ||
isInjectionFile(getModulePath(module)) ||
// Do not report runtime modules as they are very specific to webpack.
module.moduleType === 'runtime' ||
module.name?.startsWith('(webpack)') ||
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/bundler-report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import type { GlobalContext, Options, PluginOptions } from '@dd/core/types';
import path from 'path';

const PLUGIN_NAME = 'datadog-context-plugin';
const PLUGIN_NAME = 'datadog-bundler-report-plugin';

const rollupPlugin: (context: GlobalContext) => PluginOptions['rollup'] = (context) => ({
options(options) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const getInternalPlugins = (
const toInject: ToInjectItem[] = [];
const globalContext: GlobalContext = {
auth: options.auth,
pluginNames: [],
bundler: {
name: meta.framework,
fullName: `${meta.framework}${variant}`,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/plugins/injection/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const PREPARATION_PLUGIN_NAME = 'datadog-injection-preparation-plugin';
export const PLUGIN_NAME = 'datadog-injection-plugin';
export const RESOLUTION_PLUGIN_NAME = 'datadog-injection-resolution-plugin';
export const INJECTED_FILE = '__DATADOG_INJECTION_STUB';
export const INJECTION_SUFFIX = '?datadogInjected=true';
export const DISTANT_FILE_RX = /^https?:\/\//;
Loading
Loading