Skip to content

Commit

Permalink
test: improve unit tests (electron#126)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Rieseberg <[email protected]>
  • Loading branch information
erickzhao and felixrieseberg authored Feb 24, 2023
1 parent 7bd992d commit 8eec1fa
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 76 deletions.
2 changes: 1 addition & 1 deletion bin/update-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require("dotenv-safe").config();

process.title = "update-server";

const Updates = require("..");
const Updates = require("../src/updates");
const redis = require("redis");
const { promisify } = require("util");
const ms = require("ms");
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "update.electronjs.org",
"scripts": {
"start": "nodemon bin/update-server.js",
"test": "prettier --check '**/*.js' && cross-env NODE_ENV=test nyc tap test/*.js"
"lint": "prettier --check '**/*.js'",
"test": "npm run lint && cross-env NODE_ENV=test nyc tap test/*.js"
},
"dependencies": {
"dotenv-safe": "^8.2.0",
Expand Down
32 changes: 32 additions & 0 deletions src/asset-platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { PLATFORM_ARCH } = require("./constants");

const assetPlatform = (fileName) => {
if (/.*(mac|darwin|osx).*(-arm).*\.zip/i.test(fileName)) {
return PLATFORM_ARCH.DARWIN_ARM64;
}

if (/.*(mac|darwin|osx).*\.zip/i.test(fileName) && !/arm64/.test(fileName)) {
return PLATFORM_ARCH.DARWIN_X64;
}

if (/win32-ia32/.test(fileName)) return PLATFORM_ARCH.WIN_IA32;
if (/win32-x64/.test(fileName)) return PLATFORM_ARCH.WIN_X64;
if (/win32-arm64/.test(fileName)) return PLATFORM_ARCH.WIN_ARM64;

// Special case handling: We don't know what kind of asset
// we're looking at, so it might be the default x64 windows
// asset
if (
/\.exe$/.test(fileName) &&
!/arm/.test(fileName) &&
!/ia32/.test(fileName)
) {
return PLATFORM_ARCH.WIN_X64;
}

return false;
};

module.exports = {
assetPlatform,
};
20 changes: 20 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const PLATFORM = {
WIN32: "win32",
DARWIN: "darwin",
};

const PLATFORM_ARCH = {
DARWIN_X64: "darwin-x64",
DARWIN_ARM64: "darwin-arm64",
WIN_X64: "win32-x64",
WIN_IA32: "win32-ia32",
WIN_ARM64: "win32-arm64",
};

const PLATFORM_ARCHS = Object.values(PLATFORM_ARCH);

module.exports = {
PLATFORM,
PLATFORM_ARCH,
PLATFORM_ARCHS,
};
51 changes: 7 additions & 44 deletions index.js → src/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@
"use strict";

const http = require("http");
const fetch = require("node-fetch");
const { default: fetch } = require("node-fetch");
const semver = require("semver");
const assert = require("assert");
const log = require("pino")();
const crypto = require("crypto");
const requestIp = require("request-ip");

const { assetPlatform } = require("./asset-platform");
const { PLATFORM, PLATFORM_ARCH, PLATFORM_ARCHS } = require("./constants");

const { NODE_ENV: env } = process.env;
if (env === "test") log.level = "error";

const PLATFORM = {
WIN32: "win32",
DARWIN: "darwin",
};

const PLATFORM_ARCH = {
DARWIN_X64: "darwin-x64",
DARWIN_ARM64: "darwin-arm64",
WIN_X64: "win32-x64",
WIN_IA32: "win32-ia32",
WIN_ARM64: "win32-arm64",
};
const PLATFORM_ARCHS = Object.values(PLATFORM_ARCH);

class Updates {
constructor({ token, cache } = {}) {
constructor({ token, cache }) {
assert(cache, ".cache required");
this.token = token;
this.cache = cache;
Expand All @@ -54,7 +43,7 @@ class Updates {
url: req.url,
status: res.statusCode,
ipHash: this.hashIp(requestIp.getClientIp(req)),
duration: new Date() - start,
duration: new Date().valueOf() - start.valueOf(),
},
"request"
);
Expand Down Expand Up @@ -230,6 +219,7 @@ class Updates {
if (rres.status < 400) {
const body = await rres.text();
const matches = body.match(/[^ ]*\.nupkg/gim);
assert(matches);
const nuPKG = rurl.replace("RELEASES", matches[0]);
latest[key].RELEASES = body.replace(matches[0], nuPKG);
}
Expand Down Expand Up @@ -265,33 +255,6 @@ const hasAnyAsset = (latest) => {
);
};

const assetPlatform = (fileName) => {
if (/.*(mac|darwin|osx).*(-arm).*\.zip/i.test(fileName)) {
return PLATFORM_ARCH.DARWIN_ARM64;
}

if (/.*(mac|darwin|osx).*\.zip/i.test(fileName) && !/arm64/.test(fileName)) {
return PLATFORM_ARCH.DARWIN_X64;
}

if (/win32-ia32/.test(fileName)) return PLATFORM_ARCH.WIN_IA32;
if (/win32-arm64/.test(fileName)) return PLATFORM_ARCH.WIN_ARM64;
if (/win32-x64/.test(fileName)) return PLATFORM_ARCH.WIN_X64;

// Special case handling: We don't know what kind of asset
// we're looking at, so it might be the default x64 windows
// asset
if (
/\.exe$/.test(fileName) &&
!/arm/.test(fileName) &&
!/ia32/.test(fileName)
) {
return PLATFORM_ARCH.WIN_X64;
}

return false;
};

const notFound = (res, message = "Not found") => {
res.statusCode = 404;
res.end(message);
Expand Down
42 changes: 42 additions & 0 deletions test/asset-platform.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { test } = require("tap");
const { assetPlatform } = require("../src/asset-platform");

const { PLATFORM_ARCH } = require("../src/constants");

test("assetPlatform() matches the right platform", (t) => {
const releases = [
{ name: "electron-fiddle-0.27.3-1.arm64.rpm", platform: false },
{ name: "electron-fiddle-0.27.3-1.armv7hl.rpm", platform: false },
{ name: "electron-fiddle-0.27.3-1.x86_64.rpm", platform: false },
{ name: "electron-fiddle-0.27.3-full.nupkg", platform: false },
{
name: "electron-fiddle-0.27.3-win32-arm64-setup.exe",
platform: PLATFORM_ARCH.WIN_ARM64,
},
{
name: "electron-fiddle-0.27.3-win32-ia32-setup.exe",
platform: PLATFORM_ARCH.WIN_IA32,
},
{
name: "electron-fiddle-0.27.3-win32-x64-setup.exe",
platform: PLATFORM_ARCH.WIN_X64,
},
{ name: "electron-fiddle_0.27.3_amd64.deb", platform: false },
{ name: "electron-fiddle_0.27.3_arm64.deb", platform: false },
{ name: "electron-fiddle_0.27.3_armhf.deb", platform: false },
{
name: "Electron.Fiddle-darwin-arm64-0.27.3.zip",
platform: PLATFORM_ARCH.DARWIN_ARM64,
},
{
name: "Electron.Fiddle-darwin-x64-0.27.3.zip",
platform: PLATFORM_ARCH.DARWIN_X64,
},
];

for (const release of releases) {
t.equal(assetPlatform(release.name), release.platform);
}

t.end();
});
62 changes: 32 additions & 30 deletions test/index.js → test/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { test } = require("tap");
const fetch = require("node-fetch");
const Updates = require("..");
const Updates = require("../src/updates");
const nock = require("nock");

class MemoryCache {
Expand Down Expand Up @@ -51,16 +51,16 @@ nock("https://api.github.com")
browser_download_url: "mac-arm64.zip",
},
{
name: "win.exe",
browser_download_url: "win.exe",
name: "electron-fiddle-1.0.0-win32-x64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-x64-setup.exe",
},
{
name: "win32-ia32.zip",
browser_download_url: "win32-ia32.zip",
name: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
},
{
name: "win32-arm64.zip",
browser_download_url: "win32-arm64.zip",
name: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
},
],
},
Expand Down Expand Up @@ -113,16 +113,16 @@ nock("https://api.github.com")
body: "notes",
assets: [
{
name: "win32-ia32.zip",
browser_download_url: "win32-ia32.zip",
name: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
},
{
name: "win32-x64.zip",
browser_download_url: "win32-x64.zip",
name: "electron-fiddle-1.0.0-win32-x64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-x64-setup.exe",
},
{
name: "win32-arm64.zip",
browser_download_url: "win32-arm64.zip",
name: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
},
],
},
Expand All @@ -135,16 +135,16 @@ nock("https://api.github.com")
body: "notes",
assets: [
{
name: "win32-ia32.zip",
browser_download_url: "win32-ia32.zip",
name: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
},
{
name: "win32-x64.zip",
browser_download_url: "win32-x64.zip",
name: "electron-fiddle-1.0.0-win32-x64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-x64-setup.exe",
},
{
name: "win32-arm64.zip",
browser_download_url: "win32-arm64.zip",
name: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
},
],
},
Expand All @@ -157,16 +157,16 @@ nock("https://api.github.com")
body: "notes",
assets: [
{
name: "win32-ia32.zip",
browser_download_url: "win32-ia32.zip",
name: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-ia32-setup.exe",
},
{
name: "win32-x64.zip",
browser_download_url: "win32-x64.zip",
name: "electron-fiddle-1.0.0-win32-x64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-x64-setup.exe",
},
{
name: "win32-arm64.zip",
browser_download_url: "win32-arm64.zip",
name: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-arm64-setup.exe",
},
],
},
Expand Down Expand Up @@ -322,7 +322,7 @@ test("Updates", async (t) => {
const res = await fetch(`${address}/owner/repo/win32/0.0.0`);
t.equal(res.status, 200);
const body = await res.json();
t.equal(body.url, "win.exe");
t.equal(body.url, "electron-fiddle-1.0.0-win32-x64-setup.exe");
t.ok(body.name);
}
});
Expand All @@ -332,18 +332,20 @@ test("Updates", async (t) => {
const res = await fetch(
`${address}/owner/repo-win32-zip/win32-x64/0.0.0`
);

t.equal(res.status, 200);
const body = await res.json();
t.equal(body.url, "win32-x64.zip");
t.equal(body.url, "electron-fiddle-1.0.0-win32-x64-setup.exe");
t.ok(body.name);
}

for (let i = 0; i < 2; i++) {
const res = await fetch(
`${address}/owner/repo-win32-zip/win32/0.0.0`
);
t.equal(res.status, 200);
const body = await res.json();
t.equal(body.url, "win32-x64.zip");
t.equal(body.url, "electron-fiddle-1.0.0-win32-x64-setup.exe");
t.ok(body.name);
}
});
Expand All @@ -355,7 +357,7 @@ test("Updates", async (t) => {
);
t.equal(res.status, 200);
const body = await res.json();
t.equal(body.url, "win32-arm64.zip");
t.equal(body.url, "electron-fiddle-1.0.0-win32-arm64-setup.exe");
t.ok(body.name);
}
});
Expand All @@ -367,7 +369,7 @@ test("Updates", async (t) => {
);
t.equal(res.status, 200);
const body = await res.json();
t.equal(body.url, "win32-ia32.zip");
t.equal(body.url, "electron-fiddle-1.0.0-win32-ia32-setup.exe");
t.ok(body.name);
}
});
Expand Down

0 comments on commit 8eec1fa

Please sign in to comment.