mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-22 08:14:48 -06:00
0d675ca7ed
Currently if a fsnotify watcher is specified for a particular directory, we incorrectly also add it's parent directory to be monitored. This happens because the function that determines which paths are to be watched calls `filepath.Dir` on each of them to get their directory. This is the right thing to do for files as their parent directory gets watched, but when done on a directory especially for the top-level directory adds an incorrect directory to be watched. This changes attempts to fix that. Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
42 lines
930 B
Go
42 lines
930 B
Go
// Copyright 2023 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package pathwatcher
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/util/test"
|
|
)
|
|
|
|
func TestWatchPaths(t *testing.T) {
|
|
|
|
fs := map[string]string{
|
|
"/foo/bar/baz.json": "true",
|
|
"/foo/faz/baz.json": "true",
|
|
"/foo/baz.json": "true",
|
|
}
|
|
|
|
expected := []string{
|
|
"/foo", "/foo/bar", "/foo/faz",
|
|
}
|
|
|
|
test.WithTempFS(fs, func(rootDir string) {
|
|
paths, err := getWatchPaths([]string{"prefix:" + rootDir + "/foo"})
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
}
|
|
result := []string{}
|
|
for _, p := range paths {
|
|
result = append(result, filepath.Clean(strings.TrimPrefix(p, rootDir)))
|
|
}
|
|
if !reflect.DeepEqual(expected, result) {
|
|
t.Fatalf("Expected %q but got: %q", expected, result)
|
|
}
|
|
})
|
|
}
|