Skip to content

Commit

Permalink
[chore] small test improvements (#11211)
Browse files Browse the repository at this point in the history
Clean up some inconsistencies in the test code across the components.

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten committed Sep 18, 2024
1 parent a3c0565 commit fbffbb0
Show file tree
Hide file tree
Showing 50 changed files with 506 additions and 504 deletions.
28 changes: 14 additions & 14 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ import (

func TestNewContext(t *testing.T) {
testCases := []struct {
desc string
name string
cl Info
}{
{
desc: "valid client",
name: "valid client",
cl: Info{
Addr: &net.IPAddr{
IP: net.IPv4(1, 2, 3, 4),
},
},
},
{
desc: "nil client",
name: "nil client",
cl: Info{},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
ctx := NewContext(context.Background(), tC.cl)
assert.Equal(t, ctx.Value(ctxKey{}), tC.cl)
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
ctx := NewContext(context.Background(), tt.cl)
assert.Equal(t, ctx.Value(ctxKey{}), tt.cl)
})
}
}

func TestFromContext(t *testing.T) {
testCases := []struct {
desc string
name string
input context.Context
expected Info
}{
{
desc: "context with client",
name: "context with client",
input: context.WithValue(context.Background(), ctxKey{}, Info{
Addr: &net.IPAddr{
IP: net.IPv4(1, 2, 3, 4),
Expand All @@ -59,19 +59,19 @@ func TestFromContext(t *testing.T) {
},
},
{
desc: "context without client",
name: "context without client",
input: context.Background(),
expected: Info{},
},
{
desc: "context with something else in the key",
name: "context with something else in the key",
input: context.WithValue(context.Background(), ctxKey{}, "unexpected!"),
expected: Info{},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
assert.Equal(t, tC.expected, FromContext(tC.input))
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, FromContext(tt.input))
})
}
}
Expand Down
40 changes: 20 additions & 20 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func TestGenerateInvalidOutputPath(t *testing.T) {
func TestVersioning(t *testing.T) {
replaces := generateReplaces()
tests := []struct {
description string
name string
cfgBuilder func() Config
expectedErr error
}{
{
description: "defaults",
name: "defaults",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
Expand All @@ -143,7 +143,7 @@ func TestVersioning(t *testing.T) {
expectedErr: nil,
},
{
description: "require otelcol",
name: "require otelcol",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
Expand All @@ -154,7 +154,7 @@ func TestVersioning(t *testing.T) {
expectedErr: nil,
},
{
description: "only gomod file, skip generate",
name: "only gomod file, skip generate",
cfgBuilder: func() Config {
cfg := newTestConfig()
tempDir := t.TempDir()
Expand All @@ -168,7 +168,7 @@ func TestVersioning(t *testing.T) {
expectedErr: ErrDepNotFound,
},
{
description: "old otel version",
name: "old otel version",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Verbose = true
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestVersioning(t *testing.T) {
expectedErr: nil,
},
{
description: "old component version",
name: "old component version",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
Expand All @@ -216,7 +216,7 @@ func TestVersioning(t *testing.T) {
expectedErr: nil,
},
{
description: "old component version without strict mode",
name: "old component version without strict mode",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
Expand All @@ -233,14 +233,14 @@ func TestVersioning(t *testing.T) {
expectedErr: nil,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
cfg := tc.cfgBuilder()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.cfgBuilder()
require.NoError(t, cfg.SetBackwardsCompatibility())
require.NoError(t, cfg.Validate())
require.NoError(t, cfg.ParseModules())
err := GenerateAndCompile(cfg)
require.ErrorIs(t, err, tc.expectedErr)
require.ErrorIs(t, err, tt.expectedErr)
})
}
}
Expand All @@ -263,11 +263,11 @@ func TestSkipGenerate(t *testing.T) {
func TestGenerateAndCompile(t *testing.T) {
replaces := generateReplaces()
testCases := []struct {
testCase string
name string
cfgBuilder func(t *testing.T) Config
}{
{
testCase: "Default Configuration Compilation",
name: "Default Configuration Compilation",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -278,7 +278,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
},
{
testCase: "LDFlags Compilation",
name: "LDFlags Compilation",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -290,7 +290,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
},
{
testCase: "Debug Compilation",
name: "Debug Compilation",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -303,7 +303,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
},
{
testCase: "No providers",
name: "No providers",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -315,7 +315,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
},
{
testCase: "Pre-confmap factories",
name: "Pre-confmap factories",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -328,7 +328,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
},
{
testCase: "With confmap factories",
name: "With confmap factories",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -341,7 +341,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
},
{
testCase: "ConfResolverDefaultURIScheme set",
name: "ConfResolverDefaultURIScheme set",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
Expand All @@ -357,7 +357,7 @@ func TestGenerateAndCompile(t *testing.T) {
}

for _, tt := range testCases {
t.Run(tt.testCase, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.cfgBuilder(t)
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())
Expand Down
12 changes: 6 additions & 6 deletions cmd/mdatagen/internal/samplereceiver/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fbffbb0

Please sign in to comment.