1 /* 2 Copyright 2020 Google LLC 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 https://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testutils 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 ) 24 25 // FindTests finds all files matching the given pattern. 26 // It changes the working directory to `directory`, and returns a function 27 // to call to change back to the current directory. 28 // This allows tests to assert on alias finding between absolute and relative labels. 29 func FindTests(t *testing.T, directory, pattern string) ([]string, func()) { 30 wd, err := os.Getwd() 31 if err != nil { 32 t.Fatal(err) 33 } 34 if err := os.Chdir(filepath.Join(os.Getenv("TEST_SRCDIR"), os.Getenv("TEST_WORKSPACE"), directory)); err != nil { 35 t.Fatal(err) 36 } 37 files, err := filepath.Glob(pattern) 38 if err != nil { 39 t.Fatal(err) 40 } 41 if len(files) == 0 { 42 t.Fatal("Didn't find any test cases") 43 } 44 return files, func() { os.Chdir(wd) } 45 } 46