refactor(internal): convert tests to testify assert/require

Convert internal/urlutil, internal/cli/output, internal/monocular,
and internal/sympath test files from native Go testing patterns
(t.Errorf, t.Fatalf, t.Error, t.Fatal) to github.com/stretchr/testify
equivalents (assert.X, require.X).

Signed-off-by: George Jenkins <gvjenkins@gmail.com>
pull/32260/head
George Jenkins 3 months ago
parent 0bca9871ad
commit addbab2e64
No known key found for this signature in database
GPG Key ID: D79D67C9EC016739

@ -20,6 +20,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"helm.sh/helm/v4/pkg/release/common" "helm.sh/helm/v4/pkg/release/common"
) )
@ -93,14 +95,12 @@ func TestColorizeStatus(t *testing.T) {
// In test environment, term.IsTerminal will be false, so we won't get color // In test environment, term.IsTerminal will be false, so we won't get color
// unless we're testing the logic without terminal detection // unless we're testing the logic without terminal detection
if hasColor && !tt.wantColor { if hasColor {
t.Errorf("ColorizeStatus() returned color when none expected: %q", result) assert.True(t, tt.wantColor, "ColorizeStatus() returned color when none expected: %q", result)
} }
// Always check the status text is present // Always check the status text is present
if !strings.Contains(result, tt.status.String()) { assert.Contains(t, result, tt.status.String())
t.Errorf("ColorizeStatus() = %q, want to contain %q", result, tt.status.String())
}
}) })
} }
} }
@ -139,9 +139,7 @@ func TestColorizeHeader(t *testing.T) {
result := ColorizeHeader(tt.header, tt.noColor) result := ColorizeHeader(tt.header, tt.noColor)
// Always check the header text is present // Always check the header text is present
if !strings.Contains(result, tt.header) { assert.Contains(t, result, tt.header)
t.Errorf("ColorizeHeader() = %q, want to contain %q", result, tt.header)
}
}) })
} }
} }
@ -180,9 +178,7 @@ func TestColorizeNamespace(t *testing.T) {
result := ColorizeNamespace(tt.namespace, tt.noColor) result := ColorizeNamespace(tt.namespace, tt.noColor)
// Always check the namespace text is present // Always check the namespace text is present
if !strings.Contains(result, tt.namespace) { assert.Contains(t, result, tt.namespace)
t.Errorf("ColorizeNamespace() = %q, want to contain %q", result, tt.namespace)
}
}) })
} }
} }

@ -18,14 +18,13 @@ package monocular
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
c, err := New("https://hub.helm.sh") c, err := New("https://hub.helm.sh")
if err != nil { require.NoError(t, err)
t.Errorf("error creating client: %s", err) assert.Equal(t, "https://hub.helm.sh", c.BaseURL)
}
if c.BaseURL != "https://hub.helm.sh" {
t.Errorf("incorrect BaseURL. Expected \"https://hub.helm.sh\" but got %q", c.BaseURL)
}
} }

@ -21,6 +21,9 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
// A search response for phpmyadmin containing 2 results // A search response for phpmyadmin containing 2 results
@ -33,16 +36,10 @@ func TestSearch(t *testing.T) {
defer ts.Close() defer ts.Close()
c, err := New(ts.URL) c, err := New(ts.URL)
if err != nil { require.NoError(t, err, "unable to create monocular client")
t.Errorf("unable to create monocular client: %s", err)
}
results, err := c.SearchWithContext(t.Context(), "phpmyadmin") results, err := c.SearchWithContext(t.Context(), "phpmyadmin")
if err != nil { require.NoError(t, err, "unable to search monocular")
t.Errorf("unable to search monocular: %s", err)
}
if len(results) != 2 { assert.Len(t, results, 2)
t.Error("Did not receive the expected number of results")
}
} }

