From 76d2f43dc56eead863b138a05a7ca60c56629067 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 14 Aug 2015 14:13:03 +0000 Subject: [PATCH] Logic update and testing improvment (#3) - Changed options for CLI call - Finished basic and downloader tests --- .gitignore | 2 ++ README.md | 25 ++++++++--------- index.js | 23 +++++++-------- lib/converter.js | 6 +++- lib/downloader.js | 2 +- test/basicTests.js | 62 ++++++++++++++++++++++++++++++++++++++--- test/converterTests.js | 14 ++++------ test/downloaderTests.js | 22 ++++++++------- 8 files changed, 105 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index 123ae94..8eaaf09 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules +gulpfile.js + diff --git a/README.md b/README.md index 84e4dab..41902db 100644 --- a/README.md +++ b/README.md @@ -7,23 +7,20 @@ Just provide parse with link to website and arguments if needed. By default will ```bash ❯ html2json -Usage: html2json [options] [command] + Usage: index [options] + Parse provided website -Commands: + Options: -parse [website] Parse provided website + -h, --help output usage information + -V, --version output the version number + -o --output [filename] Set output file(output.json by default) + -v --verbose Add verbose logging + -q --quiet Do not display output to console -Options: + Examples: --h, --help output usage information --V, --version output the version number --c --console Log result to console --o --output [filename] Set output file(output.json by default) --v --verbose Add verbose logging - -Examples: - -html2json https://google.com -html2json -o google.json https://google.com + html2json https://google.com #parse and display output + html2json -o google.json https://google.com #parse and save output to google.json ``` diff --git a/index.js b/index.js index ce4c6b1..06c2817 100755 --- a/index.js +++ b/index.js @@ -5,38 +5,35 @@ var downloader = require('./lib/downloader'), program = require('commander'), fs = require('fs'); -//Basic help +//Help topic program .version('0.1.0') - .option('-c --console', 'Log result to console') .option('-o --output [filename]', 'Set output file(output.json by default)') .option('-v --verbose', 'Add verbose logging') - .command('parse [website]') + .option('-q --quiet', 'Do not display output to console') .description('Parse provided website') //Main function .action(function(website) { var response = downloader.downloadPage(website); var result = JSON.stringify(converter.convertToJSON(response, program.verbose), null, 2); - var filename = "output.json"; - - if(program.console) { - console.log(result); - return true; - } + if (program.output) { - filename = program.output; + var filename = (program.output) ? program.output : "output.json"; + fs.writeFileSync(filename, result); } - fs.writeFileSync(filename, result); + if(program.quiet === undefined) { + console.log(result); + } }); // Examples program.on('--help', function(){ console.log(' Examples:'); console.log(''); - console.log(' html2json https://google.com'); - console.log(' html2json -o google.json https://google.com'); + console.log(' html2json https://google.com #parse and display output '); + console.log(' html2json -o google.json https://google.com #parse and save output to google.json'); console.log(''); }); diff --git a/lib/converter.js b/lib/converter.js index 840fe6b..3e29a26 100644 --- a/lib/converter.js +++ b/lib/converter.js @@ -1,5 +1,4 @@ var _ = require('underscore'); - var verbose; module.exports = { @@ -16,6 +15,10 @@ module.exports = { } }; +//------------------ +// Private functions +//------------------ + function filterHTML(html) { if(verbose) { console.log("\nFiltering HTML\n"); @@ -78,6 +81,7 @@ function parseTags (tags) { parsedTag = _.object([pair]); } + //Appending inner content if(text) { parsedTag.text = text; } diff --git a/lib/downloader.js b/lib/downloader.js index 7d4d7a0..4b6cbb8 100644 --- a/lib/downloader.js +++ b/lib/downloader.js @@ -9,4 +9,4 @@ module.exports = { return request.responseText; } -} +}; diff --git a/test/basicTests.js b/test/basicTests.js index 24a6a67..7cf3668 100644 --- a/test/basicTests.js +++ b/test/basicTests.js @@ -1,9 +1,63 @@ -var converter = require('../lib/converter'); -var downloader = require('../lib/downloader'); var assert = require('assert'); +var html2json = require("../index"); +var exec = require('child_process').exec; -describe('html2json command usage', function() { - describe('html2json cli', function () { +describe('html2json', function() { + describe('CLI', function () { + it('should display help topic', function(done) { + exec('./index.js', function(error, stdout, stderr) { + assert(stdout.indexOf('Usage:') > -1, "Don't found 'Usage' word in help topic"); + done(); + }); + }); + + it('should display output in console', function(done) { + exec('./index.js https://google.com', function(error, stdout, stderr) { + assert(stdout.indexOf('{') > -1, "Output don't appear in console, recieved: " + stdout); + done(); + }); + }); + + it('should save response to file and display in console', function(done) { + exec('rm output.json', function(error, stdout, stderr) { + exec('./index.js -o output.json https://google.com', function(error, stdout, stderr) { + exec("ls -lAh | grep output.json | cut -d' ' -f6", function(error, stdout, stderr) { + assert.equal(stdout, '263\n', "Output file don't found or have incorrect size"); + + //Cleaning if assertion passed + exec('rm output.json', function(error, stdout, stderr) { + done(); + }); + }); + assert(stdout.indexOf('{') > -1, "Output don't appear in console, recieved: " + stdout); + }); + }); + }); + + it('should save response to file', function(done) { + exec('rm silent.json', function(error, stdout, stderr) { + exec('./index.js -o silent.json -q https://google.com', function(error, stdout, stderr) { + exec("ls -lAh | grep silent.json | cut -d' ' -f6", function(error, stdout, stderr) { + assert.equal(stdout, '263\n', "Output file don't found or have incorrect size"); + + //Cleaning if assertion passed + exec('rm silent.json', function(error, stdout, stderr) { + done(); + }); + }); + assert.equal(stdout, '', "Console output isn't empty"); + }); + }); + }); + }); + + describe('integration', function() { + it('should return parsed page', function() { + + var response = html2json.convertPage('https://google.com'); + + assert(response.indexOf('{') > -1, "Output don't appear in console, recieved: " + response); + }); }); }); diff --git a/test/converterTests.js b/test/converterTests.js index 854b33c..a681ab2 100644 --- a/test/converterTests.js +++ b/test/converterTests.js @@ -2,14 +2,12 @@ var converter = require('../lib/converter'); var assert = require('assert'); -describe('Converter check', function() { - describe('Filter html', function () { - it('should return object with head element containing text property', function () { - assert.equal(converter.convertToJSON("foo").head.text, 'foo'); - }); +describe('Converter', function() { + it('should return object with head element containing text property', function () { + assert.equal(JSON.stringify(converter.convertToJSON("foo")), JSON.stringify({head: { text: "foo"}})); + }); - it('should return second element of array', function() { - assert.equal(converter.convertToJSON("01").a[1].text, '1'); - }); + it('should return array of "a" objects', function() { + assert.equal(JSON.stringify(converter.convertToJSON("01")), JSON.stringify({a: [ { text: "0" }, { text: "1" } ] })); }); }); diff --git a/test/downloaderTests.js b/test/downloaderTests.js index 184ff27..a7902fa 100644 --- a/test/downloaderTests.js +++ b/test/downloaderTests.js @@ -3,15 +3,17 @@ var fs = require('fs'); var assert = require('assert'); -describe('Downloader check', function() { - describe('Download pages', function () { - it('should return undefined becouse page don\'t exitst', function () { - assert.equal(downloader.downloadPage("http://www.gfhfdhdfsgdhtwgegdshbfnr.net/"), undefined); - }); - - it('should return tiny page', function () { - var tinyPage = fs.readFileSync('test/418.html'); - assert.equal(downloader.downloadPage("http://httpbin.org/status/418"), tinyPage); - }); +describe('Downloader', function() { + it('should return undefined becouse page don\'t exitst', function () { + assert.equal(downloader.downloadPage("http://www.gfhfdhdfsgdhtwgegdshbfnr.net/"), undefined); + }); + + it('should return tiny page', function () { + var tinyPage = fs.readFileSync('test/418.html'); + assert.equal(downloader.downloadPage("http://httpbin.org/status/418"), tinyPage); + }); + + it('should return static page', function() { + assert(downloader.downloadPage("http://pygreen.neoname.eu/").indexOf('alt="Fork me on GitHub"') > -1, "Don't found one of static's page tags"); }); });