github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/versioning_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pcommon
     7  
     8  import (
     9  	"github.com/coreos/go-semver/semver"
    10  	"os/exec"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestParseAergoVersion(t *testing.T) {
    16  
    17  	tests := []struct {
    18  		name    string
    19  		args    string
    20  		wantM int
    21  		wantm int
    22  		wantp int
    23  		wantA string
    24  		wantErr bool
    25  	}{
    26  		{"TR1","v0.0.1", 0,0, 1, "",false},
    27  		{"TR1","v1.2.3", 1,2, 3, "",false},
    28  		{"TR1","0.0.1", 0,0, 1, "",false},
    29  		{"TR1","1.2.3", 1,2, 3, "",false},
    30  		{"TDev1","v1.2.2-20-g8905410d", 1,2, 2, "20-g8905410d",false},
    31  		{"TDev2","vd1.2.2-20-g8905410d", 0,0, 0, "",true},
    32  		{"TDev3","vd1.2.2.55", 0,0, 0, "",true},
    33  	}
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			got, err := ParseAergoVersion(tt.args)
    37  			if (err != nil) != tt.wantErr {
    38  				t.Errorf("ParseAergoVersion() error = %v, wantErr %v", err, tt.wantErr)
    39  				return
    40  			}
    41  			if !tt.wantErr {
    42  				if got.Major != int64(tt.wantM) || got.Minor != int64(tt.wantm) ||got.Patch != int64(tt.wantp) {
    43  					t.Errorf("ParseAergoVersion() got = %v, want %v.%v.%v-%v", got.String(), tt.wantM, tt.wantm, tt.wantp, tt.wantA)
    44  				} else if len(tt.wantA) > 0 && got.PreRelease != semver.PreRelease(tt.wantA) {
    45  					t.Errorf("ParseAergoVersion() got = prerelase %v, want %v", got.PreRelease, tt.wantA)
    46  				}
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestCheckVersion(t *testing.T) {
    53  	tests := []struct {
    54  		name string
    55  		arg string
    56  		want bool
    57  	}{
    58  		{"TOld", "0.0.1", false},
    59  		{"TOld2", "v1.2.0", false},
    60  		{"TPre", "1.2.1-30-g8905410d", false},
    61  		{"TMin", "1.2.3", true},
    62  		{"TMin3", "v1.2.3", true},
    63  		{"TMax", "1.2.99", true},
    64  		{"TMax3", "v1.2.99", true},
    65  		{"TSomewhatUnclear", "2.0.0-30-g8905410d", true},
    66  		{"TNew", "2.0.0", false},
    67  	}
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			if got := CheckVersion(tt.arg); got != tt.want {
    71  				t.Errorf("CheckVersion() = %v, want %v", got, tt.want)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestCurrentVersion(t *testing.T) {
    78  	t.Skip("This test is not worked well,")
    79  	var (
    80  		cmdOut []byte
    81  		err    error
    82  	)
    83  	cmdName := "git"
    84  	cmdArgs := []string{"describe", "--tags"}
    85  	if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
    86  		t.Skipf("Skip test since not testable environment: %s", err.Error())
    87  	}
    88  	tagVer := strings.TrimSpace(string(cmdOut))
    89  	_, err = ParseAergoVersion(tagVer)
    90  	if err != nil {
    91  		t.Skipf("Skip test since not testable environment: %s", err.Error())
    92  	}
    93  
    94  	if !CheckVersion(tagVer) {
    95  		t.Fatalf("invalid version filtering setting. build version %v , want between %v (inclusive) and %v (exclusive)",
    96  			tagVer, MinimumAergoVersion, MaximumAergoVersion)
    97  	}
    98  	t.Logf("build version is %v ", tagVer)
    99  
   100  }