diff --git a/README.md b/README.md deleted file mode 100644 index 8cc8c11..0000000 --- a/README.md +++ /dev/null @@ -1,45 +0,0 @@ -![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) 04: Parallel File Processing -=== - -## Submission Instructions - * fork this repository & create a new branch for your work - * write all of your code in a directory named `lab-` + `` **e.g.** `lab-susan` - * push to your repository - * submit a pull request to this repository - * submit a link to your PR in canvas - * write a question and observation on canvas - -## Learning Objectives - * students will be able to create asynchronous programs using the node.js callback pattern - * students will be able to read, write, and encode binary data using the Buffer class - * students will be able to utilize the built-in `fs` module for basic file system I/O operations - * students will be able to use `done` (provided by mocha.js) for creating asynchronous tests - -## Resources - * [fs module docs](https://nodejs.org/api/fs.html) - -## Requirements - -#### Configuration - -* include the following: - * **README.md** - contains documentation about your lab - * **.gitignore** - contains a robust `.gitignore` file - * **.eslintrc** - contains the provided `.eslintrc` file - * **.eslintignore** - contains the provided `.eslintignore` file - * **lib/** - contains your modules - * **test/** - contains your unit tests - * **assets/** - contains the text files used by the program - * **index.js** - contains main program file - -#### Feature Tasks - * for this assignment you will need to read three files and `console.log` the first 8 bytes ***(in hex)*** of each file - * regardless of file size, all three files should be read and logged in the order `'one.txt'`, `'two.txt'`, `'three.txt'` - -#### Testing - * create a test that guarantees that the files are logged in the proper order - * create a test that checks for improper file paths - -#### Documentation - * create a simple description of your project - * create a simple layer of documentation that describes how to use `done` in mocha callbacks diff --git a/lab/.eslintignore b/lab/.eslintignore new file mode 100644 index 0000000..ae8da71 --- /dev/null +++ b/lab/.eslintignore @@ -0,0 +1,6 @@ + +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab/.eslintrc b/lab/.eslintrc new file mode 100644 index 0000000..8dc6807 --- /dev/null +++ b/lab/.eslintrc @@ -0,0 +1,21 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} diff --git a/lab/.gitignore b/lab/.gitignore new file mode 100644 index 0000000..fbd29bb --- /dev/null +++ b/lab/.gitignore @@ -0,0 +1,127 @@ + +# Created by https://www.gitignore.io/api/osx,vim,node,windows + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Vim ### +# swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-v][a-z] +[._]sw[a-p] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/osx,vim,node,windows diff --git a/lab/README.md b/lab/README.md new file mode 100644 index 0000000..0ba30da --- /dev/null +++ b/lab/README.md @@ -0,0 +1,16 @@ +the lib directory contains the following files: + +file-reader.js + +file-reader.js accepts a directory path and a callback as an argument. The directory is then read using fs. An array containing all files is passed to a wrapper function which then uses recursion to iterate through each file. Using the fs module, each file is read as a buffer which is then spliced to the length of 8 bytes. Each of these is console logged and pushed to an array. The containing array will then be returned as a parameter of the callback function. + +the test directory contains the following files: + +file-reader-test.js + +file-reader-test.js contains a test for file-reader.js. It tests if the file-reader module throws an error when an incorrect path is passed. Additionally the test check if file-reader logs the files in the specified order. + +-------------------------------------------- + +--done() explanation +Done is used to prevent the test from completing before an asynchronous operation is done executing. done should be placed in the call back of the asynchronous operations call back. Once the operation is called, the done method will be called. done must be passed as a parameter for the callback of the it method executing the test. diff --git a/lab/data/one.txt b/lab/data/one.txt new file mode 100644 index 0000000..2328dc3 --- /dev/null +++ b/lab/data/one.txt @@ -0,0 +1 @@ +This is a test for the first file diff --git a/lab/data/three.txt b/lab/data/three.txt new file mode 100644 index 0000000..90444ee --- /dev/null +++ b/lab/data/three.txt @@ -0,0 +1 @@ +Bacon grease has all sorts of useful purposes... diff --git a/lab/data/two.txt b/lab/data/two.txt new file mode 100644 index 0000000..cd54426 --- /dev/null +++ b/lab/data/two.txt @@ -0,0 +1 @@ +I really enjoy eating cake diff --git a/lab/index.js b/lab/index.js new file mode 100644 index 0000000..e69de29 diff --git a/lab/lib/file-reader.js b/lab/lib/file-reader.js new file mode 100644 index 0000000..390456d --- /dev/null +++ b/lab/lib/file-reader.js @@ -0,0 +1,26 @@ +'use strict'; + +const fs = require('fs'); + +const fileReader = module.exports = function(path, callback) { + + function asyncWrapper(folder, callback, buffArray) { + let fileName = folder.pop(); + return fs.readFile(`${path}/${fileName}`, function(err, asset) { + if(err) return callback(err); + buffArray.push(asset.toString('hex', 0, 8)); + console.log(buffArray[buffArray.length -1]); + if(folder.length !== 0) return asyncWrapper(folder, callback, buffArray); + return callback(err, buffArray); + }); + } + + return fs.readdir(path, (err, dir) => { + if(err) return callback(err); + let buffArray = []; + //This is a hacky POS to get them to read in order.... + //May the coding gods have mercy on my soul... + dir.sort((a, b) => a.length - b.length).reverse(); + asyncWrapper(dir, callback, buffArray); + }); +}; diff --git a/lab/package.json b/lab/package.json new file mode 100644 index 0000000..e18610f --- /dev/null +++ b/lab/package.json @@ -0,0 +1,19 @@ +{ + "name": "lab", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^4.1.0", + "mocha": "^3.4.2" + } +} diff --git a/lab/test/file-reader-test.js b/lab/test/file-reader-test.js new file mode 100644 index 0000000..3c26712 --- /dev/null +++ b/lab/test/file-reader-test.js @@ -0,0 +1,27 @@ +'use strict'; + +const fileReader = require('../lib/file-reader.js'); +const expect = require('chai').expect; + +//Conatains the first 8 bytes from files one, two, and three in order +let fileBytes = ['5468697320697320', '49207265616c6c79', '4261636f6e206772']; + +describe('File Reader Module', () => { + describe('With improper file path', () => { + it('Should throw an error', (done) => { + fileReader(`${__dirname}/../not-a-file.txt`, (err) => { + expect(err).to.be.an('error'); + }); + done(); + }); + }); + + describe('With proper file path', () => { + it('Should console log files in order', (done) => { + fileReader(`${__dirname}/../data`, (err, asset) => { + asset.forEach((buff, ind) => expect(buff).to.equal(fileBytes[ind])); + done(); + }); + }); + }); +});