diff --git a/resource/shell/shell.go b/resource/shell/shell.go index e5cb10db2..673e22de7 100644 --- a/resource/shell/shell.go +++ b/resource/shell/shell.go @@ -81,7 +81,12 @@ func (s *Shell) StatusCode() resource.StatusLevel { if s.Status == nil { return resource.StatusFatal } - return resource.StatusLevel(s.Status.ExitStatus) + + if s.Status.ExitStatus == 0 { + return resource.StatusNoChange + } + + return resource.StatusWillChange } // Messages returns a summary of the first execution of check and/or apply. diff --git a/resource/shell/shell_test.go b/resource/shell/shell_test.go index d7cd2d3f8..2fd0d3f37 100644 --- a/resource/shell/shell_test.go +++ b/resource/shell/shell_test.go @@ -142,11 +142,28 @@ func Test_StatusCode_WhenNoStatus_ReturnsFatal(t *testing.T) { // TestStatusCodeWhenMultipleStatusReturnsMostRecentStatus tests what happens // when there are multiple status returns func TestStatusCodeWhenMultipleStatusReturnsMostRecentStatus(t *testing.T) { - var expected resource.StatusLevel = 7 + // first result returns 0 (StatusNoChange) status := &shell.CommandResults{ExitStatus: 0} - status = status.Cons("", &shell.CommandResults{ExitStatus: uint32(expected)}) + // second result returns 7 (StatusWillChange) + status = status.Cons("", &shell.CommandResults{ExitStatus: 7}) sh := &shell.Shell{Status: status} - assert.Equal(t, expected, sh.StatusCode()) + assert.Equal(t, resource.StatusWillChange, sh.StatusCode()) +} + +// TestStatusCodeWhenExitStatusZero verifies that StatusCode returns +// StatusNoChanges when a shell command has a zero exit code +func TestStatusCodeWhenExitStatusZero(t *testing.T) { + status := &shell.CommandResults{ExitStatus: 0} + sh := &shell.Shell{Status: status} + assert.Equal(t, resource.StatusNoChange, sh.StatusCode()) +} + +// TestStatusCodeWhenExitStatusNonZero verifies that StatusCode returns +// StatusWillChange when a shell command has a non-zero exit code +func TestStatusCodeWhenExitStatusNonZero(t *testing.T) { + status := &shell.CommandResults{ExitStatus: 4} + sh := &shell.Shell{Status: status} + assert.Equal(t, resource.StatusWillChange, sh.StatusCode()) } // Shell context