diff --git a/v2/goi18n/extract_command.go b/v2/goi18n/extract_command.go index d06c2639..05774cc9 100644 --- a/v2/goi18n/extract_command.go +++ b/v2/goi18n/extract_command.go @@ -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" @@ -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 diff --git a/v2/goi18n/extract_command_test.go b/v2/goi18n/extract_command_test.go index efee57e7..624ff59f 100644 --- a/v2/goi18n/extract_command_test.go +++ b/v2/goi18n/extract_command_test.go @@ -2,11 +2,9 @@ package main import ( "bytes" - "encoding/json" "io/ioutil" "os" "path/filepath" - "reflect" "testing" "github.com/nicksnyder/go-i18n/v2/i18n" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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) } @@ -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) -}