Skip to content

Commit

Permalink
Adds scribe.Emitter object
Browse files Browse the repository at this point in the history
- Adds function that prints out dependency information as well as
deprecation warnings

Signed-off-by: Forest Eckhardt <[email protected]>
  • Loading branch information
Sophie Wigmore authored and ryanmoran committed Jan 6, 2021
1 parent 3147edb commit 3c493ed
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
43 changes: 43 additions & 0 deletions scribe/emitter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scribe

import (
"io"
"time"

"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/postal"
)

type Emitter struct {
// Logger is embedded and therefore delegates all of its functions to the
// Emitter.
Logger
}

func NewEmitter(output io.Writer) Emitter {
return Emitter{
Logger: NewLogger(output),
}
}

func (e Emitter) SelectedDependency(entry packit.BuildpackPlanEntry, dependency postal.Dependency, now time.Time) {
source, ok := entry.Metadata["version-source"].(string)
if !ok {
source = "<unknown>"
}

e.Subprocess("Selected %s version (using %s): %s", dependency.Name, source, dependency.Version)

if (dependency.DeprecationDate != time.Time{}) {
deprecationDate := dependency.DeprecationDate
switch {
case (deprecationDate.Add(-30*24*time.Hour).Before(now) && deprecationDate.After(now)):
e.Action("Version %s of %s will be deprecated after %s.", dependency.Version, dependency.Name, dependency.DeprecationDate.Format("2006-01-02"))
e.Action("Migrate your application to a supported version of %s before this time.", dependency.Name)
case (deprecationDate == now || deprecationDate.Before(now)):
e.Action("Version %s of %s is deprecated.", dependency.Version, dependency.Name)
e.Action("Migrate your application to a supported version of %s.", dependency.Name)
}
}
e.Break()
}
121 changes: 121 additions & 0 deletions scribe/emitter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package scribe_test

import (
"bytes"
"testing"
"time"

"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/postal"
"github.com/paketo-buildpacks/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testEmitter(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

buffer *bytes.Buffer
emitter scribe.Emitter
)

it.Before(func() {
buffer = bytes.NewBuffer(nil)
emitter = scribe.NewEmitter(buffer)
})

context("SelectedDependency", func() {
it("prints details about the selected dependency", func() {
entry := packit.BuildpackPlanEntry{
Metadata: map[string]interface{}{"version-source": "some-source"},
}
dependency := postal.Dependency{
Name: "Some Dependency",
Version: "some-version",
}

emitter.SelectedDependency(entry, dependency, time.Now())
Expect(buffer.String()).To(Equal(" Selected Some Dependency version (using some-source): some-version\n\n"))
})

context("when the version source is missing", func() {
it("prints details about the selected dependency", func() {
dependency := postal.Dependency{
Name: "Some Dependency",
Version: "some-version",
}

emitter.SelectedDependency(packit.BuildpackPlanEntry{}, dependency, time.Now())
Expect(buffer.String()).To(Equal(" Selected Some Dependency version (using <unknown>): some-version\n\n"))
})
})

context("when it is within 30 days of the deprecation date", func() {
it("returns a warning that the dependency will be deprecated after the deprecation date", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2021-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
now := deprecationDate.Add(-29 * 24 * time.Hour)

entry := packit.BuildpackPlanEntry{
Metadata: map[string]interface{}{"version-source": "some-source"},
}
dependency := postal.Dependency{
DeprecationDate: deprecationDate,
Name: "Some Dependency",
Version: "some-version",
}

emitter.SelectedDependency(entry, dependency, now)
Expect(buffer.String()).To(ContainSubstring(" Selected Some Dependency version (using some-source): some-version\n"))
Expect(buffer.String()).To(ContainSubstring(" Version some-version of Some Dependency will be deprecated after 2021-04-01.\n"))
Expect(buffer.String()).To(ContainSubstring(" Migrate your application to a supported version of Some Dependency before this time.\n\n"))
})
})

context("when it is on the the deprecation date", func() {
it("returns a warning that the version of the dependency is no longer supported", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2021-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
now := deprecationDate

entry := packit.BuildpackPlanEntry{
Metadata: map[string]interface{}{"version-source": "some-source"},
}
dependency := postal.Dependency{
DeprecationDate: deprecationDate,
Name: "Some Dependency",
Version: "some-version",
}

emitter.SelectedDependency(entry, dependency, now)
Expect(buffer.String()).To(ContainSubstring(" Selected Some Dependency version (using some-source): some-version\n"))
Expect(buffer.String()).To(ContainSubstring(" Version some-version of Some Dependency is deprecated.\n"))
Expect(buffer.String()).To(ContainSubstring(" Migrate your application to a supported version of Some Dependency.\n\n"))
})
})

context("when it is after the the deprecation date", func() {
it("returns a warning that the version of the dependency is no longer supported", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2021-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
now := deprecationDate.Add(24 * time.Hour)

entry := packit.BuildpackPlanEntry{
Metadata: map[string]interface{}{"version-source": "some-source"},
}
dependency := postal.Dependency{
DeprecationDate: deprecationDate,
Name: "Some Dependency",
Version: "some-version",
}

emitter.SelectedDependency(entry, dependency, now)
Expect(buffer.String()).To(ContainSubstring(" Selected Some Dependency version (using some-source): some-version\n"))
Expect(buffer.String()).To(ContainSubstring(" Version some-version of Some Dependency is deprecated.\n"))
Expect(buffer.String()).To(ContainSubstring(" Migrate your application to a supported version of Some Dependency.\n\n"))
})
})
})
}
1 change: 1 addition & 0 deletions scribe/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestUnitScribe(t *testing.T) {
suite := spec.New("scribe", spec.Report(report.Terminal{}))
suite("Bar", testBar)
suite("Color", testColor)
suite("Emitter", testEmitter)
suite("FormattedList", testFormattedList)
suite("FormattedMap", testFormattedMap)
suite("Logger", testLogger)
Expand Down

0 comments on commit 3c493ed

Please sign in to comment.