Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdimiceli committed Aug 23, 2024
1 parent c9224c6 commit ac76dfd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
10 changes: 6 additions & 4 deletions terraform/binary_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ func NewBinary(terraformBinary string) *Binary {

func (binary *Binary) BinaryPath() (string, error) {
// if user sets a terraform binary use it
exists, err := binary.FS.Exists(binary.TerraformBinary)
if err == nil && exists {
return binary.TerraformBinary, nil
if binary.TerraformBinary != "" {
exists, err := binary.FS.Exists(binary.TerraformBinary)
if err == nil && exists {
return binary.TerraformBinary, nil
}
}

destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName)
exists, err = binary.FS.Exists(destinationPath)
exists, err := binary.FS.Exists(destinationPath)
if err != nil {
return "", err
}
Expand Down
27 changes: 27 additions & 0 deletions terraform/binary_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ var _ = Describe("BinaryPath", func() {
Expect(fileSystem.ChtimesCall.Receives.ModTime).To(Equal(modTime))
})

Context("when a custom terraform binary path is set", func() {
BeforeEach(func() {
binary.TerraformBinary = "/some/custom/path/bbl-terraform"
})

Context("and the file exists", func() {
BeforeEach(func() {
fileSystem.ExistsCall.Returns.Bool = true
})

It("doesn't rewrite the file", func() {
res, err := binary.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal("/some/custom/path/bbl-terraform"))
Expect(fileSystem.WriteFileCall.CallCount).To(Equal(0))
})
})

Context("but the file does not exist", func() {
It("uses the embedded binary ", func() {
res, err := binary.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal("/some/tmp/path/bbl-terraform"))
})
})
})

Context("when there is no my-terraform-binary in box", func() {
BeforeEach(func() {
binary.EmbedData = contentOnlyModTime
Expand Down

0 comments on commit ac76dfd

Please sign in to comment.