github.com/google/osv-scalibr@v0.4.1/binary/proto/plugin_test.go (about) 1 // Copyright 2025 Google LLC 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 package proto_test 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/google/osv-scalibr/binary/proto" 22 spb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto" 23 "github.com/google/osv-scalibr/plugin" 24 "google.golang.org/protobuf/testing/protocmp" 25 ) 26 27 func TestPluginStatusToProto(t *testing.T) { 28 testCases := []struct { 29 desc string 30 s *plugin.Status 31 want *spb.PluginStatus 32 }{ 33 { 34 desc: "nil", 35 s: nil, 36 want: nil, 37 }, 38 { 39 desc: "success", 40 s: &plugin.Status{ 41 Name: "test-plugin", 42 Version: 1, 43 Status: &plugin.ScanStatus{ 44 Status: plugin.ScanStatusSucceeded, 45 }, 46 }, 47 want: &spb.PluginStatus{ 48 Name: "test-plugin", 49 Version: 1, 50 Status: &spb.ScanStatus{ 51 Status: spb.ScanStatus_SUCCEEDED, 52 }, 53 }, 54 }, 55 { 56 desc: "converts_file_errors", 57 s: &plugin.Status{ 58 Name: "test-plugin-with-file-errors", 59 Version: 1, 60 Status: &plugin.ScanStatus{ 61 Status: plugin.ScanStatusPartiallySucceeded, 62 FileErrors: []*plugin.FileError{ 63 {FilePath: "file1", ErrorMessage: "error1"}, 64 {FilePath: "file2", ErrorMessage: "error2"}, 65 }, 66 }, 67 }, 68 want: &spb.PluginStatus{ 69 Name: "test-plugin-with-file-errors", 70 Version: 1, 71 Status: &spb.ScanStatus{ 72 Status: spb.ScanStatus_PARTIALLY_SUCCEEDED, 73 FileErrors: []*spb.FileError{ 74 {FilePath: "file1", ErrorMessage: "error1"}, 75 {FilePath: "file2", ErrorMessage: "error2"}, 76 }, 77 }, 78 }, 79 }, 80 { 81 desc: "nil_status", 82 s: &plugin.Status{ 83 Name: "test-plugin", 84 Version: 1, 85 }, 86 want: &spb.PluginStatus{ 87 Name: "test-plugin", 88 Version: 1, 89 }, 90 }, 91 } 92 93 for _, tc := range testCases { 94 t.Run(tc.desc, func(t *testing.T) { 95 got := proto.PluginStatusToProto(tc.s) 96 if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" { 97 t.Fatalf("PluginStatusToProto(%v) returned diff (-want +got):\n%s", tc.s, diff) 98 } 99 100 // Test the reverse conversion for completeness. 101 gotPB := proto.PluginStatusToStruct(got) 102 if diff := cmp.Diff(tc.s, gotPB, protocmp.Transform()); diff != "" { 103 t.Fatalf("PluginStatusToStruct(%v) returned diff (-want +got):\n%s", got, diff) 104 } 105 }) 106 } 107 }