|
| 1 | +import { spawn } from 'child_process'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs'; |
| 4 | +import os from 'os'; |
| 5 | +import { randomBytes } from 'crypto'; |
| 6 | + |
| 7 | +const PATH_TO_SED_JS = './build/21/sed.js'; |
| 8 | +const filename = path.join(__dirname, 'test.txt'); |
| 9 | + |
| 10 | +describe('Testing invalid args', () => { |
| 11 | + const args = [ |
| 12 | + [PATH_TO_SED_JS], |
| 13 | + [PATH_TO_SED_JS, 'invalid-pattern', 'invalid-file.txt'] |
| 14 | + ]; |
| 15 | + |
| 16 | + args.forEach((arg) => { |
| 17 | + it('should print usage for args: ' + args.toString(), (done) => { |
| 18 | + const sed = spawn('node', arg); |
| 19 | + let output = ''; |
| 20 | + |
| 21 | + sed.stdout.on('data', (data) => { |
| 22 | + output += data.toString(); |
| 23 | + }); |
| 24 | + |
| 25 | + sed.on('close', () => { |
| 26 | + expect(output).toContain('Usage'); |
| 27 | + done(); |
| 28 | + }); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should print invalid file with valid pattern', (done) => { |
| 33 | + const sed = spawn('node', [ |
| 34 | + PATH_TO_SED_JS, |
| 35 | + 's/this/that/', |
| 36 | + 'invalid-file.txt' |
| 37 | + ]); |
| 38 | + |
| 39 | + let output = ''; |
| 40 | + sed.stderr.on('data', (data) => { |
| 41 | + output += data.toString(); |
| 42 | + }); |
| 43 | + |
| 44 | + sed.on('close', () => { |
| 45 | + expect(output).toContain('Invalid file'); |
| 46 | + done(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should print "Invalid pattern or range"', (done) => { |
| 51 | + const sed = spawn('node', [ |
| 52 | + PATH_TO_SED_JS, |
| 53 | + '-n', |
| 54 | + '2,4', |
| 55 | + 'invalid-file.txt' |
| 56 | + ]); |
| 57 | + let output = ''; |
| 58 | + |
| 59 | + sed.stderr.on('data', (data) => { |
| 60 | + output += data.toString(); |
| 61 | + }); |
| 62 | + |
| 63 | + sed.on('close', () => { |
| 64 | + expect(output).toContain('Invalid pattern or range'); |
| 65 | + done(); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should print invalid file with valid range', (done) => { |
| 70 | + const sed = spawn('node', [ |
| 71 | + PATH_TO_SED_JS, |
| 72 | + '-n', |
| 73 | + '2,4p', |
| 74 | + 'invalid-file.txt' |
| 75 | + ]); |
| 76 | + let output = ''; |
| 77 | + |
| 78 | + sed.stderr.on('data', (data) => { |
| 79 | + output += data.toString(); |
| 80 | + }); |
| 81 | + |
| 82 | + sed.on('close', () => { |
| 83 | + expect(output).toContain('Invalid file'); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('Testing valid args', () => { |
| 90 | + it('should replace all occurrences', (done) => { |
| 91 | + const pattern = 's/a/b/'; |
| 92 | + const content = fs.readFileSync(filename).toString(); |
| 93 | + const expectedOutput = content.replaceAll('a', 'b'); |
| 94 | + |
| 95 | + const sed = spawn('node', [PATH_TO_SED_JS, pattern, filename]); |
| 96 | + let output = ''; |
| 97 | + |
| 98 | + sed.stdout.on('data', (data) => { |
| 99 | + output += data.toString(); |
| 100 | + }); |
| 101 | + |
| 102 | + sed.on('close', () => { |
| 103 | + expect(output).toBe(expectedOutput); |
| 104 | + done(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should print only lines mentioned', (done) => { |
| 109 | + const [start, end] = [2, 4]; |
| 110 | + const range = `${start},${end}p`; |
| 111 | + const content = fs.readFileSync(filename).toString(); |
| 112 | + const expectedOutput = content |
| 113 | + .split(/\r\n|\n/) |
| 114 | + .slice(start, end + 1) |
| 115 | + .join('\r\n'); |
| 116 | + |
| 117 | + const sed = spawn('node', [PATH_TO_SED_JS, '-n', range, filename]); |
| 118 | + let output = ''; |
| 119 | + |
| 120 | + sed.stdout.on('data', (data) => { |
| 121 | + output += data.toString(); |
| 122 | + }); |
| 123 | + |
| 124 | + sed.on('close', () => { |
| 125 | + expect(output).toBe(expectedOutput); |
| 126 | + done(); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should print line containing the specific pattern only', (done) => { |
| 131 | + const pattern = 'roads'; |
| 132 | + const content = fs.readFileSync(filename).toString(); |
| 133 | + const expectedOutput: string[] = []; |
| 134 | + content.split(/\r\n|\n/).forEach((line) => { |
| 135 | + if (line.indexOf(pattern) >= 0) { |
| 136 | + expectedOutput.push(line); |
| 137 | + } |
| 138 | + }); |
| 139 | + const sed = spawn('node', [ |
| 140 | + PATH_TO_SED_JS, |
| 141 | + '-n', |
| 142 | + `/${pattern}/p`, |
| 143 | + filename |
| 144 | + ]); |
| 145 | + |
| 146 | + let output = ''; |
| 147 | + |
| 148 | + sed.stdout.on('data', (data) => { |
| 149 | + output += data.toString(); |
| 150 | + }); |
| 151 | + |
| 152 | + sed.on('close', () => { |
| 153 | + expect(output).toBe(expectedOutput.join('\r\n')); |
| 154 | + done(); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should double space the file correctly', (done) => { |
| 159 | + const content = fs.readFileSync(filename).toString(); |
| 160 | + const expectedOutput = content.replaceAll(/\r\n|\n/g, '\r\n\r\n'); |
| 161 | + const sed = spawn('node', [PATH_TO_SED_JS, 'G', filename]); |
| 162 | + |
| 163 | + let output = ''; |
| 164 | + |
| 165 | + sed.stdout.on('data', (data) => { |
| 166 | + output += data.toString(); |
| 167 | + }); |
| 168 | + |
| 169 | + sed.on('close', () => { |
| 170 | + expect(output).toBe(expectedOutput); |
| 171 | + done(); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should remove trailing blank lines from a file', (done) => { |
| 176 | + const content = 'content\r\nsome more content '; |
| 177 | + const tmpfile = path.join(os.tmpdir(), 'temp-file.txt'); |
| 178 | + fs.writeFileSync(tmpfile, content + '\r\n\r\n\r\n'); |
| 179 | + const sed = spawn('node', [PATH_TO_SED_JS, '/^$/d', tmpfile]); |
| 180 | + |
| 181 | + let output = ''; |
| 182 | + |
| 183 | + sed.stdout.on('data', (data) => { |
| 184 | + output += data.toString(); |
| 185 | + }); |
| 186 | + |
| 187 | + sed.on('close', () => { |
| 188 | + expect(output).toBe(content.trimEnd()); |
| 189 | + done(); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should support -i option', (done) => { |
| 194 | + // First create a random test file from the `test.txt` |
| 195 | + const tempTextFile = path.join(__dirname, randomBytes(8).toString('hex')); |
| 196 | + fs.writeFileSync(tempTextFile, fs.readFileSync(filename)); |
| 197 | + |
| 198 | + // Prepare args |
| 199 | + const stringToReplace = 'Life'; |
| 200 | + const stringToReplaceWith = 'Code'; |
| 201 | + const characterReplacementInput = `s/${stringToReplace}/${stringToReplaceWith}/g`; |
| 202 | + |
| 203 | + const initialContent = fs.readFileSync(tempTextFile).toString(); |
| 204 | + const expectedContent = initialContent.replaceAll( |
| 205 | + stringToReplace, |
| 206 | + stringToReplaceWith |
| 207 | + ); |
| 208 | + |
| 209 | + const sed = spawn('node', [ |
| 210 | + PATH_TO_SED_JS, |
| 211 | + '-i', |
| 212 | + characterReplacementInput, |
| 213 | + tempTextFile |
| 214 | + ]); |
| 215 | + |
| 216 | + sed.on('close', () => { |
| 217 | + const finalContent = fs.readFileSync(tempTextFile).toString(); |
| 218 | + // Unlink random test file |
| 219 | + fs.unlinkSync(tempTextFile); |
| 220 | + |
| 221 | + expect(finalContent).toBe(expectedContent); |
| 222 | + done(); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments