Skip to content

Commit

Permalink
💤add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Sep 21, 2016
1 parent 055a846 commit 63bdfac
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 56 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js

node_js:
- "4.4.3"
- "5.11.0"
- "6.1"

before_script:
- npm install

script:
- npm run test
96 changes: 52 additions & 44 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
'use strict';

const glob = require('glob');
Expand All @@ -9,15 +8,6 @@ const sh = require('shelljs');
const mkdirp = require('mkdirp');
const convert = require('./convert');

const ARGS = yargs
.describe('output', 'Output file')
.describe('version', 'Print version number and exit')
.help('help')
.alias('o', 'output')
.alias('h', 'help')
.alias('v', 'version')
.argv;

const cwd = sh.pwd().toString();

const convertAndWrite = (file, outputPath) => {
Expand All @@ -27,49 +17,67 @@ const convertAndWrite = (file, outputPath) => {
output = convert(output);
} catch ( e ) {
console.error('ERROR: convert error. Input code is too complex to convert.');
process.exit(1);
return 1;
}
const outputDir = path.join(cwd, '/' + outputPath);
mkdirp(outputPath);
fs.writeFileSync(outputDir + path.basename(file), output);
return 0;
};

if (ARGS.version || ARGS.v) {
const json = require('./package.json');
console.log(json.name + ' ' + json.version);
process.exit(0);
}


if (ARGS._[0]) {
const input = ARGS._[0];
let output = undefined;
if (ARGS.output || ARGS.o) {
output = ARGS.output || ARGS.o;
} else {
console.error('ERROR: Invalid output');
process.exit(1);
const cli = (args) => {
if (typeof args === 'undefined') {
return 1;
}
if (fs.existsSync(input)) {
convertAndWrite(input, output);
process.exit(0);
if (typeof args !== 'object') {
return 1;
}
glob(input, (err, files) => {
if (err) {
console.error('ERROR: Invalid glob definition');
process.exit(1);
yargs
.describe('output', 'Output file')
.describe('version', 'Print version number and exit')
.help('help')
.alias('o', 'output')
.alias('h', 'help')
.alias('v', 'version');

if (args.version || args.v) {
const json = require('./package.json');
console.log(json.name + ' ' + json.version);
return 0;
}


if (args._[0]) {
const input = args._[0];
let output = undefined;
if (args.output || args.o) {
output = args.output || args.o;
} else {
console.error('ERROR: Invalid output');
return 1;
}
for (let file of files) {
convertAndWrite(file, output);
if (fs.existsSync(input)) {
return convertAndWrite(input, output);
}
});
process.exit(0);
}
glob(input, (err, files) => {
if (err) {
console.error('ERROR: Invalid glob definition');
return 1;
}
for (let file of files) {
convertAndWrite(file, output);
}
});
return 0;
}

if ((ARGS.output || ARGS.o) && !ARGS._[0]) {
console.error('ERROR: Invalid input');
process.exit(1);
}
if ((args.output || args.o) && !args._[0]) {
console.error('ERROR: Invalid input');
return 1;
}

console.log('Usage: kaomojify <input> -o <output>');
return 0;
};

console.log('Usage: kaomojify <input> -o <output>');
process.exit(0);
module.exports = cli;
30 changes: 30 additions & 0 deletions cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const assert = require('power-assert');
const cli = require('./cli');

const buildArgs = (str) => {
let args = new Object();
args._ = [];
args[str] = true;
return args;
};

describe('cli', () => {

describe('options', () => {

it('throw error of run `kaomojify` (empty)', () => {
assert.strictEqual(cli(), 1);
});

it('no error of run `kaomojify -v`', () => {
assert.strictEqual(cli(buildArgs('-v')), 0);
});

it('no error of run `kotori -h`', () => {
assert.strictEqual(cli(buildArgs('-h')), 0);
});

});
});
4 changes: 2 additions & 2 deletions convert.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = (text) => {
'use strict';

let t;
const b = [
"(c^_^o)",
Expand Down
11 changes: 11 additions & 0 deletions kaomojify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node
'use strict';

const yargs = require('yargs');
const cli = require('./cli');

const exitCode = cli(yargs.argv);

process.on('exit', () => {
process.exit(exitCode);
});
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"name": "kaomojify",
"version": "0.1.4",
"version": "0.2.0",
"description": "Kaomojify Javascript code",
"main": "",
"bin": {
"kaomojify": "./cli.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint ./"
"kaomojify": "./kaomojify.js"
},
"repository": {
"type": "git",
Expand All @@ -23,14 +19,20 @@
"url": "https://github.com/kokororin/kaomojify/issues"
},
"homepage": "https://github.com/kokororin/kaomojify#readme",
"devDependencies": {
"eslint": "^3.5.0",
"eslint-config-crocodile": "^1.0.0"
},
"dependencies": {
"glob": "^7.0.6",
"mkdirp": "^0.5.1",
"shelljs": "^0.7.4",
"yargs": "^5.0.0"
},
"devDependencies": {
"eslint": "^3.5.0",
"eslint-config-crocodile": "^1.0.0",
"mocha": "^3.0.2",
"power-assert": "^1.4.1"
},
"scripts": {
"test": "mocha ./*.test.js",
"lint": "eslint ./"
}
}

0 comments on commit 63bdfac

Please sign in to comment.