From ce633313604ccc17706b5343a79a2483cb3df6eb Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Tue, 2 Jan 2024 17:08:55 +0900 Subject: [PATCH 01/30] Using the x/text package for number formatting --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3a68a1b7..bc51829f 100644 --- a/go.mod +++ b/go.mod @@ -46,6 +46,7 @@ require ( golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb golang.org/x/image v0.10.0 golang.org/x/oauth2 v0.7.0 + golang.org/x/text v0.14.0 golang.org/x/tools v0.13.0 google.golang.org/api v0.114.0 gopkg.in/ini.v1 v1.67.0 @@ -115,7 +116,6 @@ require ( golang.org/x/net v0.19.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.15.0 // indirect - golang.org/x/text v0.14.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect From 71bfa2a8ab1f90aa6dd446f265c93193fcb68cf0 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Tue, 2 Jan 2024 17:11:06 +0900 Subject: [PATCH 02/30] Make custom metrics numbers easy to read --- report/custom.go | 62 ++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/report/custom.go b/report/custom.go index 56a2e120..53022173 100644 --- a/report/custom.go +++ b/report/custom.go @@ -11,6 +11,9 @@ import ( "github.com/olekukonko/tablewriter" "github.com/samber/lo" "github.com/xeipuuv/gojsonschema" + "golang.org/x/text/language" + "golang.org/x/text/message" + "golang.org/x/text/number" ) const swapXYMin = 5 @@ -70,13 +73,7 @@ func (s *CustomMetricSet) Table() string { ) for _, m := range s.Metrics { h = append(h, m.Name) - var v string - if isInt(m.Value) { - v = fmt.Sprintf("%d%s", int(m.Value), m.Unit) - } else { - v = fmt.Sprintf("%.1f%s", m.Value, m.Unit) - } - d = append(d, v) + d = append(d, fmt.Sprintf("%s%s", convertFormat(m.Value), m.Unit)) } buf := new(bytes.Buffer) _, _ = buf.WriteString(fmt.Sprintf("## %s\n\n", s.Name)) //nostyle:handlerrors @@ -101,13 +98,7 @@ func (s *CustomMetricSet) tableSwaped() string { table.SetCenterSeparator("|") table.SetHeader([]string{"", makeHeadTitleWithLink(s.report.Ref, s.report.Commit, nil)}) for _, m := range s.Metrics { - var v string - if isInt(m.Value) { - v = fmt.Sprintf("%d%s", int(m.Value), m.Unit) - } else { - v = fmt.Sprintf("%.1f%s", m.Value, m.Unit) - } - table.Append([]string{m.Name, v}) + table.Append([]string{m.Name, fmt.Sprintf("%s%s", convertFormat(m.Value), m.Unit)}) } table.Render() return strings.Replace(buf.String(), "---|", "--:|", len(s.Metrics)) @@ -163,13 +154,7 @@ func (s *CustomMetricSet) Out(w io.Writer) error { if m.Name == "" { m.Name = m.Key } - var v string - if isInt(m.Value) { - v = fmt.Sprintf("%d%s", int(m.Value), m.Unit) - } else { - v = fmt.Sprintf("%.1f%s", m.Value, m.Unit) - } - table.Rich([]string{m.Name, v}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) + table.Rich([]string{m.Name, fmt.Sprintf("%s%s", convertFormat(m.Value), m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) } table.Render() @@ -283,30 +268,20 @@ func (d *DiffCustomMetricSet) Table() string { continue case m.A != nil && m.B == nil: vb = "" - if isInt(*m.A) { - va = fmt.Sprintf("%d%s", int(*m.A), m.customMetricA.Unit) - diff = fmt.Sprintf("%d%s", int(m.Diff), m.customMetricA.Unit) - } else { - va = fmt.Sprintf("%.1f%s", *m.A, m.customMetricA.Unit) - diff = fmt.Sprintf("%.1f%s", m.Diff, m.customMetricA.Unit) - } + va = fmt.Sprintf("%s%s", convertFormat(*m.A), m.customMetricA.Unit) + diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricA.Unit) case m.A == nil && m.B != nil: va = "" - if isInt(*m.B) { - vb = fmt.Sprintf("%d%s", int(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%d%s", int(m.Diff), m.customMetricB.Unit) - } else { - vb = fmt.Sprintf("%.1f%s", *m.B, m.customMetricB.Unit) - diff = fmt.Sprintf("%.1f%s", m.Diff, m.customMetricB.Unit) - } + va = fmt.Sprintf("%s%s", convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricB.Unit) case isInt(*m.A) && isInt(*m.B): - va = fmt.Sprintf("%d%s", int(*m.A), m.customMetricA.Unit) - vb = fmt.Sprintf("%d%s", int(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%d%s", int(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", convertFormat(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%s%s", convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricA.Unit) default: - va = fmt.Sprintf("%.1f%s", *m.A, m.customMetricA.Unit) - vb = fmt.Sprintf("%.1f%s", *m.B, m.customMetricB.Unit) - diff = fmt.Sprintf("%.1f%s", m.Diff, m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", convertFormat(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%s%s", convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricA.Unit) } if m.Name == "" { m.Name = m.Key @@ -353,3 +328,8 @@ func (d *DiffCustomMetricSet) MetadataTable() string { func isInt(v float64) bool { return v == float64(int64(v)) } + +func convertFormat(v interface{}) string { + p := message.NewPrinter(language.Japanese) + return p.Sprint(number.Decimal(v)) +} From ba6759f01ea4cf96c9e9b156b3fafa4d43d41911 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Tue, 2 Jan 2024 17:11:59 +0900 Subject: [PATCH 03/30] Test the added functions --- report/custom_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/report/custom_test.go b/report/custom_test.go index f6ffcb67..a793af44 100644 --- a/report/custom_test.go +++ b/report/custom_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "testing" + "github.com/google/go-cmp/cmp" "github.com/tenntenn/golden" ) @@ -343,3 +344,40 @@ func TestDiffCustomMetricSetMetadataTable(t *testing.T) { }) } } + +func TestConvertFormat(t *testing.T) { + tests := []struct { + n interface{} + want string + }{ + { + int(10), + "10", + }, + { + int32(3200), + "3,200", + }, + { + float32(10.0), + "10", + }, + { + float32(1000.1), + "1,000.1", + }, + { + int(1000), + "1,000", + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + got := convertFormat(tt.n) + if diff := cmp.Diff(got, tt.want, nil); diff != "" { + t.Error(diff) + } + }) + } +} From 63ae99b33ed73864575ccbf676d0c957588f40a3 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Tue, 2 Jan 2024 17:12:22 +0900 Subject: [PATCH 04/30] Modify expectations --- testdata/custom_metrics/custom_metric_set_out.1.golden | 2 +- testdata/custom_metrics/custom_metric_set_out.2.golden | 4 ++-- .../custom_metrics/custom_metric_set_table.1.golden | 2 +- .../custom_metrics/custom_metric_set_table.2.golden | 2 +- .../custom_metrics/custom_metric_set_table.3.golden | 10 +++++----- .../diff_custom_metric_set_table.0.golden | 2 +- .../diff_custom_metric_set_table.1.golden | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/testdata/custom_metrics/custom_metric_set_out.1.golden b/testdata/custom_metrics/custom_metric_set_out.1.golden index 51826c74..3d7c195a 100644 --- a/testdata/custom_metrics/custom_metric_set_out.1.golden +++ b/testdata/custom_metrics/custom_metric_set_out.1.golden @@ -1,4 +1,4 @@  Benchmark-0  main (1234567) --------------------------------------------- - Number of iterations 1000 + Number of iterations 1,000 Nanoseconds per iteration 676.5 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.golden b/testdata/custom_metrics/custom_metric_set_out.2.golden index 4c953b44..ece4782e 100644 --- a/testdata/custom_metrics/custom_metric_set_out.2.golden +++ b/testdata/custom_metrics/custom_metric_set_out.2.golden @@ -1,4 +1,4 @@  Benchmark-1  main (1234567) --------------------------------------------- - Number of iterations 1500 - Nanoseconds per iteration 1340 ns/op + Number of iterations 1,500 + Nanoseconds per iteration 1,340 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_table.1.golden b/testdata/custom_metrics/custom_metric_set_table.1.golden index f06e0e32..f11339ed 100644 --- a/testdata/custom_metrics/custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/custom_metric_set_table.1.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1000 | 676.5 ns/op | +| 1,000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.golden b/testdata/custom_metrics/custom_metric_set_table.2.golden index bd58b389..fd776ac5 100644 --- a/testdata/custom_metrics/custom_metric_set_table.2.golden +++ b/testdata/custom_metrics/custom_metric_set_table.2.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1500 | 1340 ns/op | +| 1,500 | 1,340 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.3.golden b/testdata/custom_metrics/custom_metric_set_table.3.golden index 83b683b5..06e52f7c 100644 --- a/testdata/custom_metrics/custom_metric_set_table.3.golden +++ b/testdata/custom_metrics/custom_metric_set_table.3.golden @@ -2,8 +2,8 @@ | | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | |----------:|------------------------------------------------------------------:| -| Metrics A | 1500 | -| Metrics B | 1340 | -| Metrics C | 1600 | -| Metrics D | 1010 | -| Metrics E | 1800 | +| Metrics A | 1,500 | +| Metrics B | 1,340 | +| Metrics C | 1,600 | +| Metrics D | 1,010 | +| Metrics E | 1,800 | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden index f06e0e32..f11339ed 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1000 | 676.5 ns/op | +| 1,000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden index 8a078aa4..235b7d29 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden @@ -2,5 +2,5 @@ | | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | |-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| -| **Number of iterations** | 9393 | 1000 | -8393 | -| **Nanoseconds per iteration** | 456.0 ns/op | 676.5 ns/op | 220.5 ns/op | +| **Number of iterations** | 9,393 | 1,000 | -8,393 | +| **Nanoseconds per iteration** | 456 ns/op | 676.5 ns/op | 220.5 ns/op | From b7346e57161e2130a749a844fe6d2d7ade64cdf0 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Wed, 3 Jan 2024 19:30:57 +0900 Subject: [PATCH 05/30] Append locale --- config/config.go | 2 ++ config/yaml.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/config/config.go b/config/config.go index 1b537115..3d9cbc00 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ import ( "github.com/k1LoW/expand" "github.com/k1LoW/octocov/gh" "github.com/k1LoW/octocov/report" + "golang.org/x/text/language" ) const defaultBadgesDatastore = "local://reports" @@ -48,6 +49,7 @@ type Config struct { Body *Body `yaml:"body,omitempty"` Diff *Diff `yaml:"diff,omitempty"` Timeout time.Duration `yaml:"timeout,omitempty"` + Locale *language.Tag `yaml:"locale,omitempty"` GitRoot string `yaml:"-"` // working directory wd string diff --git a/config/yaml.go b/config/yaml.go index eef227ab..081a3b92 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -5,6 +5,7 @@ import ( "github.com/goccy/go-yaml" "github.com/k1LoW/duration" + "golang.org/x/text/language" ) var commentRe = regexp.MustCompile(`(?m)^comment:`) @@ -25,6 +26,7 @@ func (c *Config) UnmarshalYAML(data []byte) error { Body *Body `yaml:"body,omitempty"` Diff *Diff `yaml:"diff,omitempty"` Timeout string `yaml:"timeout,omitempty"` + Locale string `yaml:"locale,omitempty"` }{} err := yaml.Unmarshal(data, &s) if err != nil { @@ -85,6 +87,15 @@ func (c *Config) UnmarshalYAML(data []byte) error { c.Push = v } + if s.Locale != "" { + tag, err := language.Parse(s.Locale) + if err != nil { + return err + } + + c.Locale = &tag + } + return nil } From 9c2fc21a13d94f1ba077316ab050df802e594134 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Wed, 3 Jan 2024 19:32:16 +0900 Subject: [PATCH 06/30] Isolate formatter --- report/custom.go | 9 +-------- report/formatter.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 report/formatter.go diff --git a/report/custom.go b/report/custom.go index 53022173..dcb512e4 100644 --- a/report/custom.go +++ b/report/custom.go @@ -11,9 +11,6 @@ import ( "github.com/olekukonko/tablewriter" "github.com/samber/lo" "github.com/xeipuuv/gojsonschema" - "golang.org/x/text/language" - "golang.org/x/text/message" - "golang.org/x/text/number" ) const swapXYMin = 5 @@ -65,6 +62,7 @@ func (s *CustomMetricSet) Table() string { return "" } if len(s.Metrics) >= swapXYMin { + // TODO: conifg return s.tableSwaped() } var ( @@ -328,8 +326,3 @@ func (d *DiffCustomMetricSet) MetadataTable() string { func isInt(v float64) bool { return v == float64(int64(v)) } - -func convertFormat(v interface{}) string { - p := message.NewPrinter(language.Japanese) - return p.Sprint(number.Decimal(v)) -} diff --git a/report/formatter.go b/report/formatter.go new file mode 100644 index 00000000..af5dbc37 --- /dev/null +++ b/report/formatter.go @@ -0,0 +1,32 @@ +package report + +import ( + _ "embed" + "fmt" + + "golang.org/x/text/language" + "golang.org/x/text/message" + "golang.org/x/text/number" +) + +var locale *language.Tag + +func SetLocale(l *language.Tag) { + locale = l +} + +func convertFormat(v interface{}) string { + if locale != nil { + p := message.NewPrinter(*locale) + return p.Sprint(number.Decimal(v)) + } + + switch vv := v.(type) { + case int, int8, int16, int32, int64: + return fmt.Sprintf("%d", vv) + case float32, float64: + return fmt.Sprintf("%.1f", vv) + default: + panic(fmt.Errorf("convert format error .Unknown type:%v", vv)) + } +} From d236639d47ec8ada262f0c942cc31e0af35d75d2 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Wed, 3 Jan 2024 19:39:59 +0900 Subject: [PATCH 07/30] Set locale --- cmd/comment.go | 1 + report/custom.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/comment.go b/cmd/comment.go index 44911edc..9f43c1be 100644 --- a/cmd/comment.go +++ b/cmd/comment.go @@ -62,6 +62,7 @@ func createReportContent(ctx context.Context, c *config.Config, r, rPrev *report if hideFooterLink { footer = "Reported by octocov" } + report.SetLocale(c.Locale) var ( table, fileTable string customTables []string diff --git a/report/custom.go b/report/custom.go index dcb512e4..40592357 100644 --- a/report/custom.go +++ b/report/custom.go @@ -62,7 +62,6 @@ func (s *CustomMetricSet) Table() string { return "" } if len(s.Metrics) >= swapXYMin { - // TODO: conifg return s.tableSwaped() } var ( From ea34599fa146c2c21e3ad581d3440e032b7f9d26 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Wed, 3 Jan 2024 20:43:39 +0900 Subject: [PATCH 08/30] Revert & append --- report/custom_test.go | 118 +++++++++++++----- .../custom_metric_set_out.0.fr.golden | 0 .../custom_metric_set_out.0.ja.golden | 0 .../custom_metric_set_out.1.fr.golden | 4 + .../custom_metric_set_out.1.golden | 2 +- .../custom_metric_set_out.1.ja.golden | 4 + .../custom_metric_set_out.2.fr.golden | 4 + .../custom_metric_set_out.2.golden | 4 +- .../custom_metric_set_out.2.ja.golden | 4 + .../custom_metric_set_table.0.fr.golden | 0 .../custom_metric_set_table.0.ja.golden | 0 .../custom_metric_set_table.1.fr.golden | 5 + .../custom_metric_set_table.1.golden | 2 +- .../custom_metric_set_table.1.ja.golden | 5 + .../custom_metric_set_table.2.fr.golden | 5 + .../custom_metric_set_table.2.golden | 2 +- .../custom_metric_set_table.2.ja.golden | 5 + .../custom_metric_set_table.3.fr.golden | 9 ++ .../custom_metric_set_table.3.golden | 10 +- .../custom_metric_set_table.3.ja.golden | 9 ++ .../diff_custom_metric_set_table.0.fr.golden | 5 + .../diff_custom_metric_set_table.0.golden | 2 +- .../diff_custom_metric_set_table.0.ja.golden | 5 + .../diff_custom_metric_set_table.1.fr.golden | 6 + .../diff_custom_metric_set_table.1.golden | 4 +- .../diff_custom_metric_set_table.1.ja.golden | 6 + 26 files changed, 173 insertions(+), 47 deletions(-) create mode 100644 testdata/custom_metrics/custom_metric_set_out.0.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_out.0.ja.golden create mode 100644 testdata/custom_metrics/custom_metric_set_out.1.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_out.1.ja.golden create mode 100644 testdata/custom_metrics/custom_metric_set_out.2.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_out.2.ja.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.0.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.0.ja.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.1.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.1.ja.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.2.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.2.ja.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.3.fr.golden create mode 100644 testdata/custom_metrics/custom_metric_set_table.3.ja.golden create mode 100644 testdata/custom_metrics/diff_custom_metric_set_table.0.fr.golden create mode 100644 testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden create mode 100644 testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden create mode 100644 testdata/custom_metrics/diff_custom_metric_set_table.1.ja.golden diff --git a/report/custom_test.go b/report/custom_test.go index a793af44..9c72cab5 100644 --- a/report/custom_test.go +++ b/report/custom_test.go @@ -9,9 +9,15 @@ import ( "github.com/google/go-cmp/cmp" "github.com/tenntenn/golden" + "golang.org/x/text/language" ) func TestCustomMetricSetTable(t *testing.T) { + locales := []*language.Tag{ + nil, + &language.Japanese, + &language.French, + } tests := []struct { s *CustomMetricSet }{ @@ -52,17 +58,27 @@ func TestCustomMetricSetTable(t *testing.T) { t.Setenv("GITHUB_SERVER_URL", "https://github.com") t.Setenv("GITHUB_REPOSITORY", "owner/repo") for i, tt := range tests { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - got := tt.s.Table() - f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_table.%d", i)) - if os.Getenv("UPDATE_GOLDEN") != "" { - golden.Update(t, testdataDir(t), f, got) - return + for _, locale := range locales { + SetLocale(locale) + var tc string + if locale != nil { + tc = fmt.Sprintf("%d.%s", i, locale.String()) + } else { + tc = fmt.Sprintf("%d", i) } - if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { - t.Error(diff) - } - }) + + t.Run(tc, func(t *testing.T) { + got := tt.s.Table() + f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_table.%s", tc)) + if os.Getenv("UPDATE_GOLDEN") != "" { + golden.Update(t, testdataDir(t), f, got) + return + } + if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { + t.Error(diff) + } + }) + } } } @@ -98,6 +114,12 @@ func TestCustomMetricSetMetadataTable(t *testing.T) { } func TestCustomMetricSetOut(t *testing.T) { + locales := []*language.Tag{ + nil, + &language.Japanese, + &language.French, + } + tests := []struct { s *CustomMetricSet }{ @@ -130,20 +152,30 @@ func TestCustomMetricSetOut(t *testing.T) { }}, } for i, tt := range tests { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - got := new(bytes.Buffer) - if err := tt.s.Out(got); err != nil { - t.Fatal(err) - } - f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_out.%d", i)) - if os.Getenv("UPDATE_GOLDEN") != "" { - golden.Update(t, testdataDir(t), f, got) - return - } - if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { - t.Error(diff) + for _, locale := range locales { + SetLocale(locale) + var tc string + if locale != nil { + tc = fmt.Sprintf("%d.%s", i, locale.String()) + } else { + tc = fmt.Sprintf("%d", i) } - }) + + t.Run(tc, func(t *testing.T) { + got := new(bytes.Buffer) + if err := tt.s.Out(got); err != nil { + t.Fatal(err) + } + f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_out.%s", tc)) + if os.Getenv("UPDATE_GOLDEN") != "" { + golden.Update(t, testdataDir(t), f, got) + return + } + if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { + t.Error(diff) + } + }) + } } } @@ -208,6 +240,12 @@ func TestCustomMetricsSetValidate(t *testing.T) { } func TestDiffCustomMetricSetTable(t *testing.T) { + locales := []*language.Tag{ + nil, + &language.Japanese, + &language.French, + } + tests := []struct { a *CustomMetricSet b *CustomMetricSet @@ -261,18 +299,28 @@ func TestDiffCustomMetricSetTable(t *testing.T) { t.Setenv("GITHUB_SERVER_URL", "https://github.com") t.Setenv("GITHUB_REPOSITORY", "owner/repo") for i, tt := range tests { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - d := tt.a.Compare(tt.b) - got := d.Table() - f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_table.%d", i)) - if os.Getenv("UPDATE_GOLDEN") != "" { - golden.Update(t, testdataDir(t), f, got) - return - } - if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { - t.Error(diff) + for _, locale := range locales { + SetLocale(locale) + var tc string + if locale != nil { + tc = fmt.Sprintf("%d.%s", i, locale.String()) + } else { + tc = fmt.Sprintf("%d", i) } - }) + + t.Run(tc, func(t *testing.T) { + d := tt.a.Compare(tt.b) + got := d.Table() + f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_table.%s", tc)) + if os.Getenv("UPDATE_GOLDEN") != "" { + golden.Update(t, testdataDir(t), f, got) + return + } + if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { + t.Error(diff) + } + }) + } } } @@ -372,6 +420,8 @@ func TestConvertFormat(t *testing.T) { }, } + SetLocale(&language.Japanese) + for i, tt := range tests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { got := convertFormat(tt.n) diff --git a/testdata/custom_metrics/custom_metric_set_out.0.fr.golden b/testdata/custom_metrics/custom_metric_set_out.0.fr.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/custom_metrics/custom_metric_set_out.0.ja.golden b/testdata/custom_metrics/custom_metric_set_out.0.ja.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/custom_metrics/custom_metric_set_out.1.fr.golden b/testdata/custom_metrics/custom_metric_set_out.1.fr.golden new file mode 100644 index 00000000..44e225e0 --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_out.1.fr.golden @@ -0,0 +1,4 @@ +  Benchmark-0  main (1234567) +--------------------------------------------- + Number of iterations 1 000 + Nanoseconds per iteration 676,5 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.1.golden b/testdata/custom_metrics/custom_metric_set_out.1.golden index 3d7c195a..fc1d7e88 100644 --- a/testdata/custom_metrics/custom_metric_set_out.1.golden +++ b/testdata/custom_metrics/custom_metric_set_out.1.golden @@ -1,4 +1,4 @@  Benchmark-0  main (1234567) --------------------------------------------- - Number of iterations 1,000 + Number of iterations 1000.0 Nanoseconds per iteration 676.5 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.1.ja.golden b/testdata/custom_metrics/custom_metric_set_out.1.ja.golden new file mode 100644 index 00000000..3d7c195a --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_out.1.ja.golden @@ -0,0 +1,4 @@ +  Benchmark-0  main (1234567) +--------------------------------------------- + Number of iterations 1,000 + Nanoseconds per iteration 676.5 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.fr.golden b/testdata/custom_metrics/custom_metric_set_out.2.fr.golden new file mode 100644 index 00000000..2521f201 --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_out.2.fr.golden @@ -0,0 +1,4 @@ +  Benchmark-1  main (1234567) +--------------------------------------------- + Number of iterations 1 500 + Nanoseconds per iteration 1 340 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.golden b/testdata/custom_metrics/custom_metric_set_out.2.golden index ece4782e..46640c3a 100644 --- a/testdata/custom_metrics/custom_metric_set_out.2.golden +++ b/testdata/custom_metrics/custom_metric_set_out.2.golden @@ -1,4 +1,4 @@  Benchmark-1  main (1234567) --------------------------------------------- - Number of iterations 1,500 - Nanoseconds per iteration 1,340 ns/op + Number of iterations 1500.0 + Nanoseconds per iteration 1340.0 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.ja.golden b/testdata/custom_metrics/custom_metric_set_out.2.ja.golden new file mode 100644 index 00000000..ece4782e --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_out.2.ja.golden @@ -0,0 +1,4 @@ +  Benchmark-1  main (1234567) +--------------------------------------------- + Number of iterations 1,500 + Nanoseconds per iteration 1,340 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_table.0.fr.golden b/testdata/custom_metrics/custom_metric_set_table.0.fr.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/custom_metrics/custom_metric_set_table.0.ja.golden b/testdata/custom_metrics/custom_metric_set_table.0.ja.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/custom_metrics/custom_metric_set_table.1.fr.golden b/testdata/custom_metrics/custom_metric_set_table.1.fr.golden new file mode 100644 index 00000000..342a0952 --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_table.1.fr.golden @@ -0,0 +1,5 @@ +## Benchmark-0 + +| Number of iterations | Nanoseconds per iteration | +|---------------------:|--------------------------:| +| 1 000 | 676,5 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.1.golden b/testdata/custom_metrics/custom_metric_set_table.1.golden index f11339ed..3f4e8b7d 100644 --- a/testdata/custom_metrics/custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/custom_metric_set_table.1.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1,000 | 676.5 ns/op | +| 1000.0 | 676.5 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.1.ja.golden b/testdata/custom_metrics/custom_metric_set_table.1.ja.golden new file mode 100644 index 00000000..f11339ed --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_table.1.ja.golden @@ -0,0 +1,5 @@ +## Benchmark-0 + +| Number of iterations | Nanoseconds per iteration | +|---------------------:|--------------------------:| +| 1,000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.fr.golden b/testdata/custom_metrics/custom_metric_set_table.2.fr.golden new file mode 100644 index 00000000..decdc231 --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_table.2.fr.golden @@ -0,0 +1,5 @@ +## Benchmark-1 + +| Number of iterations | Nanoseconds per iteration | +|---------------------:|--------------------------:| +| 1 500 | 1 340 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.golden b/testdata/custom_metrics/custom_metric_set_table.2.golden index fd776ac5..4bbc42dc 100644 --- a/testdata/custom_metrics/custom_metric_set_table.2.golden +++ b/testdata/custom_metrics/custom_metric_set_table.2.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1,500 | 1,340 ns/op | +| 1500.0 | 1340.0 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.ja.golden b/testdata/custom_metrics/custom_metric_set_table.2.ja.golden new file mode 100644 index 00000000..fd776ac5 --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_table.2.ja.golden @@ -0,0 +1,5 @@ +## Benchmark-1 + +| Number of iterations | Nanoseconds per iteration | +|---------------------:|--------------------------:| +| 1,500 | 1,340 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.3.fr.golden b/testdata/custom_metrics/custom_metric_set_table.3.fr.golden new file mode 100644 index 00000000..f62e1f92 --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_table.3.fr.golden @@ -0,0 +1,9 @@ +## Many Metrics + +| | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +|----------:|------------------------------------------------------------------:| +| Metrics A | 1 500 | +| Metrics B | 1 340 | +| Metrics C | 1 600 | +| Metrics D | 1 010 | +| Metrics E | 1 800 | diff --git a/testdata/custom_metrics/custom_metric_set_table.3.golden b/testdata/custom_metrics/custom_metric_set_table.3.golden index 06e52f7c..7dc849b7 100644 --- a/testdata/custom_metrics/custom_metric_set_table.3.golden +++ b/testdata/custom_metrics/custom_metric_set_table.3.golden @@ -2,8 +2,8 @@ | | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | |----------:|------------------------------------------------------------------:| -| Metrics A | 1,500 | -| Metrics B | 1,340 | -| Metrics C | 1,600 | -| Metrics D | 1,010 | -| Metrics E | 1,800 | +| Metrics A | 1500.0 | +| Metrics B | 1340.0 | +| Metrics C | 1600.0 | +| Metrics D | 1010.0 | +| Metrics E | 1800.0 | diff --git a/testdata/custom_metrics/custom_metric_set_table.3.ja.golden b/testdata/custom_metrics/custom_metric_set_table.3.ja.golden new file mode 100644 index 00000000..06e52f7c --- /dev/null +++ b/testdata/custom_metrics/custom_metric_set_table.3.ja.golden @@ -0,0 +1,9 @@ +## Many Metrics + +| | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +|----------:|------------------------------------------------------------------:| +| Metrics A | 1,500 | +| Metrics B | 1,340 | +| Metrics C | 1,600 | +| Metrics D | 1,010 | +| Metrics E | 1,800 | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.fr.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.fr.golden new file mode 100644 index 00000000..342a0952 --- /dev/null +++ b/testdata/custom_metrics/diff_custom_metric_set_table.0.fr.golden @@ -0,0 +1,5 @@ +## Benchmark-0 + +| Number of iterations | Nanoseconds per iteration | +|---------------------:|--------------------------:| +| 1 000 | 676,5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden index f11339ed..3f4e8b7d 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1,000 | 676.5 ns/op | +| 1000.0 | 676.5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden new file mode 100644 index 00000000..f11339ed --- /dev/null +++ b/testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden @@ -0,0 +1,5 @@ +## Benchmark-0 + +| Number of iterations | Nanoseconds per iteration | +|---------------------:|--------------------------:| +| 1,000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden new file mode 100644 index 00000000..96ea4615 --- /dev/null +++ b/testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden @@ -0,0 +1,6 @@ +## Benchmark-0 + +| | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | +|-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| +| **Number of iterations** | 9 393 | 1 000 | -8 393 | +| **Nanoseconds per iteration** | 456 ns/op | 676,5 ns/op | 220,5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden index 235b7d29..32907391 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden @@ -2,5 +2,5 @@ | | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | |-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| -| **Number of iterations** | 9,393 | 1,000 | -8,393 | -| **Nanoseconds per iteration** | 456 ns/op | 676.5 ns/op | 220.5 ns/op | +| **Number of iterations** | 9393.0 | 1000.0 | -8393.0 | +| **Nanoseconds per iteration** | 456.0 ns/op | 676.5 ns/op | 220.5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.ja.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.ja.golden new file mode 100644 index 00000000..235b7d29 --- /dev/null +++ b/testdata/custom_metrics/diff_custom_metric_set_table.1.ja.golden @@ -0,0 +1,6 @@ +## Benchmark-0 + +| | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | +|-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| +| **Number of iterations** | 9,393 | 1,000 | -8,393 | +| **Nanoseconds per iteration** | 456 ns/op | 676.5 ns/op | 220.5 ns/op | From 22cfc106b88368a769ad9a4065f73766c2418355 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Wed, 3 Jan 2024 20:54:40 +0900 Subject: [PATCH 09/30] If it can be converted to int, it is processed as int. --- report/formatter.go | 5 ++++- testdata/custom_metrics/custom_metric_set_out.1.golden | 2 +- testdata/custom_metrics/custom_metric_set_out.2.golden | 4 ++-- .../custom_metrics/custom_metric_set_table.1.golden | 2 +- .../custom_metrics/custom_metric_set_table.2.golden | 2 +- .../custom_metrics/custom_metric_set_table.3.golden | 10 +++++----- .../diff_custom_metric_set_table.0.golden | 2 +- .../diff_custom_metric_set_table.1.golden | 4 ++-- 8 files changed, 17 insertions(+), 14 deletions(-) diff --git a/report/formatter.go b/report/formatter.go index af5dbc37..7c3f9814 100644 --- a/report/formatter.go +++ b/report/formatter.go @@ -24,7 +24,10 @@ func convertFormat(v interface{}) string { switch vv := v.(type) { case int, int8, int16, int32, int64: return fmt.Sprintf("%d", vv) - case float32, float64: + case float64: + if isInt(vv) { + return fmt.Sprintf("%d", int(vv)) + } return fmt.Sprintf("%.1f", vv) default: panic(fmt.Errorf("convert format error .Unknown type:%v", vv)) diff --git a/testdata/custom_metrics/custom_metric_set_out.1.golden b/testdata/custom_metrics/custom_metric_set_out.1.golden index fc1d7e88..51826c74 100644 --- a/testdata/custom_metrics/custom_metric_set_out.1.golden +++ b/testdata/custom_metrics/custom_metric_set_out.1.golden @@ -1,4 +1,4 @@  Benchmark-0  main (1234567) --------------------------------------------- - Number of iterations 1000.0 + Number of iterations 1000 Nanoseconds per iteration 676.5 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.golden b/testdata/custom_metrics/custom_metric_set_out.2.golden index 46640c3a..4c953b44 100644 --- a/testdata/custom_metrics/custom_metric_set_out.2.golden +++ b/testdata/custom_metrics/custom_metric_set_out.2.golden @@ -1,4 +1,4 @@  Benchmark-1  main (1234567) --------------------------------------------- - Number of iterations 1500.0 - Nanoseconds per iteration 1340.0 ns/op + Number of iterations 1500 + Nanoseconds per iteration 1340 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_table.1.golden b/testdata/custom_metrics/custom_metric_set_table.1.golden index 3f4e8b7d..f06e0e32 100644 --- a/testdata/custom_metrics/custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/custom_metric_set_table.1.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1000.0 | 676.5 ns/op | +| 1000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.golden b/testdata/custom_metrics/custom_metric_set_table.2.golden index 4bbc42dc..bd58b389 100644 --- a/testdata/custom_metrics/custom_metric_set_table.2.golden +++ b/testdata/custom_metrics/custom_metric_set_table.2.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1500.0 | 1340.0 ns/op | +| 1500 | 1340 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.3.golden b/testdata/custom_metrics/custom_metric_set_table.3.golden index 7dc849b7..83b683b5 100644 --- a/testdata/custom_metrics/custom_metric_set_table.3.golden +++ b/testdata/custom_metrics/custom_metric_set_table.3.golden @@ -2,8 +2,8 @@ | | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | |----------:|------------------------------------------------------------------:| -| Metrics A | 1500.0 | -| Metrics B | 1340.0 | -| Metrics C | 1600.0 | -| Metrics D | 1010.0 | -| Metrics E | 1800.0 | +| Metrics A | 1500 | +| Metrics B | 1340 | +| Metrics C | 1600 | +| Metrics D | 1010 | +| Metrics E | 1800 | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden index 3f4e8b7d..f06e0e32 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden @@ -2,4 +2,4 @@ | Number of iterations | Nanoseconds per iteration | |---------------------:|--------------------------:| -| 1000.0 | 676.5 ns/op | +| 1000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden index 32907391..4f887783 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden @@ -2,5 +2,5 @@ | | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | |-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| -| **Number of iterations** | 9393.0 | 1000.0 | -8393.0 | -| **Nanoseconds per iteration** | 456.0 ns/op | 676.5 ns/op | 220.5 ns/op | +| **Number of iterations** | 9393 | 1000 | -8393 | +| **Nanoseconds per iteration** | 456 ns/op | 676.5 ns/op | 220.5 ns/op | From 0debbc9f347b96e4c566b295a529b66d4ab4435a Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Wed, 3 Jan 2024 21:23:11 +0900 Subject: [PATCH 10/30] Append config test --- config/config_test.go | 35 +++++++++++++++++++++++++ config/testdata/locale_empty.yml | 1 + config/testdata/locale_fr.yml | 1 + config/testdata/locale_ja.yml | 1 + config/testdata/locale_ja_uppercase.yml | 1 + config/testdata/locale_nothing.yml | 0 config/testdata/locale_unknown.yml | 1 + 7 files changed, 40 insertions(+) create mode 100644 config/testdata/locale_empty.yml create mode 100644 config/testdata/locale_fr.yml create mode 100644 config/testdata/locale_ja.yml create mode 100644 config/testdata/locale_ja_uppercase.yml create mode 100644 config/testdata/locale_nothing.yml create mode 100644 config/testdata/locale_unknown.yml diff --git a/config/config_test.go b/config/config_test.go index e0beca78..d9478fce 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/google/go-cmp/cmp" + "golang.org/x/text/language" ) func TestMain(m *testing.M) { @@ -92,6 +93,40 @@ func TestLoadCentralPush(t *testing.T) { } } +func TestLoadLocale(t *testing.T) { + tests := []struct { + path string + want *language.Tag + wantError bool + }{ + {"locale_nothing.yml", nil, false}, + {"locale_empty.yml", nil, false}, + {"locale_ja.yml", &language.Japanese, false}, + {"locale_ja_uppercase.yml", &language.Japanese, false}, + {"locale_fr.yml", &language.French, false}, + {"locale_unkown.yml", nil, true}, + } + for _, tt := range tests { + c := New() + t.Run(fmt.Sprintf("%v", tt.path), func(t *testing.T) { + p := filepath.Join(testdataDir(t), tt.path) + if err := c.Load(p); err != nil { + if tt.wantError { + return + } + t.Fatal(err) + } + got := c.Locale + if tt.want == nil && got == nil { + return + } + if diff := cmp.Diff(got.String(), tt.want.String(), nil); diff != "" { + t.Error(diff) + } + }) + } +} + func TestCoveragePaths(t *testing.T) { tests := []struct { paths []string diff --git a/config/testdata/locale_empty.yml b/config/testdata/locale_empty.yml new file mode 100644 index 00000000..71514559 --- /dev/null +++ b/config/testdata/locale_empty.yml @@ -0,0 +1 @@ +locale: \ No newline at end of file diff --git a/config/testdata/locale_fr.yml b/config/testdata/locale_fr.yml new file mode 100644 index 00000000..06e6d0f8 --- /dev/null +++ b/config/testdata/locale_fr.yml @@ -0,0 +1 @@ +locale: "fr" \ No newline at end of file diff --git a/config/testdata/locale_ja.yml b/config/testdata/locale_ja.yml new file mode 100644 index 00000000..9bd62315 --- /dev/null +++ b/config/testdata/locale_ja.yml @@ -0,0 +1 @@ +locale: "ja" \ No newline at end of file diff --git a/config/testdata/locale_ja_uppercase.yml b/config/testdata/locale_ja_uppercase.yml new file mode 100644 index 00000000..e975dc65 --- /dev/null +++ b/config/testdata/locale_ja_uppercase.yml @@ -0,0 +1 @@ +locale: "JA" \ No newline at end of file diff --git a/config/testdata/locale_nothing.yml b/config/testdata/locale_nothing.yml new file mode 100644 index 00000000..e69de29b diff --git a/config/testdata/locale_unknown.yml b/config/testdata/locale_unknown.yml new file mode 100644 index 00000000..fc7cb7f5 --- /dev/null +++ b/config/testdata/locale_unknown.yml @@ -0,0 +1 @@ +locale: "unknown" \ No newline at end of file From 2b06dcd4533a188644b0ffb2fba7357f2ce2eb7e Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Thu, 4 Jan 2024 22:11:35 +0900 Subject: [PATCH 11/30] EOL --- config/testdata/locale_empty.yml | 2 +- config/testdata/locale_fr.yml | 2 +- config/testdata/locale_ja.yml | 2 +- config/testdata/locale_ja_uppercase.yml | 2 +- config/testdata/locale_unknown.yml | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/testdata/locale_empty.yml b/config/testdata/locale_empty.yml index 71514559..d1542ab8 100644 --- a/config/testdata/locale_empty.yml +++ b/config/testdata/locale_empty.yml @@ -1 +1 @@ -locale: \ No newline at end of file +locale: diff --git a/config/testdata/locale_fr.yml b/config/testdata/locale_fr.yml index 06e6d0f8..448c63fa 100644 --- a/config/testdata/locale_fr.yml +++ b/config/testdata/locale_fr.yml @@ -1 +1 @@ -locale: "fr" \ No newline at end of file +locale: "fr" diff --git a/config/testdata/locale_ja.yml b/config/testdata/locale_ja.yml index 9bd62315..38422c93 100644 --- a/config/testdata/locale_ja.yml +++ b/config/testdata/locale_ja.yml @@ -1 +1 @@ -locale: "ja" \ No newline at end of file +locale: "ja" diff --git a/config/testdata/locale_ja_uppercase.yml b/config/testdata/locale_ja_uppercase.yml index e975dc65..646d2ec8 100644 --- a/config/testdata/locale_ja_uppercase.yml +++ b/config/testdata/locale_ja_uppercase.yml @@ -1 +1 @@ -locale: "JA" \ No newline at end of file +locale: "JA" diff --git a/config/testdata/locale_unknown.yml b/config/testdata/locale_unknown.yml index fc7cb7f5..88b49b2f 100644 --- a/config/testdata/locale_unknown.yml +++ b/config/testdata/locale_unknown.yml @@ -1 +1 @@ -locale: "unknown" \ No newline at end of file +locale: "unknown" From cd2283ad0146d57046df0359c622431d9954e86b Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:05:54 +0900 Subject: [PATCH 12/30] Define option with Locale --- report/option.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 report/option.go diff --git a/report/option.go b/report/option.go new file mode 100644 index 00000000..611290af --- /dev/null +++ b/report/option.go @@ -0,0 +1,15 @@ +package report + +import "golang.org/x/text/language" + +type ReportOptions struct { + Locale *language.Tag +} + +type ReportOption func(*ReportOptions) + +func Locale(locale *language.Tag) ReportOption { + return func(args *ReportOptions) { + args.Locale = locale + } +} From 834274551160be007aa8f41d18d37ba0acc1cb72 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:06:37 +0900 Subject: [PATCH 13/30] dip --- config/config.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 3d9cbc00..8f1251f7 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,6 @@ import ( "github.com/k1LoW/duration" "github.com/k1LoW/expand" "github.com/k1LoW/octocov/gh" - "github.com/k1LoW/octocov/report" "golang.org/x/text/language" ) @@ -192,7 +191,14 @@ func (c *Config) Loaded() bool { return c.path != "" } -func (c *Config) Acceptable(r, rPrev *report.Report) error { +type Reporter interface { + CoveragePercent() float64 + CodeToTestRatioRatio() float64 + TestExecutionTimeNano() float64 + IsMeasuredTestExecutionTime() bool +} + +func (c *Config) Acceptable(r, rPrev Reporter) error { var result *multierror.Error if err := c.CoverageConfigReady(); err == nil { prev := 0.0 From a1a318005b700cce4de32b80bbf537c1b8245401 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:08:04 +0900 Subject: [PATCH 14/30] Transferring the conversion process to report --- report/formatter.go | 35 ----------------------------------- report/report.go | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 report/formatter.go diff --git a/report/formatter.go b/report/formatter.go deleted file mode 100644 index 7c3f9814..00000000 --- a/report/formatter.go +++ /dev/null @@ -1,35 +0,0 @@ -package report - -import ( - _ "embed" - "fmt" - - "golang.org/x/text/language" - "golang.org/x/text/message" - "golang.org/x/text/number" -) - -var locale *language.Tag - -func SetLocale(l *language.Tag) { - locale = l -} - -func convertFormat(v interface{}) string { - if locale != nil { - p := message.NewPrinter(*locale) - return p.Sprint(number.Decimal(v)) - } - - switch vv := v.(type) { - case int, int8, int16, int32, int64: - return fmt.Sprintf("%d", vv) - case float64: - if isInt(vv) { - return fmt.Sprintf("%d", int(vv)) - } - return fmt.Sprintf("%.1f", vv) - default: - panic(fmt.Errorf("convert format error .Unknown type:%v", vv)) - } -} diff --git a/report/report.go b/report/report.go index 123c627a..75215056 100644 --- a/report/report.go +++ b/report/report.go @@ -16,16 +16,23 @@ import ( "github.com/goccy/go-json" "github.com/hashicorp/go-multierror" - "github.com/k1LoW/octocov/gh" + "github.com/k1LoW/octocov/config" "github.com/k1LoW/octocov/coverage" + "github.com/k1LoW/octocov/gh" "github.com/k1LoW/octocov/ratio" "github.com/olekukonko/tablewriter" "github.com/samber/lo" + "golang.org/x/text/message" + "golang.org/x/text/number" ) const filesHideMin = 30 const filesSkipMax = 100 +var ( + _ config.Reporter = (*Report)(nil) +) + type Report struct { Repository string `json:"repository"` Ref string `json:"ref"` @@ -38,9 +45,10 @@ type Report struct { // coverage report paths covPaths []string + config *ReportOptions } -func New(ownerrepo string) (*Report, error) { +func New(ownerrepo string, opts ...ReportOption) (*Report, error) { if ownerrepo == "" { ownerrepo = os.Getenv("GITHUB_REPOSITORY") } @@ -60,12 +68,17 @@ func New(ownerrepo string) (*Report, error) { commit = strings.TrimSuffix(string(b), "\n") } } + config := &ReportOptions{} + for _, setter := range opts { + setter(config) + } return &Report{ Repository: ownerrepo, Ref: ref, Commit: commit, Timestamp: time.Now().UTC(), + config: config, }, nil } @@ -538,6 +551,25 @@ func (r *Report) findCustomMetricSetByKey(key string) *CustomMetricSet { return nil } +func (r *Report) convertFormat(v interface{}) string { + if r.config != nil && r.config.Locale != nil { + p := message.NewPrinter(*r.config.Locale) + return p.Sprint(number.Decimal(v)) + } + + switch vv := v.(type) { + case int, int8, int16, int32, int64: + return fmt.Sprintf("%d", vv) + case float64: + if isInt(vv) { + return fmt.Sprintf("%d", int(vv)) + } + return fmt.Sprintf("%.1f", vv) + default: + panic(fmt.Errorf("convert format error .Unknown type:%v", vv)) + } +} + func makeHeadTitle(ref, commit string, covPaths []string) string { ref = strings.TrimPrefix(ref, "refs/heads/") if strings.HasPrefix(ref, "refs/pull/") { From 944fb24f05d39627368771d5aad49c0e89130c33 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:09:57 +0900 Subject: [PATCH 15/30] Conversion processing by resport. --- report/custom.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/report/custom.go b/report/custom.go index 40592357..79d76643 100644 --- a/report/custom.go +++ b/report/custom.go @@ -45,6 +45,7 @@ type DiffCustomMetricSet struct { A *CustomMetricSet `json:"a"` B *CustomMetricSet `json:"b"` Metrics []*DiffCustomMetric `json:"metrics"` + report *Report } type DiffCustomMetric struct { @@ -70,7 +71,7 @@ func (s *CustomMetricSet) Table() string { ) for _, m := range s.Metrics { h = append(h, m.Name) - d = append(d, fmt.Sprintf("%s%s", convertFormat(m.Value), m.Unit)) + d = append(d, fmt.Sprintf("%s%s", s.report.convertFormat(m.Value), m.Unit)) } buf := new(bytes.Buffer) _, _ = buf.WriteString(fmt.Sprintf("## %s\n\n", s.Name)) //nostyle:handlerrors @@ -95,7 +96,7 @@ func (s *CustomMetricSet) tableSwaped() string { table.SetCenterSeparator("|") table.SetHeader([]string{"", makeHeadTitleWithLink(s.report.Ref, s.report.Commit, nil)}) for _, m := range s.Metrics { - table.Append([]string{m.Name, fmt.Sprintf("%s%s", convertFormat(m.Value), m.Unit)}) + table.Append([]string{m.Name, fmt.Sprintf("%s%s", s.report.convertFormat(m.Value), m.Unit)}) } table.Render() return strings.Replace(buf.String(), "---|", "--:|", len(s.Metrics)) @@ -151,7 +152,7 @@ func (s *CustomMetricSet) Out(w io.Writer) error { if m.Name == "" { m.Name = m.Key } - table.Rich([]string{m.Name, fmt.Sprintf("%s%s", convertFormat(m.Value), m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) + table.Rich([]string{m.Name, fmt.Sprintf("%s%s", s.report.convertFormat(m.Value), m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) } table.Render() @@ -160,10 +161,11 @@ func (s *CustomMetricSet) Out(w io.Writer) error { func (s *CustomMetricSet) Compare(s2 *CustomMetricSet) *DiffCustomMetricSet { d := &DiffCustomMetricSet{ - Key: s.Key, - Name: s.Name, - A: s, - B: s2, + Key: s.Key, + Name: s.Name, + A: s, + B: s2, + report: s.report, } if s2 == nil { for _, m := range s.Metrics { @@ -265,20 +267,20 @@ func (d *DiffCustomMetricSet) Table() string { continue case m.A != nil && m.B == nil: vb = "" - va = fmt.Sprintf("%s%s", convertFormat(*m.A), m.customMetricA.Unit) - diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.A), m.customMetricA.Unit) + diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricA.Unit) case m.A == nil && m.B != nil: va = "" - va = fmt.Sprintf("%s%s", convertFormat(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricB.Unit) + va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricB.Unit) case isInt(*m.A) && isInt(*m.B): - va = fmt.Sprintf("%s%s", convertFormat(*m.A), m.customMetricA.Unit) - vb = fmt.Sprintf("%s%s", convertFormat(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%s%s", d.report.convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricA.Unit) default: - va = fmt.Sprintf("%s%s", convertFormat(*m.A), m.customMetricA.Unit) - vb = fmt.Sprintf("%s%s", convertFormat(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%s%s", convertFormat(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%s%s", d.report.convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricA.Unit) } if m.Name == "" { m.Name = m.Key From 0ed35427a921a268db053a4a36f6825b082cf3fb Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:11:16 +0900 Subject: [PATCH 16/30] Allow options to carry over to reports --- cmd/comment.go | 1 - cmd/root.go | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/comment.go b/cmd/comment.go index 9f43c1be..44911edc 100644 --- a/cmd/comment.go +++ b/cmd/comment.go @@ -62,7 +62,6 @@ func createReportContent(ctx context.Context, c *config.Config, r, rPrev *report if hideFooterLink { footer = "Reported by octocov" } - report.SetLocale(c.Locale) var ( table, fileTable string customTables []string diff --git a/cmd/root.go b/cmd/root.go index bb6c3992..798ffb77 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,12 +33,12 @@ import ( "strings" "time" + "github.com/k1LoW/octocov/badge" "github.com/k1LoW/octocov/central" "github.com/k1LoW/octocov/config" "github.com/k1LoW/octocov/datastore" "github.com/k1LoW/octocov/gh" "github.com/k1LoW/octocov/internal" - "github.com/k1LoW/octocov/badge" "github.com/k1LoW/octocov/report" "github.com/k1LoW/octocov/version" "github.com/spf13/cobra" @@ -161,7 +161,7 @@ var rootCmd = &cobra.Command{ return nil } - r, err := report.New(c.Repository) + r, err := report.New(c.Repository, report.Locale(c.Locale)) if err != nil { return err } @@ -354,7 +354,7 @@ var rootCmd = &cobra.Command{ } } if c.Diff.Path != "" { - rt, err := report.New(c.Repository) + rt, err := report.New(c.Repository, report.Locale(c.Locale)) if err != nil { return err } @@ -502,7 +502,7 @@ func printMetrics(cmd *cobra.Command) error { c.CodeToTestRatio = nil c.TestExecutionTime = nil } - r, err := report.New(c.Repository) + r, err := report.New(c.Repository, report.Locale(c.Locale)) if err != nil { return err } From 89592cab1fb243550a8b0219f4b1c5da22a898af Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:12:08 +0900 Subject: [PATCH 17/30] Test modifications due to implementation changes --- report/custom_test.go | 177 +++++++++++++----------------------------- report/report_test.go | 41 ++++++++++ 2 files changed, 96 insertions(+), 122 deletions(-) diff --git a/report/custom_test.go b/report/custom_test.go index 9c72cab5..9eb81964 100644 --- a/report/custom_test.go +++ b/report/custom_test.go @@ -7,17 +7,10 @@ import ( "path/filepath" "testing" - "github.com/google/go-cmp/cmp" "github.com/tenntenn/golden" - "golang.org/x/text/language" ) func TestCustomMetricSetTable(t *testing.T) { - locales := []*language.Tag{ - nil, - &language.Japanese, - &language.French, - } tests := []struct { s *CustomMetricSet }{ @@ -58,27 +51,24 @@ func TestCustomMetricSetTable(t *testing.T) { t.Setenv("GITHUB_SERVER_URL", "https://github.com") t.Setenv("GITHUB_REPOSITORY", "owner/repo") for i, tt := range tests { - for _, locale := range locales { - SetLocale(locale) - var tc string - if locale != nil { - tc = fmt.Sprintf("%d.%s", i, locale.String()) - } else { - tc = fmt.Sprintf("%d", i) - } - - t.Run(tc, func(t *testing.T) { - got := tt.s.Table() - f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_table.%s", tc)) - if os.Getenv("UPDATE_GOLDEN") != "" { - golden.Update(t, testdataDir(t), f, got) - return - } - if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { - t.Error(diff) - } - }) + var tc string + if tt.s.report != nil && tt.s.report.config.Locale != nil { + tc = fmt.Sprintf("%d.%s", i, tt.s.report.config.Locale.String()) + } else { + tc = fmt.Sprintf("%d", i) } + + t.Run(tc, func(t *testing.T) { + got := tt.s.Table() + f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_table.%s", tc)) + if os.Getenv("UPDATE_GOLDEN") != "" { + golden.Update(t, testdataDir(t), f, got) + return + } + if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { + t.Error(diff) + } + }) } } @@ -114,12 +104,6 @@ func TestCustomMetricSetMetadataTable(t *testing.T) { } func TestCustomMetricSetOut(t *testing.T) { - locales := []*language.Tag{ - nil, - &language.Japanese, - &language.French, - } - tests := []struct { s *CustomMetricSet }{ @@ -152,30 +136,27 @@ func TestCustomMetricSetOut(t *testing.T) { }}, } for i, tt := range tests { - for _, locale := range locales { - SetLocale(locale) - var tc string - if locale != nil { - tc = fmt.Sprintf("%d.%s", i, locale.String()) - } else { - tc = fmt.Sprintf("%d", i) - } - - t.Run(tc, func(t *testing.T) { - got := new(bytes.Buffer) - if err := tt.s.Out(got); err != nil { - t.Fatal(err) - } - f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_out.%s", tc)) - if os.Getenv("UPDATE_GOLDEN") != "" { - golden.Update(t, testdataDir(t), f, got) - return - } - if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { - t.Error(diff) - } - }) + var tc string + if tt.s.report != nil && tt.s.report.config.Locale != nil { + tc = fmt.Sprintf("%d.%s", i, tt.s.report.config.Locale.String()) + } else { + tc = fmt.Sprintf("%d", i) } + + t.Run(tc, func(t *testing.T) { + got := new(bytes.Buffer) + if err := tt.s.Out(got); err != nil { + t.Fatal(err) + } + f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_out.%s", tc)) + if os.Getenv("UPDATE_GOLDEN") != "" { + golden.Update(t, testdataDir(t), f, got) + return + } + if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { + t.Error(diff) + } + }) } } @@ -240,12 +221,6 @@ func TestCustomMetricsSetValidate(t *testing.T) { } func TestDiffCustomMetricSetTable(t *testing.T) { - locales := []*language.Tag{ - nil, - &language.Japanese, - &language.French, - } - tests := []struct { a *CustomMetricSet b *CustomMetricSet @@ -299,28 +274,25 @@ func TestDiffCustomMetricSetTable(t *testing.T) { t.Setenv("GITHUB_SERVER_URL", "https://github.com") t.Setenv("GITHUB_REPOSITORY", "owner/repo") for i, tt := range tests { - for _, locale := range locales { - SetLocale(locale) - var tc string - if locale != nil { - tc = fmt.Sprintf("%d.%s", i, locale.String()) - } else { - tc = fmt.Sprintf("%d", i) - } - - t.Run(tc, func(t *testing.T) { - d := tt.a.Compare(tt.b) - got := d.Table() - f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_table.%s", tc)) - if os.Getenv("UPDATE_GOLDEN") != "" { - golden.Update(t, testdataDir(t), f, got) - return - } - if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { - t.Error(diff) - } - }) + var tc string + if tt.a.report != nil && tt.a.report.config.Locale != nil { + tc = fmt.Sprintf("%d.%s", i, tt.a.report.config.Locale.String()) + } else { + tc = fmt.Sprintf("%d", i) } + + t.Run(tc, func(t *testing.T) { + d := tt.a.Compare(tt.b) + got := d.Table() + f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_table.%s", tc)) + if os.Getenv("UPDATE_GOLDEN") != "" { + golden.Update(t, testdataDir(t), f, got) + return + } + if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" { + t.Error(diff) + } + }) } } @@ -392,42 +364,3 @@ func TestDiffCustomMetricSetMetadataTable(t *testing.T) { }) } } - -func TestConvertFormat(t *testing.T) { - tests := []struct { - n interface{} - want string - }{ - { - int(10), - "10", - }, - { - int32(3200), - "3,200", - }, - { - float32(10.0), - "10", - }, - { - float32(1000.1), - "1,000.1", - }, - { - int(1000), - "1,000", - }, - } - - SetLocale(&language.Japanese) - - for i, tt := range tests { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - got := convertFormat(tt.n) - if diff := cmp.Diff(got, tt.want, nil); diff != "" { - t.Error(diff) - } - }) - } -} diff --git a/report/report_test.go b/report/report_test.go index df3e1a44..cb624835 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -16,6 +16,7 @@ import ( "github.com/k1LoW/octocov/coverage" "github.com/k1LoW/octocov/gh" "github.com/k1LoW/octocov/ratio" + "golang.org/x/text/language" ) func TestNew(t *testing.T) { @@ -536,3 +537,43 @@ func coverageTestdataDir(t *testing.T) string { } return dir } + +func TestConvertFormat(t *testing.T) { + tests := []struct { + n interface{} + want string + }{ + { + int(10), + "10", + }, + { + int32(3200), + "3,200", + }, + { + float32(10.0), + "10", + }, + { + float32(1000.1), + "1,000.1", + }, + { + int(1000), + "1,000", + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + l := language.Japanese + r := &Report{config: &ReportOptions{Locale: &l}} + + got := r.convertFormat(tt.n) + if diff := cmp.Diff(got, tt.want, nil); diff != "" { + t.Error(diff) + } + }) + } +} From 3f1cc7f459943bf7bf79176e933f4664fc50334b Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:31:55 +0900 Subject: [PATCH 18/30] Fix --- report/custom_test.go | 147 ++++++++++++++---- .../custom_metric_set_out.0.fr.golden | 0 .../custom_metric_set_out.0.ja.golden | 0 .../custom_metric_set_out.1.ja.golden | 4 - .../custom_metric_set_out.2.fr.golden | 4 - ....golden => custom_metric_set_out.3.golden} | 0 ....golden => custom_metric_set_out.4.golden} | 0 .../custom_metric_set_table.0.fr.golden | 0 .../custom_metric_set_table.0.ja.golden | 0 .../custom_metric_set_table.1.ja.golden | 5 - .../custom_metric_set_table.2.fr.golden | 5 - .../custom_metric_set_table.3.ja.golden | 9 -- ...olden => custom_metric_set_table.4.golden} | 0 ...olden => custom_metric_set_table.5.golden} | 0 ...olden => custom_metric_set_table.6.golden} | 0 .../diff_custom_metric_set_table.0.ja.golden | 5 - .../diff_custom_metric_set_table.1.fr.golden | 6 - ... => diff_custom_metric_set_table.2.golden} | 0 ... => diff_custom_metric_set_table.3.golden} | 0 19 files changed, 120 insertions(+), 65 deletions(-) delete mode 100644 testdata/custom_metrics/custom_metric_set_out.0.fr.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_out.0.ja.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_out.1.ja.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_out.2.fr.golden rename testdata/custom_metrics/{custom_metric_set_out.1.fr.golden => custom_metric_set_out.3.golden} (100%) rename testdata/custom_metrics/{custom_metric_set_out.2.ja.golden => custom_metric_set_out.4.golden} (100%) delete mode 100644 testdata/custom_metrics/custom_metric_set_table.0.fr.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_table.0.ja.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_table.1.ja.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_table.2.fr.golden delete mode 100644 testdata/custom_metrics/custom_metric_set_table.3.ja.golden rename testdata/custom_metrics/{custom_metric_set_table.1.fr.golden => custom_metric_set_table.4.golden} (100%) rename testdata/custom_metrics/{custom_metric_set_table.2.ja.golden => custom_metric_set_table.5.golden} (100%) rename testdata/custom_metrics/{custom_metric_set_table.3.fr.golden => custom_metric_set_table.6.golden} (100%) delete mode 100644 testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden delete mode 100644 testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden rename testdata/custom_metrics/{diff_custom_metric_set_table.0.fr.golden => diff_custom_metric_set_table.2.golden} (100%) rename testdata/custom_metrics/{diff_custom_metric_set_table.1.ja.golden => diff_custom_metric_set_table.3.golden} (100%) diff --git a/report/custom_test.go b/report/custom_test.go index 9eb81964..b078b3c4 100644 --- a/report/custom_test.go +++ b/report/custom_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/tenntenn/golden" + "golang.org/x/text/language" ) func TestCustomMetricSetTable(t *testing.T) { @@ -47,20 +48,52 @@ func TestCustomMetricSetTable(t *testing.T) { covPaths: []string{"testdata/cover.out"}, }, }}, + {&CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"}, + }, + report: &Report{ + config: &ReportOptions{Locale: &language.French}, + }, + }}, + {&CustomMetricSet{ + Key: "benchmark_1", + Name: "Benchmark-1", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 1500.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 1340.0, Unit: " ns/op"}, + }, + report: &Report{ + config: &ReportOptions{Locale: &language.Japanese}, + }, + }}, + {&CustomMetricSet{ + Key: "many_metrics", + Name: "Many Metrics", + Metrics: []*CustomMetric{ + {Key: "A", Name: "Metrics A", Value: 1500.0, Unit: ""}, + {Key: "B", Name: "Metrics B", Value: 1340.0, Unit: ""}, + {Key: "C", Name: "Metrics C", Value: 1600.0, Unit: ""}, + {Key: "D", Name: "Metrics D", Value: 1010.0, Unit: ""}, + {Key: "E", Name: "Metrics E", Value: 1800.0, Unit: ""}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + config: &ReportOptions{Locale: &language.French}, + }, + }}, } t.Setenv("GITHUB_SERVER_URL", "https://github.com") t.Setenv("GITHUB_REPOSITORY", "owner/repo") for i, tt := range tests { - var tc string - if tt.s.report != nil && tt.s.report.config.Locale != nil { - tc = fmt.Sprintf("%d.%s", i, tt.s.report.config.Locale.String()) - } else { - tc = fmt.Sprintf("%d", i) - } - - t.Run(tc, func(t *testing.T) { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { got := tt.s.Table() - f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_table.%s", tc)) + f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_table.%d", i)) if os.Getenv("UPDATE_GOLDEN") != "" { golden.Update(t, testdataDir(t), f, got) return @@ -134,21 +167,42 @@ func TestCustomMetricSetOut(t *testing.T) { covPaths: []string{"testdata/cover.out"}, }, }}, + {&CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + config: &ReportOptions{Locale: &language.French}, + }, + }}, + {&CustomMetricSet{ + Key: "benchmark_1", + Name: "Benchmark-1", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 1500.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 1340.0, Unit: " ns/op"}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + config: &ReportOptions{Locale: &language.Japanese}, + }, + }}, } for i, tt := range tests { - var tc string - if tt.s.report != nil && tt.s.report.config.Locale != nil { - tc = fmt.Sprintf("%d.%s", i, tt.s.report.config.Locale.String()) - } else { - tc = fmt.Sprintf("%d", i) - } - - t.Run(tc, func(t *testing.T) { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { got := new(bytes.Buffer) if err := tt.s.Out(got); err != nil { t.Fatal(err) } - f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_out.%s", tc)) + f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_out.%d", i)) if os.Getenv("UPDATE_GOLDEN") != "" { golden.Update(t, testdataDir(t), f, got) return @@ -269,22 +323,61 @@ func TestDiffCustomMetricSetTable(t *testing.T) { }, }, }, + { + &CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + config: &ReportOptions{Locale: &language.French}, + }, + }, + nil, + }, + { + &CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + config: &ReportOptions{Locale: &language.Japanese}, + }, + }, + &CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metrics: []*CustomMetric{ + {Key: "N", Name: "Number of iterations", Value: 9393.0, Unit: ""}, + {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 456.0, Unit: " ns/op"}, + }, + report: &Report{ + Ref: "main", + Commit: "2345678901", + covPaths: []string{"testdata/cover.out"}, + }, + }, + }, } t.Setenv("GITHUB_SERVER_URL", "https://github.com") t.Setenv("GITHUB_REPOSITORY", "owner/repo") for i, tt := range tests { - var tc string - if tt.a.report != nil && tt.a.report.config.Locale != nil { - tc = fmt.Sprintf("%d.%s", i, tt.a.report.config.Locale.String()) - } else { - tc = fmt.Sprintf("%d", i) - } - - t.Run(tc, func(t *testing.T) { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { d := tt.a.Compare(tt.b) got := d.Table() - f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_table.%s", tc)) + f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_table.%d", i)) if os.Getenv("UPDATE_GOLDEN") != "" { golden.Update(t, testdataDir(t), f, got) return diff --git a/testdata/custom_metrics/custom_metric_set_out.0.fr.golden b/testdata/custom_metrics/custom_metric_set_out.0.fr.golden deleted file mode 100644 index e69de29b..00000000 diff --git a/testdata/custom_metrics/custom_metric_set_out.0.ja.golden b/testdata/custom_metrics/custom_metric_set_out.0.ja.golden deleted file mode 100644 index e69de29b..00000000 diff --git a/testdata/custom_metrics/custom_metric_set_out.1.ja.golden b/testdata/custom_metrics/custom_metric_set_out.1.ja.golden deleted file mode 100644 index 3d7c195a..00000000 --- a/testdata/custom_metrics/custom_metric_set_out.1.ja.golden +++ /dev/null @@ -1,4 +0,0 @@ -  Benchmark-0  main (1234567) ---------------------------------------------- - Number of iterations 1,000 - Nanoseconds per iteration 676.5 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.fr.golden b/testdata/custom_metrics/custom_metric_set_out.2.fr.golden deleted file mode 100644 index 2521f201..00000000 --- a/testdata/custom_metrics/custom_metric_set_out.2.fr.golden +++ /dev/null @@ -1,4 +0,0 @@ -  Benchmark-1  main (1234567) ---------------------------------------------- - Number of iterations 1 500 - Nanoseconds per iteration 1 340 ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.1.fr.golden b/testdata/custom_metrics/custom_metric_set_out.3.golden similarity index 100% rename from testdata/custom_metrics/custom_metric_set_out.1.fr.golden rename to testdata/custom_metrics/custom_metric_set_out.3.golden diff --git a/testdata/custom_metrics/custom_metric_set_out.2.ja.golden b/testdata/custom_metrics/custom_metric_set_out.4.golden similarity index 100% rename from testdata/custom_metrics/custom_metric_set_out.2.ja.golden rename to testdata/custom_metrics/custom_metric_set_out.4.golden diff --git a/testdata/custom_metrics/custom_metric_set_table.0.fr.golden b/testdata/custom_metrics/custom_metric_set_table.0.fr.golden deleted file mode 100644 index e69de29b..00000000 diff --git a/testdata/custom_metrics/custom_metric_set_table.0.ja.golden b/testdata/custom_metrics/custom_metric_set_table.0.ja.golden deleted file mode 100644 index e69de29b..00000000 diff --git a/testdata/custom_metrics/custom_metric_set_table.1.ja.golden b/testdata/custom_metrics/custom_metric_set_table.1.ja.golden deleted file mode 100644 index f11339ed..00000000 --- a/testdata/custom_metrics/custom_metric_set_table.1.ja.golden +++ /dev/null @@ -1,5 +0,0 @@ -## Benchmark-0 - -| Number of iterations | Nanoseconds per iteration | -|---------------------:|--------------------------:| -| 1,000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.fr.golden b/testdata/custom_metrics/custom_metric_set_table.2.fr.golden deleted file mode 100644 index decdc231..00000000 --- a/testdata/custom_metrics/custom_metric_set_table.2.fr.golden +++ /dev/null @@ -1,5 +0,0 @@ -## Benchmark-1 - -| Number of iterations | Nanoseconds per iteration | -|---------------------:|--------------------------:| -| 1 500 | 1 340 ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.3.ja.golden b/testdata/custom_metrics/custom_metric_set_table.3.ja.golden deleted file mode 100644 index 06e52f7c..00000000 --- a/testdata/custom_metrics/custom_metric_set_table.3.ja.golden +++ /dev/null @@ -1,9 +0,0 @@ -## Many Metrics - -| | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | -|----------:|------------------------------------------------------------------:| -| Metrics A | 1,500 | -| Metrics B | 1,340 | -| Metrics C | 1,600 | -| Metrics D | 1,010 | -| Metrics E | 1,800 | diff --git a/testdata/custom_metrics/custom_metric_set_table.1.fr.golden b/testdata/custom_metrics/custom_metric_set_table.4.golden similarity index 100% rename from testdata/custom_metrics/custom_metric_set_table.1.fr.golden rename to testdata/custom_metrics/custom_metric_set_table.4.golden diff --git a/testdata/custom_metrics/custom_metric_set_table.2.ja.golden b/testdata/custom_metrics/custom_metric_set_table.5.golden similarity index 100% rename from testdata/custom_metrics/custom_metric_set_table.2.ja.golden rename to testdata/custom_metrics/custom_metric_set_table.5.golden diff --git a/testdata/custom_metrics/custom_metric_set_table.3.fr.golden b/testdata/custom_metrics/custom_metric_set_table.6.golden similarity index 100% rename from testdata/custom_metrics/custom_metric_set_table.3.fr.golden rename to testdata/custom_metrics/custom_metric_set_table.6.golden diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden deleted file mode 100644 index f11339ed..00000000 --- a/testdata/custom_metrics/diff_custom_metric_set_table.0.ja.golden +++ /dev/null @@ -1,5 +0,0 @@ -## Benchmark-0 - -| Number of iterations | Nanoseconds per iteration | -|---------------------:|--------------------------:| -| 1,000 | 676.5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden deleted file mode 100644 index 96ea4615..00000000 --- a/testdata/custom_metrics/diff_custom_metric_set_table.1.fr.golden +++ /dev/null @@ -1,6 +0,0 @@ -## Benchmark-0 - -| | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | -|-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| -| **Number of iterations** | 9 393 | 1 000 | -8 393 | -| **Nanoseconds per iteration** | 456 ns/op | 676,5 ns/op | 220,5 ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.fr.golden b/testdata/custom_metrics/diff_custom_metric_set_table.2.golden similarity index 100% rename from testdata/custom_metrics/diff_custom_metric_set_table.0.fr.golden rename to testdata/custom_metrics/diff_custom_metric_set_table.2.golden diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.ja.golden b/testdata/custom_metrics/diff_custom_metric_set_table.3.golden similarity index 100% rename from testdata/custom_metrics/diff_custom_metric_set_table.1.ja.golden rename to testdata/custom_metrics/diff_custom_metric_set_table.3.golden From 918c0c998c035c8a3cc6b76bcd20b8367b03e3e8 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sat, 13 Jan 2024 23:45:45 +0900 Subject: [PATCH 19/30] Fix lint --- report/custom_test.go | 60 ++++++++++++++++++++++++++++++++++++++----- report/option.go | 8 +++--- report/report.go | 14 +++++----- report/report_test.go | 2 +- 4 files changed, 65 insertions(+), 19 deletions(-) diff --git a/report/custom_test.go b/report/custom_test.go index b078b3c4..5fc4cdce 100644 --- a/report/custom_test.go +++ b/report/custom_test.go @@ -56,7 +56,7 @@ func TestCustomMetricSetTable(t *testing.T) { {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"}, }, report: &Report{ - config: &ReportOptions{Locale: &language.French}, + opts: &Options{Locale: &language.French}, }, }}, {&CustomMetricSet{ @@ -67,7 +67,7 @@ func TestCustomMetricSetTable(t *testing.T) { {Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 1340.0, Unit: " ns/op"}, }, report: &Report{ - config: &ReportOptions{Locale: &language.Japanese}, + opts: &Options{Locale: &language.Japanese}, }, }}, {&CustomMetricSet{ @@ -84,7 +84,7 @@ func TestCustomMetricSetTable(t *testing.T) { Ref: "main", Commit: "1234567890", covPaths: []string{"testdata/cover.out"}, - config: &ReportOptions{Locale: &language.French}, + opts: &Options{Locale: &language.French}, }, }}, } @@ -178,7 +178,7 @@ func TestCustomMetricSetOut(t *testing.T) { Ref: "main", Commit: "1234567890", covPaths: []string{"testdata/cover.out"}, - config: &ReportOptions{Locale: &language.French}, + opts: &Options{Locale: &language.French}, }, }}, {&CustomMetricSet{ @@ -192,7 +192,7 @@ func TestCustomMetricSetOut(t *testing.T) { Ref: "main", Commit: "1234567890", covPaths: []string{"testdata/cover.out"}, - config: &ReportOptions{Locale: &language.Japanese}, + opts: &Options{Locale: &language.Japanese}, }, }}, } @@ -335,7 +335,7 @@ func TestDiffCustomMetricSetTable(t *testing.T) { Ref: "main", Commit: "1234567890", covPaths: []string{"testdata/cover.out"}, - config: &ReportOptions{Locale: &language.French}, + opts: &Options{Locale: &language.French}, }, }, nil, @@ -352,7 +352,7 @@ func TestDiffCustomMetricSetTable(t *testing.T) { Ref: "main", Commit: "1234567890", covPaths: []string{"testdata/cover.out"}, - config: &ReportOptions{Locale: &language.Japanese}, + opts: &Options{Locale: &language.Japanese}, }, }, &CustomMetricSet{ @@ -438,6 +438,52 @@ func TestDiffCustomMetricSetMetadataTable(t *testing.T) { }, }, }, + { + &CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metadata: []*MetadataKV{ + {Key: "goos", Value: "darwin"}, + {Key: "goarch", Value: "amd64"}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + opts: &Options{Locale: &language.French}, + }, + }, + nil, + }, + { + &CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metadata: []*MetadataKV{ + {Key: "goos", Value: "darwin"}, + {Key: "goarch", Value: "amd64"}, + }, + report: &Report{ + Ref: "main", + Commit: "1234567890", + covPaths: []string{"testdata/cover.out"}, + opts: &Options{Locale: &language.Japanese}, + }, + }, + &CustomMetricSet{ + Key: "benchmark_0", + Name: "Benchmark-0", + Metadata: []*MetadataKV{ + {Key: "goos", Value: "arwin"}, + {Key: "goarch", Value: "arm64"}, + }, + report: &Report{ + Ref: "main", + Commit: "2345678901", + covPaths: []string{"testdata/cover.out"}, + }, + }, + }, } t.Setenv("GITHUB_SERVER_URL", "https://github.com") diff --git a/report/option.go b/report/option.go index 611290af..6d07689f 100644 --- a/report/option.go +++ b/report/option.go @@ -2,14 +2,14 @@ package report import "golang.org/x/text/language" -type ReportOptions struct { +type Options struct { Locale *language.Tag } -type ReportOption func(*ReportOptions) +type Option func(*Options) -func Locale(locale *language.Tag) ReportOption { - return func(args *ReportOptions) { +func Locale(locale *language.Tag) Option { + return func(args *Options) { args.Locale = locale } } diff --git a/report/report.go b/report/report.go index 75215056..bfbf534c 100644 --- a/report/report.go +++ b/report/report.go @@ -45,10 +45,10 @@ type Report struct { // coverage report paths covPaths []string - config *ReportOptions + opts *Options } -func New(ownerrepo string, opts ...ReportOption) (*Report, error) { +func New(ownerrepo string, opts ...Option) (*Report, error) { if ownerrepo == "" { ownerrepo = os.Getenv("GITHUB_REPOSITORY") } @@ -68,9 +68,9 @@ func New(ownerrepo string, opts ...ReportOption) (*Report, error) { commit = strings.TrimSuffix(string(b), "\n") } } - config := &ReportOptions{} + o := &Options{} for _, setter := range opts { - setter(config) + setter(o) } return &Report{ @@ -78,7 +78,7 @@ func New(ownerrepo string, opts ...ReportOption) (*Report, error) { Ref: ref, Commit: commit, Timestamp: time.Now().UTC(), - config: config, + opts: o, }, nil } @@ -552,8 +552,8 @@ func (r *Report) findCustomMetricSetByKey(key string) *CustomMetricSet { } func (r *Report) convertFormat(v interface{}) string { - if r.config != nil && r.config.Locale != nil { - p := message.NewPrinter(*r.config.Locale) + if r.opts != nil && r.opts.Locale != nil { + p := message.NewPrinter(*r.opts.Locale) return p.Sprint(number.Decimal(v)) } diff --git a/report/report_test.go b/report/report_test.go index cb624835..c8f99e9b 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -568,7 +568,7 @@ func TestConvertFormat(t *testing.T) { for i, tt := range tests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { l := language.Japanese - r := &Report{config: &ReportOptions{Locale: &l}} + r := &Report{opts: &Options{Locale: &l}} got := r.convertFormat(tt.n) if diff := cmp.Diff(got, tt.want, nil); diff != "" { From 0a683e71dfa4006e33c60d10ec62ca9c862b7dbe Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 14 Jan 2024 09:06:46 +0900 Subject: [PATCH 20/30] Testing Functional Options Pattern --- report/report_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/report/report_test.go b/report/report_test.go index c8f99e9b..024fd4c2 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -44,6 +44,29 @@ func TestNew(t *testing.T) { } } +func TestNewWithOptions(t *testing.T) { + tests := []struct { + opts []Option + want *language.Tag + }{ + {nil, nil}, + {[]Option{Locale(&language.Japanese)}, &language.Japanese}, + {[]Option{Locale(&language.French)}, &language.French}, + {[]Option{Locale(&language.Japanese), Locale(&language.French)}, &language.French}, // Be overwritten + } + for _, tt := range tests { + r, err := New("somthing", tt.opts...) + if err != nil { + t.Error(err) + continue + } + got := r.opts.Locale + if got != tt.want { + t.Errorf("got %v\nwant %v", got, tt.want) + } + } +} + func TestMeasureCoverage(t *testing.T) { log.SetOutput(io.Discard) // Disable log in challengeParseReport() From cf30bd8511028c95082714815e00406dbc795444 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 14 Jan 2024 09:50:03 +0900 Subject: [PATCH 21/30] Nil guard --- report/custom.go | 55 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/report/custom.go b/report/custom.go index 79d76643..14b63e65 100644 --- a/report/custom.go +++ b/report/custom.go @@ -65,13 +65,17 @@ func (s *CustomMetricSet) Table() string { if len(s.Metrics) >= swapXYMin { return s.tableSwaped() } + report := s.report + if report == nil { + report = &Report{} + } var ( h []string d []string ) for _, m := range s.Metrics { h = append(h, m.Name) - d = append(d, fmt.Sprintf("%s%s", s.report.convertFormat(m.Value), m.Unit)) + d = append(d, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit)) } buf := new(bytes.Buffer) _, _ = buf.WriteString(fmt.Sprintf("## %s\n\n", s.Name)) //nostyle:handlerrors @@ -95,8 +99,13 @@ func (s *CustomMetricSet) tableSwaped() string { table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) table.SetCenterSeparator("|") table.SetHeader([]string{"", makeHeadTitleWithLink(s.report.Ref, s.report.Commit, nil)}) + + report := s.report + if report == nil { + report = &Report{} + } for _, m := range s.Metrics { - table.Append([]string{m.Name, fmt.Sprintf("%s%s", s.report.convertFormat(m.Value), m.Unit)}) + table.Append([]string{m.Name, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit)}) } table.Render() return strings.Replace(buf.String(), "---|", "--:|", len(s.Metrics)) @@ -148,11 +157,16 @@ func (s *CustomMetricSet) Out(w io.Writer) error { table.SetBorder(false) table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT}) + report := s.report + if report == nil { + report = &Report{} + } + for _, m := range s.Metrics { if m.Name == "" { m.Name = m.Key } - table.Rich([]string{m.Name, fmt.Sprintf("%s%s", s.report.convertFormat(m.Value), m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) + table.Rich([]string{m.Name, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) } table.Render() @@ -161,11 +175,11 @@ func (s *CustomMetricSet) Out(w io.Writer) error { func (s *CustomMetricSet) Compare(s2 *CustomMetricSet) *DiffCustomMetricSet { d := &DiffCustomMetricSet{ - Key: s.Key, - Name: s.Name, - A: s, - B: s2, - report: s.report, + Key: s.Key, + Name: s.Name, + A: s, + B: s2, + // report: s.report, } if s2 == nil { for _, m := range s.Metrics { @@ -260,6 +274,11 @@ func (d *DiffCustomMetricSet) Table() string { table.SetCenterSeparator("|") table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT}) table.SetHeader([]string{"", makeHeadTitleWithLink(d.B.report.Ref, d.B.report.Commit, nil), makeHeadTitleWithLink(d.A.report.Ref, d.A.report.Commit, nil), "+/-"}) + report := d.report + if report == nil { + report = &Report{} + } + for _, m := range d.Metrics { var va, vb, diff string switch { @@ -267,20 +286,20 @@ func (d *DiffCustomMetricSet) Table() string { continue case m.A != nil && m.B == nil: vb = "" - va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.A), m.customMetricA.Unit) - diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit) + diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit) case m.A == nil && m.B != nil: va = "" - va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricB.Unit) + va = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricB.Unit) case isInt(*m.A) && isInt(*m.B): - va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.A), m.customMetricA.Unit) - vb = fmt.Sprintf("%s%s", d.report.convertFormat(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit) default: - va = fmt.Sprintf("%s%s", d.report.convertFormat(*m.A), m.customMetricA.Unit) - vb = fmt.Sprintf("%s%s", d.report.convertFormat(*m.B), m.customMetricB.Unit) - diff = fmt.Sprintf("%s%s", d.report.convertFormat(m.Diff), m.customMetricA.Unit) + va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit) } if m.Name == "" { m.Name = m.Key From 2edb67424ac318c0b95b490efd819b1622e112d3 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 14 Jan 2024 09:50:13 +0900 Subject: [PATCH 22/30] UPDATE_GOLDEN --- .../diff_custom_metric_set_metadata_table.2.golden | 7 +++++++ .../diff_custom_metric_set_metadata_table.3.golden | 8 ++++++++ .../custom_metrics/diff_custom_metric_set_table.3.golden | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 testdata/custom_metrics/diff_custom_metric_set_metadata_table.2.golden create mode 100644 testdata/custom_metrics/diff_custom_metric_set_metadata_table.3.golden diff --git a/testdata/custom_metrics/diff_custom_metric_set_metadata_table.2.golden b/testdata/custom_metrics/diff_custom_metric_set_metadata_table.2.golden new file mode 100644 index 00000000..4185c6be --- /dev/null +++ b/testdata/custom_metrics/diff_custom_metric_set_metadata_table.2.golden @@ -0,0 +1,7 @@ +
Metadata + +| goos | goarch | +|-------:|-------:| +| darwin | amd64 | + +
diff --git a/testdata/custom_metrics/diff_custom_metric_set_metadata_table.3.golden b/testdata/custom_metrics/diff_custom_metric_set_metadata_table.3.golden new file mode 100644 index 00000000..4c81fd70 --- /dev/null +++ b/testdata/custom_metrics/diff_custom_metric_set_metadata_table.3.golden @@ -0,0 +1,8 @@ +
Metadata + +| | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +|------------|------------------------------------------------------------------:|------------------------------------------------------------------:| +| **goos** | arwin | darwin | +| **goarch** | arm64 | amd64 | + +
diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.3.golden b/testdata/custom_metrics/diff_custom_metric_set_table.3.golden index 235b7d29..4f887783 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.3.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.3.golden @@ -2,5 +2,5 @@ | | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | |-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| -| **Number of iterations** | 9,393 | 1,000 | -8,393 | +| **Number of iterations** | 9393 | 1000 | -8393 | | **Nanoseconds per iteration** | 456 ns/op | 676.5 ns/op | 220.5 ns/op | From 5099def57677112a6fd89767a3c50a62c0a2fb86 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 14 Jan 2024 10:03:07 +0900 Subject: [PATCH 23/30] Fix --- report/custom.go | 10 +++++----- .../diff_custom_metric_set_table.3.golden | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/report/custom.go b/report/custom.go index 14b63e65..74be6ce2 100644 --- a/report/custom.go +++ b/report/custom.go @@ -175,11 +175,11 @@ func (s *CustomMetricSet) Out(w io.Writer) error { func (s *CustomMetricSet) Compare(s2 *CustomMetricSet) *DiffCustomMetricSet { d := &DiffCustomMetricSet{ - Key: s.Key, - Name: s.Name, - A: s, - B: s2, - // report: s.report, + Key: s.Key, + Name: s.Name, + A: s, + B: s2, + report: s.report, } if s2 == nil { for _, m := range s.Metrics { diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.3.golden b/testdata/custom_metrics/diff_custom_metric_set_table.3.golden index 4f887783..235b7d29 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.3.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.3.golden @@ -2,5 +2,5 @@ | | main ([2345678](https://github.com/owner/repo/commit/2345678901)) | main ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | |-------------------------------|------------------------------------------------------------------:|------------------------------------------------------------------:|------------:| -| **Number of iterations** | 9393 | 1000 | -8393 | +| **Number of iterations** | 9,393 | 1,000 | -8,393 | | **Nanoseconds per iteration** | 456 ns/op | 676.5 ns/op | 220.5 ns/op | From 776d499f0d656e80f84e7b111807e34e9fa8daef Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 14 Jan 2024 23:43:07 +0900 Subject: [PATCH 24/30] Fix --- report/custom.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report/custom.go b/report/custom.go index 74be6ce2..4db9fc33 100644 --- a/report/custom.go +++ b/report/custom.go @@ -290,7 +290,7 @@ func (d *DiffCustomMetricSet) Table() string { diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit) case m.A == nil && m.B != nil: va = "" - va = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit) + vb = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit) diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricB.Unit) case isInt(*m.A) && isInt(*m.B): va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit) From ad8c7d2f8529283203030a7505ba98f34104a049 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 21 Jan 2024 16:45:38 +0900 Subject: [PATCH 25/30] Fix panic --- report/report.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/report/report.go b/report/report.go index bfbf534c..8ade281b 100644 --- a/report/report.go +++ b/report/report.go @@ -460,14 +460,20 @@ func (r *Report) CollectCustomMetrics() error { } func (r *Report) CoveragePercent() float64 { - if r.Coverage == nil || r.Coverage.Total == 0 { + if r.Coverage == nil { + return 0.0 + } + if r.Coverage.Total == 0 { return 0.0 } return float64(r.Coverage.Covered) / float64(r.Coverage.Total) * 100 } func (r *Report) CodeToTestRatioRatio() float64 { - if r.CodeToTestRatio == nil || r.CodeToTestRatio.Code == 0 { + if r.CodeToTestRatio == nil { + return 0.0 + } + if r.CodeToTestRatio.Code == 0 { return 0.0 } return float64(r.CodeToTestRatio.Test) / float64(r.CodeToTestRatio.Code) From c79bfab5e5405bb17ad09125e5e3d3516adfb247 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 21 Jan 2024 19:24:43 +0900 Subject: [PATCH 26/30] Fix gostyle --- report/report.go | 12 +++--------- report/report_test.go | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/report/report.go b/report/report.go index 8ade281b..a7d3dcd4 100644 --- a/report/report.go +++ b/report/report.go @@ -460,20 +460,14 @@ func (r *Report) CollectCustomMetrics() error { } func (r *Report) CoveragePercent() float64 { - if r.Coverage == nil { - return 0.0 - } - if r.Coverage.Total == 0 { + if r == nil || r.Coverage == nil || r.Coverage.Total == 0 { return 0.0 } return float64(r.Coverage.Covered) / float64(r.Coverage.Total) * 100 } func (r *Report) CodeToTestRatioRatio() float64 { - if r.CodeToTestRatio == nil { - return 0.0 - } - if r.CodeToTestRatio.Code == 0 { + if r == nil || r.CodeToTestRatio == nil || r.CodeToTestRatio.Code == 0 { return 0.0 } return float64(r.CodeToTestRatio.Test) / float64(r.CodeToTestRatio.Code) @@ -557,7 +551,7 @@ func (r *Report) findCustomMetricSetByKey(key string) *CustomMetricSet { return nil } -func (r *Report) convertFormat(v interface{}) string { +func (r *Report) convertFormat(v any) string { if r.opts != nil && r.opts.Locale != nil { p := message.NewPrinter(*r.opts.Locale) return p.Sprint(number.Decimal(v)) diff --git a/report/report_test.go b/report/report_test.go index 024fd4c2..156ab001 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -563,7 +563,7 @@ func coverageTestdataDir(t *testing.T) string { func TestConvertFormat(t *testing.T) { tests := []struct { - n interface{} + n any want string }{ { From 6c79005a6c8ff548d1a947a423421102b5f2cf3b Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 21 Jan 2024 20:27:50 +0900 Subject: [PATCH 27/30] Fix panic --- report/report.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/report/report.go b/report/report.go index a7d3dcd4..2dc37074 100644 --- a/report/report.go +++ b/report/report.go @@ -271,6 +271,9 @@ func (r *Report) IsMeasuredCodeToTestRatio() bool { } func (r *Report) IsMeasuredTestExecutionTime() bool { + if r == nil { + return false + } return r.TestExecutionTime != nil } @@ -474,7 +477,7 @@ func (r *Report) CodeToTestRatioRatio() float64 { } func (r *Report) TestExecutionTimeNano() float64 { - if r.TestExecutionTime == nil { + if r == nil || r.TestExecutionTime == nil { return 0.0 } return *r.TestExecutionTime From 7844b9f8010091896f2ffbb47796bc42e9c57ad9 Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Sun, 21 Jan 2024 23:33:59 +0900 Subject: [PATCH 28/30] Remove unnecessary nil checks --- config/config.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index 8f1251f7..94b41233 100644 --- a/config/config.go +++ b/config/config.go @@ -201,20 +201,14 @@ type Reporter interface { func (c *Config) Acceptable(r, rPrev Reporter) error { var result *multierror.Error if err := c.CoverageConfigReady(); err == nil { - prev := 0.0 - if rPrev != nil { - prev = rPrev.CoveragePercent() - } + prev := rPrev.CoveragePercent() if err := coverageAcceptable(r.CoveragePercent(), prev, c.Coverage.Acceptable); err != nil { result = multierror.Append(result, err) } } if err := c.CodeToTestRatioConfigReady(); err == nil { - prev := 0.0 - if rPrev != nil { - prev = rPrev.CodeToTestRatioRatio() - } + prev := rPrev.CodeToTestRatioRatio() if err := codeToTestRatioAcceptable(r.CodeToTestRatioRatio(), prev, c.CodeToTestRatio.Acceptable); err != nil { result = multierror.Append(result, err) } @@ -222,10 +216,8 @@ func (c *Config) Acceptable(r, rPrev Reporter) error { if err := c.TestExecutionTimeConfigReady(); err == nil { prev := largeEnoughTime - if rPrev != nil { - if rPrev.IsMeasuredTestExecutionTime() { - prev = rPrev.TestExecutionTimeNano() - } + if rPrev.IsMeasuredTestExecutionTime() { + prev = rPrev.TestExecutionTimeNano() } if err := testExecutionTimeAcceptable(r.TestExecutionTimeNano(), prev, c.TestExecutionTime.Acceptable); err != nil { From 98cba55cf181b4e5681275a3d6dbf99934b2bf0b Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Mon, 22 Jan 2024 21:06:05 +0900 Subject: [PATCH 29/30] Rename --- config/yaml.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/yaml.go b/config/yaml.go index 081a3b92..fd29c81e 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -88,12 +88,12 @@ func (c *Config) UnmarshalYAML(data []byte) error { } if s.Locale != "" { - tag, err := language.Parse(s.Locale) + l, err := language.Parse(s.Locale) if err != nil { return err } - c.Locale = &tag + c.Locale = &l } return nil From 19b968fc4be9d4489dec996ec97306b01c4d37db Mon Sep 17 00:00:00 2001 From: k2tzumi Date: Mon, 22 Jan 2024 21:12:00 +0900 Subject: [PATCH 30/30] Remove unnecessary struct variables --- report/custom.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/report/custom.go b/report/custom.go index 4db9fc33..d16a5d5d 100644 --- a/report/custom.go +++ b/report/custom.go @@ -45,7 +45,6 @@ type DiffCustomMetricSet struct { A *CustomMetricSet `json:"a"` B *CustomMetricSet `json:"b"` Metrics []*DiffCustomMetric `json:"metrics"` - report *Report } type DiffCustomMetric struct { @@ -101,9 +100,6 @@ func (s *CustomMetricSet) tableSwaped() string { table.SetHeader([]string{"", makeHeadTitleWithLink(s.report.Ref, s.report.Commit, nil)}) report := s.report - if report == nil { - report = &Report{} - } for _, m := range s.Metrics { table.Append([]string{m.Name, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit)}) } @@ -175,11 +171,10 @@ func (s *CustomMetricSet) Out(w io.Writer) error { func (s *CustomMetricSet) Compare(s2 *CustomMetricSet) *DiffCustomMetricSet { d := &DiffCustomMetricSet{ - Key: s.Key, - Name: s.Name, - A: s, - B: s2, - report: s.report, + Key: s.Key, + Name: s.Name, + A: s, + B: s2, } if s2 == nil { for _, m := range s.Metrics { @@ -274,10 +269,7 @@ func (d *DiffCustomMetricSet) Table() string { table.SetCenterSeparator("|") table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT}) table.SetHeader([]string{"", makeHeadTitleWithLink(d.B.report.Ref, d.B.report.Commit, nil), makeHeadTitleWithLink(d.A.report.Ref, d.A.report.Commit, nil), "+/-"}) - report := d.report - if report == nil { - report = &Report{} - } + report := d.report() for _, m := range d.Metrics { var va, vb, diff string @@ -343,6 +335,17 @@ func (d *DiffCustomMetricSet) MetadataTable() string { return strings.Replace(strings.Replace(buf.String(), "---|", "--:|", 4), "--:|", "---|", 1) } +func (d *DiffCustomMetricSet) report() *Report { + if d.A != nil && d.A.report != nil { + return d.A.report + } + if d.B != nil && d.B.report != nil { + return d.B.report + } + + return &Report{} +} + func isInt(v float64) bool { return v == float64(int64(v)) }