-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(baseplugin): add internalfilesloader (#240)
* feat(baseplugin): add internalfilesloader * refactor: remove new ctor * refactor: baseplugin internal file loader * test: don't browser test internal files loading * chore: add todo issue * refactor: cast modules as unknown isntead of any * chore: fix linting
- Loading branch information
1 parent
ede7886
commit d6f669a
Showing
8 changed files
with
203 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { BasePlugin, NoopTracer, NoopLogger } from '../../src'; | ||
import * as assert from 'assert'; | ||
import * as path from 'path'; | ||
import * as types from './fixtures/test-package/foo/bar/internal'; | ||
|
||
const tracer = new NoopTracer(); | ||
const logger = new NoopLogger(); | ||
|
||
describe('BasePlugin', () => { | ||
describe('internalFilesLoader', () => { | ||
it('should load internally exported files', () => { | ||
const testPackage = require('./fixtures/test-package'); | ||
const plugin = new TestPlugin(); | ||
assert.doesNotThrow(() => { | ||
plugin.enable(testPackage, tracer, logger); | ||
}); | ||
|
||
// @TODO: https://github.com/open-telemetry/opentelemetry-js/issues/285 | ||
if (typeof process !== 'undefined' && process.release.name === 'node') { | ||
assert.ok(plugin['_internalFilesExports']); | ||
assert.strictEqual( | ||
(plugin['_internalFilesExports'] | ||
.internal as typeof types).internallyExportedFunction(), | ||
true | ||
); | ||
assert.strictEqual( | ||
plugin['_internalFilesExports'].expectUndefined, | ||
undefined | ||
); | ||
assert.strictEqual( | ||
(plugin['_moduleExports']![ | ||
'externallyExportedFunction' | ||
] as Function)(), | ||
true | ||
); | ||
} else { | ||
assert.ok(true, 'Internal file loading is not tested in the browser'); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
class TestPlugin extends BasePlugin<{ [key: string]: Function }> { | ||
readonly moduleName = 'test-package'; | ||
readonly version = '0.0.1'; | ||
readonly _basedir = basedir; | ||
|
||
protected readonly _internalFilesList = { | ||
'0.0.1': { | ||
internal: 'foo/bar/internal.js', | ||
}, | ||
'^1.0.0': { | ||
expectUndefined: 'foo/bar/internal.js', | ||
}, | ||
}; | ||
|
||
protected patch(): { [key: string]: Function } { | ||
return this._moduleExports; | ||
} | ||
protected unpatch(): void {} | ||
} | ||
|
||
const basedir = path.dirname(require.resolve('./fixtures/test-package')); |
1 change: 1 addition & 0 deletions
1
packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare function internallyExportedFunction(): boolean; |
4 changes: 4 additions & 0 deletions
4
packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.internallyExportedFunction = function internallyExportedFunction() { | ||
return true; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/opentelemetry-core/test/trace/fixtures/test-package/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.externallyExportedFunction = function externallyExportedFunction() { | ||
return true; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/opentelemetry-core/test/trace/fixtures/test-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "test-package", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters