tools: Add the 'cts format' sub-command

Formats an expectation file

Bug: dawn:1342
Change-Id: Idc7728ad5b955fcbdb4062e511aff6665a711718
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/88318
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Ben Clayton 2022-04-29 19:09:17 +00:00
parent 846e3149b7
commit 251e464af1
4 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,31 @@
// 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 common
import (
"dawn.googlesource.com/dawn/tools/src/subcmd"
)
// The registered commands
var commands []Command
// Command is the type of a single cts command
type Command = subcmd.Command[Config]
// Register registers the command for use by the 'cts' tool
func Register(c Command) { commands = append(commands, c) }
// Commands returns all the commands registered
func Commands() []Command { return commands }

View File

@ -0,0 +1,38 @@
// 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 common
import (
"os"
"path/filepath"
"dawn.googlesource.com/dawn/tools/src/utils"
)
const (
// RelativeExpectationsPath is the dawn-root relative path to the
// expectations.txt file.
RelativeExpectationsPath = "webgpu-cts/expectations.txt"
)
// DefaultExpectationsPath returns the default path to the expectations.txt
// file. Returns an empty string if the file cannot be found.
func DefaultExpectationsPath() string {
path := filepath.Join(utils.DawnRoot(), RelativeExpectationsPath)
if _, err := os.Stat(path); err != nil {
return ""
}
return path
}

View File

@ -0,0 +1,55 @@
// 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 format
import (
"context"
"flag"
"dawn.googlesource.com/dawn/tools/src/cmd/cts/common"
"dawn.googlesource.com/dawn/tools/src/cts/expectations"
)
func init() {
common.Register(&cmd{})
}
type cmd struct {
flags struct {
expectations string // expectations file path
}
}
func (cmd) Name() string {
return "format"
}
func (cmd) Desc() string {
return "formats a WebGPUExpectation file"
}
func (c *cmd) RegisterFlags(ctx context.Context, cfg common.Config) ([]string, error) {
defaultExpectations := common.DefaultExpectationsPath()
flag.StringVar(&c.flags.expectations, "expectations", defaultExpectations, "path to CTS expectations file to update")
return nil, nil
}
func (c *cmd) Run(ctx context.Context, cfg common.Config) error {
ex, err := expectations.Load(c.flags.expectations)
if err != nil {
return err
}
return ex.Save(c.flags.expectations)
}

49
tools/src/cmd/cts/main.go Normal file
View File

@ -0,0 +1,49 @@
// 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.
// cts is a collection of sub-commands for operating on the WebGPU CTS.
//
// To view available commands run: '<dawn>/tools/run cts --help'
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"dawn.googlesource.com/dawn/tools/src/cmd/cts/common"
"dawn.googlesource.com/dawn/tools/src/subcmd"
"dawn.googlesource.com/dawn/tools/src/utils"
// Register sub-commands
_ "dawn.googlesource.com/dawn/tools/src/cmd/cts/format"
)
func main() {
ctx := context.Background()
cfg, err := common.LoadConfig(filepath.Join(utils.ThisDir(), "config.json"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := subcmd.Run(ctx, *cfg, common.Commands()...); err != nil {
if err != subcmd.ErrInvalidCLA {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}
}