Skip to content

Commit

Permalink
don't extract strings from _test.go files (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder authored May 2, 2019
1 parent 5a9fdb7 commit 45eb7b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
6 changes: 6 additions & 0 deletions v2/goi18n/extract_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/nicksnyder/go-i18n/v2/internal"
Expand Down Expand Up @@ -80,6 +81,11 @@ func (ec *extractCommand) execute() error {
return nil
}

// Don't extract from test files.
if strings.HasSuffix(path, "_test.go") {
return nil
}

buf, err := ioutil.ReadFile(path)
if err != nil {
return err
Expand Down
41 changes: 15 additions & 26 deletions v2/goi18n/extract_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -16,17 +14,20 @@ func TestExtract(t *testing.T) {

tests := []struct {
name string
fileName string
file string
messages []*i18n.Message
activeFile []byte
}{
{
name: "no translations",
fileName: "file.go",
file: `package main`,
messages: nil,
},
{
name: "global declaration",
name: "global declaration",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -42,7 +43,8 @@ func TestExtract(t *testing.T) {
},
},
{
name: "short form id only",
name: "no extract from test",
fileName: "file_test.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -60,7 +62,8 @@ func TestExtract(t *testing.T) {
},
},
{
name: "must short form id only",
name: "must short form id only",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -78,7 +81,8 @@ func TestExtract(t *testing.T) {
},
},
{
name: "custom package name",
name: "custom package name",
fileName: "file.go",
file: `package main
import bar "github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -96,7 +100,8 @@ func TestExtract(t *testing.T) {
},
},
{
name: "exhaustive plural translation",
name: "exhaustive plural translation",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
Expand Down Expand Up @@ -137,7 +142,8 @@ zero = "Zero translation"
`),
},
{
name: "concat id",
name: "concat id",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -159,22 +165,13 @@ zero = "Zero translation"
}

for _, test := range tests {
t.Run(test.name+" messages", func(t *testing.T) {
actualMessages, err := extractMessages([]byte(test.file))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(actualMessages, test.messages) {
t.Fatalf("file:\n%s\nexpected: %s\n got: %s", test.file, marshalTest(test.messages), marshalTest(actualMessages))
}
})
t.Run(test.name+" active file", func(t *testing.T) {
indir := mustTempDir("TestExtractCommandIn")
defer os.RemoveAll(indir)
outdir := mustTempDir("TestExtractCommandOut")
defer os.RemoveAll(outdir)

inpath := filepath.Join(indir, "file.go")
inpath := filepath.Join(indir, test.fileName)
if err := ioutil.WriteFile(inpath, []byte(test.file), 0666); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -237,11 +234,3 @@ other = "{{.Name}} has {{.UnreadEmailCount}} unread emails."
t.Fatalf("files not equal\nactual:\n%s\nexpected:\n%s", actual, expected)
}
}

func marshalTest(value interface{}) string {
buf, err := json.MarshalIndent(value, "", " ")
if err != nil {
panic(err)
}
return string(buf)
}

0 comments on commit 45eb7b3

Please sign in to comment.