Skip to content

Commit

Permalink
Fix the incorrect Windows behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanrainer committed Aug 16, 2024
1 parent 07f74a8 commit d2f443b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
20 changes: 11 additions & 9 deletions installers/npm/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,39 @@ const supportedPlatforms = [
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-pc-windows-msvc",
BINARY_NAME: `${name}-${version}.exe`,
RAW_NAME: `${name}.exe`
},
{
TYPE: "Linux",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-unknown-linux-gnu",
BINARY_NAME: `${name}-${version}`,
RAW_NAME: `${name}`
},
{
TYPE: "Linux",
ARCHITECTURE: "arm64",
RUST_TARGET: "aarch64-unknown-linux-gnu",
BINARY_NAME: `${name}-${version}`,
RAW_NAME: `${name}`
},
{
TYPE: "Darwin",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-apple-darwin",
BINARY_NAME: `${name}-${version}`,
RAW_NAME: `${name}`
},
{
TYPE: "Darwin",
ARCHITECTURE: "arm64",
RUST_TARGET: "aarch64-apple-darwin",
BINARY_NAME: `${name}-${version}`,
RAW_NAME: `${name}`
},
];

const getPlatform = () => {
const type = os.type();
const architecture = os.arch();

const getPlatform = (type = os.type(), architecture = os.arch()) => {
for (let supportedPlatform of supportedPlatforms) {
if (
type === supportedPlatform.TYPE &&
Expand Down Expand Up @@ -102,7 +104,7 @@ const getPlatform = () => {

/*! Copyright (c) 2019 Avery Harnish - MIT License */
class Binary {
constructor(name, url, installDirectory) {
constructor(name, raw_name, url, installDirectory) {
let errors = [];
if (typeof url !== "string") {
errors.push("url must be a string");
Expand Down Expand Up @@ -132,6 +134,7 @@ class Binary {
}
this.url = url;
this.name = name;
this.raw_name = raw_name;
this.installDirectory = installDirectory;

if (!existsSync(this.installDirectory)) {
Expand Down Expand Up @@ -176,7 +179,7 @@ class Binary {
});
})
.then(() => {
fs.renameSync(join(this.installDirectory, name), this.binaryPath);
fs.renameSync(join(this.installDirectory, this.raw_name), this.binaryPath);
if (!suppressLogs) {
console.error(`${this.name} has been installed!`);
}
Expand Down Expand Up @@ -212,8 +215,7 @@ class Binary {
}
}

const getBinary = (overrideInstallDirectory) => {
const platform = getPlatform();
const getBinary = (overrideInstallDirectory, platform = getPlatform()) => {
const download_host = process.env.npm_config_apollo_rover_download_host || process.env.APOLLO_ROVER_DOWNLOAD_HOST || 'https://rover.apollo.dev'
// the url for this binary is constructed from values in `package.json`
// https://rover.apollo.dev/tar/rover/x86_64-unknown-linux-gnu/v0.4.8
Expand All @@ -224,7 +226,7 @@ const getBinary = (overrideInstallDirectory) => {
if (overrideInstallDirectory != null && overrideInstallDirectory !== "") {
installDirectory = overrideInstallDirectory
}
let binary = new Binary(platform.BINARY_NAME, url, installDirectory);
let binary = new Binary(platform.BINARY_NAME, platform.RAW_NAME, url, installDirectory);

// setting this allows us to extract supergraph plugins to the proper directory
// the variable itself is read in Rust code
Expand Down
27 changes: 26 additions & 1 deletion installers/npm/tests/binary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ const fs = require("node:fs");
const crypto = require("node:crypto");
const MockAdapter = require("axios-mock-adapter");
const axios = require("axios");
const {getPlatform} = require("../binary");

var mock = new MockAdapter(axios);
mock.onGet(new RegExp("https://rover\.apollo\.dev.*")).reply(function (_) {
mock.onGet(new RegExp("https://rover\.apollo\.dev/tar/rover/x86_64-pc-windows-msvc/.*")).reply(function (_) {
return [
200,
fs.createReadStream(
path.join(__dirname, "fake_tarballs", "rover-fake-windows.tar.gz"),
),
];
});
mock.onGet(new RegExp("https://rover\.apollo\.dev/tar/rover/.*")).reply(function (_) {
return [
200,
fs.createReadStream(
Expand All @@ -17,6 +26,7 @@ mock.onGet(new RegExp("https://rover\.apollo\.dev.*")).reply(function (_) {
];
});


test("getBinary should be created with correct name and URL", () => {
fs.mkdtempSync(path.join(os.tmpdir(), "rover-tests-"));
const bin = binary.getBinary(os.tmpdir());
Expand Down Expand Up @@ -129,6 +139,21 @@ test("install renames binary properly", async () => {
).toHaveLength(1);
});

test("install renames binary properly (Windows)", async () => {
// Establish temporary directory
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "rover-tests-"));
// Create a Binary object
const bin = binary.getBinary(directory, getPlatform("Windows_NT", "x64"));
const directory_entries = await bin.install({}, true).then(async () => {
return fs.readdirSync(directory, { withFileTypes: true });
});
expect(
directory_entries.filter(
(d) => d.isFile() && d.name === `rover-${pjson.version}.exe`,
),
).toHaveLength(1);
});

test("install adds a new binary if another version exists", async () => {
// Create the temporary directory
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "rover-tests-"));
Expand Down
Binary file not shown.

0 comments on commit d2f443b

Please sign in to comment.