Skip to content

Commit

Permalink
Fix check run in case no version was provided
Browse files Browse the repository at this point in the history
Fail early if no run was provided
Refactor check test code
  • Loading branch information
isibeni committed Aug 25, 2023
1 parent 1efcb98 commit 8f6ac3a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 75 deletions.
9 changes: 7 additions & 2 deletions internal/dtr/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ func Check(in io.Reader) (CheckResult, error) {
}

if len(result) == 0 {
// Only return the incoming version to signal concourse that there is nothing new
result = append(result, config.Version)
if config.Version != nil {
// Only return the incoming version to signal concourse that there is nothing new
result = append(result, config.Version)
} else {
// Return empty result as we don't have anything
result = CheckResult{}
}
}

return result, nil
Expand Down
182 changes: 113 additions & 69 deletions internal/dtr/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,89 +36,133 @@ var _ = Describe("Check", func() {
})

Context("valid configuration", func() {
It("should just fail nicely if the provided command returns with non-zero exit code", func() {
_, err := Check(feed(Config{
Source: Source{
Check: Custom{Run: "false"},
},
}))
var (
result CheckResult
err error
config Config
)

JustBeforeEach(func() {
result, err = Check(feed(config))
})

Expect(err).To(HaveOccurred())
When("command returns with non-zero exit code", func() {
BeforeEach(func() {
config.Source.Check.Run = "false"
})

It("should just fail nicely", func() {
Expect(err).To(HaveOccurred())
})
})

It("should return provided version when no line is return by run", func() {
versionIn := Version{"ref": "barfoo"}
result, err := Check(feed(Config{
Source: Source{
Check: Custom{Run: "true"},
},
Version: versionIn,
}))

Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{versionIn}))
When("no line is returned by run", func() {
BeforeEach(func() {
config = Config{
Source: Source{
Check: Custom{Run: "true"},
},
}
})

When("version is provided by input", func() {
var versionIn Version

BeforeEach(func() {
versionIn = Version{"ref": "barfoo"}
config.Version = versionIn
})

It("should return provided version", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{versionIn}))
})
})

When("no version is provided by input", func() {
BeforeEach(func() {
config.Version = nil
})

It("should return empty result", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{}))
})
})
})

It("should return two versions when two lines are returned", func() {
result, err := Check(feed(Config{
Source: Source{
Check: Custom{Run: "echo foobar && echo barfoo"},
},
}))

Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{
Version{"ref": "foobar"},
Version{"ref": "barfoo"},
}))
When("command returns two lines", func() {
BeforeEach(func() {
config.Source.Check.Run = "echo foobar && echo barfoo"
})

It("should return two versions", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{
Version{"ref": "foobar"},
Version{"ref": "barfoo"},
}))
})
})

It("should run before commands before returning a version", func() {
result, err := Check(feed(Config{
Source: Source{
Check: Custom{
Before: "echo foobar >/tmp/version",
Run: "cat /tmp/version",
},
},
}))
When("run command is not defined", func() {
BeforeEach(func() {
config.Source.Check = Custom{
Run: "",
}
})

Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{
Version{"ref": "foobar"},
}))
It("should just fail nicely", func() {
Expect(err).To(HaveOccurred())
})
})

It("should just fail nicely if the provided before command return with non-zero exit code", func() {
_, err := Check(feed(Config{
Source: Source{
Check: Custom{
Before: "false",
Run: "true",
},
},
}))
When("before command is defined", func() {
BeforeEach(func() {
config.Source.Check = Custom{
Before: "echo foobar >/tmp/version",
Run: "cat /tmp/version",
}
})

It("should run before commands before returning a version", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{
Version{"ref": "foobar"},
}))
})
})

Expect(err).To(HaveOccurred())
When("before command returns with non-zero exit code", func() {
BeforeEach(func() {
config.Source.Check = Custom{
Before: "false",
Run: "true",
}
})

It("should just fail nicely", func() {
Expect(err).To(HaveOccurred())
})
})

It("should set environment variables that can be used in run", func() {
result, err := Check(feed(Config{
Source: Source{
Check: Custom{
Env: map[string]string{
"FOO": "foo",
"BAR": "bar",
},
Run: "echo ${FOO}${BAR}",
When("ENV is configured", func() {
BeforeEach(func() {
config.Source.Check = Custom{
Env: map[string]string{
"FOO": "foo",
"BAR": "bar",
},
},
}))

Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{
Version{"ref": "foobar"},
}))
Run: "echo ${FOO}${BAR}",
}
})

It("should set environment variables that can be used in run", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(CheckResult{
Version{"ref": "foobar"},
}))
})
})
})
})
8 changes: 4 additions & 4 deletions internal/dtr/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ 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 {
return nil, fmt.Errorf("failure while running before command: %w", err)
}
}

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

var outStream bytes.Buffer
run := command(ctx, entry.Env, entry.Run, &outStream)
if err := run.Run(); err != nil {
Expand Down

0 comments on commit 8f6ac3a

Please sign in to comment.