Skip to content

Commit

Permalink
Fix validation for empty check runs
Browse files Browse the repository at this point in the history
Fixes bug introduced by #31 that did not allow no/op In configuration
Validates check config before running execute
  • Loading branch information
isibeni committed Aug 27, 2023
1 parent 6c4b9f9 commit 7148535
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
13 changes: 13 additions & 0 deletions internal/dtr/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package dtr

import (
"fmt"
"io"
)

Expand All @@ -30,6 +31,11 @@ func Check(in io.Reader) (CheckResult, error) {
return nil, err
}

err = validateCheckConfig(config.Source.Check)
if err != nil {
return nil, err
}

output, err := execute(config.Source.Check)
if err != nil {
return nil, err
Expand All @@ -52,3 +58,10 @@ func Check(in io.Reader) (CheckResult, error) {

return result, nil
}

func validateCheckConfig(c Custom) error {
if c.Run == "" {
return fmt.Errorf("run command not specified. Bailing out")
}
return nil
}
17 changes: 10 additions & 7 deletions internal/dtr/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ func execute(entry Custom) ([]string, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if entry.Run == "" {
return nil, fmt.Errorf("run command not specified. Bailing out")
}

if entry.Before != "" {
before := command(ctx, entry.Env, entry.Before, os.Stderr)
if err := before.Run(); err != nil {
Expand All @@ -116,9 +112,16 @@ func execute(entry Custom) ([]string, error) {
}

var outStream bytes.Buffer
run := command(ctx, entry.Env, entry.Run, &outStream)
if err := run.Run(); err != nil {
return nil, fmt.Errorf("failure while running run command: %w", err)
// User does not always need to define Run
// e.g. user just implements Check but not In to trigger jobs
// In this scenario a user would still use get on the resource. Then In would be executed.
// For this scenario we would still like to allow no-op In runs
// Therefore we can skip command execution if nothing is defined
if entry.Run != "" {
run := command(ctx, entry.Env, entry.Run, &outStream)
if err := run.Run(); err != nil {
return nil, fmt.Errorf("failure while running run command: %w", err)
}
}

var result []string
Expand Down
19 changes: 19 additions & 0 deletions internal/dtr/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ var _ = Describe("In", func() {
}))
})
})

Context("empty/no-op configuration", func() {
It("should not fail", func() {
_, err := In(feed(Config{}))

Expect(err).NotTo(HaveOccurred())
})

It("should return given version", func() {
version := Version{"ref": "foobar"}
result, _ := In(feed(Config{
Version: version,
}))

Expect(result).To(Equal(InOutResult{
Version: version,
}))
})
})
})

0 comments on commit 7148535

Please sign in to comment.