| 1 |
/* |
| 2 |
Copyright 2020 The Knative Authors |
| 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 depcheck_test demonstrates the usage of depcheck. |
| 18 |
// Tests should be put into the special `_test` package to avoid |
| 19 |
// polluting the dependencies of the package they are testing. |
| 20 |
package depcheck_test |
| 21 |
|
| 22 |
import ( |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/depcheck-test/depcheck-test/depcheck" |
| 27 |
) |
| 28 |
|
| 29 |
const thisPackage = "github.com/depcheck-test/depcheck-test/depcheck" |
| 30 |
const golangXPackages = "golang.org/x/tools/go/packages" |
| 31 |
|
| 32 |
// TestExample doesn't follow the Go Example style because it isn't well |
| 33 |
// suited for libraries needing *testing.T |
| 34 |
func TestExample(t *testing.T) { |
| 35 |
|
| 36 |
// Check that this package doesn't depend on a known heavy package. |
| 37 |
depcheck.AssertNoDependency(t, map[string][]string{ |
| 38 |
"github.com/depcheck-test/depcheck-test/depcheck": {"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"}, |
| 39 |
}) |
| 40 |
|
| 41 |
// Sample failure case, this package depends on golang.org/x/tools/go/packages. |
| 42 |
if err := depcheck.CheckNoDependency("github.com/depcheck-test/depcheck-test/depcheck", []string{"golang.org/x/tools/go/packages"}); err == nil { |
| 43 |
t.Error("CheckNoDependency() = nil, wanted error") |
| 44 |
} else if !strings.Contains(err.Error(), "depends on banned dependency") { |
| 45 |
t.Errorf("CheckNoDependency() = %v, expected to contain: %q", err, "depends on banned dependency") |
| 46 |
} else { |
| 47 |
t.Log("CheckNoDependency() =", err) |
| 48 |
} |
| 49 |
|
| 50 |
// For small packages, it can make the most sense to curate |
| 51 |
// the external dependencies very carefully. |
| 52 |
depcheck.AssertOnlyDependencies(t, map[string][]string{ |
| 53 |
"github.com/depcheck-test/depcheck-test/testdata": {"fmt"}, |
| 54 |
}) |
| 55 |
|
| 56 |
// Sample failure case, doesn't include transitive dependencies! |
| 57 |
if err := depcheck.CheckOnlyDependencies(thisPackage, map[string]struct{}{ |
| 58 |
"fmt": {}, |
| 59 |
"sort": {}, |
| 60 |
"strings": {}, |
| 61 |
"testing": {}, |
| 62 |
"golang.org/x/tools/go/packages": {}, |
| 63 |
}); err == nil { |
| 64 |
t.Error("CheckOnlyDependencies() = nil, wanted error") |
| 65 |
} else if !strings.Contains(err.Error(), "golang.org/x/tools/go/gcexportdata") { |
| 66 |
t.Errorf("CheckOnlyDependencies() = %v, expected to contain: %v", err, "golang.org/x/tools/go/gcexportdata") |
| 67 |
} |
| 68 |
} |