1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-23 22:34:47 +02:00
Files
opentelemetry-go/exporters/zipkin/internal/matchers/expectation.go

300 lines
7.2 KiB
Go
Raw Normal View History

2023-08-10 16:00:53 -07:00
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/matchers/expectation.go.tmpl
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
2023-08-10 16:00:53 -07:00
fix(deps): update module github.com/golangci/golangci-lint to v2 (#6499) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [github.com/golangci/golangci-lint](https://redirect.github.com/golangci/golangci-lint) | `v1.64.8` -> `v2.0.2` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fgolangci%2fgolangci-lint/v2.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fgolangci%2fgolangci-lint/v2.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fgolangci%2fgolangci-lint/v1.64.8/v2.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fgolangci%2fgolangci-lint/v1.64.8/v2.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>golangci/golangci-lint (github.com/golangci/golangci-lint)</summary> ### [`v2.0.2`](https://redirect.github.com/golangci/golangci-lint/blob/HEAD/CHANGELOG.md#v202) [Compare Source](https://redirect.github.com/golangci/golangci-lint/compare/v2.0.1...v2.0.2) 1. Misc. - Fixes flags parsing for formatters - Fixes the filepath used by the exclusion `source` option 2. Documentation - Adds a section about flags migration - Cleaning pages with v1 options ### [`v2.0.1`](https://redirect.github.com/golangci/golangci-lint/blob/HEAD/CHANGELOG.md#v201) [Compare Source](https://redirect.github.com/golangci/golangci-lint/compare/v2.0.0...v2.0.1) 1. Linters/formatters bug fixes - `golines`: fix settings during linter load 2. Misc. - Validates the `version` field before the configuration - `forbidigo`: fix migration ### [`v2.0.0`](https://redirect.github.com/golangci/golangci-lint/blob/HEAD/CHANGELOG.md#v200) [Compare Source](https://redirect.github.com/golangci/golangci-lint/compare/v1.64.8...v2.0.0) 1. Enhancements - 🌟 New `golangci-lint fmt` command with dedicated formatter configuration (https://golangci-lint.run/welcome/quick-start/#formatting) - ♻️ New `golangci-lint migrate` command to help migration from v1 to v2 (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#command-migrate)) - ⚠️ New default values (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/)) - ⚠️ No exclusions by default (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#issuesexclude-use-default)) - ⚠️ New default sort order (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#outputsort-order)) - 🌟 New option `run.relative-path-mode` (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#runrelative-path-mode)) - 🌟 New linters configuration (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#linters)) - 🌟 New output format configuration (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#output)) - 🌟 New `--fast-only` flag (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#lintersfast)) - 🌟 New option `linters.exclusions.warn-unused` to log a warning if an exclusion rule is unused. 2. New linters/formatters - Add `golines` formatter https://github.com/segmentio/golines 3. Linters new features - ⚠️ Merge `staticcheck`, `stylecheck`, `gosimple` into one linter (`staticcheck`) (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#lintersenablestylecheckgosimplestaticcheck)) - `go-critic`: from 0.12.0 to 0.13.0 - `gomodguard`: from 1.3.5 to 1.4.1 (block explicit indirect dependencies) - `nilnil`: from 1.0.1 to 1.1.0 (new option: `only-two`) - `perfsprint`: from 0.8.2 to 0.9.1 (checker name in the diagnostic message) - `staticcheck`: new `quickfix` set of rules - `testifylint`: from 1.5.2 to 1.6.0 (new options: `equal-values`, `suite-method-signature`, `require-string-msg`) - `wsl`: from 4.5.0 to 4.6.0 (new option: `allow-cuddle-used-in-block`) 4. Linters bug fixes - `bidichk`: from 0.3.2 to 0.3.3 - `errchkjson`: from 0.4.0 to 0.4.1 - `errname`: from 1.0.0 to 1.1.0 - `funlen`: fix `ignore-comments` option - `gci`: from 0.13.5 to 0.13.6 - `gosmopolitan`: from 1.2.2 to 1.3.0 - `inamedparam`: from 0.1.3 to 0.2.0 - `intrange`: from 0.3.0 to 0.3.1 - `protogetter`: from 0.3.9 to 0.3.12 - `unparam`: from [`8a5130c`](https://redirect.github.com/golangci/golangci-lint/commit/8a5130ca722f) to [`0df0534`](https://redirect.github.com/golangci/golangci-lint/commit/0df0534333a4) 5. Misc. - 🧹 Configuration options renaming (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/)) - 🧹 Remove options (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/)) - 🧹 Remove flags (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/)) - 🧹 Remove alternative names (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/#alternative-linter-names)) - 🧹 Remove or replace deprecated elements (cf. [Migration guide](https://golangci-lint.run/product/migration-guide/)) - Adds an option to display some commands as JSON: - `golangci-lint config path --json` - `golangci-lint help linters --json` - `golangci-lint help formatters --json` - `golangci-lint linters --json` - `golangci-lint formatters --json` - `golangci-lint version --json` 6. Documentation - [Migration guide](https://golangci-lint.run/product/migration-guide/) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/open-telemetry/opentelemetry-go). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yMDcuMSIsInVwZGF0ZWRJblZlciI6IjM5LjIwNy4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJTa2lwIENoYW5nZWxvZyIsImRlcGVuZGVuY2llcyJdfQ==--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Tyler Yahn <codingalias@gmail.com>
2025-03-26 10:46:44 -07:00
// Package matchers provides comparison and matching functionality for tests.
2023-08-10 16:00:53 -07:00
package matchers // import "go.opentelemetry.io/otel/exporters/zipkin/internal/matchers"
import (
"fmt"
"reflect"
"regexp"
"runtime/debug"
"strings"
"testing"
"time"
)
var stackTracePruneRE = regexp.MustCompile(`runtime\/debug|testing|internal\/matchers`)
2023-08-10 16:00:53 -07:00
type Expectation struct {
t *testing.T
actual interface{}
}
func (e *Expectation) ToEqual(expected interface{}) {
e.verifyExpectedNotNil(expected)
if !reflect.DeepEqual(e.actual, expected) {
e.fatalf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) NotToEqual(expected interface{}) {
e.verifyExpectedNotNil(expected)
if reflect.DeepEqual(e.actual, expected) {
e.fatalf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) ToBeNil() {
if e.actual != nil {
e.fatalf("Expected\n\t%v\nto be nil", e.actual)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) NotToBeNil() {
if e.actual == nil {
e.fatalf("Expected\n\t%v\nnot to be nil", e.actual)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) ToBeTrue() {
switch a := e.actual.(type) {
case bool:
if !a {
e.fatalf("Expected\n\t%v\nto be true", e.actual)
2023-08-10 16:00:53 -07:00
}
default:
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) ToBeFalse() {
switch a := e.actual.(type) {
case bool:
if a {
e.fatalf("Expected\n\t%v\nto be false", e.actual)
2023-08-10 16:00:53 -07:00
}
default:
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) NotToPanic() {
switch a := e.actual.(type) {
case func():
func() {
defer func() {
if recovered := recover(); recovered != nil {
e.fatalf("Expected panic\n\t%v\nto have not been raised", recovered)
2023-08-10 16:00:53 -07:00
}
}()
a()
}()
default:
e.fatalf("Cannot check if non-func value\n\t%v\nis truthy", a)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) ToSucceed() {
switch actual := e.actual.(type) {
case error:
if actual != nil {
e.fatalf("Expected error\n\t%v\nto have succeeded", actual)
2023-08-10 16:00:53 -07:00
}
default:
e.fatalf("Cannot check if non-error value\n\t%v\nsucceeded", actual)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) ToMatchError(expected interface{}) {
e.verifyExpectedNotNil(expected)
actual, ok := e.actual.(error)
if !ok {
e.fatalf("Cannot check if non-error value\n\t%v\nmatches error", e.actual)
2023-08-10 16:00:53 -07:00
}
switch expected := expected.(type) {
case error:
if !reflect.DeepEqual(actual, expected) {
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
2023-08-10 16:00:53 -07:00
}
case string:
if actual.Error() != expected {
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
2023-08-10 16:00:53 -07:00
}
default:
e.fatalf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected)
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) ToContain(expected interface{}) {
actualValue := reflect.ValueOf(e.actual)
actualKind := actualValue.Kind()
switch actualKind {
case reflect.Array, reflect.Slice:
default:
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
2023-08-10 16:00:53 -07:00
return
}
expectedValue := reflect.ValueOf(expected)
expectedKind := expectedValue.Kind()
switch expectedKind {
case reflect.Array, reflect.Slice:
default:
expectedValue = reflect.ValueOf([]interface{}{expected})
}
for i := 0; i < expectedValue.Len(); i++ {
var contained bool
expectedElem := expectedValue.Index(i).Interface()
for j := 0; j < actualValue.Len(); j++ {
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
contained = true
break
}
}
if !contained {
e.fatalf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem)
2023-08-10 16:00:53 -07:00
return
}
}
}
func (e *Expectation) NotToContain(expected interface{}) {
actualValue := reflect.ValueOf(e.actual)
actualKind := actualValue.Kind()
switch actualKind {
case reflect.Array, reflect.Slice:
default:
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
2023-08-10 16:00:53 -07:00
return
}
expectedValue := reflect.ValueOf(expected)
expectedKind := expectedValue.Kind()
switch expectedKind {
case reflect.Array, reflect.Slice:
default:
expectedValue = reflect.ValueOf([]interface{}{expected})
}
for i := 0; i < expectedValue.Len(); i++ {
expectedElem := expectedValue.Index(i).Interface()
for j := 0; j < actualValue.Len(); j++ {
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
e.fatalf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem)
2023-08-10 16:00:53 -07:00
return
}
}
}
}
func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
expectedValue := reflect.ValueOf(expected)
expectedKind := expectedValue.Kind()
switch expectedKind {
case reflect.Array, reflect.Slice:
default:
e.fatalf("Expected\n\t%v\nto be an array", expected)
2023-08-10 16:00:53 -07:00
return
}
actualValue := reflect.ValueOf(e.actual)
actualKind := actualValue.Kind()
if actualKind != expectedKind {
e.fatalf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected)
2023-08-10 16:00:53 -07:00
return
}
if actualValue.Len() != expectedValue.Len() {
e.fatalf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected)
2023-08-10 16:00:53 -07:00
return
}
var unmatched []interface{}
for i := 0; i < expectedValue.Len(); i++ {
unmatched = append(unmatched, expectedValue.Index(i).Interface())
}
for i := 0; i < actualValue.Len(); i++ {
var found bool
for j, elem := range unmatched {
if reflect.DeepEqual(actualValue.Index(i).Interface(), elem) {
found = true
unmatched = append(unmatched[:j], unmatched[j+1:]...)
break
}
}
if !found {
e.fatalf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected)
2023-08-10 16:00:53 -07:00
}
}
}
func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interface{}) {
if actual, ok := e.actual.(time.Time); ok {
ct, ok := compareTo.(time.Time)
if !ok {
e.fatalf("Cannot compare to non-temporal value\n\t%v", compareTo)
2023-08-10 16:00:53 -07:00
return
}
switch matcher {
case Before:
if !actual.Before(ct) {
e.fatalf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo)
2023-08-10 16:00:53 -07:00
}
case BeforeOrSameTime:
if actual.After(ct) {
e.fatalf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo)
2023-08-10 16:00:53 -07:00
}
case After:
if !actual.After(ct) {
e.fatalf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo)
2023-08-10 16:00:53 -07:00
}
case AfterOrSameTime:
if actual.Before(ct) {
e.fatalf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo)
2023-08-10 16:00:53 -07:00
}
default:
e.fatalf("Cannot compare times with unexpected temporal matcher")
2023-08-10 16:00:53 -07:00
}
return
}
e.fatalf("Cannot compare non-temporal value\n\t%v", e.actual)
2023-08-10 16:00:53 -07:00
}
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
if expected == nil {
e.fatalf("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
2023-08-10 16:00:53 -07:00
}
}
func (e *Expectation) fatalf(format string, a ...any) {
2023-08-10 16:00:53 -07:00
// Prune the stack trace so that it's easier to see relevant lines
stack := strings.Split(string(debug.Stack()), "\n")
var prunedStack []string
for _, line := range stack {
if !stackTracePruneRE.MatchString(line) {
prunedStack = append(prunedStack, line)
}
}
msg := fmt.Sprintf(format, a...)
2023-08-10 16:00:53 -07:00
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
}