kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/serving/pipeline/filetree_test.go (about) 1 /* 2 * Copyright 2018 The Kythe Authors. All rights reserved. 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 * http://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 pipeline 18 19 import ( 20 "testing" 21 22 "github.com/apache/beam/sdks/go/pkg/beam" 23 "github.com/apache/beam/sdks/go/pkg/beam/testing/passert" 24 "github.com/apache/beam/sdks/go/pkg/beam/testing/ptest" 25 "github.com/apache/beam/sdks/go/pkg/beam/x/debug" 26 27 scpb "kythe.io/kythe/proto/schema_go_proto" 28 srvpb "kythe.io/kythe/proto/serving_go_proto" 29 spb "kythe.io/kythe/proto/storage_go_proto" 30 ) 31 32 func TestCorpusRoots(t *testing.T) { 33 testNodes := []*scpb.Node{{ 34 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path"}, 35 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 36 }, { 37 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path2"}, 38 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 39 }, { 40 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path3"}, 41 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 42 }, { 43 Source: &spb.VName{Corpus: "corpus", Path: "p/to/file.go"}, 44 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 45 }, { 46 Source: &spb.VName{Corpus: "corpus2", Path: "p/to/file.go"}, 47 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 48 }, { 49 Source: &spb.VName{Corpus: "corpus", Path: "path", Signature: "a0"}, 50 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 51 }, { 52 Source: &spb.VName{Corpus: "corpus", Path: "path", Signature: "a1"}, 53 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 54 Fact: []*scpb.Fact{{ 55 Name: &scpb.Fact_KytheName{scpb.FactName_BUILD_CONFIG}, 56 Value: []byte("test-build-config"), 57 }}, 58 }, { 59 Source: &spb.VName{Corpus: "corpus", Path: "p/to/file.go", Signature: "a0"}, 60 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 61 Fact: []*scpb.Fact{{ 62 Name: &scpb.Fact_KytheName{scpb.FactName_BUILD_CONFIG}, 63 Value: []byte("test-build-config"), 64 }}, 65 }} 66 67 expected := []*srvpb.CorpusRoots{{ 68 Corpus: []*srvpb.CorpusRoots_Corpus{{ 69 Corpus: "corpus", 70 Root: []string{"", "root"}, 71 BuildConfig: []string{"", "test-build-config"}, 72 }, { 73 Corpus: "corpus2", 74 Root: []string{""}, 75 }}, 76 }} 77 78 p, s, nodes := ptest.CreateList(testNodes) 79 cr := beam.DropKey(s, FromNodes(s, nodes).CorpusRoots()) 80 debug.Print(s, cr) 81 passert.Equals(s, cr, beam.CreateList(s, expected)) 82 83 if err := ptest.Run(p); err != nil { 84 t.Fatalf("Pipeline error: %+v", err) 85 } 86 } 87 88 func TestDirectories(t *testing.T) { 89 testNodes := []*scpb.Node{{ 90 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path"}, 91 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 92 }, { 93 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path", Signature: "a0"}, 94 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 95 Fact: []*scpb.Fact{{ 96 Name: &scpb.Fact_KytheName{scpb.FactName_BUILD_CONFIG}, 97 Value: []byte("test-build-config"), 98 }}, 99 }, { 100 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path", Signature: "a1"}, 101 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 102 Fact: []*scpb.Fact{{ 103 Name: &scpb.Fact_KytheName{scpb.FactName_BUILD_CONFIG}, 104 Value: []byte("test-build-config2"), 105 }}, 106 }, { 107 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path2"}, 108 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 109 }, { 110 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path2", Signature: "a0"}, 111 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 112 // no build-config 113 }, { 114 Source: &spb.VName{Corpus: "corpus", Root: "root", Path: "path3"}, 115 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 116 }, { 117 Source: &spb.VName{Corpus: "corpus", Path: "p/to/file.go"}, 118 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 119 }, { 120 Source: &spb.VName{Corpus: "corpus", Path: "p/to/file.go", Signature: "a0"}, 121 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 122 Fact: []*scpb.Fact{{ 123 Name: &scpb.Fact_KytheName{scpb.FactName_BUILD_CONFIG}, 124 Value: []byte("test-build-config"), 125 }}, 126 }, { 127 Source: &spb.VName{Corpus: "corpus2", Path: "p/to/file.go"}, 128 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 129 }} 130 131 expected := []*srvpb.FileDirectory{{ 132 Entry: []*srvpb.FileDirectory_Entry{{ 133 Kind: srvpb.FileDirectory_DIRECTORY, 134 Name: "p", 135 }}, 136 }, { 137 Entry: []*srvpb.FileDirectory_Entry{{ 138 Kind: srvpb.FileDirectory_DIRECTORY, 139 Name: "to", 140 }}, 141 }, { 142 Entry: []*srvpb.FileDirectory_Entry{{ 143 Kind: srvpb.FileDirectory_FILE, 144 Name: "file.go", 145 }}, 146 }, { 147 Entry: []*srvpb.FileDirectory_Entry{{ 148 Kind: srvpb.FileDirectory_DIRECTORY, 149 Name: "p", 150 BuildConfig: []string{"test-build-config"}, 151 }}, 152 }, { 153 Entry: []*srvpb.FileDirectory_Entry{{ 154 Kind: srvpb.FileDirectory_DIRECTORY, 155 Name: "to", 156 BuildConfig: []string{"test-build-config"}, 157 }}, 158 }, { 159 Entry: []*srvpb.FileDirectory_Entry{{ 160 Kind: srvpb.FileDirectory_FILE, 161 Name: "file.go", 162 BuildConfig: []string{"test-build-config"}, 163 }}, 164 }, { 165 Entry: []*srvpb.FileDirectory_Entry{{ 166 Kind: srvpb.FileDirectory_FILE, 167 Name: "path", 168 BuildConfig: []string{"test-build-config", "test-build-config2"}, 169 }, { 170 Kind: srvpb.FileDirectory_FILE, 171 Name: "path2", 172 BuildConfig: []string{""}, 173 }, { 174 Kind: srvpb.FileDirectory_FILE, 175 Name: "path3", 176 }}, 177 }} 178 179 p, s, nodes := ptest.CreateList(testNodes) 180 dirs := beam.DropKey(s, FromNodes(s, nodes).Directories()) 181 debug.Print(s, dirs) 182 passert.Equals(s, dirs, beam.CreateList(s, expected)) 183 184 if err := ptest.Run(p); err != nil { 185 t.Fatalf("Pipeline error: %+v", err) 186 } 187 }