Skip to content

Commit

Permalink
Optional display of current day and related test imporvements
Browse files Browse the repository at this point in the history
  • Loading branch information
peteraba committed Jul 26, 2020
1 parent 20fe4c5 commit 091ad50
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 17 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ generate:
find pkg -name "mocks" -type d -exec rm -rf {} +

goldenfiles:
go test -mod=readonly -tags=e2e,integration ./cmd/roadmapper -update
go test -mod=readonly ./pkg/roadmap -update

test:
Expand Down
6 changes: 4 additions & 2 deletions cmd/roadmapper/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Render renders a roadmap
func Render(io roadmap.IO, l *zap.Logger, content, output string, fileFormat, dateFormat, baseUrl string, fw, lh uint64) error {
func Render(io roadmap.IO, l *zap.Logger, content, output string, fileFormat, dateFormat, baseUrl string, fw, lh uint64, mt bool) error {
format, err := roadmap.NewFormatType(fileFormat)
if err != nil {
l.Info("format is not supported", zap.Error(err))
Expand All @@ -20,7 +20,9 @@ func Render(io roadmap.IO, l *zap.Logger, content, output string, fileFormat, da
fw, lh = roadmap.GetCanvasSizes(fw, lh)

r := roadmap.Content(content).ToRoadmap(0, nil, "", dateFormat, baseUrl, time.Now())
cvs := r.ToVisual().Draw(float64(fw), float64(lh))

cvs := r.ToVisual().Draw(float64(fw), float64(lh), mt)

img := roadmap.RenderImg(cvs, format)

err = io.Write(output, string(img))
Expand Down
24 changes: 17 additions & 7 deletions cmd/roadmapper/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"testing"
Expand All @@ -13,6 +12,7 @@ import (
"go.uber.org/zap"

"github.com/peteraba/roadmapper/pkg/roadmap"
"github.com/peteraba/roadmapper/pkg/testutils"
)

func TestE2E_CLI(t *testing.T) {
Expand All @@ -28,6 +28,7 @@ func TestE2E_CLI(t *testing.T) {
format string
dateFormat, baseUrl string
fw, lh uint64
mt bool
}

tests := []struct {
Expand All @@ -45,6 +46,7 @@ func TestE2E_CLI(t *testing.T) {
e2eBaseURL,
fw,
lh,
false,
},
},
{
Expand All @@ -58,13 +60,16 @@ func TestE2E_CLI(t *testing.T) {
e2eBaseURL,
fw,
lh,
false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := zap.NewNop()

expectedData := testutils.LoadFile(t, "golden_files", tt.args.output)

err := Render(
rw,
logger,
Expand All @@ -75,20 +80,25 @@ func TestE2E_CLI(t *testing.T) {
tt.args.baseUrl,
tt.args.fw,
tt.args.lh,
tt.args.mt,
)

require.NoError(t, err)

expectedData, err := ioutil.ReadFile(fmt.Sprintf("../../res/golden_files/%s", tt.args.output))
require.NoError(t, err)
actualData, err := ioutil.ReadFile(tt.args.output)
require.NoError(t, err)

ed0, ad0 := float64(len(expectedData)), float64(len(actualData))
ed1, ad1 := ed0*1.2, ad0*1.2
if testutils.ShouldUpdateGoldenFiles() {
testutils.SaveFile(t, actualData, "golden_files", tt.args.output)
}

assert.Equal(t, expectedData, actualData)

assert.Greater(t, ed1, ad0, "generated and golden files differ a lot")
assert.Less(t, ed0, ad1, "generated and golden files differ a lot")
// ed0, ad0 := float64(len(expectedData)), float64(len(actualData))
// ed1, ad1 := ed0*1.2, ad0*1.2
//
// assert.Greater(t, ed1, ad0, "generated and golden files differ a lot")
// assert.Less(t, ed0, ad1, "generated and golden files differ a lot")

if !t.Failed() {
err = os.Remove(tt.args.output) // remove a single file
Expand Down
2 changes: 2 additions & 0 deletions cmd/roadmapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func createCLICommand(logger *zap.Logger) *cli.Command {
&cli.Uint64Flag{Name: "lineHeight", Usage: "width of output file", Aliases: []string{"lh"}},
&cli.StringFlag{Name: "dateFormat", Usage: "date format to use", Value: "2006-01-02", EnvVars: []string{"DATE_FORMAT"}},
&cli.StringFlag{Name: "baseURL", Usage: "base url to use for non-color, non-date extra values", Value: "", EnvVars: []string{"BASE_URL"}},
&cli.StringFlag{Name: "markToday", Usage: "weather or not to add a line to mark the current day", Value: "", EnvVars: []string{"MARK_TODAY"}},
},
Action: func(c *cli.Context) error {
io := roadmap.NewIO()
Expand All @@ -117,6 +118,7 @@ func createCLICommand(logger *zap.Logger) *cli.Command {
c.String("baseURL"),
c.Uint64("width"),
c.Uint64("lineHeight"),
c.Bool("markToday"),
)
if err != nil {
logger.Error("failed to render roadmap", zap.Error(err))
Expand Down
6 changes: 4 additions & 2 deletions pkg/roadmap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,18 @@ func (h *Handler) GetRoadmapImage(ctx echo.Context) error {

lh, _ := strconv.ParseUint(ctx.QueryParam("lineHeight"), 10, 64)

mt, _ := strconv.ParseBool(ctx.QueryParam("markToday"))

fw, lh = GetCanvasSizes(fw, lh)

roadmap, err := load(h.repo, h.cb, ctx.Param("identifier"))
r, err := load(h.repo, h.cb, ctx.Param("identifier"))
if err != nil {
h.Logger.Info("roadmap not found", zap.Error(err))

return ctx.String(herr.ToHttpCode(err, http.StatusNotFound), "roadmap not found")
}

cvs := roadmap.ToVisual().Draw(float64(fw), float64(lh))
cvs := r.ToVisual().Draw(float64(fw), float64(lh), mt)

img := RenderImg(cvs, format)

Expand Down
11 changes: 7 additions & 4 deletions pkg/roadmap/image_rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var myDarkGray = color.RGBA{R: 95, G: 95, B: 95, A: 255}
var defaultMilestoneColor = &canvas.Darkgray

// Draw will draw a roadmap on a canvas.Canvas
func (vr *VisualRoadmap) Draw(fullW, lineH float64) *canvas.Canvas {
func (vr *VisualRoadmap) Draw(fullW, lineH float64, withToday bool) *canvas.Canvas {
headerH := 0.0
if vr.Dates != nil {
headerH = lineH * 3
Expand Down Expand Up @@ -54,7 +54,7 @@ func (vr *VisualRoadmap) Draw(fullW, lineH float64) *canvas.Canvas {

vr.drawLines(ctx, fullW, fullH, headerH, lineH)

vr.drawToday(ctx, fullW, fullH, lineH)
vr.drawToday(ctx, fullW, fullH, lineH, withToday)

vr.writeTitle(ctx, fullW, fullH, lineH)

Expand Down Expand Up @@ -237,12 +237,15 @@ func (vr *VisualRoadmap) drawMilestones(ctx *canvas.Context, fullW, fullH, heade
ctx.SetDashes(0.0)
}

func (vr *VisualRoadmap) drawToday(ctx *canvas.Context, fullW, fullH, lineH float64) {
if vr.Dates == nil {
func (vr *VisualRoadmap) drawToday(ctx *canvas.Context, fullW, fullH, lineH float64, withToday bool) {
if vr.Dates == nil || !withToday {
return
}

now := time.Now()
if vr.Dates.StartAt.After(now) || vr.Dates.EndAt.Before(now) {
return
}

if now.Before(vr.Dates.StartAt) || now.After(vr.Dates.EndAt) {
return
Expand Down
52 changes: 52 additions & 0 deletions pkg/roadmap/mocks/db_read_writer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified res/golden_files/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions res/golden_files/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 091ad50

Please sign in to comment.