Repository navigation
feat(i18n): keep translation source hashes in .review.json (spec 019 T078) #849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5148bb3
feat(i18n): keep translation source hashes in .review.json
claude 762510b
chore(i18n): move the French source hashes to fr/.review.json
claude a7905b3
docs(specs): document review files and tick Weblate tasks (spec 019)
claude 8703f21
refactor(i18n): wrap review marshal errors in encodeJSON
claude 898c17a
fix(operator): skip the locales review file in module folders
claude 3558403
test(gp-module): check stamp writes locales/.review.json
claude 68cce9e
fix(i18n): never drop a review hash when stamping fails
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package i18n | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/GameplanePanel/gameplane/i18n/pack" | ||
| ) | ||
|
|
||
| // TestStampMovesInlineHashToReview checks that Stamp moves an inline source hash into the review file. | ||
| func TestStampMovesInlineHashToReview(t *testing.T) { | ||
| t.Parallel() | ||
| dir := writeModule(t, testModule, testTemplate) | ||
| locales := filepath.Join(dir, "locales") | ||
| if err := os.MkdirAll(locales, 0o750); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| hash := pack.SourceHash("Demo Game") | ||
| fr := filepath.Join(locales, "fr.json") | ||
| body := `{"template.displayName":{"defaultMessage":"Jeu","sourceHash":"` + hash + `"}}` | ||
| if err := os.WriteFile(fr, []byte(body), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| stamped, err := Stamp(dir) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(stamped) != 2 { | ||
| t.Fatalf("Stamp = %v; want fr.json and .review.json", stamped) | ||
| } | ||
| frData, err := os.ReadFile(fr) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if strings.Contains(string(frData), "sourceHash") { | ||
| t.Errorf("fr.json still carries sourceHash: %s", frData) | ||
| } | ||
| reviewData, err := os.ReadFile(filepath.Join(locales, pack.ReviewFile)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| review, err := pack.ParseModuleReview(reviewData) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := review["fr"]["template.displayName"]; got != hash { | ||
| t.Errorf("review hash = %q; want %q", got, hash) | ||
| } | ||
| } | ||
|
|
||
| // TestStampKeepsExistingReview checks that Stamp keeps an existing review entry instead of overwriting it. | ||
| func TestStampKeepsExistingReview(t *testing.T) { | ||
| t.Parallel() | ||
| dir := writeModule(t, testModule, testTemplate) | ||
| locales := filepath.Join(dir, "locales") | ||
| if err := os.MkdirAll(locales, 0o750); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| hash := pack.SourceHash("Demo Game") | ||
| review := `{"fr":{"template.displayName":"` + hash + `"}}` | ||
| if err := os.WriteFile(filepath.Join(locales, pack.ReviewFile), []byte(review), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(locales, "fr.json"), []byte(`{"template.displayName":{"defaultMessage":"Jeu"}}`), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| stamped, err := Stamp(dir) | ||
| if err != nil || len(stamped) != 0 { | ||
| t.Fatalf("Stamp = %v, %v; want a no-op", stamped, err) | ||
| } | ||
| after, err := os.ReadFile(filepath.Join(locales, pack.ReviewFile)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(after) != review { | ||
| t.Errorf("review file was rewritten: %q", after) | ||
| } | ||
| } | ||
|
|
||
| // TestStampMalformedReviewIsError checks that Stamp reports an error when the review file is malformed. | ||
| func TestStampMalformedReviewIsError(t *testing.T) { | ||
| t.Parallel() | ||
| dir := writeModule(t, testModule, testTemplate) | ||
| locales := filepath.Join(dir, "locales") | ||
| if err := os.MkdirAll(locales, 0o750); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(locales, pack.ReviewFile), []byte(`[`), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err := Stamp(dir) | ||
| if err == nil || !strings.Contains(err.Error(), pack.ReviewFile) { | ||
| t.Fatalf("Stamp error = %v; want one naming %s", err, pack.ReviewFile) | ||
| } | ||
| } | ||
|
|
||
| // TestStampWritesNothingWhenALocaleIsMalformed checks that a malformed locale | ||
| // stops Stamp before any locale file loses its inline hashes. | ||
| func TestStampWritesNothingWhenALocaleIsMalformed(t *testing.T) { | ||
| t.Parallel() | ||
| dir := writeModule(t, testModule, testTemplate) | ||
| locales := filepath.Join(dir, "locales") | ||
| if err := os.MkdirAll(locales, 0o750); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(locales, "en.json"), []byte(`{}`), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| de := `{"template.displayName":{"defaultMessage":"Spiel","sourceHash":"000000000000"}}` | ||
| if err := os.WriteFile(filepath.Join(locales, "de.json"), []byte(de), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(locales, "fr.json"), []byte(`{`), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := Stamp(dir); err == nil { | ||
| t.Fatal("Stamp succeeded with a malformed fr.json") | ||
| } | ||
| got, err := os.ReadFile(filepath.Join(locales, "de.json")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != de { | ||
| t.Errorf("de.json = %q; want it unchanged", got) | ||
| } | ||
| if _, err := os.Stat(filepath.Join(locales, pack.ReviewFile)); !errors.Is(err, fs.ErrNotExist) { | ||
| t.Errorf("review file exists or stat failed: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.