Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite tests to use sub-tests, improve error-handling in tests, and use t.Cleanup() #275

Merged
merged 10 commits into from
May 27, 2023
31 changes: 19 additions & 12 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestStore(t *testing.T) {

for _, v := range valid {
if err := Store(mockProgramFn, &v); err != nil {
t.Fatal(err)
t.Error(err)
}
}

Expand All @@ -123,7 +123,7 @@ func TestStore(t *testing.T) {

for _, v := range invalid {
if err := Store(mockProgramFn, &v); err == nil {
t.Fatalf("Expected error for server %s, got nil", v.ServerURL)
t.Errorf("Expected error for server %s, got nil", v.ServerURL)
}
}
}
Expand Down Expand Up @@ -152,10 +152,10 @@ func TestGet(t *testing.T) {
}

if c.Username != v.Username {
t.Fatalf("expected username `%s`, got %s", v.Username, c.Username)
t.Errorf("expected username `%s`, got %s", v.Username, c.Username)
}
if c.Secret != v.Secret {
t.Fatalf("expected secret `%s`, got %s", v.Secret, c.Secret)
t.Errorf("expected secret `%s`, got %s", v.Secret, c.Secret)
}
}

Expand All @@ -165,10 +165,17 @@ func TestGet(t *testing.T) {
serverURL string
err string
}{
{missingCredsAddress, credentials.NewErrCredentialsNotFound().Error()},
{invalidServerAddress, "error getting credentials - err: exited 1, out: `program failed`"},
{"", fmt.Sprintf("error getting credentials - err: %s, out: `%s`",
missingServerURLErr.Error(), missingServerURLErr.Error())},
{
serverURL: missingCredsAddress,
err: credentials.NewErrCredentialsNotFound().Error(),
},
{
serverURL: invalidServerAddress,
err: "error getting credentials - err: exited 1, out: `program failed`",
},
{
err: fmt.Sprintf("error getting credentials - err: %s, out: `%s`", missingServerURLErr.Error(), missingServerURLErr.Error()),
},
}

for _, v := range invalid {
Expand All @@ -177,7 +184,7 @@ func TestGet(t *testing.T) {
t.Fatalf("Expected error for server %s, got nil", v.serverURL)
}
if err.Error() != v.err {
t.Fatalf("Expected error `%s`, got `%v`", v.err, err)
t.Errorf("Expected error `%s`, got `%v`", v.err, err)
}
}
}
Expand All @@ -192,11 +199,11 @@ func ExampleErase() {

func TestErase(t *testing.T) {
if err := Erase(mockProgramFn, validServerAddress); err != nil {
t.Fatal(err)
t.Error(err)
}

if err := Erase(mockProgramFn, invalidServerAddress); err == nil {
t.Fatalf("Expected error for server %s, got nil", invalidServerAddress)
t.Errorf("Expected error for server %s, got nil", invalidServerAddress)
}
}

Expand All @@ -207,6 +214,6 @@ func TestList(t *testing.T) {
}

if username, exists := auths[validServerAddress]; !exists || username != validUsername {
t.Fatalf("auths[%s] returned %s, %t; expected %s, %t", validServerAddress, username, exists, validUsername, true)
t.Errorf("auths[%s] returned %s, %t; expected %s, %t", validServerAddress, username, exists, validUsername, true)
}
}
28 changes: 14 additions & 14 deletions credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *memoryStore) List() (map[string]string, error) {
}

func TestStore(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
const serverURL = "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Expand All @@ -65,11 +65,11 @@ func TestStore(t *testing.T) {
}

if c.Username != "foo" {
t.Fatalf("expected username foo, got %s\n", c.Username)
t.Errorf("expected username foo, got %s\n", c.Username)
}

if c.Secret != "bar" {
t.Fatalf("expected username bar, got %s\n", c.Secret)
t.Errorf("expected username bar, got %s\n", c.Secret)
}
}

Expand All @@ -89,7 +89,7 @@ func TestStoreMissingServerURL(t *testing.T) {
h := newMemoryStore()

if err := Store(h, in); IsCredentialsMissingServerURL(err) == false {
t.Fatal(err)
t.Error(err)
}
}

Expand All @@ -109,12 +109,12 @@ func TestStoreMissingUsername(t *testing.T) {
h := newMemoryStore()

if err := Store(h, in); IsCredentialsMissingUsername(err) == false {
t.Fatal(err)
t.Error(err)
}
}

func TestGet(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
const serverURL = "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Expand Down Expand Up @@ -147,16 +147,16 @@ func TestGet(t *testing.T) {
}

if c.Username != "foo" {
t.Fatalf("expected username foo, got %s\n", c.Username)
t.Errorf("expected username foo, got %s\n", c.Username)
}

if c.Secret != "bar" {
t.Fatalf("expected username bar, got %s\n", c.Secret)
t.Errorf("expected username bar, got %s\n", c.Secret)
}
}

func TestGetMissingServerURL(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
const serverURL = "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Expand All @@ -177,12 +177,12 @@ func TestGetMissingServerURL(t *testing.T) {
w := new(bytes.Buffer)

if err := Get(h, buf, w); IsCredentialsMissingServerURL(err) == false {
t.Fatal(err)
t.Error(err)
}
}

func TestErase(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
const serverURL = "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Expand All @@ -206,12 +206,12 @@ func TestErase(t *testing.T) {

w := new(bytes.Buffer)
if err := Get(h, buf, w); err == nil {
t.Fatal("expected error getting missing creds, got empty")
t.Error("expected error getting missing creds, got empty")
}
}

func TestEraseMissingServerURL(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
const serverURL = "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Expand Down Expand Up @@ -244,6 +244,6 @@ func TestList(t *testing.T) {
}
// testing that there is an output
if out.Len() == 0 {
t.Fatalf("expected output in the writer, got %d", 0)
t.Error("expected output in the writer, got 0")
}
}
Loading