Add validation tests to translations files

This commit is contained in:
Deluan 2020-05-04 19:54:10 -04:00
parent 8f66e87099
commit 8d99c3ab92
3 changed files with 63 additions and 10 deletions

View file

@ -27,6 +27,8 @@ const (
RequestThrottleBacklogLimit = 100 RequestThrottleBacklogLimit = 100
RequestThrottleBacklogTimeout = time.Minute RequestThrottleBacklogTimeout = time.Minute
I18nFolder = "i18n"
) )
// Cache options // Cache options
@ -62,5 +64,4 @@ var (
VariousArtists = "Various Artists" VariousArtists = "Various Artists"
VariousArtistsID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(VariousArtists)))) VariousArtistsID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(VariousArtists))))
UnknownArtist = "[Unknown Artist]" UnknownArtist = "[Unknown Artist]"
UnknownArtistID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(UnknownArtist))))
) )

View file

@ -4,17 +4,18 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"io/ioutil"
"net/http"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/resources" "github.com/deluan/navidrome/resources"
"github.com/deluan/rest" "github.com/deluan/rest"
) )
const i18nFolder = "i18n"
type translation struct { type translation struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@ -27,7 +28,8 @@ var (
) )
func newTranslationRepository(context.Context) rest.Repository { func newTranslationRepository(context.Context) rest.Repository {
if err := loadTranslations(); err != nil { dir := resources.AssetFile()
if err := loadTranslations(dir); err != nil {
log.Error("Error loading translation files", err) log.Error("Error loading translation files", err)
} }
return &translationRepository{} return &translationRepository{}
@ -65,10 +67,10 @@ func (r *translationRepository) NewInstance() interface{} {
return &translation{} return &translation{}
} }
func loadTranslations() (loadError error) { func loadTranslations(fs http.FileSystem) (loadError error) {
once.Do(func() { once.Do(func() {
translations = make(map[string]translation) translations = make(map[string]translation)
dir, err := resources.AssetFile().Open(i18nFolder) dir, err := fs.Open(consts.I18nFolder)
if err != nil { if err != nil {
loadError = err loadError = err
return return
@ -80,7 +82,7 @@ func loadTranslations() (loadError error) {
} }
var languages []string var languages []string
for _, f := range files { for _, f := range files {
t, err := loadTranslation(f.Name()) t, err := loadTranslation(fs, f.Name())
if err != nil { if err != nil {
log.Error("Error loading translation file", "file", f.Name(), err) log.Error("Error loading translation file", "file", f.Name(), err)
continue continue
@ -93,17 +95,18 @@ func loadTranslations() (loadError error) {
return return
} }
func loadTranslation(fileName string) (translation translation, err error) { func loadTranslation(fs http.FileSystem, fileName string) (translation translation, err error) {
// Get id and full path // Get id and full path
name := filepath.Base(fileName) name := filepath.Base(fileName)
id := strings.TrimSuffix(name, filepath.Ext(name)) id := strings.TrimSuffix(name, filepath.Ext(name))
filePath := filepath.Join(i18nFolder, name) filePath := filepath.Join(consts.I18nFolder, name)
// Load translation from json file // Load translation from json file
data, err := resources.Asset(filePath) file, err := fs.Open(filePath)
if err != nil { if err != nil {
return return
} }
data, err := ioutil.ReadAll(file)
var out map[string]interface{} var out map[string]interface{}
if err = json.Unmarshal(data, &out); err != nil { if err = json.Unmarshal(data, &out); err != nil {
return return

View file

@ -0,0 +1,49 @@
package app
import (
"encoding/json"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/resources"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Translations", func() {
Describe("I18n files", func() {
var fs http.FileSystem
BeforeEach(func() {
fs = resources.AssetFile()
})
It("contains only valid json language files", func() {
dir, _ := fs.Open(consts.I18nFolder)
files, _ := dir.Readdir(0)
for _, f := range files {
name := filepath.Base(f.Name())
filePath := filepath.Join(consts.I18nFolder, name)
file, _ := fs.Open(filePath)
data, _ := ioutil.ReadAll(file)
var out map[string]interface{}
Expect(filepath.Ext(filePath)).To(Equal(".json"), filePath)
Expect(json.Unmarshal(data, &out)).To(BeNil(), filePath)
Expect(out["languageName"]).ToNot(BeEmpty(), filePath)
}
})
})
Describe("loadTranslation", func() {
It("loads a translation file correctly", func() {
fs := http.Dir("ui/src")
tr, err := loadTranslation(fs, "en.json")
Expect(err).To(BeNil())
Expect(tr.ID).To(Equal("en"))
Expect(tr.Name).To(Equal("English"))
var out map[string]interface{}
Expect(json.Unmarshal([]byte(tr.Data), &out)).To(BeNil())
})
})
})