Skip to content

Commit

Permalink
Fixing analytics tests (#4687)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen authored and takameyer committed Jun 30, 2022
1 parent df74ce8 commit 327fcd7
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
* <Either mention core version or upgrade>
* <Using Realm Core vX.Y.Z>
* <Upgraded Realm Core from vX.Y.Z to vA.B.C>
Expand Down
52 changes: 20 additions & 32 deletions integration-tests/tests/src/node/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>) {
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());
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "fake-electron-package",
"version": "1.2.3",
"devDependencies": {
"electron": "^1.0.0"
}
}
4 changes: 4 additions & 0 deletions integration-tests/tests/src/node/fixtures/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "fake-node-package",
"version": "1.2.3"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "fake-react-native-package",
"version": "1.2.3",
"dependencies": {
"react-native": "^1.0.0"
}
}
2 changes: 1 addition & 1 deletion integration-tests/tests/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ declare module "*.json" {
}

declare module "realm/scripts/submit-analytics" {
export function collectPlatformData(packageJson: Record<string, unknown>): Promise<Record<string, unknown>>;
export function collectPlatformData(packagePath: string): Promise<Record<string, unknown>>;
}
18 changes: 10 additions & 8 deletions scripts/submit-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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");

Expand All @@ -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"];
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 327fcd7

Please sign in to comment.