|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var {execSync, spawn} = require('child_process'); |
| 4 | + var fs = require('fs'); |
| 5 | + var path = require('path'); |
| 6 | +} |
| 7 | + |
| 8 | +describe('Test CLI - Command Line Interface - Issue #2149 (Pipe handling with txt())', function () { |
| 9 | + console.log(__dirname); |
| 10 | + const cliPath = path.join(__dirname, '..', 'bin', 'alasql-cli.js'); |
| 11 | + const testSqlFile = path.join(__dirname, 'temp-test.sql'); |
| 12 | + const testWithTxtFile = path.join(__dirname, 'test2149-with-txt.sql'); |
| 13 | + const testWithoutTxtFile = path.join(__dirname, 'test2149-without-txt.sql'); |
| 14 | + |
| 15 | + before(function () { |
| 16 | + // Create a temporary SQL file for testing |
| 17 | + fs.writeFileSync(testSqlFile, 'SELECT VALUE 42'); |
| 18 | + }); |
| 19 | + |
| 20 | + after(function () { |
| 21 | + // Clean up temporary files |
| 22 | + if (fs.existsSync(testSqlFile)) { |
| 23 | + fs.unlinkSync(testSqlFile); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it('1. Should execute simple SQL statement', function () { |
| 28 | + const result = execSync(`node "${cliPath}" "SELECT VALUE 42"`).toString().trim(); |
| 29 | + assert.strictEqual(result, '42'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('2. Should handle parameters', function () { |
| 33 | + const result = execSync(`node "${cliPath}" "SELECT VALUE ?" 100`).toString().trim(); |
| 34 | + assert.strictEqual(result, '100'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('3. Should execute SQL from file', function () { |
| 38 | + const result = execSync(`node "${cliPath}" -f "${testSqlFile}"`).toString().trim(); |
| 39 | + assert.strictEqual(result, '42'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('4. Should output minified JSON with -m flag', function () { |
| 43 | + const result = execSync(`node "${cliPath}" -m "SELECT {a:1,b:2} as obj"`).toString().trim(); |
| 44 | + assert.strictEqual(result, '[{"obj":{"a":1,"b":2}}]'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('5. Should show version with -v flag', function () { |
| 48 | + const result = execSync(`node "${cliPath}" -v`).toString().trim(); |
| 49 | + assert.match(result, /^\d+\.\d+\.\d+/); |
| 50 | + }); |
| 51 | + |
| 52 | + it('6. Should output AST with --ast flag', function () { |
| 53 | + const result = execSync(`node "${cliPath}" --ast "SELECT 1"`).toString().trim(); |
| 54 | + const ast = JSON.parse(result); |
| 55 | + assert.strictEqual(ast.statements[0].columns[0].value, 1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('7. Should handle file not found error', function () { |
| 59 | + try { |
| 60 | + execSync(`node "${cliPath}" -f "nonexistent.sql"`, {stdio: 'pipe'}); |
| 61 | + assert.fail('Should have thrown an error'); |
| 62 | + } catch (error) { |
| 63 | + assert.match(error.stderr.toString(), /Error: file not found/); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it('8. Should handle piped input data with txt() function - Issue #2149', function () { |
| 68 | + const result = execSync( |
| 69 | + `echo "hello" | node "${cliPath}" "SELECT COUNT(*) > 0 as Success FROM txt()"` |
| 70 | + ).toString(); |
| 71 | + assert.deepEqual( |
| 72 | + [ |
| 73 | + { |
| 74 | + Success: true, |
| 75 | + }, |
| 76 | + ], |
| 77 | + JSON.parse(result) |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('9. Should handle piped input data without txt() function - backward compatibility', function () { |
| 82 | + const result = execSync(`echo "SELECT 1 as Success" | node "${cliPath}"`).toString(); |
| 83 | + assert.deepEqual( |
| 84 | + [ |
| 85 | + { |
| 86 | + Success: 1, |
| 87 | + }, |
| 88 | + ], |
| 89 | + JSON.parse(result) |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('10. Should handle redirected file input with txt() function', function () { |
| 94 | + const result = execSync( |
| 95 | + `node "${cliPath}" "SELECT COUNT(*) > 0 as Success FROM txt()" < ${testSqlFile}` |
| 96 | + ).toString(); |
| 97 | + assert.deepEqual( |
| 98 | + [ |
| 99 | + { |
| 100 | + Success: true, |
| 101 | + }, |
| 102 | + ], |
| 103 | + JSON.parse(result) |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('11. Should handle file with txt() function and piped data - Issue #2149', function () { |
| 108 | + const result = execSync( |
| 109 | + `echo "hello world" | node "${cliPath}" -f "${testWithTxtFile}"` |
| 110 | + ).toString(); |
| 111 | + assert.deepEqual( |
| 112 | + [ |
| 113 | + { |
| 114 | + Success: true, |
| 115 | + }, |
| 116 | + ], |
| 117 | + JSON.parse(result) |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('12. Should handle file without txt() function normally', function () { |
| 122 | + const result = execSync(`node "${cliPath}" -f "${testWithoutTxtFile}"`).toString(); |
| 123 | + assert.deepEqual( |
| 124 | + [ |
| 125 | + { |
| 126 | + Success: 1, |
| 127 | + }, |
| 128 | + ], |
| 129 | + JSON.parse(result) |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('13. Should handle piped input to file without txt() function - should be ignored', function () { |
| 134 | + const result = execSync( |
| 135 | + `echo "this should be ignored" | node "${cliPath}" -f "${testWithoutTxtFile}"` |
| 136 | + ).toString(); |
| 137 | + assert.deepEqual( |
| 138 | + [ |
| 139 | + { |
| 140 | + Success: 1, |
| 141 | + }, |
| 142 | + ], |
| 143 | + JSON.parse(result) |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('14. Should handle complex SQL with txt() and piped data', function () { |
| 148 | + const result = execSync( |
| 149 | + `echo -e "line1\nline2\nline3" | node "${cliPath}" "SELECT COUNT(*) as LineCount FROM txt()"` |
| 150 | + ).toString(); |
| 151 | + assert.deepEqual( |
| 152 | + [ |
| 153 | + { |
| 154 | + LineCount: 3, |
| 155 | + }, |
| 156 | + ], |
| 157 | + JSON.parse(result) |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it('15. Should handle empty SQL error', function () { |
| 162 | + try { |
| 163 | + execSync(`node "${cliPath}" ""`, {stdio: 'pipe'}); |
| 164 | + assert.fail('Should have thrown an error'); |
| 165 | + } catch (error) { |
| 166 | + assert.match(error.stderr.toString(), /No SQL to process/); |
| 167 | + } |
| 168 | + }); |
| 169 | +}); |
0 commit comments