Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristinapathak committed May 27, 2024
1 parent d26542f commit b901f03
Showing 1 changed file with 132 additions and 2 deletions.
134 changes: 132 additions & 2 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ require (
go.opentelemetry.io/collector/processor v0.94.1
go.opentelemetry.io/collector/receiver v0.94.1
go.opentelemetry.io/collector v0.96.0
)`
invalidDependencyGoMod = `// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
module go.opentelemetry.io/collector/cmd/builder/internal/tester
go 1.20
require (
go.opentelemetry.io/collector/bad/otelcol v0.94.1
go.opentelemetry.io/collector v0.96.0
)`
modulePrefix = "go.opentelemetry.io/collector"
)
Expand Down Expand Up @@ -251,8 +259,9 @@ func TestSkipGenerate(t *testing.T) {
func TestGenerateAndCompile(t *testing.T) {
replaces := generateReplaces()
testCases := []struct {
testCase string
cfgBuilder func(t *testing.T) Config
testCase string
cfgBuilder func(t *testing.T) Config
verifyFiles func(t *testing.T, dir string)
}{
{
testCase: "Default Configuration Compilation",
Expand All @@ -262,6 +271,40 @@ func TestGenerateAndCompile(t *testing.T) {
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
}, {
testCase: "Skip New Gomod Configuration Compilation",
cfgBuilder: func(t *testing.T) Config {
cfg := NewDefaultConfig()
tempDir := t.TempDir()
err := makeModule(tempDir, []byte(goModTestFile))
require.NoError(t, err)
cfg.Distribution.OutputPath = filepath.Clean(filepath.Join(tempDir, "output"))
cfg.Replaces = nil
cfg.Excludes = nil
cfg.SkipNewGoModule = true
return cfg
},
verifyFiles: func(t *testing.T, dir string) {
assert.FileExists(t, filepath.Clean(filepath.Join(dir, mainTemplate.Name())))
assert.NoFileExists(t, filepath.Clean(filepath.Join(dir, "go.mod")))
},
},
{
testCase: "Skip Everything",
cfgBuilder: func(t *testing.T) Config {
cfg := NewDefaultConfig()
tempDir := t.TempDir()
err := makeModule(tempDir, []byte(goModTestFile))
require.NoError(t, err)
cfg.Distribution.OutputPath = filepath.Clean(filepath.Join(tempDir, "output"))
cfg.Replaces = nil
cfg.Excludes = nil
cfg.SkipCompilation = true
cfg.SkipGenerate = true
cfg.SkipGetModules = true
cfg.SkipNewGoModule = true
return cfg
},
},
{
testCase: "LDFlags Compilation",
Expand Down Expand Up @@ -325,6 +368,93 @@ func TestGenerateAndCompile(t *testing.T) {
assert.NoError(t, cfg.SetGoPath())
assert.NoError(t, cfg.ParseModules())
require.NoError(t, GenerateAndCompile(cfg))
if tt.verifyFiles != nil {
tt.verifyFiles(t, cfg.Distribution.OutputPath)
}
})
}
}
func TestGetModules(t *testing.T) {
testCases := []struct {
description string
cfgBuilder func(t *testing.T) Config
expectedErr string
}{
{
description: "Skip New Gomod Success",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
tempDir := t.TempDir()
require.NoError(t, makeModule(tempDir, []byte(goModTestFile)))
outputDir := filepath.Clean(filepath.Join(tempDir, "output"))
cfg.Distribution.OutputPath = outputDir
cfg.Replaces = nil
cfg.Excludes = nil
cfg.SkipNewGoModule = true
require.NoError(t, cfg.ParseModules())
return cfg
},
},
// this test requires SkipNewGoModule and StrictVersioning
/* {
description: "Skip New Gomod Success with Strict Versioning",
cfgBuilder: func(t *testing.T) Config {
cfg := NewDefaultConfig()
cfg.downloadModules.wait = 0
cfg.Distribution.Go = "go"
cfg.StrictVersioning = true
tempDir := t.TempDir()
require.NoError(t, makeModule(tempDir, goModTestFile))
outputDir := filepath.Clean(filepath.Join(tempDir, "output"))
cfg.Distribution.OutputPath = outputDir
cfg.Replaces = nil
cfg.Excludes = nil
cfg.SkipNewGoModule = true
return cfg
},
}, */
{
description: "No Go Distribution",
cfgBuilder: func(_ *testing.T) Config {
cfg := NewDefaultConfig()
cfg.downloadModules.wait = 0
return cfg
},
expectedErr: "failed to go get",
},
{
description: "Invalid Dependency",
cfgBuilder: func(t *testing.T) Config {
cfg := NewDefaultConfig()
cfg.downloadModules.wait = 0
cfg.Distribution.Go = "go"
tempDir := t.TempDir()
require.NoError(t, makeModule(tempDir, []byte(invalidDependencyGoMod)))
outputDir := filepath.Clean(filepath.Join(tempDir, "output"))
cfg.Distribution.OutputPath = outputDir
cfg.Replaces = nil
cfg.Excludes = nil
cfg.SkipNewGoModule = true
return cfg
},
expectedErr: "failed to go get",
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
cfg := tc.cfgBuilder(t)
// GenerateAndCompile calls GetModules(). We want to call Generate()
// first so our dependencies stay in the gomod after go mod tidy.
err := GenerateAndCompile(cfg)
if len(tc.expectedErr) == 0 {
if !assert.NoError(t, err) {
mf, mvm, readErr := cfg.readGoModFile()
t.Log("go mod file", mf, mvm, readErr)
}
return
}
assert.ErrorContains(t, err, tc.expectedErr)
})
}
}
Expand Down

0 comments on commit b901f03

Please sign in to comment.