package golang_test import ( "os" "strings" "testing" golang "edge-infra.dev/hack/build/ci/leaf/pkg/go" ) func TestDiffParsing(t *testing.T) { tcs := []struct { Description string DiffPath string Targets map[string]struct{} }{ { Description: "Simple deps.bzl diff", DiffPath: "testdata/deps-bzl-diff", Targets: map[string]struct{}{ "@com_github_sourcegraph_go_diff//...": {}, "@in_gopkg_yaml_v3//...": {}, "@com_github_pkg_errors//...": {}, "@com_github_mattn_go_isatty//...": {}, "@com_github_cpuguy83_go_md2man_v2//...": {}, }, }, { Description: "deps.bzl diff with build_file_name", DiffPath: "testdata/deps-bzl-diff-multiple-name-strings", Targets: map[string]struct{}{ "@com_github_envoyproxy_go_control_plane//...": {}, "@com_github_golang_groupcache//...": {}, "@com_github_golang_mock//...": {}, "@com_github_golang_snappy//...": {}, "@com_github_google_martian_v3//...": {}, "@com_github_google_pprof//...": {}, "@com_github_googleapis_gax_go_v2//...": {}, "@com_github_grpc_ecosystem_grpc_gateway//...": {}, "@com_github_rogpeppe_fastuuid//...": {}, "@com_google_cloud_go//...": {}, "@com_google_cloud_go_storage//...": {}, "@org_golang_google_api//...": {}, "@org_golang_google_protobuf//...": {}, "@org_golang_x_lint//...": {}, "@org_golang_x_oauth2//...": {}, "@org_golang_x_sys//...": {}, "@org_golang_x_text//...": {}, "@org_golang_x_tools//...": {}, }, }, { Description: "More complex deps.bzl diff", DiffPath: "testdata/deps-bzl-diff-huge", Targets: map[string]struct{}{ "@com_github_beorn7_perks//...": {}, "@com_github_coreos_bbolt//...": {}, "@com_github_coreos_pkg//...": {}, "@com_github_elazarl_goproxy//...": {}, "@com_github_fsnotify_fsnotify//...": {}, "@com_github_go_logfmt_logfmt//...": {}, "@com_github_gogo_protobuf//...": {}, "@com_github_golang_groupcache//...": {}, "@com_github_golang_protobuf//...": {}, "@com_github_google_gofuzz//...": {}, "@com_github_hashicorp_golang_lru//...": {}, "@com_github_imdario_mergo//...": {}, "@com_github_json_iterator_go//...": {}, "@com_github_onsi_ginkgo//...": {}, "@com_github_onsi_gomega//...": {}, "@com_github_prometheus_client_model//...": {}, "@com_github_prometheus_procfs//...": {}, "@com_github_spf13_cobra//...": {}, "@com_github_spf13_viper//...": {}, "@com_github_tmc_grpc_websocket_proxy//...": {}, "@in_gopkg_yaml_v2//...": {}, "@io_k8s_api//...": {}, "@io_k8s_apimachinery//...": {}, "@io_k8s_apiextensions_apiserver//...": {}, "@io_k8s_apiserver//...": {}, "@io_k8s_client_go//...": {}, "@io_k8s_code_generator//...": {}, "@io_k8s_component_base//...": {}, "@io_k8s_gengo//...": {}, "@io_k8s_kube_openapi//...": {}, "@io_k8s_sigs_controller_runtime//...": {}, "@io_k8s_sigs_controller_tools//...": {}, "@io_k8s_sigs_yaml//...": {}, "@io_k8s_utils//...": {}, "@org_golang_google_genproto//...": {}, "@org_golang_google_grpc//...": {}, "@org_golang_google_protobuf//...": {}, "@org_golang_x_net//...": {}, "@org_golang_x_text//...": {}, "@org_golang_x_tools//...": {}, "@org_uber_go_atomic//...": {}, "@org_golang_x_mod//...": {}, "@com_github_envoyproxy_go_control_plane//...": {}, "@com_github_yuin_goldmark//...": {}, "@com_github_cpuguy83_go_md2man_v2//...": {}, }, }, } for _, tc := range tcs { diffBytes, err := os.ReadFile(tc.DiffPath) if err != nil { t.Error(err) } results := golang.Parse(strings.Split(string(diffBytes), "\n")) t.Logf("Parsed modules: %s", results) if len(results) != len(tc.Targets) { t.Errorf("Parse: Expected %d results, got %v", len(tc.Targets), len(results)) } for _, result := range results { if _, ok := tc.Targets[result]; !ok { t.Errorf("Parse: Expected to find %s in result", result) } } } }