Skip to content

Commit

Permalink
Merge pull request #2 from marcomilon/refactoring-try-again
Browse files Browse the repository at this point in the history
Adding more tests
  • Loading branch information
marcomilon authored Oct 20, 2023
2 parents e77db2f + facf536 commit b6dcd1d
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 79 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/marcomilon/gstatic

go 1.14

require gopkg.in/yaml.v2 v2.2.8
require (
golang.org/x/sync v0.4.0
gopkg.in/yaml.v2 v2.4.0
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
42 changes: 0 additions & 42 deletions internal/gstatic/executor.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/gstatic/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"gopkg.in/yaml.v2"
)

// getVarsForTpl is a implementation for VarReader. This implementatin will get variables from a Yaml file
func getVarsForTpl(r io.Reader) (map[interface{}]interface{}, error) {

data, err := io.ReadAll(r)
Expand Down
82 changes: 52 additions & 30 deletions internal/gstatic/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strings"
"sync"

"golang.org/x/sync/errgroup"
)

type generator struct {
Expand All @@ -19,26 +21,24 @@ type task struct {
targetPath string
}

type copyFileWorker task
type layoutRenderWorker task
type simpleRenderWorker task

func (copyFileWorker copyFileWorker) execute() error {
targetAssetname := getTargetDirname(copyFileWorker.generator.sourcePath, copyFileWorker.targetPath)
targetFile := filepath.Join(copyFileWorker.generator.targetPath, targetAssetname)
func copyFileWorker(t task) error {
targetAssetname := getTargetDirname(t.generator.sourcePath, t.targetPath)
targetFile := filepath.Join(t.generator.targetPath, targetAssetname)

return copyFile(copyFileWorker.targetPath, targetFile)
return copyFile(t.targetPath, targetFile)
}

func (layoutRenderWorker layoutRenderWorker) execute() error {
targetAssetname := getTargetDirname(layoutRenderWorker.generator.sourcePath, layoutRenderWorker.targetPath)
targetFile := filepath.Join(layoutRenderWorker.generator.targetPath, targetAssetname)
tpl := gstaticLayoutTpl{layoutRenderWorker.generator.layoutPath, layoutRenderWorker.targetPath, targetFile}
func layoutRenderWorker(t task) error {
targetAssetname := getTargetDirname(t.generator.sourcePath, t.targetPath)
targetFile := filepath.Join(t.generator.targetPath, targetAssetname)
tpl := gstaticLayoutTpl{t.generator.layoutPath, t.targetPath, targetFile}
return tpl.render()
}

func (simpleRenderWorker simpleRenderWorker) execute() error {
tpl := gstaticSimpleTpl{simpleRenderWorker.generator.sourcePath, simpleRenderWorker.generator.targetPath}
func simpleRenderWorker(t task) error {
targetAssetname := getTargetDirname(t.generator.sourcePath, t.targetPath)
targetFile := filepath.Join(t.generator.targetPath, targetAssetname)
tpl := gstaticSimpleTpl{t.targetPath, targetFile}
return tpl.render()
}

Expand All @@ -61,31 +61,37 @@ func findLayout(sourcePath string) (string, error) {
func Generate(srcFolder string, targetFolder string) error {

var wg sync.WaitGroup
eg := errgroup.Group{}

layoutFile, err := findLayout(srcFolder)
if err != nil {
return fmt.Errorf("[%s] %v", "generate", err)
return fmt.Errorf("%v", err)
}

generator := generator{layoutFile, srcFolder, targetFolder}

resolver := resolver(generator, &wg)
resolver := resolver(generator, &wg, &eg)

err = filepath.Walk(srcFolder, resolver)
wg.Wait()

errGroup := eg.Wait()
if errGroup != nil {
return errGroup
}

return err

}

func resolver(generator generator, wg *sync.WaitGroup) filepath.WalkFunc {
func resolver(generator generator, wg *sync.WaitGroup, eg *errgroup.Group) filepath.WalkFunc {

useLayout := generator.layoutPath != ""

return func(path string, info os.FileInfo, err error) error {

if err != nil {
return fmt.Errorf("[%s] %v", "resolver", err)
return fmt.Errorf("%v", err)
}

if path == generator.layoutPath {
Expand Down Expand Up @@ -114,37 +120,45 @@ func resolver(generator generator, wg *sync.WaitGroup) filepath.WalkFunc {
return nil
}

task := task{generator, path}

if ext == ".html" {

if useLayout {
if isTplPlainHtml(path) {

wg.Add(1)
go func() {
defer wg.Done()
wg.Add(1)
identifier := fmt.Sprintf("layoutRenderWorker: %s", path)
executor(identifier, layoutRenderWorker{generator, path}, false)
copyFileWorker(task)
}()

return nil

}

go func() {
defer wg.Done()
if useLayout {
wg.Add(1)
identifier := fmt.Sprintf("simpleRenderWorker: %s", path)
executor(identifier, simpleRenderWorker{generator, path}, false)
}()
go func() {
defer wg.Done()
layoutRenderWorker(task)
}()

return nil

}

eg.Go(func() error {
return simpleRenderWorker(task)
})

return nil

}

wg.Add(1)
go func() {
defer wg.Done()
wg.Add(1)
identifier := fmt.Sprintf("copyFileWorker: %s", path)
executor(identifier, copyFileWorker{generator, path}, false)
copyFileWorker(task)
}()

return nil
Expand All @@ -153,6 +167,14 @@ func resolver(generator generator, wg *sync.WaitGroup) filepath.WalkFunc {

}

func isTplPlainHtml(sourceFile string) bool {
s := strings.Replace(sourceFile, ".html", "", 1)
sourceFile = s + ".yaml"
_, err := os.Stat(sourceFile)
return os.IsNotExist(err)

}

func getTargetDirname(srcFolder, path string) string {
s := strings.Replace(path, srcFolder, "", 1)
return strings.TrimLeft(s, "/")
Expand Down
78 changes: 77 additions & 1 deletion internal/gstatic/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
package gstatic

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

var generatorTestTargetFolder string = os.TempDir() + "gstatictest"

func TestGenerateSimple(t *testing.T) {

generatorTemplateTest(t)

sourcePath := "testdata/simpletpl"
targetPath := generatorTestTargetFolder

err := Generate(sourcePath, targetPath)
if err != nil {
t.Errorf("expected %v; got %v", nil, err)
}

targetFile := filepath.Join(generatorTestTargetFolder, "about.html")
_, err = os.Stat(targetFile)
if err != nil {
t.Errorf("expected %v; got %v", nil, err)
}

}

func TestGenerateLayout(t *testing.T) {

generatorTemplateTest(t)

sourcePath := "testdata/layouttpl"
targetPath := generatorTestTargetFolder

err := Generate(sourcePath, targetPath)
if err != nil {
t.Errorf("expected %v; got %v", nil, err)
}

}

func TestIsTplPlainHtml(t *testing.T) {

sourceFile := "testdata/simpletpl/index.html"
hasSource := isTplPlainHtml(sourceFile)
if hasSource {
t.Errorf("expected %v; got %v", false, hasSource)
}

sourceFile2 := "testdata/simpletpl/about.html"
hasSource2 := isTplPlainHtml(sourceFile2)
if !hasSource2 {
t.Errorf("expected %v; got %v", true, hasSource)
}

}

func TestFindLayout(t *testing.T) {

Expand Down Expand Up @@ -31,3 +89,21 @@ func TestFindLayout(t *testing.T) {
}

}

func generatorTemplateTest(t *testing.T) {
log.SetOutput(io.Discard)
files, err := filepath.Glob(filepath.Join(generatorTestTargetFolder, "*"))
if err != nil {
t.Fatal("Unable to setup test")
}
for _, file := range files {
err = os.RemoveAll(file)
if err != nil {
t.Fatal("Unable to setup test")
}
}

os.Remove(generatorTestTargetFolder)

os.MkdirAll(generatorTestTargetFolder, 0755)
}
1 change: 1 addition & 0 deletions internal/gstatic/testdata/layouttpl/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>About</p>
3 changes: 3 additions & 0 deletions internal/gstatic/testdata/layouttpl/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.red {
color: red;
}
1 change: 1 addition & 0 deletions internal/gstatic/testdata/simpletpl/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>About</p>
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import (
"flag"
"fmt"
"os"
"time"

"github.com/marcomilon/gstatic/internal/gstatic"
)

type elapsedTime struct {
identifier string
start time.Time
end time.Time
}

func main() {

if len(os.Args) < 3 {
Expand All @@ -18,13 +25,24 @@ func main() {
srcFolder := argsWithoutProg[0]
targetFolder := argsWithoutProg[1]

elapsedTime := gstatic.StartTimer("gstatic")
elapsedTime := startTimer("gstatic")
err := gstatic.Generate(srcFolder, targetFolder)
if err != nil {
fmt.Fprintf(os.Stderr, "Something went wrong: %v\n", err.Error())
os.Exit(1)
}
gstatic.EndTimer(elapsedTime)
endTimer(elapsedTime)

}

func startTimer(identifier string) elapsedTime {
return elapsedTime{identifier, time.Now(), time.Time{}}
}

func endTimer(elapsedTime elapsedTime) {
elapsedTime.end = time.Now()
elapsed := elapsedTime.end.Sub(elapsedTime.start)
fmt.Printf("[%s]: %v\n", elapsedTime.identifier, elapsed)
}

func usage() {
Expand Down

0 comments on commit b6dcd1d

Please sign in to comment.