|
| 1 | +/** |
| 2 | +yarn add mocha -D |
| 3 | +
|
| 4 | +package.json |
| 5 | + "imports": { |
| 6 | + "##/*": { |
| 7 | + "default": "./*" |
| 8 | + }, |
| 9 | + }, |
| 10 | + "type": "module", |
| 11 | +
|
| 12 | + jsconfig.json |
| 13 | + { |
| 14 | + "compilerOptions": { |
| 15 | + "baseUrl": ".", |
| 16 | + "paths": { |
| 17 | + "##/*": ["./*"] |
| 18 | + } |
| 19 | + }, |
| 20 | + "exclude": ["node_modules", ".nuxt", "dist"] |
| 21 | +} |
| 22 | +
|
| 23 | +
|
| 24 | +
|
| 25 | +*/ |
| 26 | +// import { createRequire } from 'module'; |
| 27 | +// const require = createRequire(import.meta.url); |
| 28 | +// const assert = require('assert'); |
| 29 | +// const {describe,it} = require('mocha'); |
| 30 | +import assert from 'node:assert'; |
| 31 | +import { describe, it} from 'mocha'; |
| 32 | +/* |
| 33 | +1. |
| 34 | +yarn add mocha @babel/polyfill @babel/register @babel/preset-env babel-plugin-module-resolver --dev |
| 35 | +yarn add @babel/core --dev |
| 36 | +2. |
| 37 | +-r @babel/register -r babel-plugin-module-resolver |
| 38 | +
|
| 39 | +3. |
| 40 | +.babelrc |
| 41 | +{ |
| 42 | +
|
| 43 | + "presets": ["@babel/preset-env"], |
| 44 | + "plugins": [ |
| 45 | + ["module-resolver", { |
| 46 | + "root": ["./src"], |
| 47 | + "alias": { |
| 48 | + "test": "./test", |
| 49 | + "underscore": "lodash", |
| 50 | +
|
| 51 | + "~": "./" |
| 52 | + } |
| 53 | + }] |
| 54 | + ] |
| 55 | +
|
| 56 | +} |
| 57 | +test specific timeout |
| 58 | +this.timeout(500);//500ms |
| 59 | +*/ |
| 60 | +/** |
| 61 | + * Should put this somewhere safe |
| 62 | + * todo filepath needs to be initialized as well... |
| 63 | + * @param fileName .json |
| 64 | + * @param data will automatically be changed |
| 65 | + */ |
| 66 | +import fs from 'node:fs'; |
| 67 | +function writeToFile(fileName,data,space=2){ |
| 68 | + const sFileName = /\./.test(fileName) ? fileName : fileName + '.json'; |
| 69 | + const filePath = `dev/pbs/test/${sFileName}` |
| 70 | + fs.writeFileSync(filePath, |
| 71 | + typeof data === 'string' ? data :JSON.stringify(data,null,+space) |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +/** |
| 77 | + * todo move / extract |
| 78 | + * @param line {string} |
| 79 | + * @returns {string|null} |
| 80 | + */ |
| 81 | +function handleBashLine(line){ |
| 82 | + /* this regex checks for empty lines and lines with only comments */ |
| 83 | + if(/^\s*#$/.test(line)) return null; |
| 84 | + const rowWithoutComment = line |
| 85 | + .replace(/#.*$/,'') |
| 86 | + .trim(); |
| 87 | + if(rowWithoutComment === '') return null; |
| 88 | + return rowWithoutComment; |
| 89 | +} |
| 90 | +describe('handleBashLine', function(){ |
| 91 | + it('empty line', function(){ |
| 92 | + const line = ''; |
| 93 | + const expected = null; |
| 94 | + const result = handleBashLine(line); |
| 95 | + assert.strictEqual(result,expected); |
| 96 | + }); |
| 97 | + it("comment line", function(){ |
| 98 | + const line = '# some comments'; |
| 99 | + const expected = null; |
| 100 | + const result = handleBashLine(line); |
| 101 | + assert.strictEqual(result,expected); |
| 102 | + }); |
| 103 | + /* need to double check space infront of hash but leave for now */ |
| 104 | + it('comment at end of line', function(){ |
| 105 | + const line = 'echo hi # some comments'; |
| 106 | + const expected = "echo hi"; |
| 107 | + const result = handleBashLine(line); |
| 108 | + assert.strictEqual(result,expected); |
| 109 | + }); |
| 110 | +}); |
| 111 | +describe('cli-escape-lines.test.mjs', function(){ |
| 112 | + let filePath = "dev/windows_env_paths/cli-escape-lines.txt.sh" |
| 113 | + |
| 114 | + /** @type {string} */ |
| 115 | + let rawData; |
| 116 | + /** @type {string[]} */ |
| 117 | + let data; |
| 118 | + before(()=>{ |
| 119 | + /* load file */ |
| 120 | + rawData = fs.readFileSync(filePath,'utf8') |
| 121 | + .replace(/\r\n/g,'\n'); |
| 122 | + data = rawData.split('\n') |
| 123 | + }); |
| 124 | + it('Ignore # and empty lines', function(){ |
| 125 | + //assert.strictEqual(1,1);//require assert |
| 126 | + assert.strictEqual(typeof rawData,'string'); |
| 127 | + /* open write stream */ |
| 128 | + const ws = fs.createWriteStream('dev/windows_env_paths/cli-escape-lines.parsed.txt'); |
| 129 | + for (let i = 0; i < data.length; i++) { |
| 130 | + const line = data[i]; |
| 131 | + const row = handleBashLine(line); |
| 132 | + if(row === null) continue; |
| 133 | + ws.write(row + '\n'); |
| 134 | + } |
| 135 | + |
| 136 | + ws.end(); |
| 137 | + |
| 138 | + }); |
| 139 | +}); |
0 commit comments