From 2b73d37c9c949096a707c31c81179ec81d19ee42 Mon Sep 17 00:00:00 2001 From: Annosha <111076986+Annosha@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:54:09 +0500 Subject: [PATCH] [Fixes # 4533] Replaced cpx2 with local test module (#5077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Pichler Co-authored-by: Raphaël Thériault <113933910+raphael-theriault-swi@users.noreply.github.com> Co-authored-by: Jakub Ziółkowski Co-authored-by: David Luna Co-authored-by: Trent Mick Co-authored-by: Marylia Gutierrez Co-authored-by: Mend Renovate Co-authored-by: Mercy Bassey <57226464+mercybassey@users.noreply.github.com> Co-authored-by: OpenTelemetry Bot <107717825+opentelemetrybot@users.noreply.github.com> --- .../package.json | 2 +- .../node/RequireInTheMiddleSingleton.test.ts | 44 +++++++++++++------ .../test-non-core-module/lib/copy-sync.js | 3 ++ .../test-non-core-module/lib/index.js | 4 ++ .../test-non-core-module/package.json | 6 +++ 5 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/copy-sync.js create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/index.js create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/package.json diff --git a/experimental/packages/opentelemetry-instrumentation/package.json b/experimental/packages/opentelemetry-instrumentation/package.json index 9add816e4c5..9637731c990 100644 --- a/experimental/packages/opentelemetry-instrumentation/package.json +++ b/experimental/packages/opentelemetry-instrumentation/package.json @@ -93,7 +93,7 @@ "babel-loader": "8.4.1", "babel-plugin-istanbul": "7.0.0", "codecov": "3.8.3", - "cpx2": "2.0.0", + "cross-var": "1.1.0", "karma": "6.4.4", "karma-chrome-launcher": "3.1.0", diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts b/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts index 75cd0ad6282..8c1be214460 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts @@ -39,8 +39,8 @@ describe('RequireInTheMiddleSingleton', () => { const onRequireFsPromisesStub = makeOnRequiresStub('fs-promises'); const onRequireCodecovStub = makeOnRequiresStub('codecov'); const onRequireCodecovLibStub = makeOnRequiresStub('codecov-lib'); - const onRequireCpxStub = makeOnRequiresStub('cpx2'); - const onRequireCpxLibStub = makeOnRequiresStub('cpx2-lib'); + const onRequireCpxStub = makeOnRequiresStub('test-non-core-module'); + const onRequireCpxLibStub = makeOnRequiresStub('test-non-core-module-lib'); before(() => { requireInTheMiddleSingleton.register('fs', onRequireFsStub); @@ -53,9 +53,12 @@ describe('RequireInTheMiddleSingleton', () => { 'codecov/lib/codecov.js', onRequireCodecovLibStub ); - requireInTheMiddleSingleton.register('cpx2', onRequireCpxStub); requireInTheMiddleSingleton.register( - 'cpx2/lib/copy-sync.js', + 'test-non-core-module', + onRequireCpxStub + ); + requireInTheMiddleSingleton.register( + 'test-non-core-module/lib/copy-sync.js', onRequireCpxLibStub ); }); @@ -120,8 +123,12 @@ describe('RequireInTheMiddleSingleton', () => { describe('non-core module', () => { describe('AND module name matches', () => { - const baseDir = path.dirname(require.resolve('codecov')); - const modulePath = path.join('codecov', 'lib', 'codecov.js'); + const baseDir = path.normalize( + path.dirname(require.resolve('codecov')) + ); + const modulePath = path.normalize( + path.join('codecov', 'lib', 'codecov.js') + ); it('should call `onRequire`', () => { const exports = require('codecov'); assert.deepStrictEqual(exports.__ritmOnRequires, ['codecov']); @@ -149,20 +156,29 @@ describe('RequireInTheMiddleSingleton', () => { describe('non-core module with sub-path', () => { describe('AND module name matches', () => { - const baseDir = path.resolve( - path.dirname(require.resolve('cpx2')), - '..' + const baseDir = path.normalize( + path.resolve( + path.dirname(require.resolve('test-non-core-module')), + '..' + ) + ); + const modulePath = path.normalize( + path.join('test-non-core-module', 'lib', 'copy-sync.js') ); - const modulePath = path.join('cpx2', 'lib', 'copy-sync.js'); it('should call `onRequire`', () => { - const exports = require('cpx2/lib/copy-sync'); + const exports = require('test-non-core-module/lib/copy-sync'); assert.deepStrictEqual(exports.__ritmOnRequires, [ - 'cpx2', - 'cpx2-lib', + 'test-non-core-module', + 'test-non-core-module-lib', ]); sinon.assert.calledWithMatch( onRequireCpxStub, - { __ritmOnRequires: ['cpx2', 'cpx2-lib'] }, + { + __ritmOnRequires: [ + 'test-non-core-module', + 'test-non-core-module-lib', + ], + }, modulePath, baseDir ); diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/copy-sync.js b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/copy-sync.js new file mode 100644 index 00000000000..97da7acc8cd --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/copy-sync.js @@ -0,0 +1,3 @@ +module.exports = function copySync() { + console.log('Mock copySync called'); +}; diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/index.js b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/index.js new file mode 100644 index 00000000000..55b19e028b2 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/lib/index.js @@ -0,0 +1,4 @@ +module.exports = { + copy: require('./lib/copy-sync') // matches the original API + }; + \ No newline at end of file diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/package.json b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/package.json new file mode 100644 index 00000000000..f09115be964 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/test-non-core-module/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-non-core-module", + "version": "0.0.1", + "description": "Local test module for require-in-the-middle singleton", + "main": "lib/index.js" + } \ No newline at end of file