Skip to content

Commit

Permalink
refactor assetFileName and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Feb 1, 2025
1 parent eb3d007 commit a16d8e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
22 changes: 3 additions & 19 deletions src/neovim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function parseVersion(v: string): Version | null {
};
}

function assetFileName(os: Os, arch: Arch, version: string): string {
function assetFileName(version: string, os: Os, arch: Arch): string {
switch (os) {
case 'macos': {
const v = parseVersion(version);
Expand All @@ -43,23 +43,7 @@ function assetFileName(os: Os, arch: Arch, version: string): string {
}
}
case 'linux': {
const v = parseVersion(version);
if (v !== null && (v.minor < 10 || (v.minor === 10 && v.patch < 4))) {
if (arch === 'arm64') {
throw Error(
`Linux arm64 has been only supported since Neovim v0.10.4 but the requested version is ${version}`,
);
}
return 'nvim-linux64.tar.gz';
}
switch (arch) {
case 'arm64':
return 'nvim-linux-arm64.tar.gz';
case 'x86_64':
return 'nvim-linux-x86_64.tar.gz';
default:
throw Error(`Unsupported arch for Neovim ${version} on ${os}: ${process.arch}`); // Should be unreachable
}
return assetDirName(version, os, arch) + '.tar.gz';
}
case 'windows':
return 'nvim-win64.zip';
Expand Down Expand Up @@ -139,7 +123,7 @@ async function unarchiveAsset(asset: string, dirName: string): Promise<string> {

// version = 'stable' or 'nightly' or version string
export async function downloadNeovim(version: string, os: Os, arch: Arch): Promise<Installed> {
const file = assetFileName(os, arch, version);
const file = assetFileName(version, os, arch);
const destDir = path.join(homedir(), `nvim-${version}`);
const url = `https://github.com/neovim/neovim/releases/download/${version}/${file}`;
console.log(`Downloading Neovim ${version} on ${os} from ${url} to ${destDir}`);
Expand Down
25 changes: 24 additions & 1 deletion test/neovim.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { strict as A } from 'assert';
import * as path from 'path';
import mock = require('mock-require');
import { downloadNeovim, type downloadStableNeovim, type buildNightlyNeovim, assetDirName } from '../src/neovim';
import {
downloadNeovim,
type downloadStableNeovim,
type buildNightlyNeovim,
assetDirName,
assetFileName,
} from '../src/neovim';
import { mockFetch, ExecStub, mockExec } from './helper';

function reRequire(): typeof import('../src/neovim') {
Expand Down Expand Up @@ -169,6 +175,23 @@ describe('Neovim installation', function () {
A.equal(assetDirName('v0.11.0', 'linux', 'x86_64'), 'nvim-linux-x86_64');
A.equal(assetDirName('v0.10.4', 'linux', 'arm64'), 'nvim-linux-arm64');
A.equal(assetDirName('v0.11.0', 'linux', 'arm64'), 'nvim-linux-arm64');
A.equal(assetDirName('stable', 'linux', 'x86_64'), 'nvim-linux-x86_64');
A.equal(assetDirName('stable', 'linux', 'arm64'), 'nvim-linux-arm64');
});
});

describe('assetFileName', function () {
it('returns asset file name following the Neovim version and CPU arch on Linux', function () {
A.equal(assetFileName('v0.10.3', 'linux', 'x86_64'), 'nvim-linux64.tar.gz');
A.equal(assetFileName('v0.10.4', 'linux', 'x86_64'), 'nvim-linux-x86_64.tar.gz');
A.equal(assetFileName('v0.10.4', 'linux', 'arm64'), 'nvim-linux-arm64.tar.gz');
});

it('returns asset file name following the Neovim version and CPU arch on macOS', function () {
A.equal(assetFileName('v0.7.0', 'macos', 'x86_64'), 'nvim-macos.tar.gz');
A.equal(assetFileName('v0.7.1', 'macos', 'x86_64'), 'nvim-macos.tar.gz');
A.equal(assetFileName('v0.10.4', 'macos', 'x86_64'), 'nvim-macos-x86_64.tar.gz');
A.equal(assetFileName('v0.10.4', 'macos', 'arm64'), 'nvim-macos-arm64.tar.gz');
});
});
});
2 changes: 1 addition & 1 deletion test/vim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('versionIsOlderThan()', function () {

for (const tc of testCases) {
const [v, expected] = tc;
it(`${v} is ${expected ? 'older' : 'equal or newer than'} 8.2.1119`, function () {
it(`${v} is ${expected ? 'older than' : 'equal or newer than'} 8.2.1119`, function () {
A.equal(versionIsOlderThan(v, 8, 2, 1119), expected);
});
}
Expand Down

0 comments on commit a16d8e9

Please sign in to comment.