| // Copyright 2023 The Dawn & Tint Authors | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // 1. Redistributions of source code must retain the above copyright notice, this | |
| // list of conditions and the following disclaimer. | |
| // | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // | |
| // 3. Neither the name of the copyright holder nor the names of its | |
| // contributors may be used to endorse or promote products derived from | |
| // this software without specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| package common | |
| import ( | |
| "errors" | |
| "flag" | |
| "fmt" | |
| "io" | |
| "os/exec" | |
| "path/filepath" | |
| "runtime" | |
| "dawn.googlesource.com/dawn/tools/src/fileutils" | |
| "dawn.googlesource.com/dawn/tools/src/oswrapper" | |
| "dawn.googlesource.com/dawn/tools/src/subcmd" | |
| "dawn.googlesource.com/dawn/tools/src/term" | |
| ) | |
| // Flags holds the common flags for all CTS runners | |
| type Flags struct { | |
| Verbose Verbosity | |
| Colors bool | |
| FailuresOnly bool | |
| NumRunners int | |
| Log string | |
| ResultsPath string | |
| ExpectationsPath string | |
| ExpectationsTags string | |
| CTS string // Path to the CTS directory or CTS zip | |
| Node string // Path to the Node executable | |
| Npx string // Path to the npx executable | |
| } | |
| // ErrNodeNotFound is the error for a command that runs node when Flags.Node is | |
| // not set. | |
| var ErrNodeNotFound = errors.New("cannot find path to node. Specify with --node") | |
| func (f *Flags) Register(fsReader oswrapper.FilesystemReader) { | |
| flag.Var(&f.Verbose, "verbose", "how much to print, as -verbose=`level`: summary (the default: the lists of unexpected results and the counts), unexpected (also the output of each test case with an unexpected result), all (also the output of every other test case) or debug (also the commands that run-cts runs and Dawn's verbose output). -verbose without a level is -verbose=debug") | |
| flag.BoolVar(&f.Colors, "colors", term.CanUseAnsiEscapeSequences(), "enable / disable colors") | |
| flag.BoolVar(&f.FailuresOnly, "failures-only", false, "output only failures to log file") | |
| flag.IntVar(&f.NumRunners, "j", runtime.NumCPU()/2, "number of concurrent runners. 0 runs serially") | |
| flag.StringVar(&f.Log, "log", "", "path to log file of tests run and result") | |
| flag.StringVar(&f.ResultsPath, "output", "", "path to write the results to, as JSON: the status of each test case, xfail for a failure that the -expect file expects") | |
| flag.StringVar(&f.ExpectationsPath, "expect", "", "path to expectations file: a results file written by -output, or a file in the format of webgpu-cts/expectations.txt. The test cases that it skips are not run, and the failures that it expects do not fail the run") | |
| flag.StringVar(&f.ExpectationsTags, "expect-tags", "", "comma-separated tags that select the lines of an expectations.txt file. 'run-cts standalone' adds them to the tags that it works out from the OS, the GPU and the build") | |
| flag.StringVar(&f.CTS, "cts", DefaultCtsPath(fsReader), "root directory of WebGPU CTS") | |
| flag.StringVar(&f.Node, "node", fileutils.NodePath(fsReader), "path to node executable") | |
| flag.StringVar(&f.Npx, "npx", DefaultNpxPath(), "path to npx executable") | |
| } | |
| // Process processes the flags, returning a State. | |
| // Note: Ensure you call Close() on the returned State | |
| func (f *Flags) Process(fsReaderWriter oswrapper.FilesystemReaderWriter) (*State, error) { | |
| s := &State{ | |
| resultsPath: f.ResultsPath, | |
| failuresOnly: f.FailuresOnly, | |
| } | |
| // Create the stdout writer | |
| s.Stdout = term.NewAnsiWriter(f.Colors) | |
| // Check CTS path | |
| if f.CTS == "" { | |
| return nil, subcmd.InvalidCLA() | |
| } | |
| if _, err := fsReaderWriter.Stat(f.CTS); err != nil { | |
| return nil, fmt.Errorf("'%v' does not exist", f.CTS) | |
| } | |
| absCTS, err := filepath.Abs(f.CTS) | |
| if err != nil { | |
| return nil, fmt.Errorf("unable to get absolute path for '%v'", f.CTS) | |
| } | |
| f.CTS = absCTS | |
| // Build the logger, if needed | |
| if f.Log != "" { | |
| writer, err := fsReaderWriter.Create(f.Log) | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to open log '%v': %w", f.Log, err) | |
| } | |
| s.Log = NewLogger(writer) | |
| s.logWriter = writer | |
| } | |
| // If an expectations file was specified, load it. | |
| if f.ExpectationsPath != "" { | |
| if err := s.loadExpectations(f.ExpectationsPath, f.ExpectationsTags, fsReaderWriter); err != nil { | |
| return nil, err | |
| } | |
| } else if f.ExpectationsTags != "" { | |
| return nil, fmt.Errorf("-expect-tags needs -expect") | |
| } | |
| s.CTS = NewCTS(f.CTS, f.Npx, f.Node) | |
| return s, nil | |
| } | |
| // State holds the common state for commands | |
| type State struct { | |
| Stdout io.WriteCloser | |
| Log Logger | |
| CTS CTS | |
| resultsPath string | |
| failuresOnly bool | |
| logWriter io.WriteCloser | |
| expectFile *expectationsFile // the -expect file | |
| } | |
| // Close closes s.Stdout and saves the results to the results path (if set) | |
| func (s *State) Close(results Results) error { | |
| if err := s.Stdout.Close(); err != nil { | |
| return err | |
| } | |
| // If an result file was specified, save results to it. | |
| if s.resultsPath != "" { | |
| statuses := Expectations{} | |
| for testCase, result := range results { | |
| if !s.failuresOnly || result.Status.isFailure() { | |
| statuses[testCase] = s.resultsFileStatus(testCase, result.Status) | |
| } | |
| } | |
| if err := statuses.Save(s.resultsPath); err != nil { | |
| return err | |
| } | |
| } | |
| if s.logWriter != nil { | |
| if err := s.logWriter.Close(); err != nil { | |
| return fmt.Errorf("failed to close log file: %w", err) | |
| } | |
| } | |
| return nil | |
| } | |
| // DefaultCtsPath looks for the webgpu-cts directory in dawn's third_party | |
| // directory. This is used as the default for the --cts command line flag. | |
| func DefaultCtsPath(fsReader oswrapper.FilesystemReader) string { | |
| if dawnRoot := fileutils.DawnRoot(fsReader); dawnRoot != "" { | |
| cts := filepath.Join(dawnRoot, "third_party", "webgpu-cts") | |
| if info, err := fsReader.Stat(cts); err == nil && info.IsDir() { | |
| return cts | |
| } | |
| } | |
| return "" | |
| } | |
| // DefaultNpxPath looks for the npx executable on PATH. This is used as the | |
| // default for the --npx command line flag. | |
| func DefaultNpxPath() string { | |
| if p, err := exec.LookPath("npx"); err == nil { | |
| return p | |
| } | |
| return "" | |
| } |