github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/diagnostics/diagnostics.go (about)

     1  // Copyright 2016 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package diagnostics provides the Diagnostics interface for reporting various
    16  // test statistics as well as a no-op implementation of the interface.
    17  package diagnostics
    18  
    19  import (
    20  	"log"
    21  	"time"
    22  
    23  	"github.com/bazelbuild/rules_webtesting/go/errors"
    24  )
    25  
    26  // Diagnostics manages and outputs a test diagnostics.
    27  type Diagnostics interface {
    28  	// Name is the name of the component used in error messages.
    29  	Name() string
    30  	// Timing reports timing info.
    31  	Timing(component, description, detail string, begin, end time.Time) error
    32  	// Severe reports an error that is highly likely to cause problems in tests.
    33  	Severe(err error) error
    34  	// Warning reports a error that is unlikely to cause problems in tests, but
    35  	// that you want to track anyway.
    36  	Warning(err error) error
    37  	// Close closes this diagnostics object.
    38  	Close() error
    39  }
    40  
    41  type noOPDiagnostics struct {
    42  	closed bool
    43  }
    44  
    45  // NoOP creates a new empty Diagnostics object.
    46  func NoOP() Diagnostics {
    47  	return &noOPDiagnostics{}
    48  }
    49  
    50  // Name is the name of the component used in error messages.
    51  func (d *noOPDiagnostics) Name() string {
    52  	return "No-OP Diagnostics"
    53  }
    54  
    55  // Timing reports timing info.
    56  func (d *noOPDiagnostics) Timing(_, _, _ string, _, _ time.Time) error {
    57  	if d.closed {
    58  		return errors.New(d.Name(), "cannot add timing data after diagnostics are closed")
    59  	}
    60  	return nil
    61  }
    62  
    63  // Severe reports an error that is highly likely to cause problems in tests.
    64  func (d *noOPDiagnostics) Severe(err error) error {
    65  	if d.closed {
    66  		return errors.New(d.Name(), "cannot add errors after diagnostics are closed")
    67  	}
    68  	log.Print(err)
    69  	return nil
    70  }
    71  
    72  // Warning reports a error that is unlikely to cause problems in tests, but
    73  // that you want to track anyway.
    74  func (d *noOPDiagnostics) Warning(err error) error {
    75  	if d.closed {
    76  		return errors.New(d.Name(), "cannot add errors after diagnostics are closed")
    77  	}
    78  	log.Print(err)
    79  	return nil
    80  }
    81  
    82  // Close closes this diagnostics object.
    83  func (d *noOPDiagnostics) Close() error {
    84  	d.closed = true
    85  	return nil
    86  }
    87  
    88  // String returns a string representation of the diagnostics.
    89  func (d *noOPDiagnostics) String() string {
    90  	return "No-OP Diagnostics"
    91  }