Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BranislavLazic committed Sep 26, 2024
1 parent 38e388d commit afd5ffe
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin
release
.vscode
.idea
.idea
out
4 changes: 4 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packages:
github.com/codeengio/idi/writer:
config:
all: True
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP=idi
MODULE := github.com/codeengio/idi
VERSION := v0.1
VERSION := 1.0.0

.PHONY: clean bin test

Expand Down
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate mockery --all --log-level=debug --disable-version-string --inpackage
37 changes: 0 additions & 37 deletions filewriter/writer.go

This file was deleted.

17 changes: 11 additions & 6 deletions generator/app.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package generator

import (
"embed"
"io/fs"

"github.com/codeengio/idi/filewriter"
"github.com/codeengio/idi/writer"
"github.com/rs/zerolog"
)

type App struct {
Logger zerolog.Logger
logger zerolog.Logger
writer writer.Writer
}

func (f *App) GenerateNew(name, goModule string, templatesMap map[string]string, templateFS embed.FS) error {
func NewApp(logger zerolog.Logger, w writer.Writer) *App {
return &App{logger: logger, writer: w}
}

func (f *App) GenerateNew(name, goModule string, templatesMap map[string]string, templateFS fs.FS) error {
for fileName, templatePath := range templatesMap {
f.Logger.Info().Str("create", fileName).Msg("generating file")
err := filewriter.WriteFile(
f.logger.Info().Str("create", fileName).Msg("generating file")
err := f.writer.WriteTemplate(
name,
templatePath,
fileName,
Expand Down
63 changes: 63 additions & 0 deletions generator/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package generator

import (
"errors"
"github.com/codeengio/idi/mocks/github.com/codeengio/idi/writer"
"github.com/codeengio/idi/tests"
"github.com/rs/zerolog"
"github.com/stretchr/testify/mock"
"testing"
)

func TestApp_GenerateNew(t *testing.T) {
w := &writer.MockWriter{}

w.On("WriteTemplate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil)

appGen := NewApp(zerolog.Logger{}, w)
err := appGen.GenerateNew(
"app",
"example.com/module/app",
map[string]string{"README.md": "templates/readme.md.tmpl"},
tests.GetFS(),
)
if err != nil {
t.Error(err)
}

if len(w.Calls) != 1 {
t.Fatalf("expected 1 call, got=%d", len(w.Calls))
}
}

func TestApp_GenerateNew_EmptyTemplates(t *testing.T) {
w := &writer.MockWriter{}
appGen := NewApp(zerolog.Logger{}, w)
err := appGen.GenerateNew("app", "example.com/module/app", map[string]string{}, tests.GetFS())
if err != nil {
t.Error(err)
}

if len(w.Calls) > 0 {
t.Fatalf("should not have any calls")
}
}

func TestApp_GenerateNew_WriteError(t *testing.T) {
w := &writer.MockWriter{}

w.On("WriteTemplate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(errors.New("write error"))

appGen := NewApp(zerolog.Logger{}, w)
err := appGen.GenerateNew(
"app",
"example.com/module/app",
map[string]string{"README.md": "templates/readme.md.tmpl"},
tests.GetFS(),
)
if err == nil {
t.Fatalf("should have errored")
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand All @@ -10,6 +12,8 @@ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand All @@ -18,10 +22,15 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"embed"
"github.com/codeengio/idi/writer"
"os"
"time"

Expand Down Expand Up @@ -57,7 +58,7 @@ func runNewApp(logger zerolog.Logger) func(*cobra.Command, []string) error {
return err
}

appGen := generator.App{Logger: logger}
appGen := generator.NewApp(logger, writer.NewFS(logger))
err = appGen.GenerateNew(name, module, templates, templateFS)
if err != nil {
logger.Error().Err(err).Msg("failed to generate new app")
Expand Down
86 changes: 86 additions & 0 deletions mocks/github.com/codeengio/idi/writer/mock_Writer.go

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

7 changes: 7 additions & 0 deletions tests/data/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("{{.AppName}}")
}
13 changes: 13 additions & 0 deletions tests/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tests

import (
"embed"
"io/fs"
)

//go:embed data/*
var data embed.FS

var GetFS = func() fs.FS {
return data
}
46 changes: 46 additions & 0 deletions writer/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package writer

import (
"fmt"
"github.com/rs/zerolog"
"html/template"
"io/fs"
"os"
"path/filepath"
)

type FS struct {
logger zerolog.Logger
}

func NewFS(logger zerolog.Logger) *FS {
return &FS{logger: logger}
}

func (fs *FS) WriteTemplate(appDir, templateName, outFileName string, templateFS fs.FS, args map[string]string) error {
tmpl, err := template.ParseFS(templateFS, templateName)
if err != nil {
fs.logger.Error().Err(err).Msg("error parsing template")
return err
}

fp := fmt.Sprintf("%s/%s", appDir, outFileName)
if err := os.MkdirAll(filepath.Dir(fp), 0770); err != nil {
fs.logger.Error().Err(err).Msg("error creating directory")
return err
}

f, err := os.Create(fp)
if err != nil {
fs.logger.Error().Err(err).Msg("error creating file")
return err
}

err = tmpl.Execute(f, args)
if err != nil {
fs.logger.Error().Err(err).Msg("error executing template")
return err
}

return err
}
Loading

0 comments on commit afd5ffe

Please sign in to comment.