Skip to content

Commit

Permalink
test(cli): check for consistent results with partial extracts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-cherel committed Jun 24, 2024
1 parent 6d02e73 commit 2209e41
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 1 deletion.
99 changes: 99 additions & 0 deletions packages/cli/test/extract-obsolete/existing/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"addToCart": {
"translation": "Add To Cart",
"message": "Add To Cart",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
22
],
[
"fixtures/file-a.ts",
23
]
]
},
"f9Atdk": {
"translation": "Hello this is JSX Translation",
"message": "Hello this is JSX Translation",
"comments": [
"this is a comment"
],
"origin": [
[
"fixtures/file-b.tsx",
6
]
]
},
"6qYmGe": {
"translation": "Hello this is JSX Translation",
"message": "Hello this is JSX Translation",
"context": "my context",
"comments": [],
"origin": [
[
"fixtures/file-b.tsx",
11
]
]
},
"1nGWAC": {
"translation": "Hello world",
"message": "Hello world",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
4
]
]
},
"2Ps/YQ": {
"translation": "Hello world",
"message": "Hello world",
"context": "custom context",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
6
]
]
},
"BcXPt3": {
"translation": "Message in descriptor",
"message": "Message in descriptor",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
16
]
]
},
"custom.id": {
"translation": "This message has custom id",
"message": "This message has custom id",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
11
]
],
"obsolete": true
},
"jsx.custom.id": {
"translation": "This JSX element has custom id",
"message": "This JSX element has custom id",
"comments": [],
"origin": [
[
"fixtures/file-b.tsx",
15
]
]
}
}
99 changes: 99 additions & 0 deletions packages/cli/test/extract-obsolete/expected/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"addToCart": {
"translation": "Add To Cart",
"message": "Add To Cart",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
22
],
[
"fixtures/file-a.ts",
23
]
]
},
"f9Atdk": {
"translation": "Hello this is JSX Translation",
"message": "Hello this is JSX Translation",
"comments": [
"this is a comment"
],
"origin": [
[
"fixtures/file-b.tsx",
6
]
]
},
"6qYmGe": {
"translation": "Hello this is JSX Translation",
"message": "Hello this is JSX Translation",
"context": "my context",
"comments": [],
"origin": [
[
"fixtures/file-b.tsx",
11
]
]
},
"1nGWAC": {
"translation": "Hello world",
"message": "Hello world",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
4
]
]
},
"2Ps/YQ": {
"translation": "Hello world",
"message": "Hello world",
"context": "custom context",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
6
]
]
},
"BcXPt3": {
"translation": "Message in descriptor",
"message": "Message in descriptor",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
16
]
]
},
"custom.id": {
"translation": "This message has custom id",
"message": "This message has custom id",
"comments": [],
"origin": [
[
"fixtures/file-a.ts",
11
]
],
"obsolete": true
},
"jsx.custom.id": {
"translation": "Updated: This JSX element has custom id",
"message": "Updated: This JSX element has custom id",
"comments": [],
"origin": [
[
"fixtures/file-b.tsx",
15
]
]
}
}
23 changes: 23 additions & 0 deletions packages/cli/test/extract-obsolete/fixtures/file-a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { i18n } from "@lingui/core"
import { defineMessage, t } from "@lingui/macro"

const msg = t`Hello world`

const msg2 = t({
message: "Hello world",
context: "custom context",
})

const msg3 = null /* original translation commented to mark message obsolete *//*t({
message: "This message has custom id",
id: "custom.id",
})*/

const msgDescriptor = defineMessage({
message: "Message in descriptor",
})

i18n._(msgDescriptor)

i18n._("addToCart")
i18n._({id: "addToCart", message: "Add To Cart with change ignored"})
16 changes: 16 additions & 0 deletions packages/cli/test/extract-obsolete/fixtures/file-b.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Trans } from "@lingui/macro"
import React from "react"

export function MyComponent() {
return (
<Trans comment={"this is a comment"}>Hello this is JSX Translation</Trans>
)
}

export function MyComponent2() {
return <Trans context="my context">Hello this is JSX Translation</Trans>
}

export function MyComponent3() {
return <Trans id={"jsx.custom.id"}>Updated: This JSX element has custom id</Trans>
}
36 changes: 35 additions & 1 deletion packages/cli/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { command as compileCommand } from "../src/lingui-compile"
import fs from "fs/promises"
import os from "os"
import nodepath from "path"
import glob from "glob"
import { makeConfig } from "@lingui/conf"
import { listingToHumanReadable, readFsToJson } from "../src/tests"
import { getConsoleMockCalls, mockConsole } from "@lingui/jest-mocks"
Expand All @@ -22,13 +23,18 @@ async function prepare(caseFolderName: string) {

const actualPath = nodepath.join(rootDir, "actual")
const expectedPath = nodepath.join(rootDir, "expected")
const existingPath = nodepath.join(rootDir, "existing")

await fs.rm(actualPath, {
recursive: true,
force: true,
})

return { rootDir, actualPath, expectedPath }
if (glob.sync(existingPath).length === 1) {
await fs.cp(existingPath, actualPath, { recursive: true })
}

return { rootDir, actualPath, existingPath, expectedPath }
}

describe("E2E Extractor Test", () => {
Expand Down Expand Up @@ -288,4 +294,32 @@ describe("E2E Extractor Test", () => {
compareFolders(actualPath, expectedPath)
})
})

it("should extract consistently with files argument", async () => {
const { formatter } = await import("../../format-json")
const { rootDir, actualPath, expectedPath } = await prepare(
"extract-obsolete"
)

await extractCommand(
makeConfig({
rootDir: rootDir,
locales: ["en"],
sourceLocale: "en",
format: formatter({ style: "lingui" }),
catalogs: [
{
path: "<rootDir>/actual/{locale}",
include: ["<rootDir>/fixtures"],
},
],
}),
{
files: [nodepath.join(rootDir, "fixtures", "file-b.tsx")]
}
)

compareFolders(actualPath, expectedPath)
})

})

0 comments on commit 2209e41

Please sign in to comment.