From bf4efc745791bc30b966f3a4953f3d271dc6b299 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Wed, 6 Mar 2024 16:11:30 -0800 Subject: [PATCH] [Azure Monitor OpenTelemetry] Use dev-tool to run tests (#28805) ### Packages impacted by this PR @azure/monitor-opentelemetry @azure/monitor-opentelemetry-exporter --- common/config/rush/pnpm-lock.yaml | 9 ++++--- .../package.json | 15 ++++++----- .../test/internal/metricUtil.test.ts | 2 +- .../test/internal/spanUtils.test.ts | 2 +- .../monitor-opentelemetry/package.json | 22 ++++++++-------- .../src/shared/jsonConfig.ts | 15 ++++++----- .../test/internal/unit/shared/config.test.ts | 5 +--- .../internal/unit/shared/jsonConfig.test.ts | 25 +++++-------------- 8 files changed, 39 insertions(+), 56 deletions(-) diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index 5f50893f5d6..a394cd7a190 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -2285,8 +2285,8 @@ packages: - '@types/node' dev: false - /@microsoft/applicationinsights-web-snippet@1.1.1: - resolution: {integrity: sha512-uITh+P/cfN+nkUfazVt9paML6rjc6H/VWdw0jb4PUj7JG1w4hamjxkYWFvDjMZfhZ75tC4kgepPgOeG2i610Qg==} + /@microsoft/applicationinsights-web-snippet@1.0.1: + resolution: {integrity: sha512-2IHAOaLauc8qaAitvWS+U931T+ze+7MNWrDHY47IENP5y2UA0vqJDu67kWZDdpCN1fFC77sfgfB+HV7SrKshnQ==} dev: false /@microsoft/tsdoc-config@0.16.2: @@ -20275,13 +20275,13 @@ packages: dev: false file:projects/monitor-opentelemetry.tgz: - resolution: {integrity: sha512-znsAUq8KrMWEZpRYoUUwSamsOFAjKPrQJc2leRHM/QcMAO3ghlYvgfgAovlsaaQ8jQ+Q7V1uTGBIu77mAy2Nrw==, tarball: file:projects/monitor-opentelemetry.tgz} + resolution: {integrity: sha512-sc28ib7qotd1/rGYwlwDlfVxgvIi1DsFJT1pUvM3kaPmfN0a2hzSpFZyUuqejK8op4BtMpTS6tfzesAo4oL0aQ==, tarball: file:projects/monitor-opentelemetry.tgz} name: '@rush-temp/monitor-opentelemetry' version: 0.0.0 dependencies: '@azure/functions': 3.5.1 '@microsoft/api-extractor': 7.41.0(@types/node@18.19.18) - '@microsoft/applicationinsights-web-snippet': 1.1.1 + '@microsoft/applicationinsights-web-snippet': 1.0.1 '@opentelemetry/api': 1.7.0 '@opentelemetry/api-logs': 0.48.0 '@opentelemetry/core': 1.21.0(@opentelemetry/api@1.7.0) @@ -20305,6 +20305,7 @@ packages: '@types/node': 18.19.18 '@types/sinon': 17.0.3 c8: 8.0.1 + cross-env: 7.0.3 dotenv: 16.4.5 eslint: 8.57.0 eslint-plugin-node: 11.1.0(eslint@8.57.0) diff --git a/sdk/monitor/monitor-opentelemetry-exporter/package.json b/sdk/monitor/monitor-opentelemetry-exporter/package.json index c6627e32b5c..5e5bf81850c 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/package.json +++ b/sdk/monitor/monitor-opentelemetry-exporter/package.json @@ -10,9 +10,9 @@ "scripts": { "clean": "rimraf dist-esm types dist", "build:browser": "echo skipped", - "build:test": "echo skipped", "build:node": "tsc -p . && dev-tool run bundle --browser-test=false", - "build:samples": "dev-tool samples publish --force", + "build:test": "tsc -p . && dev-tool run bundle --browser-test=false", + "build:samples": "echo Obsolete.", "build": "npm run build:node && npm run build:browser && api-extractor run --local", "check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "execute:samples": "dev-tool samples run samples-dev", @@ -21,17 +21,16 @@ "generate:client": "autorest --typescript ./swagger/README.md", "lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]", "lint": "eslint package.json api-extractor.json src test --ext .ts -f html -o telemetry-exporter-lintReport.html || exit 0", - "test": "npm run test:node && npm run test:browser", - "test:node": "npm run unit-test:node", + "test": "npm run clean && npm run build:test && npm run unit-test", + "test:node": "npm run clean && npm run build:test && npm run unit-test:node", "test:browser": "npm run unit-test:browser", "unit-test:browser": "echo skipped", - "unit-test:node": "mocha -r ../../../common/tools/esm-workaround.js -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml --timeout 1200000 --full-trace \"dist-esm/test/internal/**/*.test.js\"", - "unit-test:node:debug": "mocha --inspect-brk -r ../../../common/tools/esm-workaround.js -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml --timeout 1200000 --full-trace \"dist-esm/test/internal/**/*.test.js\"", + "unit-test:node": "dev-tool run test:node-ts-input --no-test-proxy=true -- --timeout 1200000 \"test/internal/**/*.test.ts\"", + "unit-test:node:debug": "dev-tool run test:node-ts-input --no-test-proxy=true -- --inspect-brk \"test/internal/**/*.test.ts\"", "unit-test:node:no-timeout": "echo skipped", "unit-test": "npm run unit-test:node && npm run unit-test:browser", - "functional-test": "mocha -r ../../../common/tools/esm-workaround.js -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml --timeout 1200000 --full-trace \"dist-esm/test/internal/functional/*.test.js\"", "integration-test:browser": "echo skipped", - "integration-test:node": "npm run functional-test", + "integration-test:node": "dev-tool run test:node-ts-input --no-test-proxy=true -- --timeout 1200000 \"test/internal/functional/**/*.test.ts\"", "integration-test": "npm run integration-test:node && npm run integration-test:browser", "report": "nyc report --reporter=json", "test-opentelemetry-versions": "node test-opentelemetry-versions.js 2>&1", diff --git a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/metricUtil.test.ts b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/metricUtil.test.ts index 671d9dc84b1..3abc478ded3 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/metricUtil.test.ts +++ b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/metricUtil.test.ts @@ -26,7 +26,7 @@ import { Tags } from "../../src/types"; import { Context, getInstance } from "../../src/platform"; const context = getInstance(); -const packageJsonPath = path.resolve(__dirname, "../../../", "./package.json"); +const packageJsonPath = path.resolve(__dirname, "../../", "./package.json"); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); let testMetrics: ResourceMetrics; diff --git a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts index 1a110afeab0..3e4e07ed44e 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts +++ b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts @@ -32,7 +32,7 @@ const tracerProviderConfig: TracerConfig = { }; const tracer = new BasicTracerProvider(tracerProviderConfig).getTracer("default"); -const packageJsonPath = path.resolve(__dirname, "../../../", "./package.json"); +const packageJsonPath = path.resolve(__dirname, "../../", "./package.json"); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); function assertEnvelope( diff --git a/sdk/monitor/monitor-opentelemetry/package.json b/sdk/monitor/monitor-opentelemetry/package.json index 8f70e5f9356..c02dfd7c06e 100644 --- a/sdk/monitor/monitor-opentelemetry/package.json +++ b/sdk/monitor/monitor-opentelemetry/package.json @@ -8,30 +8,28 @@ "module": "dist-esm/src/index.js", "types": "types/monitor-opentelemetry.d.ts", "scripts": { - "clean": "rimraf dist-esm types dist", + "build:samples": "echo Obsolete.", "build:browser": "echo skipped", - "build:test": "echo skipped", "build:node": "tsc -p . && dev-tool run bundle --browser-test=false", - "build:samples": "dev-tool samples publish --force", + "build:test": "tsc -p . && dev-tool run bundle --browser-test=false", "build": "npm run build:node && npm run build:browser && api-extractor run --local", "check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", + "clean": "rimraf dist-esm types dist", "execute:samples": "dev-tool samples run samples-dev", "extract-api": "tsc -p . && api-extractor run --local", "format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "generate:client": "autorest --typescript ./swagger/README.md", "lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]", "lint": "eslint package.json api-extractor.json src test --ext .ts -f html -o telemetry-lintReport.html || exit 0", - "test": "npm run test:node && npm run test:browser", - "test:node": "npm run unit-test:node", + "test:node": "npm run clean && npm run build:test && npm run unit-test:node", + "test": "npm run clean && npm run build:test && npm run unit-test", "test:browser": "npm run unit-test:browser", "unit-test:browser": "echo skipped", - "unit-test:node": "mocha -r ../../../common/tools/esm-workaround -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml --timeout 1200000 --full-trace \"dist-esm/test/internal/unit/**/*.test.js\"", - "unit-test:node:debug": "mocha --inspect-brk -r ../../../common/tools/esm-workaround -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml --timeout 1200000 --full-trace \"dist-esm/test/internal/unit/**/*.test.js\"", - "unit-test:node:no-timeout": "echo skipped", + "unit-test:node": "dev-tool run test:node-ts-input --no-test-proxy=true -- \"test/internal/unit/**/*.test.ts\"", + "unit-test:node:debug": "dev-tool run test:node-ts-input --no-test-proxy=true -- --inspect-brk \"test/internal/unit/**/*.test.ts\"", "unit-test": "npm run unit-test:node && npm run unit-test:browser", - "functional-test": "mocha -r ../../../common/tools/esm-workaround -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml --timeout 1200000 --full-trace \"dist-esm/test/internal/functional/*.test.js\"", "integration-test:browser": "echo skipped", - "integration-test:node": "npm run functional-test", + "integration-test:node": "dev-tool run test:node-ts-input --no-test-proxy=true -- \"test/internal/functional/**/*.test.ts\"", "integration-test": "npm run integration-test:node && npm run integration-test:browser", "report": "nyc report --reporter=json", "test-opentelemetry-versions": "node test-opentelemetry-versions.js 2>&1", @@ -68,6 +66,7 @@ "devDependencies": { "@azure/dev-tool": "^1.0.0", "@azure/eslint-plugin-azure-sdk": "^3.0.0", + "@azure/test-utils": "^1.0.0", "@microsoft/api-extractor": "^7.31.1", "@types/mocha": "^10.0.0", "@types/node": "^18.0.0", @@ -78,6 +77,7 @@ "mocha": "^10.0.0", "nock": "^12.0.3", "c8": "^8.0.0", + "cross-env": "^7.0.3", "rimraf": "^3.0.0", "sinon": "^17.0.0", "ts-node": "^10.0.0", @@ -111,7 +111,7 @@ "@opentelemetry/sdk-trace-node": "^1.21.0", "@opentelemetry/semantic-conventions": "^1.21.0", "tslib": "^2.2.0", - "@microsoft/applicationinsights-web-snippet": "^1.0.1", + "@microsoft/applicationinsights-web-snippet": "1.0.1", "@opentelemetry/resource-detector-azure": "^0.2.4" }, "sideEffects": false, diff --git a/sdk/monitor/monitor-opentelemetry/src/shared/jsonConfig.ts b/sdk/monitor/monitor-opentelemetry/src/shared/jsonConfig.ts index 6c6d3f3fc3d..9e0fc0c8553 100644 --- a/sdk/monitor/monitor-opentelemetry/src/shared/jsonConfig.ts +++ b/sdk/monitor/monitor-opentelemetry/src/shared/jsonConfig.ts @@ -36,6 +36,8 @@ export class JsonConfig implements AzureMonitorOpenTelemetryOptions { private static _instance: JsonConfig; + private _tempDir: string; + /** Get Singleton instance */ public static getInstance() { if (!JsonConfig._instance) { @@ -48,11 +50,8 @@ export class JsonConfig implements AzureMonitorOpenTelemetryOptions { * Initializes a new instance of the JsonConfig class. */ constructor() { - this._loadJsonFile(); - } - - private _loadJsonFile() { let jsonString = ""; + this._tempDir = ""; const contentJsonConfig = process.env[ENV_CONTENT]; // JSON string added directly in env variable if (contentJsonConfig) { @@ -62,17 +61,17 @@ export class JsonConfig implements AzureMonitorOpenTelemetryOptions { else { let configFileName = "applicationinsights.json"; let rootPath = path.join(__dirname, "../../../"); // Root of folder (__dirname = ../dist-esm/src) - let tempDir = path.join(rootPath, configFileName); // default + this._tempDir = path.join(rootPath, configFileName); // default let configFile = process.env[ENV_CONFIGURATION_FILE]; if (configFile) { if (path.isAbsolute(configFile)) { - tempDir = configFile; + this._tempDir = configFile; } else { - tempDir = path.join(rootPath, configFile); // Relative path to applicationinsights folder + this._tempDir = path.join(rootPath, configFile); // Relative path to applicationinsights folder } } try { - jsonString = fs.readFileSync(tempDir, "utf8"); + jsonString = fs.readFileSync(this._tempDir, "utf8"); } catch (err) { Logger.getInstance().info("Failed to read JSON config file: ", err); } diff --git a/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/config.test.ts b/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/config.test.ts index c944b5b831e..4b2e68dfd7b 100644 --- a/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/config.test.ts +++ b/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/config.test.ts @@ -34,10 +34,7 @@ describe("Library/Config", () => { it("merge JSON config", () => { (JsonConfig["_instance"] as any) = undefined; const env = <{ [id: string]: string }>{}; - const customConfigJSONPath = path.resolve( - __dirname, - "../../../../../test/internal/unit/shared/config.json", - ); + const customConfigJSONPath = path.resolve(__dirname, "config.json"); env["APPLICATIONINSIGHTS_CONFIGURATION_FILE"] = customConfigJSONPath; // Load JSON config process.env = env; const config = new InternalConfig(); diff --git a/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/jsonConfig.test.ts b/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/jsonConfig.test.ts index 1f9bc414e05..adb7efcc099 100644 --- a/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/jsonConfig.test.ts +++ b/sdk/monitor/monitor-opentelemetry/test/internal/unit/shared/jsonConfig.test.ts @@ -3,7 +3,6 @@ import * as assert from "assert"; import * as sinon from "sinon"; -import * as fs from "fs"; import * as path from "path"; import { JsonConfig } from "../../../../src/shared/jsonConfig"; @@ -28,20 +27,14 @@ describe("Json Config", () => { describe("config path", () => { it("Default file path", () => { - const fileSpy = sandbox.spy(fs, "readFileSync"); const config = JsonConfig.getInstance(); - config["_loadJsonFile"](); - assert.ok(fileSpy.called); - const defaultPath = path.resolve(process.cwd(), "applicationinsights.json"); - assert.deepStrictEqual(fileSpy.args[0][0], defaultPath); + let defaultPath = path.join(process.cwd(), "../", "applicationinsights.json"); + assert.deepStrictEqual(config["_tempDir"], defaultPath); }); it("Absolute file path", () => { const env = <{ [id: string]: string }>{}; - const customConfigJSONPath = path.resolve( - __dirname, - "../../../../../test/internal/unit/shared/config.json", - ); + const customConfigJSONPath = path.resolve(__dirname, "config.json"); env["APPLICATIONINSIGHTS_CONFIGURATION_FILE"] = customConfigJSONPath; process.env = env; const config = JsonConfig.getInstance(); @@ -53,7 +46,7 @@ describe("Json Config", () => { it("Relative file path", () => { const env = <{ [id: string]: string }>{}; - const customConfigJSONPath = "./test/internal/unit/shared/config.json"; + const customConfigJSONPath = "monitor-opentelemetry/test/internal/unit/shared/config.json"; env["APPLICATIONINSIGHTS_CONFIGURATION_FILE"] = customConfigJSONPath; process.env = env; const config = JsonConfig.getInstance(); @@ -67,10 +60,7 @@ describe("Json Config", () => { describe("configuration values", () => { it("Should take configurations from JSON config file", () => { const env = <{ [id: string]: string }>{}; - const customConfigJSONPath = path.resolve( - __dirname, - "../../../../../test/internal/unit/shared/config.json", - ); + const customConfigJSONPath = path.resolve(__dirname, "config.json"); env["APPLICATIONINSIGHTS_CONFIGURATION_FILE"] = customConfigJSONPath; process.env = env; const config = JsonConfig.getInstance(); @@ -103,10 +93,7 @@ describe("Json Config", () => { it("Should take configurations from JSON config file over environment variables if both are configured", () => { const env = <{ [id: string]: string }>{}; - const customConfigJSONPath = path.resolve( - __dirname, - "../../../../../test/internal/unit/shared/config.json", - ); + const customConfigJSONPath = path.resolve(__dirname, "config.json"); env["APPLICATIONINSIGHTS_CONFIGURATION_FILE"] = customConfigJSONPath; env["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "TestConnectionString"; process.env = env;