|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const path = require('path'); |
| 5 | +const { spawn } = require('child_process'); |
| 6 | +const fs = require('fs'); |
| 7 | +const { promises: fsPromises } = require('fs'); |
| 8 | + |
| 9 | +describe('test/bin.test.js', () => { |
| 10 | + const binPath = path.join(__dirname, '../bin/easy-i18n-cli.js'); |
| 11 | + const testDistDir = path.join(__dirname, 'fixtures', 'output'); |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + // Ensure output directory exists |
| 15 | + await fsPromises.mkdir(testDistDir, { recursive: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + function runCLI(args, options = {}) { |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + const proc = spawn('node', [binPath, ...args], { |
| 21 | + cwd: options.cwd || process.cwd(), |
| 22 | + env: { ...process.env, ...options.env }, |
| 23 | + }); |
| 24 | + |
| 25 | + let stdout = ''; |
| 26 | + let stderr = ''; |
| 27 | + |
| 28 | + proc.stdout.on('data', (data) => { |
| 29 | + stdout += data.toString(); |
| 30 | + }); |
| 31 | + |
| 32 | + proc.stderr.on('data', (data) => { |
| 33 | + stderr += data.toString(); |
| 34 | + }); |
| 35 | + |
| 36 | + proc.on('close', (code) => { |
| 37 | + resolve({ code, stdout, stderr }); |
| 38 | + }); |
| 39 | + |
| 40 | + proc.on('error', (err) => { |
| 41 | + reject(err); |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + describe('--version flag', () => { |
| 47 | + it('should display version and exit', async () => { |
| 48 | + const result = await runCLI(['--version']); |
| 49 | + assert.equal(result.code, 0); |
| 50 | + assert(result.stdout.includes('2.0.3') || result.stdout.match(/\d+\.\d+\.\d+/)); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should display version with -v flag', async () => { |
| 54 | + const result = await runCLI(['-v']); |
| 55 | + assert.equal(result.code, 0); |
| 56 | + assert(result.stdout.includes('2.0.3') || result.stdout.match(/\d+\.\d+\.\d+/)); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('--config flag', () => { |
| 61 | + it('should load config from specified file', async () => { |
| 62 | + const configFile = path.join(__dirname, 'fixtures', 'test-config.js'); |
| 63 | + const outputFile = path.join(testDistDir, 'cli-config-test.js'); |
| 64 | + |
| 65 | + // Create a test config |
| 66 | + await fsPromises.writeFile( |
| 67 | + configFile, |
| 68 | + `const path = require('path'); |
| 69 | +module.exports = { |
| 70 | + srcDirs: ['file1.js'], |
| 71 | + distDir: path.join(__dirname, 'output'), |
| 72 | + distFileName: 'cli-config-test.js', |
| 73 | + cwd: __dirname, |
| 74 | + };` |
| 75 | + ); |
| 76 | + |
| 77 | + const result = await runCLI(['-c', configFile]); |
| 78 | + assert.equal(result.code, 0); |
| 79 | + assert(fs.existsSync(outputFile)); |
| 80 | + |
| 81 | + // Clean up |
| 82 | + await fsPromises.unlink(configFile); |
| 83 | + await fsPromises.unlink(outputFile); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should work with --config long form', async () => { |
| 87 | + const configFile = path.join(__dirname, 'fixtures', 'test-config2.js'); |
| 88 | + const outputFile = path.join(testDistDir, 'cli-config-test2.js'); |
| 89 | + |
| 90 | + // Create a test config |
| 91 | + await fsPromises.writeFile( |
| 92 | + configFile, |
| 93 | + `const path = require('path'); |
| 94 | +module.exports = { |
| 95 | + srcDirs: ['file1.js'], |
| 96 | + distDir: path.join(__dirname, 'output'), |
| 97 | + distFileName: 'cli-config-test2.js', |
| 98 | + cwd: __dirname, |
| 99 | + };` |
| 100 | + ); |
| 101 | + |
| 102 | + const result = await runCLI(['--config', configFile]); |
| 103 | + assert.equal(result.code, 0); |
| 104 | + assert(fs.existsSync(outputFile)); |
| 105 | + |
| 106 | + // Clean up |
| 107 | + await fsPromises.unlink(configFile); |
| 108 | + await fsPromises.unlink(outputFile); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('--check flag', () => { |
| 113 | + it('should check locale file for untranslated text', async () => { |
| 114 | + const configFile = path.join(__dirname, 'fixtures', 'check-config.js'); |
| 115 | + const outputFile = path.join(testDistDir, 'check-test.js'); |
| 116 | + |
| 117 | + // Create output file with Chinese text directly |
| 118 | + await fsPromises.writeFile( |
| 119 | + outputFile, |
| 120 | + 'module.exports = { "测试1": "测试1" };' |
| 121 | + ); |
| 122 | + |
| 123 | + // Create a config that points to this file |
| 124 | + await fsPromises.writeFile( |
| 125 | + configFile, |
| 126 | + `const path = require('path'); |
| 127 | +module.exports = { |
| 128 | + srcDirs: ['file1.js'], |
| 129 | + distDir: path.join(__dirname, 'output'), |
| 130 | + distFileName: 'check-test.js', |
| 131 | + cwd: __dirname, |
| 132 | + };` |
| 133 | + ); |
| 134 | + |
| 135 | + // Now check should fail |
| 136 | + const result = await runCLI(['--check', '-c', configFile]); |
| 137 | + // CLI logs error to stderr but exits with 0 (current behavior) |
| 138 | + assert(result.stderr.includes('need translate') || result.stderr.includes('Error')); |
| 139 | + |
| 140 | + // Clean up |
| 141 | + await fsPromises.unlink(configFile); |
| 142 | + if (fs.existsSync(outputFile)) { |
| 143 | + await fsPromises.unlink(outputFile); |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + it('should pass check when all text is translated', async () => { |
| 148 | + const configFile = path.join(__dirname, 'fixtures', 'check-pass-config.js'); |
| 149 | + const outputFile = path.join(testDistDir, 'check-pass-test.js'); |
| 150 | + |
| 151 | + // Create output file with translated text |
| 152 | + await fsPromises.writeFile( |
| 153 | + outputFile, |
| 154 | + 'module.exports = { test: "English text" };' |
| 155 | + ); |
| 156 | + |
| 157 | + // Create a test config |
| 158 | + await fsPromises.writeFile( |
| 159 | + configFile, |
| 160 | + `const path = require('path'); |
| 161 | +module.exports = { |
| 162 | + srcDirs: ['file1.js'], |
| 163 | + distDir: path.join(__dirname, 'output'), |
| 164 | + distFileName: 'check-pass-test.js', |
| 165 | + cwd: __dirname, |
| 166 | + };` |
| 167 | + ); |
| 168 | + |
| 169 | + const result = await runCLI(['--check', '-c', configFile]); |
| 170 | + assert.equal(result.code, 0); |
| 171 | + |
| 172 | + // Clean up |
| 173 | + await fsPromises.unlink(configFile); |
| 174 | + await fsPromises.unlink(outputFile); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('default behavior', () => { |
| 179 | + it('should run extraction without flags', async () => { |
| 180 | + const configFile = path.join(__dirname, 'fixtures', 'default-config.js'); |
| 181 | + const outputFile = path.join(testDistDir, 'default-test.js'); |
| 182 | + |
| 183 | + // Create a test config |
| 184 | + await fsPromises.writeFile( |
| 185 | + configFile, |
| 186 | + `const path = require('path'); |
| 187 | +module.exports = { |
| 188 | + srcDirs: ['file1.js'], |
| 189 | + distDir: path.join(__dirname, 'output'), |
| 190 | + distFileName: 'default-test.js', |
| 191 | + cwd: __dirname, |
| 192 | + };` |
| 193 | + ); |
| 194 | + |
| 195 | + const result = await runCLI(['-c', configFile]); |
| 196 | + assert.equal(result.code, 0); |
| 197 | + assert(fs.existsSync(outputFile)); |
| 198 | + |
| 199 | + // Clean up |
| 200 | + await fsPromises.unlink(configFile); |
| 201 | + await fsPromises.unlink(outputFile); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should handle running without config', async () => { |
| 205 | + // Running without config will use default options which may error |
| 206 | + // but it should at least try to run |
| 207 | + const result = await runCLI([]); |
| 208 | + // May succeed or fail depending on default paths, just check it doesn't crash |
| 209 | + assert(typeof result.code === 'number'); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments