github.com/moby/docker@v26.1.3+incompatible/internal/mod/mod_test.go (about)

     1  package mod
     2  
     3  import (
     4  	"runtime/debug"
     5  	"testing"
     6  )
     7  
     8  func TestModuleVersion(t *testing.T) {
     9  	tests := []struct {
    10  		name        string
    11  		module      string
    12  		biContent   string
    13  		wantVersion string
    14  	}{
    15  		{
    16  			name: "returns empty string if build information not available",
    17  			biContent: `
    18  go	go1.20.3
    19  path	github.com/docker/docker/builder/builder-next/worker
    20  mod	github.com/docker/docker	(devel)
    21  			`,
    22  			module:      "github.com/moby/buildkit",
    23  			wantVersion: "",
    24  		},
    25  		{
    26  			name: "returns the version of buildkit dependency",
    27  			biContent: `
    28  go	go1.20.3
    29  path	github.com/docker/docker/builder/builder-next/worker
    30  mod	github.com/docker/docker	(devel)
    31  dep	github.com/moby/buildkit	v0.11.5	h1:JZvvWzulcnA2G4c/gJiSIqKDUoBjctYw2WMuS+XJexU=
    32  			`,
    33  			module:      "github.com/moby/buildkit",
    34  			wantVersion: "v0.11.5",
    35  		},
    36  		{
    37  			name: "returns the replaced version of buildkit dependency",
    38  			biContent: `
    39  go	go1.20.3
    40  path	github.com/docker/docker/builder/builder-next/worker
    41  mod	github.com/docker/docker	(devel)
    42  dep	github.com/moby/buildkit	v0.11.5	h1:JZvvWzulcnA2G4c/gJiSIqKDUoBjctYw2WMuS+XJexU=
    43  =>	github.com/moby/buildkit	v0.12.0	h1:3YO8J4RtmG7elEgaWMb4HgmpS2CfY1QlaOz9nwB+ZSs=
    44  			`,
    45  			module:      "github.com/moby/buildkit",
    46  			wantVersion: "v0.12.0",
    47  		},
    48  		{
    49  			name: "returns the base version of pseudo version",
    50  			biContent: `
    51  go	go1.20.3
    52  path	github.com/docker/docker/builder/builder-next/worker
    53  mod	github.com/docker/docker	(devel)
    54  dep	github.com/moby/buildkit	v0.10.7-0.20230306143919-70f2ad56d3e5	h1:JZvvWzulcnA2G4c/gJiSIqKDUoBjctYw2WMuS+XJexU=
    55  			`,
    56  			module:      "github.com/moby/buildkit",
    57  			wantVersion: "v0.10.6+70f2ad56d3e5",
    58  		},
    59  	}
    60  
    61  	for _, tt := range tests {
    62  		tt := tt
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			bi, err := debug.ParseBuildInfo(tt.biContent)
    65  			if err != nil {
    66  				t.Fatalf("failed to parse build info: %v", err)
    67  			}
    68  			if gotVersion := moduleVersion(tt.module, bi); gotVersion != tt.wantVersion {
    69  				t.Errorf("moduleVersion() = %v, want %v", gotVersion, tt.wantVersion)
    70  			}
    71  		})
    72  	}
    73  }