@ -24,6 +24,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type Node struct { type Node struct {
@ -80,21 +83,14 @@ func makeTree(t *testing.T) {
walkTree(tree, tree.name, func(path string, n *Node) { walkTree(tree, tree.name, func(path string, n *Node) {
if n.entries == nil { if n.entries == nil {
if n.symLinkedTo != "" { if n.symLinkedTo != "" {
if err := os.Symlink(n.symLinkedTo, path); err != nil { require.NoError(t, os.Symlink(n.symLinkedTo, path), "makeTree")
t.Fatalf("makeTree: %v", err)
}
} else { } else {
fd, err := os.Create(path) fd, err := os.Create(path)
if err != nil { require.NoError(t, err, "makeTree")
t.Fatalf("makeTree: %v", err)
return
}
fd.Close() fd.Close()
} }
} else { } else {
if err := os.Mkdir(path, 0770); err != nil { require.NoError(t, os.Mkdir(path, 0770), "makeTree")
t.Fatalf("makeTree: %v", err)
}
} }
}) })
} }
@ -102,8 +98,8 @@ func makeTree(t *testing.T) {
func checkMarks(t *testing.T, report bool) { func checkMarks(t *testing.T, report bool) {
t.Helper() t.Helper()
walkTree(tree, tree.name, func(path string, n *Node) { walkTree(tree, tree.name, func(path string, n *Node) {
if n.marks != n.expectedMarks && report { if report {
t.Errorf("node %s mark = %d; expected %d", path, n.marks, n.expectedMarks) assert.Equal(t, n.expectedMarks, n.marks, "node %s", path)
} }
n.marks = 0 n.marks = 0
}) })
@ -137,16 +133,10 @@ func TestWalk(t *testing.T) {
} }
// Expect no errors. // Expect no errors.
err := Walk(tree.name, markFn) err := Walk(tree.name, markFn)
if err != nil { require.NoError(t, err)
t.Fatalf("no error expected, found: %s", err) require.Empty(t, errors, "unexpected errors")
}
if len(errors) != 0 {
t.Fatalf("unexpected errors: %s", errors)
}
checkMarks(t, true) checkMarks(t, true)
// cleanup // cleanup
if err := os.RemoveAll(tree.name); err != nil { assert.NoError(t, os.RemoveAll(tree.name), "removeTree")
t.Errorf("removeTree: %v", err)
}
} }

@ -16,7 +16,11 @@ limitations under the License.
package urlutil package urlutil
import "testing" import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestURLJoin(t *testing.T) { func TestURLJoin(t *testing.T) {
tests := []struct { tests := []struct {
@ -31,11 +35,9 @@ func TestURLJoin(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
if got, err := URLJoin(tt.url, tt.paths...); err != nil { got, err := URLJoin(tt.url, tt.paths...)
t.Errorf("%s: error %q", tt.name, err) assert.NoError(t, err, tt.name)
} else if got != tt.expect { assert.Equal(t, tt.expect, got, tt.name)
t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got)
}
} }
} }
@ -60,9 +62,7 @@ func TestEqual(t *testing.T) {
{"%/1234", "%/123", false}, {"%/1234", "%/123", false},
{"/1234", "%/1234", false}, {"/1234", "%/1234", false},
} { } {
if tt.match != Equal(tt.a, tt.b) { assert.Equal(t, tt.match, Equal(tt.a, tt.b), "Expected %q==%q to be %t", tt.a, tt.b, tt.match)
t.Errorf("Expected %q==%q to be %t", tt.a, tt.b, tt.match)
}
} }
} }
@ -74,8 +74,7 @@ func TestExtractHostname(t *testing.T) {
"https://example.com:31337/not/with/a/bang/but/a/whimper": "example.com", "https://example.com:31337/not/with/a/bang/but/a/whimper": "example.com",
} }
for start, expect := range tests { for start, expect := range tests {
if got, _ := ExtractHostname(start); got != expect { got, _ := ExtractHostname(start)
t.Errorf("Got %q, expected %q", got, expect) assert.Equal(t, expect, got)
}
} }
} }

Loading…
Cancel
Save