|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict' |
| 3 | + |
| 4 | +/* eslint-disable no-console */ |
| 5 | + |
| 6 | +// Black-box coverage for the vendored flagging provider under webpack. Unit tests on |
| 7 | +// `server-sdk-bridge.js` fake the real `@openfeature/server-sdk` emitter in plain JS and |
| 8 | +// never touch a bundler, so they cannot catch a regression in the generic bundler |
| 9 | +// instrumentation mechanism (`modulesOfInterest` / `dd-trace:bundler:load`) that the |
| 10 | +// `openfeature-server-sdk` instrumentation relies on to see the app's inlined require of |
| 11 | +// `@openfeature/server-sdk` -- deliberately left out of `externals` below so the plugin has |
| 12 | +// to intercept it, the same as a real customer bundle would. This builds and runs |
| 13 | +// `openfeature-app.js`, which fails unless the bundled provider is real AND the bridged |
| 14 | +// emitter actually fires. |
| 15 | + |
| 16 | +const fs = require('fs') |
| 17 | +const path = require('path') |
| 18 | +const assert = require('assert') |
| 19 | +const { execFileSync } = require('child_process') |
| 20 | +const webpack = require('webpack') |
| 21 | +const DatadogWebpackPlugin = require('../../webpack') // dd-trace/webpack |
| 22 | +const experiments = require('./webpack-experiments') |
| 23 | + |
| 24 | +const OUTFILE = path.join(__dirname, 'openfeature-out.js') |
| 25 | + |
| 26 | +function build () { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + webpack({ |
| 29 | + mode: 'development', |
| 30 | + entry: path.join(__dirname, 'openfeature-app.js'), |
| 31 | + target: 'node', |
| 32 | + externalsType: 'commonjs', |
| 33 | + ...(experiments && { experiments }), |
| 34 | + output: { filename: path.basename(OUTFILE), path: path.dirname(OUTFILE), hashFunction: 'sha256' }, |
| 35 | + externals: [ |
| 36 | + 'diagnostics_channel', |
| 37 | + 'pg', 'mysql2', 'better-sqlite3', 'sqlite3', 'mysql', 'oracledb', 'pg-query-stream', 'tedious', |
| 38 | + '@yaacovcr/transform', |
| 39 | + '@datadog/native-appsec', '@datadog/native-iast-taint-tracking', '@datadog/native-metrics', |
| 40 | + '@datadog/pprof', '@datadog/libdatadog', |
| 41 | + ], |
| 42 | + plugins: [new DatadogWebpackPlugin()], |
| 43 | + }, (err, stats) => { |
| 44 | + if (err) return reject(err) |
| 45 | + if (stats.hasErrors()) return reject(new Error(stats.toString({ errors: true }))) |
| 46 | + resolve() |
| 47 | + }) |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +async function main () { |
| 52 | + try { |
| 53 | + await build() |
| 54 | + |
| 55 | + const runOutput = execFileSync(process.execPath, [OUTFILE], { encoding: 'utf8' }) |
| 56 | + assert( |
| 57 | + runOutput.includes('PROVIDER_OK'), |
| 58 | + `bundled app did not load a working OpenFeature provider:\n${runOutput}` |
| 59 | + ) |
| 60 | + |
| 61 | + console.log('ok') |
| 62 | + } finally { |
| 63 | + fs.rmSync(OUTFILE, { force: true }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +main().catch((error) => { |
| 68 | + console.error(error) |
| 69 | + process.exitCode = 1 |
| 70 | +}) |
0 commit comments