Skip to content

Commit

Permalink
Add test for watch mode (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored Feb 6, 2025
1 parent 6c76ff9 commit b1fb04e
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 42 deletions.
14 changes: 8 additions & 6 deletions internal/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

func buildCommand() (*cobra.Command, error) {
func buildCommand(stopWatch chan struct{}) (*cobra.Command, error) {
v := viper.New()
var config string
var watch bool
Expand Down Expand Up @@ -42,6 +42,12 @@ Complete documentation at https://mlange-42.github.io/modo/`,
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
defer func() {
if err := os.Chdir(cwd); err != nil {
fmt.Println(err)
}
}()

start := time.Now()

cliArgs, err := document.ConfigFromViper(v)
Expand All @@ -52,11 +58,7 @@ Complete documentation at https://mlange-42.github.io/modo/`,
return err
}
if watch {
return watchAndRun(cliArgs, runBuild)
}

if err := os.Chdir(cwd); err != nil {
return err
return watchAndRun(cliArgs, runBuild, stopWatch)
}

fmt.Printf("Completed in %.1fms 🧯\n", float64(time.Since(start).Microseconds())/1000.0)
Expand Down
22 changes: 21 additions & 1 deletion internal/cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@ package cmd

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestBuild(t *testing.T) {
cmd, err := buildCommand()
cmd, err := buildCommand(nil)
assert.Nil(t, err)

cmd.SetArgs([]string{"../../test"})

err = cmd.Execute()
assert.Nil(t, err)
}

func TestBuildWatch(t *testing.T) {
stop := make(chan struct{})

cmd, err := buildCommand(stop)
assert.Nil(t, err)

cmd.SetArgs([]string{"../../test", "--watch"})

go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
<-ticker.C
stop <- struct{}{}
}()

err = cmd.Execute()
assert.Nil(t, err)
}
12 changes: 7 additions & 5 deletions internal/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

func cleanCommand() (*cobra.Command, error) {
func cleanCommand(_ chan struct{}) (*cobra.Command, error) {
v := viper.New()
var config string

Expand All @@ -36,6 +36,12 @@ Complete documentation at https://mlange-42.github.io/modo/`,
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
defer func() {
if err := os.Chdir(cwd); err != nil {
fmt.Println(err)
}
}()

start := time.Now()

cliArgs, err := document.ConfigFromViper(v)
Expand All @@ -46,10 +52,6 @@ Complete documentation at https://mlange-42.github.io/modo/`,
return err
}

if err := os.Chdir(cwd); err != nil {
return err
}

fmt.Printf("Completed in %.1fms 🧯\n", float64(time.Since(start).Microseconds())/1000.0)
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

func TestClean(t *testing.T) {
cmd, err := buildCommand()
cmd, err := buildCommand(nil)
assert.Nil(t, err)
cmd.SetArgs([]string{"../../test"})

err = cmd.Execute()
assert.Nil(t, err)

cmd, err = cleanCommand()
cmd, err = cleanCommand(nil)
assert.Nil(t, err)
cmd.SetArgs([]string{"../../test"})

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type initArgs struct {
NoFolders bool
}

func initCommand() (*cobra.Command, error) {
func initCommand(_ chan struct{}) (*cobra.Command, error) {
initArgs := initArgs{}
var config string

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestInitHugo(t *testing.T) {
dir := t.TempDir()
cwd := setupProject(t, dir, packages)

cmd, err := initCommand()
cmd, err := initCommand(nil)
assert.Nil(t, err)

cmd.SetArgs([]string{"hugo"})
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Complete documentation at https://mlange-42.github.io/modo/`,

root.CompletionOptions.HiddenDefaultCmd = true

for _, fn := range []func() (*cobra.Command, error){initCommand, buildCommand, testCommand, cleanCommand} {
cmd, err := fn()
for _, fn := range []func(chan struct{}) (*cobra.Command, error){initCommand, buildCommand, testCommand, cleanCommand} {
cmd, err := fn(nil)
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

func testCommand() (*cobra.Command, error) {
func testCommand(stopWatch chan struct{}) (*cobra.Command, error) {
v := viper.New()
var config string
var watch bool
Expand Down Expand Up @@ -42,6 +42,12 @@ Complete documentation at https://mlange-42.github.io/modo/`,
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
defer func() {
if err := os.Chdir(cwd); err != nil {
fmt.Println(err)
}
}()

start := time.Now()

cliArgs, err := document.ConfigFromViper(v)
Expand All @@ -52,11 +58,7 @@ Complete documentation at https://mlange-42.github.io/modo/`,
return err
}
if watch {
return watchAndRun(cliArgs, runTest)
}

if err := os.Chdir(cwd); err != nil {
return err
return watchAndRun(cliArgs, runTest, stopWatch)
}

fmt.Printf("Completed in %.1fms 🧯\n", float64(time.Since(start).Microseconds())/1000.0)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestTest(t *testing.T) {
cmd, err := testCommand()
cmd, err := testCommand(nil)
assert.Nil(t, err)

cmd.SetArgs([]string{"../../test"})
Expand Down
38 changes: 21 additions & 17 deletions internal/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func checkConfigFile(f string) error {
return nil
}

func watchAndRun(args *document.Config, command func(*document.Config) error) error {
func watchAndRun(args *document.Config, command func(*document.Config) error, stop chan struct{}) error {
args.RemovePostScripts()

c := make(chan notify.EventInfo, 32)
Expand Down Expand Up @@ -271,27 +271,31 @@ func watchAndRun(args *document.Config, command func(*document.Config) error) er
}
}()

for events := range collected {
if events == nil {
continue
}
trigger := false
for _, e := range events {
for _, ext := range watchExtensions {
if strings.HasSuffix(e.Path(), ext) {
trigger = true
break
for {
select {
case events := <-collected:
if events == nil {
continue
}
trigger := false
for _, e := range events {
for _, ext := range watchExtensions {
if strings.HasSuffix(e.Path(), ext) {
trigger = true
break
}
}
}
}
if trigger {
if err := command(args); err != nil {
return err
if trigger {
if err := command(args); err != nil {
return err
}
fmt.Printf("Watching for changes: %s\n", strings.Join(toWatch, ", "))
}
fmt.Printf("Watching for changes: %s\n", strings.Join(toWatch, ", "))
case <-stop:
return nil
}
}
return nil
}

func getWatchPaths(args *document.Config) ([]string, error) {
Expand Down

0 comments on commit b1fb04e

Please sign in to comment.