From da82b7f536c42c4651b7620cee01da295691e3c2 Mon Sep 17 00:00:00 2001 From: maddalax Date: Sun, 13 Oct 2024 16:33:08 -0500 Subject: [PATCH 1/8] config wip --- cli/htmgo/go.mod | 2 + cli/htmgo/go.sum | 2 + cli/htmgo/internal/dirutil/dir.go | 5 +++ cli/htmgo/internal/dirutil/glob.go | 50 +++++++++++++++++++++++ cli/htmgo/tasks/copyassets/bundle.go | 2 +- cli/htmgo/watcher.go | 61 +++++++++++++++++----------- framework/config/default.yml | 1 + framework/config/project.go | 55 +++++++++++++++++++++++++ htmgo-site/htmgo.yml | 5 +++ 9 files changed, 158 insertions(+), 25 deletions(-) create mode 100644 cli/htmgo/internal/dirutil/glob.go create mode 100644 framework/config/default.yml create mode 100644 framework/config/project.go create mode 100644 htmgo-site/htmgo.yml diff --git a/cli/htmgo/go.mod b/cli/htmgo/go.mod index 6bad2bb..ba191b9 100644 --- a/cli/htmgo/go.mod +++ b/cli/htmgo/go.mod @@ -11,3 +11,5 @@ require ( golang.org/x/sys v0.25.0 golang.org/x/tools v0.25.0 ) + +require github.com/bmatcuk/doublestar/v4 v4.7.1 // indirect diff --git a/cli/htmgo/go.sum b/cli/htmgo/go.sum index b8b03a7..87e7902 100644 --- a/cli/htmgo/go.sum +++ b/cli/htmgo/go.sum @@ -1,3 +1,5 @@ +github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q= +github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/dave/jennifer v1.7.1 h1:B4jJJDHelWcDhlRQxWeo0Npa/pYKBLrirAQoTN45txo= github.com/dave/jennifer v1.7.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= diff --git a/cli/htmgo/internal/dirutil/dir.go b/cli/htmgo/internal/dirutil/dir.go index 2715277..7063e83 100644 --- a/cli/htmgo/internal/dirutil/dir.go +++ b/cli/htmgo/internal/dirutil/dir.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/maddalax/htmgo/cli/htmgo/tasks/process" + "github.com/maddalax/htmgo/framework/config" "io" "log/slog" "os" @@ -17,6 +18,10 @@ func HasFileFromRoot(file string) bool { return err == nil } +func GetConfig() *config.ProjectConfig { + return config.FromConfigFile(process.GetWorkingDir()) +} + func CreateHtmgoDir() { if !HasFileFromRoot("__htmgo") { CreateDirFromRoot("__htmgo") diff --git a/cli/htmgo/internal/dirutil/glob.go b/cli/htmgo/internal/dirutil/glob.go new file mode 100644 index 0000000..3e015c2 --- /dev/null +++ b/cli/htmgo/internal/dirutil/glob.go @@ -0,0 +1,50 @@ +package dirutil + +import ( + "fmt" + "github.com/bmatcuk/doublestar/v4" + "io/fs" + "path/filepath" +) + +func matchesAny(patterns []string, path string) bool { + for _, pattern := range patterns { + matched, err := doublestar.Match(pattern, path) + if err != nil { + fmt.Printf("Error matching pattern: %v\n", err) + return false + } + if matched { + return true + } + } + return false +} + +func IsGlobExclude(path string, excludePatterns []string) bool { + return matchesAny(excludePatterns, path) +} + +func IsGlobMatch(path string, patterns []string, excludePatterns []string) bool { + if matchesAny(excludePatterns, path) { + return false + } + return matchesAny(patterns, path) +} + +func GlobMatchDirs(root string, includePatterns, excludePatterns []string, cb func(string)) { + //directories := map[string]bool{} + // Walk through the directory recursively + _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if matchesAny(excludePatterns, path) { + return fs.SkipDir + } + if matchesAny(includePatterns, path) { + cb(path) + } + return nil + }) +} diff --git a/cli/htmgo/tasks/copyassets/bundle.go b/cli/htmgo/tasks/copyassets/bundle.go index a24da76..f0f7699 100644 --- a/cli/htmgo/tasks/copyassets/bundle.go +++ b/cli/htmgo/tasks/copyassets/bundle.go @@ -92,7 +92,7 @@ func CopyAssets() { }) } - if !dirutil.HasFileFromRoot("tailwind.config.js") { + if dirutil.GetConfig().Tailwind && !dirutil.HasFileFromRoot("tailwind.config.js") { err = dirutil.CopyFile( filepath.Join(assetCssDir, "tailwind.config.js"), filepath.Join(process.GetWorkingDir(), "tailwind.config.js"), diff --git a/cli/htmgo/watcher.go b/cli/htmgo/watcher.go index 0509b3d..5af47b7 100644 --- a/cli/htmgo/watcher.go +++ b/cli/htmgo/watcher.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "github.com/fsnotify/fsnotify" "github.com/google/uuid" "github.com/maddalax/htmgo/cli/htmgo/internal" + "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" "github.com/maddalax/htmgo/cli/htmgo/tasks/module" "log" "log/slog" @@ -13,11 +15,10 @@ import ( "time" ) -var ignoredDirs = []string{".git", ".idea", "node_modules", "vendor"} - func startWatcher(cb func(version string, file []*fsnotify.Event)) { events := make([]*fsnotify.Event, 0) debouncer := internal.NewDebouncer(500 * time.Millisecond) + config := dirutil.GetConfig() defer func() { if r := recover(); r != nil { @@ -42,16 +43,17 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { } if event.Has(fsnotify.Remove) { - info, err := os.Stat(event.Name) - if err != nil { + if dirutil.IsGlobMatch(event.Name, config.WatchFiles, config.WatchIgnore) { + watcher.Remove(event.Name) continue } - if info.IsDir() { - _ = watcher.Remove(event.Name) - } } if event.Has(fsnotify.Create) { + if dirutil.IsGlobMatch(event.Name, config.WatchFiles, config.WatchIgnore) { + watcher.Add(event.Name) + continue + } info, err := os.Stat(event.Name) if err != nil { slog.Error("Error getting file info:", slog.String("path", event.Name), slog.String("error", err.Error())) @@ -67,20 +69,22 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { } } - if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) { - events = append(events, &event) - debouncer.Do(func() { - seen := make(map[string]bool) - dedupe := make([]*fsnotify.Event, 0) - for _, e := range events { - if _, ok := seen[e.Name]; !ok { - seen[e.Name] = true - dedupe = append(dedupe, e) + if dirutil.IsGlobMatch(event.Name, config.WatchFiles, config.WatchIgnore) { + if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) { + events = append(events, &event) + debouncer.Do(func() { + seen := make(map[string]bool) + dedupe := make([]*fsnotify.Event, 0) + for _, e := range events { + if _, ok := seen[e.Name]; !ok { + seen[e.Name] = true + dedupe = append(dedupe, e) + } } - } - cb(uuid.NewString()[0:6], dedupe) - events = make([]*fsnotify.Event, 0) - }) + cb(uuid.NewString()[0:6], dedupe) + events = make([]*fsnotify.Event, 0) + }) + } } case err, ok := <-watcher.Errors: if !ok { @@ -101,17 +105,25 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { watcher.Add(assetPath) } + go func() { + for { + time.Sleep(time.Second * 5) + files := watcher.WatchList() + count := len(files) + fmt.Printf("Watching %d dirs\n", count) + } + }() + // Walk through the root directory and add all subdirectories to the watcher err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } // Ignore directories in the ignoredDirs list - for _, ignoredDir := range ignoredDirs { - if ignoredDir == info.Name() { - return filepath.SkipDir - } + if dirutil.IsGlobExclude(path, config.WatchIgnore) { + return filepath.SkipDir } + // Only watch directories if info.IsDir() { err = watcher.Add(path) @@ -123,6 +135,7 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { } return nil }) + if err != nil { log.Fatal(err) } diff --git a/framework/config/default.yml b/framework/config/default.yml new file mode 100644 index 0000000..bd7be9a --- /dev/null +++ b/framework/config/default.yml @@ -0,0 +1 @@ +tailwind: true diff --git a/framework/config/project.go b/framework/config/project.go new file mode 100644 index 0000000..a3cd1d2 --- /dev/null +++ b/framework/config/project.go @@ -0,0 +1,55 @@ +package config + +import ( + "gopkg.in/yaml.v3" + "os" + "path" +) + +type ProjectConfig struct { + Tailwind bool `yaml:"tailwind"` + WatchIgnore []string `yaml:"watch_ignore"` + WatchFiles []string `yaml:"watch_files"` +} + +func DefaultProjectConfig() *ProjectConfig { + return &ProjectConfig{ + Tailwind: true, + WatchIgnore: []string{ + "node_modules", ".git", ".idea", "assets/dist", + }, + WatchFiles: []string{ + "**/*.go", "**/*.html", "**/*.css", "**/*.js", "**/*.json", "**/*.yaml", "**/*.yml", + }, + } +} + +func (cfg *ProjectConfig) EnhanceWithDefaults() *ProjectConfig { + defaultCfg := DefaultProjectConfig() + if len(cfg.WatchFiles) == 0 { + cfg.WatchFiles = defaultCfg.WatchFiles + } + if len(cfg.WatchIgnore) == 0 { + cfg.WatchIgnore = defaultCfg.WatchIgnore + } + return cfg +} + +func FromConfigFile(workingDir string) *ProjectConfig { + defaultCfg := DefaultProjectConfig() + names := []string{"htmgo.yaml", "htmgo.yml", "_htmgo.yaml", "_htmgo.yml"} + for _, name := range names { + filePath := path.Join(workingDir, name) + if _, err := os.Stat(filePath); err == nil { + cfg := &ProjectConfig{} + bytes, err := os.ReadFile(filePath) + if err == nil { + err = yaml.Unmarshal(bytes, cfg) + if err == nil { + return cfg.EnhanceWithDefaults() + } + } + } + } + return defaultCfg +} diff --git a/htmgo-site/htmgo.yml b/htmgo-site/htmgo.yml new file mode 100644 index 0000000..67500d7 --- /dev/null +++ b/htmgo-site/htmgo.yml @@ -0,0 +1,5 @@ +tailwind: true +# which directories to ignore when watching for changes, supports glob patterns +watch_ignore: [".git", "node_modules", "dist/*"] +# files to watch for changes that are not included by default, supports glob patterns +watch_files: ["**/*.go", "**/*.css"] From 2bac9307c6abf1de966a431023dd86b381900c81 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 09:49:48 -0500 Subject: [PATCH 2/8] more config --- cli/htmgo/tasks/css/css.go | 2 +- cli/htmgo/watcher.go | 44 ++++++++++++++--------------------- framework/config/default.yml | 1 - framework/config/project.go | 2 +- htmgo-site/htmgo.yml | 11 ++++++--- htmgo-site/pages/base/root.go | 3 ++- 6 files changed, 29 insertions(+), 34 deletions(-) delete mode 100644 framework/config/default.yml diff --git a/cli/htmgo/tasks/css/css.go b/cli/htmgo/tasks/css/css.go index 0bdaf58..1c07ec2 100644 --- a/cli/htmgo/tasks/css/css.go +++ b/cli/htmgo/tasks/css/css.go @@ -12,7 +12,7 @@ import ( ) func IsTailwindEnabled() bool { - return dirutil.HasFileFromRoot("tailwind.config.js") + return dirutil.GetConfig().Tailwind && dirutil.HasFileFromRoot("tailwind.config.js") } func Setup() bool { diff --git a/cli/htmgo/watcher.go b/cli/htmgo/watcher.go index 5af47b7..4838d14 100644 --- a/cli/htmgo/watcher.go +++ b/cli/htmgo/watcher.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "github.com/fsnotify/fsnotify" "github.com/google/uuid" "github.com/maddalax/htmgo/cli/htmgo/internal" @@ -36,8 +35,6 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { for { select { case event, ok := <-watcher.Events: - slog.Debug("event:", slog.String("name", event.Name), slog.String("op", event.Op.String())) - if !ok { return } @@ -69,23 +66,25 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { } } - if dirutil.IsGlobMatch(event.Name, config.WatchFiles, config.WatchIgnore) { - if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) { - events = append(events, &event) - debouncer.Do(func() { - seen := make(map[string]bool) - dedupe := make([]*fsnotify.Event, 0) - for _, e := range events { - if _, ok := seen[e.Name]; !ok { - seen[e.Name] = true - dedupe = append(dedupe, e) - } - } - cb(uuid.NewString()[0:6], dedupe) - events = make([]*fsnotify.Event, 0) - }) + if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) { + if !dirutil.IsGlobMatch(event.Name, config.WatchFiles, config.WatchIgnore) { + continue } + events = append(events, &event) + debouncer.Do(func() { + seen := make(map[string]bool) + dedupe := make([]*fsnotify.Event, 0) + for _, e := range events { + if _, ok := seen[e.Name]; !ok { + seen[e.Name] = true + dedupe = append(dedupe, e) + } + } + cb(uuid.NewString()[0:6], dedupe) + events = make([]*fsnotify.Event, 0) + }) } + case err, ok := <-watcher.Errors: if !ok { return @@ -105,15 +104,6 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { watcher.Add(assetPath) } - go func() { - for { - time.Sleep(time.Second * 5) - files := watcher.WatchList() - count := len(files) - fmt.Printf("Watching %d dirs\n", count) - } - }() - // Walk through the root directory and add all subdirectories to the watcher err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { if err != nil { diff --git a/framework/config/default.yml b/framework/config/default.yml deleted file mode 100644 index bd7be9a..0000000 --- a/framework/config/default.yml +++ /dev/null @@ -1 +0,0 @@ -tailwind: true diff --git a/framework/config/project.go b/framework/config/project.go index a3cd1d2..688241d 100644 --- a/framework/config/project.go +++ b/framework/config/project.go @@ -19,7 +19,7 @@ func DefaultProjectConfig() *ProjectConfig { "node_modules", ".git", ".idea", "assets/dist", }, WatchFiles: []string{ - "**/*.go", "**/*.html", "**/*.css", "**/*.js", "**/*.json", "**/*.yaml", "**/*.yml", + "**/*.go", "**/*.html", "**/*.css", "**/*.js", "**/*.json", "**/*.yaml", "**/*.yml", "**/*.md", }, } } diff --git a/htmgo-site/htmgo.yml b/htmgo-site/htmgo.yml index 67500d7..d60d2ff 100644 --- a/htmgo-site/htmgo.yml +++ b/htmgo-site/htmgo.yml @@ -1,5 +1,10 @@ +# htmgo configuration + +# if tailwindcss is enabled, htmgo will automatically compile your tailwind and output it to assets/dist tailwind: true -# which directories to ignore when watching for changes, supports glob patterns + +# which directories to ignore when watching for changes, supports glob patterns through https://github.com/bmatcuk/doublestar watch_ignore: [".git", "node_modules", "dist/*"] -# files to watch for changes that are not included by default, supports glob patterns -watch_files: ["**/*.go", "**/*.css"] + +# files to watch for changes, supports glob patterns through https://github.com/bmatcuk/doublestar +watch_files: ["**/*.go", "**/*.css", "**/*.md"] diff --git a/htmgo-site/pages/base/root.go b/htmgo-site/pages/base/root.go index c15900b..b110caa 100644 --- a/htmgo-site/pages/base/root.go +++ b/htmgo-site/pages/base/root.go @@ -45,7 +45,8 @@ func PageWithNav(ctx *h.RequestContext, children ...h.Ren) *h.Element { return RootPage(ctx, h.Fragment( partials.NavBar(ctx, partials.NavBarProps{ - Expanded: false, + Expanded: false, + ShowPreRelease: true, }), h.Div( children..., From 49f4067bc516f13afb0859fcde5bb5e40413c3a5 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 09:52:06 -0500 Subject: [PATCH 3/8] add default config file to template --- templates/starter/htmgo.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 templates/starter/htmgo.yml diff --git a/templates/starter/htmgo.yml b/templates/starter/htmgo.yml new file mode 100644 index 0000000..d60d2ff --- /dev/null +++ b/templates/starter/htmgo.yml @@ -0,0 +1,10 @@ +# htmgo configuration + +# if tailwindcss is enabled, htmgo will automatically compile your tailwind and output it to assets/dist +tailwind: true + +# which directories to ignore when watching for changes, supports glob patterns through https://github.com/bmatcuk/doublestar +watch_ignore: [".git", "node_modules", "dist/*"] + +# files to watch for changes, supports glob patterns through https://github.com/bmatcuk/doublestar +watch_files: ["**/*.go", "**/*.css", "**/*.md"] From 3f502dba8110b6a3d7f9eb408b72cc1bce7e5bd7 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 09:58:37 -0500 Subject: [PATCH 4/8] cleanup / add err handling --- cli/htmgo/internal/dirutil/glob.go | 19 ------------------- framework/config/project.go | 7 +++++-- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/cli/htmgo/internal/dirutil/glob.go b/cli/htmgo/internal/dirutil/glob.go index 3e015c2..1315c66 100644 --- a/cli/htmgo/internal/dirutil/glob.go +++ b/cli/htmgo/internal/dirutil/glob.go @@ -3,8 +3,6 @@ package dirutil import ( "fmt" "github.com/bmatcuk/doublestar/v4" - "io/fs" - "path/filepath" ) func matchesAny(patterns []string, path string) bool { @@ -31,20 +29,3 @@ func IsGlobMatch(path string, patterns []string, excludePatterns []string) bool } return matchesAny(patterns, path) } - -func GlobMatchDirs(root string, includePatterns, excludePatterns []string, cb func(string)) { - //directories := map[string]bool{} - // Walk through the directory recursively - _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if matchesAny(excludePatterns, path) { - return fs.SkipDir - } - if matchesAny(includePatterns, path) { - cb(path) - } - return nil - }) -} diff --git a/framework/config/project.go b/framework/config/project.go index 688241d..72e222d 100644 --- a/framework/config/project.go +++ b/framework/config/project.go @@ -2,6 +2,7 @@ package config import ( "gopkg.in/yaml.v3" + "log/slog" "os" "path" ) @@ -45,9 +46,11 @@ func FromConfigFile(workingDir string) *ProjectConfig { bytes, err := os.ReadFile(filePath) if err == nil { err = yaml.Unmarshal(bytes, cfg) - if err == nil { - return cfg.EnhanceWithDefaults() + if err != nil { + slog.Error("Error parsing config file", slog.String("file", filePath), slog.String("error", err.Error())) + os.Exit(1) } + return cfg.EnhanceWithDefaults() } } } From d21afa5f6db75f8c4d2f1c98b91806770f447e06 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 10:02:04 -0500 Subject: [PATCH 5/8] few tests --- framework/config/project_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 framework/config/project_test.go diff --git a/framework/config/project_test.go b/framework/config/project_test.go new file mode 100644 index 0000000..e3d8d17 --- /dev/null +++ b/framework/config/project_test.go @@ -0,0 +1,31 @@ +package config + +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestDefaultProjectConfig(t *testing.T) { + cfg := DefaultProjectConfig() + assert.Equal(t, true, cfg.Tailwind) + assert.Equal(t, 4, len(cfg.WatchIgnore)) + assert.Equal(t, 8, len(cfg.WatchFiles)) +} + +func TestNoConfigFileUsesDefault(t *testing.T) { + cfg := FromConfigFile("testdata") + assert.Equal(t, true, cfg.Tailwind) + assert.Equal(t, 4, len(cfg.WatchIgnore)) + assert.Equal(t, 8, len(cfg.WatchFiles)) +} + +func TestPartialConfigMerges(t *testing.T) { + os.Mkdir("testdata", 0755) + defer os.RemoveAll("testdata") + os.WriteFile("testdata/htmgo.yaml", []byte("tailwind: false"), 0644) + cfg := FromConfigFile("testdata") + assert.Equal(t, false, cfg.Tailwind) + assert.Equal(t, 4, len(cfg.WatchIgnore)) + assert.Equal(t, 8, len(cfg.WatchFiles)) +} From c3be0e92d280edfad79547315dcf56afce71c778 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 10:05:14 -0500 Subject: [PATCH 6/8] few tests --- framework/config/project_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/framework/config/project_test.go b/framework/config/project_test.go index e3d8d17..4e832c4 100644 --- a/framework/config/project_test.go +++ b/framework/config/project_test.go @@ -7,6 +7,7 @@ import ( ) func TestDefaultProjectConfig(t *testing.T) { + t.Parallel() cfg := DefaultProjectConfig() assert.Equal(t, true, cfg.Tailwind) assert.Equal(t, 4, len(cfg.WatchIgnore)) @@ -14,6 +15,7 @@ func TestDefaultProjectConfig(t *testing.T) { } func TestNoConfigFileUsesDefault(t *testing.T) { + t.Parallel() cfg := FromConfigFile("testdata") assert.Equal(t, true, cfg.Tailwind) assert.Equal(t, 4, len(cfg.WatchIgnore)) @@ -21,6 +23,7 @@ func TestNoConfigFileUsesDefault(t *testing.T) { } func TestPartialConfigMerges(t *testing.T) { + t.Parallel() os.Mkdir("testdata", 0755) defer os.RemoveAll("testdata") os.WriteFile("testdata/htmgo.yaml", []byte("tailwind: false"), 0644) @@ -29,3 +32,14 @@ func TestPartialConfigMerges(t *testing.T) { assert.Equal(t, 4, len(cfg.WatchIgnore)) assert.Equal(t, 8, len(cfg.WatchFiles)) } + +func TestShouldNotSetTailwindTrue(t *testing.T) { + t.Parallel() + os.Mkdir("testdata1", 0755) + defer os.RemoveAll("testdata1") + os.WriteFile("testdata1/htmgo.yaml", []byte("someValue: false"), 0644) + cfg := FromConfigFile("testdata") + assert.Equal(t, false, cfg.Tailwind) + assert.Equal(t, 4, len(cfg.WatchIgnore)) + assert.Equal(t, 8, len(cfg.WatchFiles)) +} From 55337cde13666accb0dec3521858e49e16ee8de4 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 10:06:00 -0500 Subject: [PATCH 7/8] use diff dir --- framework/config/project_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/config/project_test.go b/framework/config/project_test.go index 4e832c4..46b900a 100644 --- a/framework/config/project_test.go +++ b/framework/config/project_test.go @@ -16,7 +16,7 @@ func TestDefaultProjectConfig(t *testing.T) { func TestNoConfigFileUsesDefault(t *testing.T) { t.Parallel() - cfg := FromConfigFile("testdata") + cfg := FromConfigFile("testdata2") assert.Equal(t, true, cfg.Tailwind) assert.Equal(t, 4, len(cfg.WatchIgnore)) assert.Equal(t, 8, len(cfg.WatchFiles)) From aff426829dfb7b19a7cc93850b2335c4a508dfa0 Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 14 Oct 2024 10:14:22 -0500 Subject: [PATCH 8/8] fix tests --- framework/config/project_test.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/framework/config/project_test.go b/framework/config/project_test.go index 46b900a..f015635 100644 --- a/framework/config/project_test.go +++ b/framework/config/project_test.go @@ -3,6 +3,7 @@ package config import ( "github.com/stretchr/testify/assert" "os" + "path" "testing" ) @@ -16,7 +17,7 @@ func TestDefaultProjectConfig(t *testing.T) { func TestNoConfigFileUsesDefault(t *testing.T) { t.Parallel() - cfg := FromConfigFile("testdata2") + cfg := FromConfigFile("non-existing-dir") assert.Equal(t, true, cfg.Tailwind) assert.Equal(t, 4, len(cfg.WatchIgnore)) assert.Equal(t, 8, len(cfg.WatchFiles)) @@ -24,10 +25,8 @@ func TestNoConfigFileUsesDefault(t *testing.T) { func TestPartialConfigMerges(t *testing.T) { t.Parallel() - os.Mkdir("testdata", 0755) - defer os.RemoveAll("testdata") - os.WriteFile("testdata/htmgo.yaml", []byte("tailwind: false"), 0644) - cfg := FromConfigFile("testdata") + dir := writeConfigFile(t, "tailwind: false") + cfg := FromConfigFile(dir) assert.Equal(t, false, cfg.Tailwind) assert.Equal(t, 4, len(cfg.WatchIgnore)) assert.Equal(t, 8, len(cfg.WatchFiles)) @@ -35,11 +34,17 @@ func TestPartialConfigMerges(t *testing.T) { func TestShouldNotSetTailwindTrue(t *testing.T) { t.Parallel() - os.Mkdir("testdata1", 0755) - defer os.RemoveAll("testdata1") - os.WriteFile("testdata1/htmgo.yaml", []byte("someValue: false"), 0644) - cfg := FromConfigFile("testdata") + dir := writeConfigFile(t, "someValue: true") + cfg := FromConfigFile(dir) assert.Equal(t, false, cfg.Tailwind) assert.Equal(t, 4, len(cfg.WatchIgnore)) assert.Equal(t, 8, len(cfg.WatchFiles)) } + +func writeConfigFile(t *testing.T, content string) string { + temp := os.TempDir() + os.Mkdir(temp, 0755) + err := os.WriteFile(path.Join(temp, "htmgo.yml"), []byte(content), 0644) + assert.Nil(t, err) + return temp +}