github.com/google/osv-scalibr@v0.4.1/enricher/govulncheck/source/internal/url/url_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  // Derived from https://github.com/golang/go/blob/7c2b69080a0b9e35174cc9c93497b6e7176f8275/src/cmd/go/internal/web/url.go
    16  // TODO(golang.org/issue/32456): If accepted, move these functions into the
    17  // net/url package.
    18  //
    19  // Copyright 2023 The Go Authors. All rights reserved.
    20  // Use of this source code is governed by a BSD-style
    21  // license that can be found in the LICENSE file.
    22  package url_test
    23  
    24  import (
    25  	"testing"
    26  
    27  	"github.com/google/osv-scalibr/enricher/govulncheck/source/internal/url"
    28  )
    29  
    30  // Code copied from https://github.com/golang/go/blob/7c2b69080a0b9e35174cc9c93497b6e7176f8275/src/cmd/go/internal/web/url_test.go
    31  
    32  func TestURLFromFilePath(t *testing.T) {
    33  	for _, tc := range urlTests {
    34  		if tc.filePath == "" {
    35  			continue
    36  		}
    37  
    38  		t.Run(tc.filePath, func(t *testing.T) {
    39  			u, err := url.FromFilePath(tc.filePath)
    40  			if err != nil {
    41  				if err.Error() == tc.wantErr {
    42  					return
    43  				}
    44  				if tc.wantErr == "" {
    45  					t.Fatalf("urlFromFilePath(%v): %v; want <nil>", tc.filePath, err)
    46  				} else {
    47  					t.Fatalf("urlFromFilePath(%v): %v; want %s", tc.filePath, err, tc.wantErr)
    48  				}
    49  			}
    50  
    51  			if tc.wantErr != "" {
    52  				t.Fatalf("urlFromFilePath(%v) = <nil>; want error: %s", tc.filePath, tc.wantErr)
    53  			}
    54  
    55  			wantURL := tc.url
    56  			if tc.canonicalURL != "" {
    57  				wantURL = tc.canonicalURL
    58  			}
    59  			if u.String() != wantURL {
    60  				t.Errorf("urlFromFilePath(%v) = %v; want %s", tc.filePath, u, wantURL)
    61  			}
    62  		})
    63  	}
    64  }