-
Notifications
You must be signed in to change notification settings - Fork 413
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
feat: run local --watch flag #5413
Merged
mergify
merged 28 commits into
aws:mainline
from
CaptainCarpensir:cli/run-local-watch-flag
Nov 7, 2023
Merged
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9b15223
write file package to track filesystem changes
3bb3f2f
run local --watch flag
e265dd5
reverse accidental line spacing
423fafd
handle returned errors
61bcafc
fix merge conflict
14eb108
Merge branch 'mainline' into cli/run-local-watch-flag
CaptainCarpensir 8a7d8a2
reorder test calls
9701f70
Merge branch 'cli/run-local-watch-flag' of https://github.com/Captain…
21d6605
simplify debounce
8ea2c38
removed unused parameter
142bc12
address feedback
80aa093
dont update for hidden files and folders
3ccfae2
watcher integration test
0364052
localintegration build tag
64dffc8
Merge branch 'mainline' into cli/run-local-watch-flag
CaptainCarpensir 52ca8e7
fix merge errors
497df05
remove race conditions
b09f63a
fix merge change causing tests to time out
e0c82a8
buffer watcher for tests
28c34b7
make integration test non-blocking on failure
edcc10f
two events
563bdc3
address adi feedback
c361a7d
make watcher close not handle outstanding events/errors
6439229
Update internal/pkg/cli/file/watch.go
CaptainCarpensir e4c2f41
Merge branch 'mainline' into cli/run-local-watch-flag
CaptainCarpensir 193b84e
fix merge changes
6349e0c
address wanxian feedback, fix tests
dd9dd0b
Merge branch 'mainline' into cli/run-local-watch-flag
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filetest | ||
|
||
import "github.com/fsnotify/fsnotify" | ||
|
||
// Double is a test double for file.RecursiveWatcher | ||
type Double struct { | ||
EventsFn func() <-chan fsnotify.Event | ||
ErrorsFn func() <-chan error | ||
} | ||
|
||
// Add is a no-op for Double. | ||
func (d *Double) Add(string) error { | ||
return nil | ||
} | ||
|
||
// Close is a no-op for Double. | ||
func (d *Double) Close() error { | ||
return nil | ||
} | ||
|
||
// Events calls the stubbed function. | ||
func (d *Double) Events() <-chan fsnotify.Event { | ||
if d.EventsFn == nil { | ||
return nil | ||
} | ||
return d.EventsFn() | ||
} | ||
|
||
// Errors calls the stubbed function. | ||
func (d *Double) Errors() <-chan error { | ||
if d.ErrorsFn == nil { | ||
return nil | ||
} | ||
return d.ErrorsFn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build !windows | ||
|
||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package file | ||
|
||
import "path/filepath" | ||
|
||
// IsHiddenFile returns true if the file is hidden on non-windows. | ||
CaptainCarpensir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func IsHiddenFile(filename string) (bool, error) { | ||
return filepath.Base(filename)[0] == '.', nil | ||
CaptainCarpensir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package file | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// IsHiddenFile returns true if the file is hidden on windows. | ||
func IsHiddenFile(filename string) (bool, error) { | ||
pointer, err := syscall.UTF16PtrFromString(filename) | ||
if err != nil { | ||
return false, err | ||
} | ||
attributes, err := syscall.GetFileAttributes(pointer) | ||
if err != nil { | ||
return false, err | ||
} | ||
return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil | ||
} |
CaptainCarpensir marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package file | ||
|
||
import ( | ||
"io/fs" | ||
"path/filepath" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
) | ||
|
||
// RecursiveWatcher wraps an fsnotify Watcher to recursively watch all files in a directory. | ||
type RecursiveWatcher struct { | ||
fsnotifyWatcher *fsnotify.Watcher | ||
done chan struct{} | ||
closed bool | ||
events chan fsnotify.Event | ||
errors chan error | ||
} | ||
|
||
// NewRecursiveWatcher returns a RecursiveWatcher which notifies when changes are made to files inside a recursive directory tree. | ||
func NewRecursiveWatcher(buffer uint) (*RecursiveWatcher, error) { | ||
watcher, err := fsnotify.NewBufferedWatcher(buffer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rw := &RecursiveWatcher{ | ||
events: make(chan fsnotify.Event, buffer), | ||
errors: make(chan error), | ||
fsnotifyWatcher: watcher, | ||
done: make(chan struct{}), | ||
closed: false, | ||
} | ||
|
||
go rw.start() | ||
|
||
return rw, nil | ||
} | ||
|
||
// Add recursively adds a directory tree to the list of watched files. | ||
func (rw *RecursiveWatcher) Add(path string) error { | ||
if rw.closed { | ||
return fsnotify.ErrClosed | ||
} | ||
return filepath.WalkDir(path, func(p string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
// swallow error from WalkDir, don't attempt to add to watcher. | ||
return nil | ||
} | ||
if d.IsDir() { | ||
return rw.fsnotifyWatcher.Add(p) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
// Events returns the events channel. | ||
func (rw *RecursiveWatcher) Events() <-chan fsnotify.Event { | ||
return rw.events | ||
} | ||
|
||
// Errors returns the errors channel. | ||
func (rw *RecursiveWatcher) Errors() <-chan error { | ||
return rw.errors | ||
} | ||
|
||
// Close closes the RecursiveWatcher. | ||
func (rw *RecursiveWatcher) Close() error { | ||
if !rw.closed { | ||
rw.closed = true | ||
close(rw.done) | ||
return rw.fsnotifyWatcher.Close() | ||
} | ||
return nil | ||
CaptainCarpensir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (rw *RecursiveWatcher) start() { | ||
for { | ||
select { | ||
case <-rw.done: | ||
close(rw.events) | ||
close(rw.errors) | ||
return | ||
case event := <-rw.fsnotifyWatcher.Events: | ||
// handle recursive watch | ||
switch event.Op { | ||
case fsnotify.Create: | ||
if err := rw.Add(event.Name); err != nil { | ||
rw.errors <- err | ||
} | ||
} | ||
|
||
rw.events <- event | ||
case err := <-rw.fsnotifyWatcher.Errors: | ||
rw.errors <- err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
//go:build integration || localintegration | ||
|
||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package file_test | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/copilot-cli/internal/pkg/cli/file" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRecursiveWatcher(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome tests! |
||
var ( | ||
watcher *file.RecursiveWatcher | ||
tmp string | ||
eventsExpected []fsnotify.Event | ||
eventsActual []fsnotify.Event | ||
) | ||
|
||
tmp = os.TempDir() | ||
eventsActual = make([]fsnotify.Event, 0) | ||
eventsExpected = []fsnotify.Event{ | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp), | ||
Op: fsnotify.Create, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp), | ||
Op: fsnotify.Chmod, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp), | ||
Op: fsnotify.Write, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp), | ||
Op: fsnotify.Write, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir", tmp), | ||
Op: fsnotify.Rename, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir2", tmp), | ||
Op: fsnotify.Create, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir", tmp), | ||
Op: fsnotify.Rename, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir2/testfile", tmp), | ||
Op: fsnotify.Rename, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir2/testfile2", tmp), | ||
Op: fsnotify.Create, | ||
}, | ||
{ | ||
Name: fmt.Sprintf("%s/watch/subdir2/testfile2", tmp), | ||
Op: fsnotify.Remove, | ||
}, | ||
} | ||
|
||
t.Run("Setup Watcher", func(t *testing.T) { | ||
err := os.MkdirAll(fmt.Sprintf("%s/watch/subdir", tmp), 0755) | ||
require.NoError(t, err) | ||
|
||
watcher, err = file.NewRecursiveWatcher(uint(len(eventsExpected))) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("Watch", func(t *testing.T) { | ||
// SETUP | ||
err := watcher.Add(fmt.Sprintf("%s/watch", tmp)) | ||
require.NoError(t, err) | ||
|
||
eventsCh := watcher.Events() | ||
errorsCh := watcher.Errors() | ||
|
||
expectEvents := func(t *testing.T, n int) []fsnotify.Event { | ||
receivedEvents := []fsnotify.Event{} | ||
for i := 0; i < n; i++ { | ||
select { | ||
case e := <-eventsCh: | ||
receivedEvents = append(receivedEvents, e) | ||
case <-time.After(time.Second): | ||
} | ||
} | ||
return receivedEvents | ||
} | ||
|
||
// WATCH | ||
file, err := os.Create(fmt.Sprintf("%s/watch/subdir/testfile", tmp)) | ||
require.NoError(t, err) | ||
eventsActual = append(eventsActual, expectEvents(t, 1)...) | ||
|
||
err = os.Chmod(fmt.Sprintf("%s/watch/subdir/testfile", tmp), 0755) | ||
require.NoError(t, err) | ||
eventsActual = append(eventsActual, expectEvents(t, 1)...) | ||
|
||
err = os.WriteFile(fmt.Sprintf("%s/watch/subdir/testfile", tmp), []byte("write to file"), fs.ModeAppend) | ||
require.NoError(t, err) | ||
eventsActual = append(eventsActual, expectEvents(t, 2)...) | ||
|
||
err = file.Close() | ||
require.NoError(t, err) | ||
|
||
err = os.Rename(fmt.Sprintf("%s/watch/subdir", tmp), fmt.Sprintf("%s/watch/subdir2", tmp)) | ||
require.NoError(t, err) | ||
eventsActual = append(eventsActual, expectEvents(t, 3)...) | ||
|
||
err = os.Rename(fmt.Sprintf("%s/watch/subdir2/testfile", tmp), fmt.Sprintf("%s/watch/subdir2/testfile2", tmp)) | ||
require.NoError(t, err) | ||
eventsActual = append(eventsActual, expectEvents(t, 2)...) | ||
|
||
err = os.Remove(fmt.Sprintf("%s/watch/subdir2/testfile2", tmp)) | ||
require.NoError(t, err) | ||
eventsActual = append(eventsActual, expectEvents(t, 1)...) | ||
|
||
// CLOSE | ||
err = watcher.Close() | ||
require.NoError(t, err) | ||
require.Empty(t, errorsCh) | ||
|
||
require.Equal(t, eventsExpected, eventsActual) | ||
}) | ||
|
||
t.Run("Clean", func(t *testing.T) { | ||
err := os.RemoveAll(fmt.Sprintf("%s/watch", tmp)) | ||
require.NoError(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's used in
run_local_test.go
but maybe the package name should be changed.