From d7c53e022d27b9fc692222690ff53d0a9cbaab76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 29 Jun 2022 18:02:02 +0200 Subject: [PATCH] Fixing analytics tests (#4687) --- CHANGELOG.md | 1 + integration-tests/tests/src/node/analytics.ts | 52 +++++++------------ .../node_modules/electron/package.json | 4 ++ .../src/node/fixtures/electron/package.json | 7 +++ .../tests/src/node/fixtures/node/package.json | 4 ++ .../node_modules/react-native/package.json | 4 ++ .../node/fixtures/react-native/package.json | 7 +++ integration-tests/tests/src/typings.d.ts | 2 +- scripts/submit-analytics.js | 18 ++++--- 9 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 integration-tests/tests/src/node/fixtures/electron/node_modules/electron/package.json create mode 100644 integration-tests/tests/src/node/fixtures/electron/package.json create mode 100644 integration-tests/tests/src/node/fixtures/node/package.json create mode 100644 integration-tests/tests/src/node/fixtures/react-native/node_modules/react-native/package.json create mode 100644 integration-tests/tests/src/node/fixtures/react-native/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index b0fbd055ad..ce7d626400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ x.x.x Release notes (yyyy-MM-dd) * File format: generates Realms with format v22 (reads and upgrades file format v5 or later for non-synced Realm, upgrades file format v10 or later for synced Realms). ### Internal +* Fixed analytics tests to reflect the fact that framework versions are read from node_modules, not package.json. ([#4687](https://github.com/realm/realm-js/pull/4687)) * * * diff --git a/integration-tests/tests/src/node/analytics.ts b/integration-tests/tests/src/node/analytics.ts index 951451ee7f..1c2bd07e3f 100644 --- a/integration-tests/tests/src/node/analytics.ts +++ b/integration-tests/tests/src/node/analytics.ts @@ -23,66 +23,54 @@ import { expect } from "chai"; import { collectPlatformData } from "realm/scripts/submit-analytics"; import { readJsonSync } from "fs-extra"; +type Fixture = "node" | "react-native" | "electron"; + describe("Analytics", () => { - function resolvePath(fileName: string) { - // tests are executed in directory `environments/node` - return ["..", "..", "tests", "src", "node", fileName].join(path.sep); + function resolvePath(fixture: Fixture) { + return path.resolve(__dirname, "fixtures", fixture); } function getRealmVersion() { - // tests are executed in directory `environments/node` - const rootPath = ["..", "..", "..", "package.json"].join(path.sep); - const realmPackageJson = readJsonSync(rootPath); + const realmPath = path.resolve(__dirname, "../../../../package.json"); + const realmPackageJson = readJsonSync(realmPath); return realmPackageJson["version"]; } - it("returns the expected version", async () => { - const packageJson = { version: "1.2.3" }; - const data = await collectPlatformData(packageJson); - - // common to all cases + function expectCommon(data: Record) { expect(data["JS Analytics Version"]).equals(2); expect(data.Binding).equals("javascript"); expect(data.Language).equals("javascript"); expect(data["Host OS Type"]).equals(os.platform()); expect(data["Host OS Version"]).equals(os.release()); expect(data["Node.js version"]).equals(process.version); + expect(data["Realm Version"]).equals(getRealmVersion()); expect(data.token).equals("ce0fac19508f6c8f20066d345d360fd0"); - - // specific to package.json - expect(data.Version).equals("1.2.3"); - }); + } it("parses node.js package.json", async () => { - const packageJson = readJsonSync(resolvePath("node-package.json")); - - const data = await collectPlatformData(packageJson); - expect(data.Version).equals("1.11.1"); + const data = await collectPlatformData(resolvePath("node")); + expectCommon(data); + expect(data.Version).equals("1.2.3"); expect(data.Framework).equals("node.js"); expect(data["Framework Version"]).equals(process.version); expect(data["JavaScript Engine"]).equals("v8"); - expect(data["Realm Version"]).equals(getRealmVersion()); }); it("parses electron package.json", async () => { - const packageJson = readJsonSync(resolvePath("electron-package.json")); - - const data = await collectPlatformData(packageJson); - expect(data.Version).equals("11.1.1"); + const data = await collectPlatformData(resolvePath("electron")); + expectCommon(data); + expect(data.Version).equals("1.2.3"); expect(data.Framework).equals("electron"); - expect(data["Framework Version"]).equals("^16.0.4"); + expect(data["Framework Version"]).equals("1.0.1"); expect(data["JavaScript Engine"]).equals("v8"); - expect(data["Realm Version"]).equals(getRealmVersion()); }); it("parses rn package.json", async () => { - const packageJson = readJsonSync(resolvePath("rn-package.json")); - - const data = await collectPlatformData(packageJson); - expect(data.Version).equals("0.0.1"); + const data = await collectPlatformData(resolvePath("react-native")); + expectCommon(data); + expect(data.Version).equals("1.2.3"); expect(data.Framework).equals("react-native"); - expect(data["Framework Version"]).equals("0.64.2"); + expect(data["Framework Version"]).equals("1.0.1"); expect(data["JavaScript Engine"]).equals("unknown"); - expect(data["Realm Version"]).equals(getRealmVersion()); }); }); diff --git a/integration-tests/tests/src/node/fixtures/electron/node_modules/electron/package.json b/integration-tests/tests/src/node/fixtures/electron/node_modules/electron/package.json new file mode 100644 index 0000000000..e2a8f03a41 --- /dev/null +++ b/integration-tests/tests/src/node/fixtures/electron/node_modules/electron/package.json @@ -0,0 +1,4 @@ +{ + "name": "electron", + "version": "1.0.1" +} \ No newline at end of file diff --git a/integration-tests/tests/src/node/fixtures/electron/package.json b/integration-tests/tests/src/node/fixtures/electron/package.json new file mode 100644 index 0000000000..d1611ce25b --- /dev/null +++ b/integration-tests/tests/src/node/fixtures/electron/package.json @@ -0,0 +1,7 @@ +{ + "name": "fake-electron-package", + "version": "1.2.3", + "devDependencies": { + "electron": "^1.0.0" + } +} \ No newline at end of file diff --git a/integration-tests/tests/src/node/fixtures/node/package.json b/integration-tests/tests/src/node/fixtures/node/package.json new file mode 100644 index 0000000000..9a729548bd --- /dev/null +++ b/integration-tests/tests/src/node/fixtures/node/package.json @@ -0,0 +1,4 @@ +{ + "name": "fake-node-package", + "version": "1.2.3" +} \ No newline at end of file diff --git a/integration-tests/tests/src/node/fixtures/react-native/node_modules/react-native/package.json b/integration-tests/tests/src/node/fixtures/react-native/node_modules/react-native/package.json new file mode 100644 index 0000000000..fb9a6eb054 --- /dev/null +++ b/integration-tests/tests/src/node/fixtures/react-native/node_modules/react-native/package.json @@ -0,0 +1,4 @@ +{ + "name": "react-native", + "version": "1.0.1" +} \ No newline at end of file diff --git a/integration-tests/tests/src/node/fixtures/react-native/package.json b/integration-tests/tests/src/node/fixtures/react-native/package.json new file mode 100644 index 0000000000..4030f518ca --- /dev/null +++ b/integration-tests/tests/src/node/fixtures/react-native/package.json @@ -0,0 +1,7 @@ +{ + "name": "fake-react-native-package", + "version": "1.2.3", + "dependencies": { + "react-native": "^1.0.0" + } +} \ No newline at end of file diff --git a/integration-tests/tests/src/typings.d.ts b/integration-tests/tests/src/typings.d.ts index 97688e4420..449bb30654 100644 --- a/integration-tests/tests/src/typings.d.ts +++ b/integration-tests/tests/src/typings.d.ts @@ -87,5 +87,5 @@ declare module "*.json" { } declare module "realm/scripts/submit-analytics" { - export function collectPlatformData(packageJson: Record): Promise>; + export function collectPlatformData(packagePath: string): Promise>; } diff --git a/scripts/submit-analytics.js b/scripts/submit-analytics.js index 27ef380da9..ba134e7b82 100644 --- a/scripts/submit-analytics.js +++ b/scripts/submit-analytics.js @@ -92,8 +92,8 @@ function getProjectRoot() { * * @returns package.json as a JavaScript object */ -function getPackageJson() { - const packageJson = getProjectRoot() + path.sep + "package.json"; +function getPackageJson(packagePath) { + const packageJson = path.resolve(packagePath, "package.json"); return fse.readJsonSync(packageJson); } @@ -127,7 +127,7 @@ function getRealmVersion() { * @param {Object} packageJson The app's package.json parsed as an object * @returns {Object} Analytics payload */ -async function collectPlatformData(packageJson) { +async function collectPlatformData(packagePath = getProjectRoot()) { const os = require("os"); const { machineId } = require("node-machine-id"); @@ -143,6 +143,8 @@ async function collectPlatformData(packageJson) { let frameworkVersion = process.version; let jsEngine = "v8"; + const packageJson = getPackageJson(packagePath); + if (packageJson.dependencies && packageJson.dependencies["react-native"]) { framework = "react-native"; frameworkVersion = packageJson.dependencies["react-native"]; @@ -154,7 +156,8 @@ async function collectPlatformData(packageJson) { if (framework === "react-native") { try { - const podfile = fs.readFileSync(getProjectRoot() + "/ios/Podfile", "utf8"); // no need to use path.sep as we are on MacOS + const podfilePath = path.join(packagePath, "ios/Podfile"); + const podfile = fs.readFileSync(podfilePath, "utf8"); if (/hermes_enabled.*true/.test(podfile)) { jsEngine = "hermes"; } else { @@ -166,7 +169,7 @@ async function collectPlatformData(packageJson) { } try { - const rnPath = [getProjectRoot(), "node_modules", "react-native", "package.json"].join(path.sep); + const rnPath = path.join(packagePath, "node_modules", "react-native", "package.json"); const rnPackageJson = JSON.parse(fs.readFileSync(rnPath, "utf-8")); frameworkVersion = rnPackageJson["version"]; } catch (err) { @@ -184,7 +187,7 @@ async function collectPlatformData(packageJson) { } if (framework === "electron") { try { - const electronPath = [getProjectRoot(), "node_modules", "electron", "package.json"].join(path.sep); + const electronPath = path.join(packagePath, "node_modules", "electron", "package.json"); const electronPackageJson = JSON.parse(fs.readFileSync(electronPath, "utf-8")); frameworkVersion = electronPackageJson["version"]; } catch (err) { @@ -237,8 +240,7 @@ async function dispatchAnalytics(payload) { } async function submitAnalytics(dryRun) { - const packageJson = getPackageJson(); - const data = await collectPlatformData(packageJson); + const data = await collectPlatformData(); const payload = { webHook: { event: "install",