1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-11-23 21:44:44 +02:00

Use assert for test (#3201)

instead of `if`s
This commit is contained in:
qwerty287
2024-01-14 19:33:58 +01:00
committed by GitHub
parent b9f6f3f9fb
commit 001b5639a6
34 changed files with 417 additions and 1140 deletions

View File

@@ -141,11 +141,8 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
assert.NoError(t, err)
if step.State != model.StatusFailure {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusFailure, step.State)
} else if step.ExitCode != 1 {
t.Errorf("Step exit code not equals 1 != %d", step.ExitCode)
}
assert.Equal(t, model.StatusFailure, step.State)
assert.Equal(t, 1, step.ExitCode)
}
func TestUpdateStepPToStatusStarted(t *testing.T) {
@@ -154,11 +151,8 @@ func TestUpdateStepPToStatusStarted(t *testing.T) {
state := rpc.State{Started: int64(42)}
step, _ := UpdateStepToStatusStarted(&mockUpdateStepStore{}, model.Step{}, state)
if step.State != model.StatusRunning {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusRunning, step.State)
} else if step.Started != int64(42) {
t.Errorf("Step started not equals 42 != %d", step.Started)
}
assert.Equal(t, model.StatusRunning, step.State)
assert.EqualValues(t, 42, step.Started)
}
func TestUpdateStepToStatusSkipped(t *testing.T) {
@@ -166,11 +160,8 @@ func TestUpdateStepToStatusSkipped(t *testing.T) {
step, _ := UpdateStepToStatusSkipped(&mockUpdateStepStore{}, model.Step{}, int64(1))
if step.State != model.StatusSkipped {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusSkipped, step.State)
} else if step.Stopped != int64(0) {
t.Errorf("Step stopped not equals 0 != %d", step.Stopped)
}
assert.Equal(t, model.StatusSkipped, step.State)
assert.EqualValues(t, 0, step.Stopped)
}
func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
@@ -182,11 +173,8 @@ func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
step, _ = UpdateStepToStatusSkipped(&mockUpdateStepStore{}, *step, int64(1))
if step.State != model.StatusSuccess {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusSuccess, step.State)
} else if step.Stopped != int64(1) {
t.Errorf("Step stopped not equals 1 != %d", step.Stopped)
}
assert.Equal(t, model.StatusSuccess, step.State)
assert.EqualValues(t, 1, step.Stopped)
}
func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
@@ -198,16 +186,10 @@ func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
switch {
case step.State != model.StatusSkipped:
t.Errorf("Step status not equals '%s' != '%s'", model.StatusSkipped, step.State)
case step.Stopped != int64(34):
t.Errorf("Step stopped not equals 34 != %d", step.Stopped)
case step.Error != "":
t.Errorf("Step error not equals '' != '%s'", step.Error)
case step.ExitCode != 0:
t.Errorf("Step exit code not equals 0 != %d", step.ExitCode)
}
assert.Equal(t, model.StatusSkipped, step.State)
assert.EqualValues(t, 34, step.Stopped)
assert.Empty(t, step.Error)
assert.Equal(t, 0, step.ExitCode)
}
func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
@@ -220,16 +202,10 @@ func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
switch {
case step.State != model.StatusSuccess:
t.Errorf("Step status not equals '%s' != '%s'", model.StatusSuccess, step.State)
case step.Stopped != int64(34):
t.Errorf("Step stopped not equals 34 != %d", step.Stopped)
case step.Error != "":
t.Errorf("Step error not equals '' != '%s'", step.Error)
case step.ExitCode != 0:
t.Errorf("Step exit code not equals 0 != %d", step.ExitCode)
}
assert.Equal(t, model.StatusSuccess, step.State)
assert.EqualValues(t, 34, step.Stopped)
assert.Empty(t, step.Error)
assert.Equal(t, 0, step.ExitCode)
}
func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
@@ -239,9 +215,7 @@ func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
if step.State != model.StatusFailure {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusFailure, step.State)
}
assert.Equal(t, model.StatusFailure, step.State)
}
func TestUpdateStepStatusToDoneFailureWithExitCode(t *testing.T) {
@@ -251,9 +225,7 @@ func TestUpdateStepStatusToDoneFailureWithExitCode(t *testing.T) {
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
if step.State != model.StatusFailure {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusFailure, step.State)
}
assert.Equal(t, model.StatusFailure, step.State)
}
func TestUpdateStepToStatusKilledStarted(t *testing.T) {
@@ -263,16 +235,10 @@ func TestUpdateStepToStatusKilledStarted(t *testing.T) {
step, _ := UpdateStepToStatusKilled(&mockUpdateStepStore{}, model.Step{})
switch {
case step.State != model.StatusKilled:
t.Errorf("Step status not equals '%s' != '%s'", model.StatusKilled, step.State)
case step.Stopped < now:
t.Errorf("Step stopped not equals %d < %d", now, step.Stopped)
case step.Started != step.Stopped:
t.Errorf("Step started not equals %d != %d", step.Stopped, step.Started)
case step.ExitCode != 137:
t.Errorf("Step exit code not equals 137 != %d", step.ExitCode)
}
assert.Equal(t, model.StatusKilled, step.State)
assert.LessOrEqual(t, now, step.Stopped)
assert.Equal(t, step.Stopped, step.Started)
assert.Equal(t, 137, step.ExitCode)
}
func TestUpdateStepToStatusKilledNotStarted(t *testing.T) {
@@ -280,7 +246,5 @@ func TestUpdateStepToStatusKilledNotStarted(t *testing.T) {
step, _ := UpdateStepToStatusKilled(&mockUpdateStepStore{}, model.Step{Started: int64(1)})
if step.Started != int64(1) {
t.Errorf("Step started not equals 1 != %d", step.Started)
}
assert.EqualValues(t, 1, step.Started)
}