Skip to content

Commit

Permalink
refactor: zerolog.New(io.Discard) -> zerolog.Nop()
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Feb 10, 2022
1 parent 594cb15 commit f22debf
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 36 deletions.
3 changes: 1 addition & 2 deletions internal/cli/cmd/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -118,7 +117,7 @@ func (o Options) validateMain(mainPkgDir string, errs *[]error) error {
return nil
}

pkg, err := gomod.LoadPackage(zerolog.New(io.Discard), o.ModuleDir, o.Main)
pkg, err := gomod.LoadPackage(zerolog.Nop(), o.ModuleDir, o.Main)
if err != nil {
return fmt.Errorf("failed to load package: %w", err)
}
Expand Down
13 changes: 6 additions & 7 deletions internal/gocmd/gocmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package gocmd
import (
"bytes"
"encoding/json"
"io"
"runtime"
"strings"
"testing"
Expand All @@ -31,7 +30,7 @@ import (
)

func TestGetVersion(t *testing.T) {
version, err := GetVersion(zerolog.New(io.Discard))
version, err := GetVersion(zerolog.Nop())
require.NoError(t, err)
require.Equal(t, runtime.Version(), version)
}
Expand All @@ -57,7 +56,7 @@ func TestParseVersion(t *testing.T) {
}

func TestGetEnv(t *testing.T) {
env, err := GetEnv(zerolog.New(io.Discard))
env, err := GetEnv(zerolog.Nop())
require.NoError(t, err)

require.Contains(t, env, "CGO_ENABLED")
Expand All @@ -69,7 +68,7 @@ func TestGetEnv(t *testing.T) {

func TestListModule(t *testing.T) {
buf := new(bytes.Buffer)
err := ListModule(zerolog.New(io.Discard), "../../", buf)
err := ListModule(zerolog.Nop(), "../../", buf)
require.NoError(t, err)

mod := make(map[string]interface{})
Expand All @@ -81,7 +80,7 @@ func TestListModule(t *testing.T) {

func TestListModules(t *testing.T) {
buf := new(bytes.Buffer)
err := ListModules(zerolog.New(io.Discard), "../../", buf)
err := ListModules(zerolog.Nop(), "../../", buf)
require.NoError(t, err)

mod := make(map[string]interface{})
Expand All @@ -94,15 +93,15 @@ func TestListModules(t *testing.T) {

func TestGetModuleGraph(t *testing.T) {
buf := new(bytes.Buffer)
err := GetModuleGraph(zerolog.New(io.Discard), "../../", buf)
err := GetModuleGraph(zerolog.Nop(), "../../", buf)
require.NoError(t, err)

assert.Equal(t, 0, strings.Index(buf.String(), "github.com/CycloneDX/cyclonedx-gomod"))
}

func TestModWhy(t *testing.T) {
buf := new(bytes.Buffer)
err := ModWhy(zerolog.New(io.Discard), "../../", []string{"github.com/CycloneDX/cyclonedx-go"}, buf)
err := ModWhy(zerolog.Nop(), "../../", []string{"github.com/CycloneDX/cyclonedx-go"}, buf)
require.NoError(t, err)

require.Equal(t, `# github.com/CycloneDX/cyclonedx-go
Expand Down
5 changes: 2 additions & 3 deletions internal/gomod/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package gomod

import (
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,7 +45,7 @@ func TestDownload(t *testing.T) {

os.Setenv("GOMODCACHE", tmpDir)

downloads, err := Download(zerolog.New(io.Discard), []Module{
downloads, err := Download(zerolog.Nop(), []Module{
{
Path: "github.com/CycloneDX/cyclonedx-go",
Version: "v0.4.0",
Expand All @@ -60,7 +59,7 @@ func TestDownload(t *testing.T) {
})

t.Run("Error", func(t *testing.T) {
downloads, err := Download(zerolog.New(io.Discard), []Module{
downloads, err := Download(zerolog.Nop(), []Module{
{
Path: "doesnotexist",
Version: "v0.0.0",
Expand Down
3 changes: 1 addition & 2 deletions internal/gomod/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
package gomod

import (
"io"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestLoadStdlibModule(t *testing.T) {
module, err := LoadStdlibModule(zerolog.New(io.Discard))
module, err := LoadStdlibModule(zerolog.Nop())
require.NoError(t, err)
require.Equal(t, "std", module.Path)
require.Regexp(t, `^go\d\.`, module.Version)
Expand Down
3 changes: 1 addition & 2 deletions internal/gomod/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package gomod

import (
"io"
"testing"

"github.com/go-git/go-git/v5"
Expand All @@ -36,7 +35,7 @@ func TestGetLatestTag(t *testing.T) {
headCommit, err := repo.CommitObject(plumbing.NewHash("a20be9f00d406e7b792973ee1826e637e58a23d7"))
require.NoError(t, err)

tag, err := GetLatestTag(zerolog.New(io.Discard), repo, headCommit)
tag, err := GetLatestTag(zerolog.Nop(), repo, headCommit)
require.NoError(t, err)
require.NotNil(t, tag)

Expand Down
19 changes: 9 additions & 10 deletions internal/sbom/convert/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package module
import (
"bytes"
"errors"
"io"
"os/exec"
"path/filepath"
"testing"
Expand Down Expand Up @@ -126,7 +125,7 @@ func TestWithModuleHashes(t *testing.T) {
component := new(cdx.Component)

// Calculate hashes
err = WithModuleHashes()(zerolog.New(io.Discard), module, component)
err = WithModuleHashes()(zerolog.Nop(), module, component)
require.NoError(t, err)
require.NotNil(t, component.Hashes)

Expand All @@ -141,7 +140,7 @@ func TestWithComponentType(t *testing.T) {
module := gomod.Module{}
component := cdx.Component{}

err := WithComponentType(cdx.ComponentTypeContainer)(zerolog.New(io.Discard), module, &component)
err := WithComponentType(cdx.ComponentTypeContainer)(zerolog.Nop(), module, &component)
require.NoError(t, err)
require.Equal(t, cdx.ComponentTypeContainer, component.Type)
}
Expand All @@ -150,7 +149,7 @@ func TestWithScope(t *testing.T) {
module := gomod.Module{}
component := cdx.Component{}

err := WithScope(cdx.ScopeExcluded)(zerolog.New(io.Discard), module, &component)
err := WithScope(cdx.ScopeExcluded)(zerolog.Nop(), module, &component)
require.NoError(t, err)
require.Equal(t, cdx.ScopeExcluded, component.Scope)
}
Expand All @@ -162,7 +161,7 @@ func TestWithTestScope(t *testing.T) {
}
component := cdx.Component{}

err := WithTestScope(cdx.ScopeExcluded)(zerolog.New(io.Discard), module, &component)
err := WithTestScope(cdx.ScopeExcluded)(zerolog.Nop(), module, &component)
require.NoError(t, err)
require.Equal(t, cdx.ScopeExcluded, component.Scope)
})
Expand All @@ -173,7 +172,7 @@ func TestWithTestScope(t *testing.T) {
}
component := cdx.Component{}

err := WithTestScope(cdx.ScopeExcluded)(zerolog.New(io.Discard), module, &component)
err := WithTestScope(cdx.ScopeExcluded)(zerolog.Nop(), module, &component)
require.NoError(t, err)
require.Equal(t, cdx.Scope(""), component.Scope)
})
Expand All @@ -186,7 +185,7 @@ func TestToComponent(t *testing.T) {
Version: "version",
}

component, err := ToComponent(zerolog.New(io.Discard), module)
component, err := ToComponent(zerolog.Nop(), module)
require.NoError(t, err)
require.NotNil(t, component)

Expand All @@ -205,7 +204,7 @@ func TestToComponent(t *testing.T) {
TestOnly: true,
}

component, err := ToComponent(zerolog.New(io.Discard), module)
component, err := ToComponent(zerolog.Nop(), module)
require.NoError(t, err)
require.NotNil(t, component)

Expand All @@ -227,7 +226,7 @@ func TestToComponent(t *testing.T) {
},
}

component, err := ToComponent(zerolog.New(io.Discard), module)
component, err := ToComponent(zerolog.Nop(), module)
require.NoError(t, err)
require.NotNil(t, component)

Expand All @@ -246,7 +245,7 @@ func TestToComponent(t *testing.T) {
Sum: "h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=",
}

component, err := ToComponent(zerolog.New(io.Discard), module)
component, err := ToComponent(zerolog.Nop(), module)
require.NoError(t, err)
require.NotNil(t, component)

Expand Down
3 changes: 1 addition & 2 deletions internal/sbom/convert/pkg/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package pkg

import (
"io"
"testing"

"github.com/rs/zerolog"
Expand All @@ -37,7 +36,7 @@ func TestToComponent(t *testing.T) {
ImportPath: "packagePath",
}

c, err := ToComponent(zerolog.New(io.Discard), p, m)
c, err := ToComponent(zerolog.Nop(), p, m)
require.NoError(t, err)
require.Equal(t, "packagePath", c.Name)
require.Equal(t, "moduleVersion", c.Version)
Expand Down
7 changes: 3 additions & 4 deletions internal/sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package sbom

import (
"io"
"testing"

cdx "github.com/CycloneDX/cyclonedx-go"
Expand All @@ -38,7 +37,7 @@ func TestCalculateFileHashes(t *testing.T) {
cdx.HashAlgoSHA3_512,
}

hashes, err := CalculateFileHashes(zerolog.New(io.Discard), "../../NOTICE", algos...) // TODO: use another file (create a tempfile?)
hashes, err := CalculateFileHashes(zerolog.Nop(), "../../NOTICE", algos...) // TODO: use another file (create a tempfile?)
require.NoError(t, err)
require.Len(t, hashes, 7)
require.Equal(t, "90b8bc82c30341e88830b0ea82f18548", hashes[0].Value)
Expand All @@ -60,15 +59,15 @@ func TestCalculateFileHashes(t *testing.T) {

for _, algo := range algos {
t.Run(string(algo), func(t *testing.T) {
_, err := CalculateFileHashes(zerolog.New(io.Discard), "", algo)
_, err := CalculateFileHashes(zerolog.Nop(), "", algo)
require.Error(t, err)
require.Contains(t, err.Error(), "unsupported hash algorithm")
})
}
})

t.Run("NoAlgorithms", func(t *testing.T) {
hashes, err := CalculateFileHashes(zerolog.New(io.Discard), "")
hashes, err := CalculateFileHashes(zerolog.Nop(), "")
require.NoError(t, err)
require.Empty(t, hashes)
})
Expand Down
3 changes: 1 addition & 2 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package testutil
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -39,7 +38,7 @@ import (
const Redacted = "REDACTED"

// SilentLogger discards all inputs.
var SilentLogger = zerolog.New(io.Discard)
var SilentLogger = zerolog.Nop()

// ExtractFixtureArchive extracts a test fixture's TAR archive to a temporary directory and returns its path.
func ExtractFixtureArchive(t *testing.T, archivePath string) string {
Expand Down
3 changes: 1 addition & 2 deletions pkg/generate/app/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package app

import (
"errors"
"io"
"os"
"runtime"
"testing"
Expand Down Expand Up @@ -246,7 +245,7 @@ func TestGenerator_Generate(t *testing.T) {

func TestGenerator_CreateBuildProperties(t *testing.T) {
g := generator{
logger: zerolog.New(io.Discard),
logger: zerolog.Nop(),
}

origGoflags := os.Getenv("GOFLAGS")
Expand Down

0 comments on commit f22debf

Please sign in to comment.