Skip to content

Commit

Permalink
Merge pull request #114 from homeport/add/ref-file
Browse files Browse the repository at this point in the history
Write version details into file
  • Loading branch information
HeavyWombat authored Dec 17, 2024
2 parents 1b4af6d + 05f6d45 commit 047306b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/dtr/dtr_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"bytes"
"encoding/json"
"io"
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -42,3 +43,14 @@ func feed(config Config) io.Reader {
Expect(err).ToNot(HaveOccurred())
return bytes.NewReader(data)
}

func withTempDir(f func(dir string)) {
GinkgoHelper()

dir, err := os.MkdirTemp("", "dtr")
Expect(err).ToNot(HaveOccurred())

f(dir)

os.RemoveAll(dir)
}
16 changes: 16 additions & 0 deletions internal/dtr/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package dtr

import (
"io"
"os"
"path/filepath"
)

func In(in io.Reader, args ...string) (InOutResult, error) {
Expand All @@ -30,6 +32,20 @@ func In(in io.Reader, args ...string) (InOutResult, error) {
return InOutResult{}, err
}

if len(args) > 0 {
// First argument should be a directory, but just to be on the safe side
// let's check that it's a directory and only then put the version details
// into a file in the input directory
var dir = args[0]
if fi, err := os.Stat(dir); err == nil && fi.IsDir() {
name := filepath.Join(dir, config.Source.ID)
data := []byte(config.Version[config.Source.ID])
if err := os.WriteFile(name, data, os.FileMode(0644)); err != nil {
return InOutResult{}, err
}
}
}

output, err := execute(config.Source.In, args...)
if err != nil {
return InOutResult{}, err
Expand Down
26 changes: 26 additions & 0 deletions internal/dtr/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package dtr_test

import (
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -77,6 +80,29 @@ var _ = Describe("In", func() {
{Name: "foo", Value: "bar"},
}))
})

It("should store the input version in a file", func() {
withTempDir(func(dir string) {
version := Version{"ref": "foobar"}
result, err := In(
feed(Config{
Source: Source{In: Custom{Run: "true"}},
Version: version,
}),
dir,
)

Expect(err).NotTo(HaveOccurred())
Expect(result.Version).To(Equal(version))

refFile := filepath.Join(dir, "ref")
Expect(refFile).To(BeAnExistingFile())

data, err := os.ReadFile(refFile)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal([]byte("foobar")))
})
})
})

Context("empty/no-op configuration", func() {
Expand Down

0 comments on commit 047306b

Please sign in to comment.