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 15package respipe 16 17import ( 18 "io/ioutil" 19 "os" 20 "path" 21 "reflect" 22 "sort" 23 "testing" 24 25 "context" 26) 27 28func TestEmitPathInfosDir(t *testing.T) { 29 tmpDir, err := ioutil.TempDir("", "") 30 if err != nil { 31 t.Fatalf("%s: make failed: %v", tmpDir, err) 32 } 33 defer func() { 34 if err := os.RemoveAll(tmpDir); err != nil { 35 t.Errorf("%s: could not remove: %v", tmpDir, err) 36 } 37 }() 38 39 touch := func(p string) string { 40 if err := os.MkdirAll(path.Dir(path.Join(tmpDir, p)), 0744); err != nil { 41 t.Fatalf("%s: mkdir failed: %v", p, err) 42 } 43 f, err := os.OpenFile(path.Join(tmpDir, p), os.O_CREATE|os.O_TRUNC, 0644) 44 if err != nil { 45 t.Fatalf("%s: touch failed: %v", p, err) 46 } 47 defer f.Close() 48 return f.Name() 49 } 50 wantPaths := []string{ 51 "values/strings.xml", 52 "values/styles.xml", 53 "layout-land/hello.xml", 54 "layout/hello.xml", 55 "values-v19/styles.xml", 56 "drawable-ldpi/foo.png", 57 "raw/data.xml", 58 "xml/perf.xml", 59 } 60 for i, p := range wantPaths { 61 wantPaths[i] = touch(p) 62 } 63 touch("values/.placeholder") 64 touch("something_random/data.txt") 65 66 ctx, cxlFn := context.WithCancel(context.Background()) 67 defer cxlFn() 68 piC, errC := EmitPathInfosDir(ctx, tmpDir) 69 var gotPaths []string 70Loop: 71 for { 72 select { 73 case p, ok := <-piC: 74 if !ok { 75 break Loop 76 } 77 gotPaths = append(gotPaths, p.Path) 78 case e, ok := <-errC: 79 if !ok { 80 break Loop 81 } 82 t.Fatalf("Unexpected failure: %v", e) 83 84 } 85 } 86 sort.Strings(gotPaths) 87 sort.Strings(wantPaths) 88 if !reflect.DeepEqual(gotPaths, wantPaths) { 89 t.Errorf("EmitPathInfosDir(): %v wanted: %v", gotPaths, wantPaths) 90 } 91 92} 93