Skip to content

Commit

Permalink
test: add cases
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Sep 4, 2019
1 parent 1226844 commit f78298a
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/command/check.ts
@@ -1,3 +1,4 @@
// istanbul ignore file
import assert from 'assert';
import Command from 'common-bin';
import fs from 'fs';
Expand Down
1 change: 1 addition & 0 deletions lib/command/speed.ts
@@ -1,3 +1,4 @@
// istanbul ignore file
import assert from 'assert';
import chalk from 'chalk';
import { ChildProcess } from 'child_process';
Expand Down
1 change: 1 addition & 0 deletions lib/command/upload.ts
@@ -1,3 +1,4 @@
// istanbul ignore file
import OSS from 'ali-oss';
import assert from 'assert';
import Command from 'common-bin';
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.ts
Expand Up @@ -35,7 +35,7 @@ const ConfigCache = new LRU<string, any>({
maxAge: 10 * 60 * 1000, // 10min
});

// istanbul ignore next;
// istanbul ignore next
export const resolveRoot = (...args: readonly string[]): string => path.join(__dirname, '../../', ...args);

export const getDownloadUrl = (baseUrl: string = '/', artifactName: string): string => `${baseUrl}${artifactName}`;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -96,9 +96,11 @@
},
"ava": {
"failFast": true,
"babel": false,
"compileEnhancements": false,
"extensions": [
"ts"
"ts",
"js"
],
"files": [
"test/**/*.test.ts"
Expand Down
28 changes: 24 additions & 4 deletions test/cli.test.ts
Expand Up @@ -12,22 +12,42 @@ const resolve = p => path.join(fixture, p);
test('cli works', async t => {
const { code } = await coffee.fork(cli, ['generate'], {
cwd: resolve('plain'),
execArgv: ['--require', require.resolve('./stub-axios.js')],
})
.debug()
.end();
const confString = (await fs.readFile(resolve('plain/dist/test.conf'))).toString();
const conf = ini.decode(confString);

t.is(code, 0);
t.is(confString.split('\n')[0], '#!MANAGED-CONFIG https://example.com/test.conf interval=43200 strict=false');

const confString = fs.readFileSync(resolve('plain/dist/ss_json.conf'), {
encoding: 'utf8',
});
const conf = ini.decode(confString);

t.truthy(fs.existsSync(resolve('plain/dist/ss.conf')));
t.truthy(fs.existsSync(resolve('plain/dist/ssr.conf')));
t.truthy(fs.existsSync(resolve('plain/dist/v2rayn.conf')));
t.is(confString.split('\n')[0], '#!MANAGED-CONFIG https://example.com/ss_json.conf interval=43200 strict=false');
t.is(Object.keys(conf.Proxy).length, 4);
});

test('template error', async t => {
const { code } = await coffee.fork(cli, ['generate'], {
cwd: resolve('template-error'),
execArgv: ['--require', require.resolve('./stub-axios.js')],
})
.end();

t.is(code, 1);
});

test('not specify binPath', async t => {
const { stderr, code } = await coffee.fork(cli, ['generate'], {
cwd: resolve('not-specify-binPath'),
execArgv: ['--require', require.resolve('./stub-axios.js')],
})
.debug()
.end();

t.is(code, 1);
t.truthy(stderr.includes('You must specify a binary file path for Shadowsocksr'));
});
6 changes: 6 additions & 0 deletions test/fixture/not-specify-binPath/provider/ssr.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
url: '/test-ssr-sub.txt',
type: 'shadowsocksr_subscribe',
};
12 changes: 12 additions & 0 deletions test/fixture/not-specify-binPath/surgio.conf.js
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
artifacts: [
{
name: 'ssr.conf',
template: 'test',
provider: 'ssr',
},
],
urlBase: 'https://example.com/',
};
7 changes: 7 additions & 0 deletions test/fixture/not-specify-binPath/template/test.tpl
@@ -0,0 +1,7 @@
#!MANAGED-CONFIG {{ downloadUrl }} interval=43200 strict=false

[Proxy]
{{ getSurgeNodes(nodeList) }}

[Proxy Group]
Proxy = select, {{ getNodeNames(nodeList, ['shadowsocks']) }}
6 changes: 6 additions & 0 deletions test/fixture/plain/provider/ss.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
url: '/test-ss-sub.txt',
type: 'shadowsocks_subscribe',
};
6 changes: 6 additions & 0 deletions test/fixture/plain/provider/ss_json.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
url: '/gui-config.json',
type: 'shadowsocks_json_subscribe',
};
6 changes: 6 additions & 0 deletions test/fixture/plain/provider/ssr.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
url: '/test-ssr-sub.txt',
type: 'shadowsocksr_subscribe',
};
6 changes: 0 additions & 6 deletions test/fixture/plain/provider/test.js

This file was deleted.

6 changes: 6 additions & 0 deletions test/fixture/plain/provider/v2rayn.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
url: '/test-v2rayn-sub.txt',
type: 'v2rayn_subscribe',
};
24 changes: 21 additions & 3 deletions test/fixture/plain/surgio.conf.js
@@ -1,13 +1,31 @@
'use strict';

console.log(123);
module.exports = {
artifacts: [
{
name: 'test.conf',
name: 'ss_json.conf',
template: 'test',
provider: 'test',
provider: 'ss_json',
},
{
name: 'ss.conf',
template: 'test',
provider: 'ss',
},
{
name: 'ssr.conf',
template: 'test',
provider: 'ssr',
},
{
name: 'v2rayn.conf',
template: 'test',
provider: 'v2rayn',
},
],
urlBase: 'https://example.com/',
binPath: {
shadowsocksr: '/usr/local/bin/ssr-local',
v2ray: '/usr/local/bin/v2ray',
},
};
2 changes: 1 addition & 1 deletion test/fixture/template-error/provider/test.js
@@ -1,6 +1,6 @@
'use strict';

module.exports = {
url: 'https://tgbot.lbyczf.com/surge2sswin?url=https://raw.githubusercontent.com/lhie1/Surge/master/Surge.conf',
url: '/gui-config.json',
type: 'shadowsocks_json_subscribe',
};
32 changes: 32 additions & 0 deletions test/stub-axios.js
@@ -0,0 +1,32 @@
'use strict';

const moxios = require('moxios');
const fs = require('fs');
const path = require('path');

moxios.install();

moxios.stubRequest('/gui-config.json', {
status: 200,
responseText: fs.readFileSync(path.join(__dirname, 'asset/gui-config-1.json'), {
encoding: 'utf8',
}),
});
moxios.stubRequest('/test-ss-sub.txt', {
status: 200,
responseText: fs.readFileSync(path.join(__dirname, 'asset/test-ss-sub.txt'), {
encoding: 'utf8',
}),
});
moxios.stubRequest('/test-ssr-sub.txt', {
status: 200,
responseText: fs.readFileSync(path.join(__dirname, 'asset/test-ssr-sub.txt'), {
encoding: 'utf8',
}),
});
moxios.stubRequest('/test-v2rayn-sub.txt', {
status: 200,
responseText: fs.readFileSync(path.join(__dirname, 'asset/test-v2rayn-sub.txt'), {
encoding: 'utf8',
}),
});
1 change: 1 addition & 0 deletions test/utils.test.ts
Expand Up @@ -3,6 +3,7 @@ import test from 'ava';
import moxios from 'moxios';
import fs from 'fs';
import path from 'path';

import {
NodeTypeEnum,
ShadowsocksNodeConfig,
Expand Down

0 comments on commit f78298a

Please sign in to comment.