1// Copyright 2020 Google Inc. 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 classifier 16 17import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21) 22 23func TestInitTrace(t *testing.T) { 24 tests := []struct { 25 name, licFlag, phaseFlag string 26 expectedLics map[string]bool 27 expectedPhases map[string]bool 28 }{ 29 { 30 name: "empty flags", 31 licFlag: "", 32 phaseFlag: "", 33 expectedLics: map[string]bool{}, 34 expectedPhases: map[string]bool{}, 35 }, 36 { 37 name: "single entries", 38 licFlag: "one_license", 39 phaseFlag: "setup", 40 expectedLics: map[string]bool{"one_license": true}, 41 expectedPhases: map[string]bool{"setup": true}, 42 }, 43 { 44 name: "multiple entries", 45 licFlag: "one_license,two_license", 46 phaseFlag: "setup,teardown", 47 expectedLics: map[string]bool{"one_license": true, "two_license": true}, 48 expectedPhases: map[string]bool{"setup": true, "teardown": true}, 49 }, 50 } 51 52 for _, test := range tests { 53 t.Run(test.name, func(t *testing.T) { 54 tc := &TraceConfiguration{ 55 TraceLicenses: test.licFlag, 56 TracePhases: test.phaseFlag, 57 } 58 tc.init() 59 if !cmp.Equal(tc.traceLicenses, test.expectedLics) { 60 t.Errorf("got %v want %v", traceLicenses, test.expectedLics) 61 } 62 if !cmp.Equal(tc.tracePhases, test.expectedPhases) { 63 t.Errorf("got %v want %v", traceLicenses, test.expectedPhases) 64 } 65 }) 66 } 67} 68 69func TestPhaseWildcardMatching(t *testing.T) { 70 tests := []struct { 71 name string 72 phases string 73 hits []string 74 misses []string 75 }{ 76 { 77 name: "exact match", 78 phases: "scoring", 79 hits: []string{"scoring"}, 80 misses: []string{"tokenize"}, 81 }, 82 { 83 name: "all match", 84 phases: "*", 85 hits: []string{"scoring", "tokenize"}, 86 misses: nil, 87 }, 88 } 89 for _, test := range tests { 90 t.Run(test.name, func(t *testing.T) { 91 tc := &TraceConfiguration{ 92 TracePhases: test.phases, 93 } 94 tc.init() 95 for _, h := range test.hits { 96 if !tc.shouldTrace(h) { 97 t.Errorf("unexpected miss on phase %s", h) 98 } 99 } 100 101 for _, m := range test.misses { 102 if tc.shouldTrace(m) { 103 t.Errorf("unexpected hit on phase %s", m) 104 } 105 } 106 }) 107 } 108} 109 110func TestLicenseWildcardMatching(t *testing.T) { 111 tests := []struct { 112 name string 113 licenses string 114 hits []string 115 misses []string 116 }{ 117 { 118 name: "exact match", 119 hits: []string{"GPL-2.0"}, 120 misses: []string{"Apache-2.0", "GPL-3.0"}, 121 licenses: "GPL-2.0", 122 }, 123 { 124 name: "prefix match", 125 hits: []string{"GPL-2.0", "GPL-3.0"}, 126 misses: []string{"Apache-2.0"}, 127 licenses: "GPL-*", 128 }, 129 { 130 name: "all match", 131 hits: []string{"GPL-2.0", "GPL-3.0", "Apache-2.0"}, 132 misses: nil, 133 licenses: "*", 134 }, 135 } 136 137 for _, test := range tests { 138 t.Run(test.name, func(t *testing.T) { 139 tc := &TraceConfiguration{ 140 TraceLicenses: test.licenses, 141 } 142 tc.init() 143 for _, h := range test.hits { 144 if !tc.isTraceLicense(h) { 145 t.Errorf("unexpected miss on license %s", h) 146 } 147 } 148 149 for _, m := range test.misses { 150 if tc.isTraceLicense(m) { 151 t.Errorf("unexpected hit on license %s", m) 152 } 153 } 154 }) 155 } 156} 157 158// The TraceConfiguration is only explicitly initialized and propagated to a 159// variety of helper structs. For convenience, we just make it work safely in 160// the case the pointer is nil. This test ensures that behavior so users of the 161// TraceConfiguration don't need to explicitly initialize it. 162func TestNilSafety(t *testing.T) { 163 var tc *TraceConfiguration 164 tc.init() 165 if tc.isTraceLicense("GPL-2.0") { 166 t.Errorf("unexpected hit on license") 167 } 168 169 if tc.shouldTrace("scoring") { 170 t.Errorf("unexpected hit on phase") 171 } 172} 173