diff --git a/tools/src/cmd/cts/main.go b/tools/src/cmd/cts/main.go index d2997dddf9..af8660d30b 100644 --- a/tools/src/cmd/cts/main.go +++ b/tools/src/cmd/cts/main.go @@ -29,6 +29,7 @@ import ( // Register sub-commands _ "dawn.googlesource.com/dawn/tools/src/cmd/cts/format" + _ "dawn.googlesource.com/dawn/tools/src/cmd/cts/merge" _ "dawn.googlesource.com/dawn/tools/src/cmd/cts/results" _ "dawn.googlesource.com/dawn/tools/src/cmd/cts/time" ) diff --git a/tools/src/cmd/cts/merge/merge.go b/tools/src/cmd/cts/merge/merge.go new file mode 100644 index 0000000000..b2b80e8cb0 --- /dev/null +++ b/tools/src/cmd/cts/merge/merge.go @@ -0,0 +1,76 @@ +// Copyright 2022 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package merge + +import ( + "context" + "flag" + "fmt" + "os" + + "dawn.googlesource.com/dawn/tools/src/cmd/cts/common" + "dawn.googlesource.com/dawn/tools/src/cts/result" +) + +func init() { + common.Register(&cmd{}) +} + +type cmd struct { + flags struct { + output string + } +} + +func (cmd) Name() string { return "merge" } + +func (cmd) Desc() string { return "merges results files into one" } + +func (c *cmd) RegisterFlags(ctx context.Context, cfg common.Config) ([]string, error) { + flag.StringVar(&c.flags.output, "o", "results.txt", "output file. '-' writes to stdout") + return []string{"first-results.txt", "second-results.txt ..."}, nil +} + +func (c *cmd) Run(ctx context.Context, cfg common.Config) error { + // Load each of the results files and merge together + var results result.List + for _, path := range flag.Args() { + // Load results + r, err := result.Load(path) + if err != nil { + return fmt.Errorf("while reading '%v': %w", path, err) + } + // Combine and merge + if len(results) > 0 { + results = result.Merge(results, r) + } else { + results = r + } + } + + // Open output file + output := os.Stdout + if c.flags.output != "-" { + var err error + output, err = os.Create(c.flags.output) + if err != nil { + return fmt.Errorf("failed to open output file '%v': %w", c.flags.output, err) + } + defer output.Close() + } + + // Write out + return result.Write(output, results) +}