From 2a61e7671ef33face8965272a8d911f76c26d353 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 16 Jan 2018 15:15:51 +0200 Subject: [PATCH] Script and tag cleanup, fixed `select` tests --- .travis.yml | 3 ++- package.json | 4 ++-- scripts/module.js | 4 ++-- scripts/tag.js | 2 +- scripts/tdd.js | 32 ++++++++++++++++++++------------ tag_database | 4 ++-- test/select/select.js | 4 ++-- test/select/select.test.js | 4 ++-- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index ffb9c3da1..06a727cc9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,8 @@ script: - npm run linter - npm run builder - npm run webber -- npm run module +- npm run packager +- npm run tester after_success: - chmod +x .travis/push.sh - .travis/push.sh diff --git a/package.json b/package.json index 1092b1ef3..6662d462d 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,8 @@ "linter": "node ./scripts/lint.js", "tagger": "node ./scripts/tag.js", "webber": "node ./scripts/web.js", - "tdd": "node ./scripts/tdd.js", - "module": "node ./scripts/module.js", + "tester": "node ./scripts/tdd.js", + "packager": "node ./scripts/module.js", "test": "tape test/**/*.test.js | tap-spec" }, "repository": { diff --git a/scripts/module.js b/scripts/module.js index 9d5f73cdb..566c16568 100644 --- a/scripts/module.js +++ b/scripts/module.js @@ -19,7 +19,7 @@ const IMPORTS = './imports.js'; // Regex for selecting code blocks const codeRE = /```\s*js([\s\S]*?)```/; // Start the timer of the script -console.time('Module'); +console.time('Packager'); // Load tag data from the database and snippets from their folder try { const tagDatabase = fs.readFileSync('tag_database', 'utf8'); @@ -72,7 +72,7 @@ try { // Log a success message console.log(`${chalk.green('SUCCESS!')} Snippet module built!`); // Log the time taken - console.timeEnd('Module'); + console.timeEnd('Packager'); } catch (err) { // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During module creation: ${err}`); diff --git a/scripts/tag.js b/scripts/tag.js index c3dd9aa64..733e45f18 100644 --- a/scripts/tag.js +++ b/scripts/tag.js @@ -59,7 +59,7 @@ try { tagDbStats = Object.entries(tagDbData) .sort((a, b) => a[1][0].localeCompare(b[1][0])) .reduce((acc, val) => { - acc.hasOwnProperty(val[1]) ? acc[val[1]]++ : (acc[val[1]] = 1); + val[1].forEach(v => acc.hasOwnProperty(v) ? acc[v]++ : (acc[v] = 1)); return acc; }, {}); } catch (err) { diff --git a/scripts/tdd.js b/scripts/tdd.js index d008c6933..4d2183440 100644 --- a/scripts/tdd.js +++ b/scripts/tdd.js @@ -4,7 +4,8 @@ */ // Load modules -const fs = require('fs-extra'); +const fs = require('fs-extra'), path = require('path'); +const child_process = require('child_process'); const chalk = require('chalk'); // Load helper functions (these are from existing snippets in 30 seconds of code!) const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env; @@ -14,7 +15,7 @@ if(isTravisCI() && process.env['TRAVIS_EVENT_TYPE'] !== 'cron' && process.env['T } // Declare paths const SNIPPETS_ACTIVE = './snippets'; -const SNIPPETS_ARCHIVE = './snippets_archive'; +const SNIPPETS_ARCHIVE = './snippets_archive'; const TEST_PATH = './test'; // Array of snippet names @@ -29,12 +30,12 @@ snippetFiles.push(...snippetFilesArchive); // Current Snippet that depend on node_modules const errSnippets = ['JSONToFile', 'readFileLines', 'UUIDGeneratorNode']; - +console.time('Tester'); snippetFiles .filter(fileName => !errSnippets.includes(fileName)) .map(fileName => { // Check if fileName for snippet exist in test/ dir, if doesnt create - fs.ensureDirSync(`${TEST_PATH}/${fileName}`); + fs.ensureDirSync(path.join(TEST_PATH,fileName)); // return fileName for later use return fileName; @@ -42,7 +43,7 @@ snippetFiles .map(fileName => { const activeOrArchive = snippetFilesActive.includes(fileName) ? SNIPPETS_ACTIVE : SNIPPETS_ARCHIVE; // Grab snippetData - const fileData = fs.readFileSync(`${activeOrArchive}/${fileName}.md`, 'utf8'); + const fileData = fs.readFileSync(path.join(activeOrArchive,`${fileName}.md`), 'utf8'); // Grab snippet Code blocks const fileCode = fileData.slice(fileData.search(/```\s*js/i), fileData.lastIndexOf('```') + 3); // Split code based on code markers @@ -55,7 +56,7 @@ snippetFiles .split('\n') .map(line => line.trim()) .filter((_, i) => blockMarkers[0] < i && i < blockMarkers[1]); - // Grab snippet example based on code markers + // Grab snippet example based on code markers const fileExample = fileCode .split('\n') .map(line => line.trim()) @@ -64,13 +65,13 @@ snippetFiles // Export template for snippetName.js which takes into account snippet name.length when generating snippetName.js file const exportFile = `module.exports = ${fileName} = ${fileFunction.join('\n').slice(9 + fileName.length)}`; - // Export template for snippetName.test.js which generates a example test & other information + // Export template for snippetName.test.js which generates a example test & other information const exportTest = [ `const test = require('tape');`, `const ${fileName} = require('./${fileName}.js');`, `\ntest('Testing ${fileName}', (t) => {`, `\t//For more information on all the methods supported by tape\n\t//Please go to https://github.com/substack/tape`, - `\tt.true(typeof ${fileName} === 'function', '${fileName} is a Function');`, + `\tt.true(typeof ${fileName} === 'function', '${fileName} is a Function');`, `\t//t.deepEqual(${fileName}(args..), 'Expected');`, `\t//t.equal(${fileName}(args..), 'Expected');`, `\t//t.false(${fileName}(args..), 'Expected');`, @@ -80,14 +81,21 @@ snippetFiles ].join('\n'); // Write/Update exportFile which is snippetName.js in respective dir - fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.js`, exportFile); + fs.writeFileSync(path.join(TEST_PATH,fileName,`${fileName}.js`), exportFile); - if ( !fs.existsSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`) ) { - // if snippetName.test.js doesn't exist inrespective dir exportTest + if ( !fs.existsSync(path.join(TEST_PATH,fileName,`${fileName}.test.js`)) ) { + // if snippetName.test.js doesn't exist inrespective dir exportTest fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`, exportTest); } // return fileName for later use return fileName; }); - +try { + fs.writeFileSync(path.join(TEST_PATH,'testlog'),`Test log for: ${new Date().toString()}\n`); + child_process.execSync(`npm test >> ${TEST_PATH}/testlog`); +} +catch (e) { + fs.appendFileSync(path.join(TEST_PATH,'testlog')); +} +console.timeEnd('Tester'); diff --git a/tag_database b/tag_database index b7d1682f6..20fda9170 100644 --- a/tag_database +++ b/tag_database @@ -75,7 +75,7 @@ indexOfAll:array initial:array initialize2DArray:array initializeArrayWithRange:array,math -initializeArrayWithValues:array,amth +initializeArrayWithValues:array,math inRange:math intersection:array,math invertKeyValues:object @@ -116,7 +116,7 @@ median:math,array memoize:function merge:object,array minBy:math,array,function -minN:array,amth +minN:array,math negate:function nthElement:array objectFromPairs:object,array diff --git a/test/select/select.js b/test/select/select.js index 62aed5333..ab3fea695 100644 --- a/test/select/select.js +++ b/test/select/select.js @@ -1,2 +1,2 @@ -module.exports = select = (from, selector) => -selector.split('.').reduce((prev, cur) => prev && prev[cur], from); \ No newline at end of file +module.exports = select = (from, ...selectors) => +[...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from)); \ No newline at end of file diff --git a/test/select/select.test.js b/test/select/select.test.js index 55b50beab..eb5a1b3a2 100644 --- a/test/select/select.test.js +++ b/test/select/select.test.js @@ -6,10 +6,10 @@ test('Testing select', (t) => { //Please go to https://github.com/substack/tape t.true(typeof select === 'function', 'select is a Function'); const obj = { selector: { to: { val: 'val to select' } } }; - t.equal(select(obj, 'selector.to.val'), 'val to select', "Retrieve a property indicated by the selector from an object."); + t.deepEqual(select(obj, 'selector.to.val'), ['val to select'], "Retrieve a property indicated by the selector from an object."); //t.deepEqual(select(args..), 'Expected'); //t.equal(select(args..), 'Expected'); //t.false(select(args..), 'Expected'); //t.throws(select(args..), 'Expected'); t.end(); -}); \ No newline at end of file +});