|
| 1 | +import {command} from "../lingui-compile" |
| 2 | +import {makeConfig} from "@lingui/conf" |
| 3 | +import path from "path" |
| 4 | +import {getConsoleMockCalls, mockConsole} from "@lingui/jest-mocks" |
| 5 | +import mockFs from "mock-fs" |
| 6 | +import fs from 'fs' |
| 7 | + |
| 8 | +function readFsToJson(directory: string, filter?: (filename: string) => boolean) { |
| 9 | + const out = {} |
| 10 | + |
| 11 | + fs |
| 12 | + .readdirSync(directory) |
| 13 | + .map((filename) => { |
| 14 | + const filepath = path.join(directory, filename) |
| 15 | + |
| 16 | + if (fs.lstatSync(filepath).isDirectory()) { |
| 17 | + out[filename] = readFsToJson(filepath) |
| 18 | + return out |
| 19 | + } |
| 20 | + |
| 21 | + if (!filter || filter(filename)) { |
| 22 | + out[filename] = fs.readFileSync(filepath).toString() |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + return out |
| 27 | +} |
| 28 | + |
| 29 | +describe('CLI Command: Compile', () => { |
| 30 | + xit('Should return error if no messages', () => { |
| 31 | + const config = makeConfig({ |
| 32 | + locales: ['en', 'pl'], |
| 33 | + rootDir: path.join(__dirname, 'fixtures/compile'), |
| 34 | + catalogs: [{ |
| 35 | + path: '<rootDir>/{locale}', |
| 36 | + include: ['<rootDir>'] |
| 37 | + }] |
| 38 | + }) |
| 39 | + |
| 40 | + const result = command(config, {}) |
| 41 | + expect(result).toBeFalsy() |
| 42 | + }) |
| 43 | + |
| 44 | + describe('Merge Catalogs', () => { |
| 45 | + // todo |
| 46 | + }) |
| 47 | + |
| 48 | + describe('Locales Validation', () => { |
| 49 | + // todo: should be moved to @lingui/conf |
| 50 | + it('Should throw error for invalid locale', () => { |
| 51 | + const config = makeConfig({ |
| 52 | + locales: ['abra'], |
| 53 | + rootDir: '/test', |
| 54 | + catalogs: [{ |
| 55 | + path: '<rootDir>/{locale}', |
| 56 | + include: ['<rootDir>'], |
| 57 | + exclude: [] |
| 58 | + }] |
| 59 | + }) |
| 60 | + |
| 61 | + mockFs() |
| 62 | + |
| 63 | + mockConsole((console) => { |
| 64 | + const result = command(config, {}) |
| 65 | + mockFs.restore() |
| 66 | + const log = getConsoleMockCalls(console.error) |
| 67 | + expect(log).toMatchSnapshot() |
| 68 | + |
| 69 | + expect(result).toBeTruthy() |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('Should not throw error for pseudolocale', () => { |
| 74 | + const config = makeConfig({ |
| 75 | + locales: ['abracadabra'], |
| 76 | + rootDir: '/test', |
| 77 | + pseudoLocale: 'abracadabra', |
| 78 | + catalogs: [{ |
| 79 | + path: '<rootDir>/{locale}', |
| 80 | + include: ['<rootDir>'], |
| 81 | + exclude: [] |
| 82 | + }] |
| 83 | + }) |
| 84 | + |
| 85 | + mockFs() |
| 86 | + |
| 87 | + mockConsole((console) => { |
| 88 | + const result = command(config, {}) |
| 89 | + mockFs.restore() |
| 90 | + expect(console.error).not.toBeCalled() |
| 91 | + expect(result).toBeTruthy() |
| 92 | + }) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('allowEmpty = false', () => { |
| 97 | + const config = makeConfig({ |
| 98 | + locales: ['en', 'pl'], |
| 99 | + sourceLocale: 'en', |
| 100 | + rootDir: '/test', |
| 101 | + catalogs: [{ |
| 102 | + path: '<rootDir>/{locale}', |
| 103 | + include: ['<rootDir>'], |
| 104 | + exclude: [] |
| 105 | + }] |
| 106 | + }) |
| 107 | + |
| 108 | + it('Should show error and stop compilation of catalog ' + |
| 109 | + 'if message doesnt have a translation (no template)', () => { |
| 110 | + mockFs({ |
| 111 | + '/test': { |
| 112 | + 'en.po': ` |
| 113 | +msgid "Hello World" |
| 114 | +msgstr "Hello World" |
| 115 | + `, |
| 116 | + 'pl.po': ` |
| 117 | +msgid "Hello World" |
| 118 | +msgstr "Cześć świat" |
| 119 | +
|
| 120 | +msgid "Test String" |
| 121 | +msgstr "" |
| 122 | + ` |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + mockConsole((console) => { |
| 127 | + const result = command(config, { |
| 128 | + allowEmpty: false |
| 129 | + }) |
| 130 | + const actualFiles = readFsToJson('/test') |
| 131 | + |
| 132 | + expect(actualFiles['pl.js']).toBeFalsy() |
| 133 | + expect(actualFiles['en.js']).toBeTruthy() |
| 134 | + mockFs.restore() |
| 135 | + |
| 136 | + const log = getConsoleMockCalls(console.error) |
| 137 | + expect(log).toMatchSnapshot() |
| 138 | + expect(result).toBeFalsy() |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('Should show error and stop compilation of catalog ' + |
| 143 | + ' if message doesnt have a translation (with template)', () => { |
| 144 | + mockFs({ |
| 145 | + '/test': { |
| 146 | + 'messages.pot': ` |
| 147 | +msgid "Hello World" |
| 148 | +msgstr "" |
| 149 | + `, |
| 150 | + 'pl.po': `` |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + mockConsole((console) => { |
| 155 | + const result = command(config, { |
| 156 | + allowEmpty: false |
| 157 | + }) |
| 158 | + |
| 159 | + const actualFiles = readFsToJson('/test') |
| 160 | + |
| 161 | + expect({ |
| 162 | + pl: actualFiles['pl.js'], |
| 163 | + en: actualFiles['en.js'] |
| 164 | + }).toMatchSnapshot() |
| 165 | + |
| 166 | + mockFs.restore() |
| 167 | + |
| 168 | + const log = getConsoleMockCalls(console.error) |
| 169 | + expect(log).toMatchSnapshot() |
| 170 | + expect(result).toBeFalsy() |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + |
| 175 | + it('Should show missing messages verbosely when verbose = true', () => { |
| 176 | + mockFs({ |
| 177 | + '/test': { |
| 178 | + 'pl.po': ` |
| 179 | +msgid "Hello World" |
| 180 | +msgstr "" |
| 181 | +
|
| 182 | +msgid "Test String" |
| 183 | +msgstr "" |
| 184 | + ` |
| 185 | + } |
| 186 | + }) |
| 187 | + |
| 188 | + mockConsole((console) => { |
| 189 | + const result = command(config, { |
| 190 | + allowEmpty: false, |
| 191 | + verbose: true |
| 192 | + }) |
| 193 | + |
| 194 | + mockFs.restore() |
| 195 | + |
| 196 | + const log = getConsoleMockCalls(console.error) |
| 197 | + expect(log).toMatchSnapshot() |
| 198 | + expect(result).toBeFalsy() |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + }) |
| 203 | +}) |
0 commit comments