add tests

This commit is contained in:
OpportunityLiu
2020-03-20 22:14:58 +08:00
Unverified
parent 7d75884a86
commit be19756845
15 changed files with 3508 additions and 41 deletions
+14
View File
@@ -0,0 +1,14 @@
import * as git from './git';
import * as semver from 'semver';
describe('lsRemote', () => {
it('should return correct records', async () => {
const records = await git.lsRemote();
expect(Object.keys(records).length).not.toBeLessThan(10);
for (const tag in records) {
const ref = records[tag];
expect(semver.parse(tag)).toBeInstanceOf(semver.SemVer);
expect(ref).toMatch(/^[0-9a-f]{40}$/gi);
}
}, 100000);
});
+1
View File
@@ -9,6 +9,7 @@ const opt = { cwd: folder };
export async function lsRemote(): Promise<Record<string, string>> {
let out = '';
await exec('git', ['ls-remote', '--tags', 'https://github.com/xmake-io/xmake.git'], {
silent: true,
listeners: {
stdout: d => (out += d.toString()),
},
+1 -3
View File
@@ -1,7 +1,5 @@
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import * as _fs from 'fs';
const fs = _fs.promises;
import * as io from '@actions/io';
import * as toolCache from '@actions/tool-cache';
import * as os from 'os';
@@ -21,7 +19,7 @@ async function winInstall(version: string): Promise<void> {
core.info(`downloading from ${url}`);
const file = await toolCache.downloadTool(url);
const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined });
await fs.rename(file, exe);
await io.mv(file, exe);
core.info(`downloaded to ${exe}`);
return exe;
});
+35
View File
@@ -0,0 +1,35 @@
jest.mock('./git', () => ({
lsRemote() {
return {
'1.0.0': '100000000',
'1.0.1': '100000001',
'2.0.0': '200000000',
'2.0.1': '200000001',
};
},
}));
import { selectVersion } from './versions';
describe('selectVersion', () => {
it('should return correct version for latest', async () => {
await expect(selectVersion('latest')).resolves.toEqual({
version: '2.0.1',
sha: '200000001',
});
});
it('should throw for no matched', async () => {
await expect(selectVersion('1.2.3')).rejects.toThrowError(/No matched releases of xmake-version/);
});
it('should return correct version for given', async () => {
await expect(selectVersion('v1.0.1')).resolves.toEqual({
version: '1.0.1',
sha: '100000001',
});
});
it('should return correct version for range', async () => {
await expect(selectVersion('>=2')).resolves.toEqual({
version: '2.0.1',
sha: '200000001',
});
});
});