Skip to content

feat: Add pattern matching and multiple paths to watch configuration #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 52 additions & 46 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3051,7 +3051,8 @@ services:
}

func TestLoadDevelopConfig(t *testing.T) {
project, err := LoadWithContext(context.TODO(), buildConfigDetails(`
t.Run("successfully load watch config", func(t *testing.T) {
project, err := LoadWithContext(context.Background(), buildConfigDetails(`
name: load-develop
services:
frontend:
Expand All @@ -3066,15 +3067,16 @@ services:
target: /var/www
ignore:
- node_modules/

backend:
image: example/backend
build: ./backend
develop:
watch:
# rebuild image and recreate service
- path: ./backend/src
action: rebuild
- action: rebuild
path:
- ./backend/src
- ./backend
proxy:
image: example/proxy
build: ./proxy
Expand All @@ -3085,67 +3087,71 @@ services:
action: sync+restart
target: /etc/nginx/proxy.conf
`, nil), func(options *Options) {
options.ResolvePaths = false
options.SkipValidation = true
})
assert.NilError(t, err)
frontend, err := project.GetService("frontend")
assert.NilError(t, err)
assert.DeepEqual(t, *frontend.Develop, types.DevelopConfig{
Watch: []types.Trigger{
{
Path: "./webapp/html",
Action: types.WatchActionSync,
Target: "/var/www",
Ignore: []string{"node_modules/"},
Extensions: types.Extensions{
"x-initialSync": true,
options.ResolvePaths = false
options.SkipValidation = true
})
assert.NilError(t, err)
frontend, err := project.GetService("frontend")
assert.NilError(t, err)
assert.DeepEqual(t, *frontend.Develop, types.DevelopConfig{
Watch: []types.Trigger{
{
Path: "./webapp/html",
Action: types.WatchActionSync,
Target: "/var/www",
Ignore: []string{"node_modules/"},
Extensions: types.Extensions{
"x-initialSync": true,
},
},
},
},
})
backend, err := project.GetService("backend")
assert.NilError(t, err)
assert.DeepEqual(t, *backend.Develop, types.DevelopConfig{
Watch: []types.Trigger{
{
Path: "./backend/src",
Action: types.WatchActionRebuild,
})
backend, err := project.GetService("backend")
assert.NilError(t, err)
assert.DeepEqual(t, *backend.Develop, types.DevelopConfig{
Watch: []types.Trigger{
{
Path: "./backend/src",
Action: types.WatchActionRebuild,
},
{
Path: "./backend",
Action: types.WatchActionRebuild,
},
},
},
})
proxy, err := project.GetService("proxy")
assert.NilError(t, err)
assert.DeepEqual(t, *proxy.Develop, types.DevelopConfig{
Watch: []types.Trigger{
{
Path: "./proxy/proxy.conf",
Action: types.WatchActionSyncRestart,
Target: "/etc/nginx/proxy.conf",
})
proxy, err := project.GetService("proxy")
assert.NilError(t, err)
assert.DeepEqual(t, *proxy.Develop, types.DevelopConfig{
Watch: []types.Trigger{
{
Path: "./proxy/proxy.conf",
Action: types.WatchActionSyncRestart,
Target: "/etc/nginx/proxy.conf",
},
},
},
})
})
}

func TestBadDevelopConfig(t *testing.T) {
_, err := LoadWithContext(context.TODO(), buildConfigDetails(`
t.Run("should not load successfully bad watch config", func(t *testing.T) {
_, err := LoadWithContext(context.TODO(), buildConfigDetails(`
name: load-develop
services:
frontend:
image: example/webapp
build: ./webapp
develop:
watch:
# sync static content
# sync static content
- path: ./webapp/html
target: /var/www
ignore:
- node_modules/

`, nil), func(options *Options) {
options.ResolvePaths = false
options.ResolvePaths = false
})
assert.ErrorContains(t, err, "validating filename0.yml: services.frontend.develop.watch.0 action is required")
})
assert.ErrorContains(t, err, "validating filename0.yml: services.frontend.develop.watch.0 action is required")
}

func TestBadServiceConfig(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions loader/testdata/watch/compose-test-watch-star.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: compose-test-watch-star
services:
app:
image: example/app
develop:
watch:
- path: ./watch/*.txt
action: rebuild
# - path: ./watch/*
# target: ./app
# action: sync
Empty file.
Empty file.
7 changes: 5 additions & 2 deletions loader/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,11 @@ func checkConsistency(project *types.Project) error { //nolint:gocyclo

if s.Develop != nil && s.Develop.Watch != nil {
for _, watch := range s.Develop.Watch {
if watch.Target == "" && watch.Action != types.WatchActionRebuild && watch.Action != types.WatchActionRestart {
return fmt.Errorf("services.%s.develop.watch: target is required for non-rebuild actions: %w", s.Name, errdefs.ErrInvalid)
if watch.Action != types.WatchActionRebuild && watch.Action != types.WatchActionRestart {
if watch.Target == "" {
return fmt.Errorf("services.%s.develop.watch: target is required for %s, %s and %s actions: %w",
s.Name, types.WatchActionSync, types.WatchActionSyncExec, types.WatchActionSyncRestart, errdefs.ErrInvalid)
}
}
}
}
Expand Down
147 changes: 84 additions & 63 deletions loader/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package loader

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -303,69 +304,6 @@ func TestValidateWatch(t *testing.T) {
assert.NilError(t, err)
})

t.Run("watch missing target for sync action", func(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Develop: &types.DevelopConfig{
Watch: []types.Trigger{
{
Action: types.WatchActionSync,
Path: "/app",
},
},
},
},
},
}
err := checkConsistency(&project)
assert.Error(t, err, "services.myservice.develop.watch: target is required for non-rebuild actions: invalid compose project")
})

t.Run("watch missing target for sync+restart action", func(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Develop: &types.DevelopConfig{
Watch: []types.Trigger{
{
Action: types.WatchActionSyncRestart,
Path: "/app",
},
},
},
},
},
}
err := checkConsistency(&project)
assert.Error(t, err, "services.myservice.develop.watch: target is required for non-rebuild actions: invalid compose project")
})

t.Run("watch config valid with missing target for rebuild action", func(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Develop: &types.DevelopConfig{
Watch: []types.Trigger{
{
Action: types.WatchActionRebuild,
Path: "/app",
},
},
},
},
},
}
err := checkConsistency(&project)
assert.NilError(t, err)
})

t.Run("depends on disabled service", func(t *testing.T) {
project := types.Project{
Services: types.Services{
Expand Down Expand Up @@ -406,4 +344,87 @@ func TestValidateWatch(t *testing.T) {
err := checkConsistency(&project)
assert.ErrorContains(t, err, "depends on undefined service")
})

type WatchActionTest struct {
action types.WatchAction
}
tests := []WatchActionTest{
{action: types.WatchActionSync},
{action: types.WatchActionSyncRestart},
{action: types.WatchActionSyncExec},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("watch config is INVALID when missing target for %s action", tt.action), func(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Develop: &types.DevelopConfig{
Watch: []types.Trigger{
{
Action: tt.action,
Path: "/app",
// Missing Target
},
},
},
},
},
}
err := checkConsistency(&project)
assert.Error(t, err, "services.myservice.develop.watch: target is required for sync, sync+exec and sync+restart actions: invalid compose project")
})
}
tests = []WatchActionTest{
{action: types.WatchActionRebuild},
{action: types.WatchActionRestart},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("watch config is VALID with missing target for %s action", tt.action), func(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Develop: &types.DevelopConfig{
Watch: []types.Trigger{
{
Action: tt.action,
Path: "/app",
},
},
},
},
},
}
err := checkConsistency(&project)
assert.NilError(t, err)
})

t.Run(fmt.Sprintf("watch config is VALID with one or more paths for %s action", tt.action), func(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Develop: &types.DevelopConfig{
Watch: []types.Trigger{
{
Action: tt.action,
Path: "/app",
},
{
Action: tt.action,
Path: "/app2",
},
},
},
},
},
}
err := checkConsistency(&project)
assert.NilError(t, err)
})
}
}
2 changes: 1 addition & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@
"required": ["path", "action"],
"properties": {
"ignore": {"type": "array", "items": {"type": "string"}},
"path": {"type": "string"},
"path": {"$ref": "#/definitions/string_or_list"},
"action": {"type": "string", "enum": ["rebuild", "sync", "restart", "sync+restart", "sync+exec"]},
"target": {"type": "string"},
"exec": {"$ref": "#/definitions/service_hook"}
Expand Down
Loading