github.com/bazelbuild/bazel-watcher@v0.25.2/internal/ibazel/fswatcher/fsevents/fsevents_test.go (about) 1 // Copyright 2022 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build darwin 16 // +build darwin 17 18 package fsevents 19 20 import ( 21 "github.com/google/go-cmp/cmp" 22 "testing" 23 ) 24 25 func TestFindCommonRoot(t *testing.T) { 26 tests := []struct { 27 in []string 28 want []string 29 }{ 30 // Finds common sub-root of two directories 31 { 32 []string{ 33 "/a/b/c/", 34 "/a/d/", 35 }, 36 []string{"/a/"}, 37 }, 38 // Finds common sub-root of multiple recursive directories 39 { 40 []string{ 41 "/a/b/c", 42 "/a/b/c/e", 43 "/a/b/d/e", 44 "/a/b/d/f", 45 }, 46 []string{"/a/b/"}, 47 }, 48 // Returns the single input. 49 { 50 []string{ 51 "/a/b/c/", 52 }, 53 []string{"/a/b/c/"}, 54 }, 55 // Returns an empty slice if there are no inputs. 56 { 57 []string{}, 58 []string{}, 59 }, 60 } 61 for _, test := range tests { 62 got, err := findCommonRoot(test.in) 63 if err != nil { 64 t.Errorf("unexpected error %v", err.Error()) 65 } 66 if diff := cmp.Diff(got, test.want); diff != "" { 67 t.Errorf("findCommonRoot diff (-got,+want):\n%s", diff) 68 } 69 } 70 } 71 72 func TestNoCommonRootError(t *testing.T) { 73 _, err := findCommonRoot([]string{"/a/", "/b/"}) 74 if err == nil { 75 t.Error("expected error when there is no common root") 76 } 77 }