github.com/ETCDEVTeam/janus@v0.2.4-0.20180611132348-f6c8fba730fa/gitvv/gitvv_test.go (about)

     1  package gitvv
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  const testDir = "testdata"
    12  
    13  var (
    14  	baseProjectDir string
    15  	noTagsDir      string
    16  	onTagDir       string
    17  	aboveTagDir    string
    18  )
    19  
    20  // Initialize multiple repo testdirs.
    21  // Test repos have different git states
    22  func TestMain(m *testing.M) {
    23  	projectDir, e := os.Getwd()
    24  	if e != nil {
    25  		log.Fatalln(e)
    26  	}
    27  	baseProjectDir = projectDir
    28  	noTagsDir = filepath.Join(baseProjectDir, testDir, "no-tags")
    29  	onTagDir = filepath.Join(baseProjectDir, testDir, "on-tag")
    30  	aboveTagDir = filepath.Join(baseProjectDir, testDir, "above-tag")
    31  	os.Exit(m.Run())
    32  }
    33  
    34  func Test_getLastTag(t *testing.T) {
    35  	table := []struct {
    36  		dir   string
    37  		wants string
    38  		wantb bool
    39  	}{
    40  		{noTagsDir, "", false},
    41  		{aboveTagDir, "v0.0.1", true},
    42  		{onTagDir, "v0.0.1", true},
    43  	}
    44  
    45  	for _, repo := range table {
    46  		if e := os.Chdir(repo.dir); e != nil {
    47  			t.Fatal(e)
    48  		}
    49  		// Print that we're in the right directory.
    50  		cwd, e := os.Getwd()
    51  		if e != nil {
    52  			t.Error(e)
    53  		}
    54  		t.Log(cwd)
    55  
    56  		// Clear cache
    57  		cacheLastTagName = ""
    58  
    59  		tag, ok := getLastTag(repo.dir)
    60  		if ok != repo.wantb {
    61  			t.Errorf("got: %v, want: %v", ok, repo.wantb)
    62  		}
    63  		if tag != repo.wants {
    64  			t.Errorf("got: %v, want: %v", tag, repo.wants)
    65  		}
    66  
    67  		if e := os.Chdir(baseProjectDir); e != nil {
    68  			t.Fatal(e)
    69  		}
    70  	}
    71  }
    72  
    73  func Test_getTagIfTagOnHEADCommit(t *testing.T) {
    74  	table := []struct {
    75  		dir   string
    76  		wants string
    77  		wantb bool
    78  	}{
    79  		{noTagsDir, "", false},
    80  		{aboveTagDir, "", false},
    81  		{onTagDir, "v0.0.1", true},
    82  	}
    83  
    84  	for _, repo := range table {
    85  		if e := os.Chdir(repo.dir); e != nil {
    86  			t.Fatal(e)
    87  		}
    88  		// Print that we're in the right directory.
    89  		cwd, e := os.Getwd()
    90  		if e != nil {
    91  			t.Error(e)
    92  		}
    93  		t.Log(cwd)
    94  
    95  		// Clear cache
    96  		cacheLastTagName = ""
    97  
    98  		tag, ok := getTagIfTagOnHEADCommit(repo.dir)
    99  		if ok != repo.wantb {
   100  			t.Errorf("got: %v, want: %v", ok, repo.wantb)
   101  		}
   102  		if tag != repo.wants {
   103  			t.Errorf("got: %v, want: %v", tag, repo.wants)
   104  		}
   105  
   106  		if e := os.Chdir(baseProjectDir); e != nil {
   107  			t.Fatal(e)
   108  		}
   109  	}
   110  }
   111  
   112  func Test_getHEADHash(t *testing.T) {
   113  	table := []struct {
   114  		dir        string
   115  		want       string
   116  		hashLength int
   117  	}{
   118  		{noTagsDir, "8673a80f120d8e11d607f1580da41c717e13863f", 7},
   119  		{noTagsDir, "8673a80f120d8e11d607f1580da41c717e13863f", 9},
   120  		{noTagsDir, "8673a80f120d8e11d607f1580da41c717e13863f", 2},
   121  		{aboveTagDir, "fe53b1e838d2fa761b3ce11d9fec683209f093a4", 7},
   122  		{onTagDir, "e35b683e9d2b32c444976484472980582b4c68a9", 7},
   123  	}
   124  
   125  	for _, repo := range table {
   126  		if e := os.Chdir(repo.dir); e != nil {
   127  			t.Fatal(e)
   128  		}
   129  		// Print that we're in the right directory.
   130  		cwd, e := os.Getwd()
   131  		if e != nil {
   132  			t.Error(e)
   133  		}
   134  		t.Log(cwd)
   135  
   136  		// Clear cache
   137  		cacheHEADHash = ""
   138  
   139  		h := getHEADHash(repo.hashLength, repo.dir)
   140  		if len(h) != repo.hashLength {
   141  			t.Errorf("want: %d, got: %d, h: %s", repo.hashLength, len(h), h)
   142  		}
   143  		if !isHash(h) {
   144  			t.Error("unexpected: not a hash")
   145  		}
   146  
   147  		if h != repo.want[:repo.hashLength] {
   148  			t.Errorf("unexpected hash: want: %s, got: %s", repo.want, h)
   149  		}
   150  
   151  		if e := os.Chdir(baseProjectDir); e != nil {
   152  			t.Fatal(e)
   153  		}
   154  	}
   155  }
   156  
   157  func Test_getCommitCountFrom(t *testing.T) {
   158  	table := []struct {
   159  		dir     string
   160  		fromTag string
   161  		want    string
   162  	}{
   163  		{noTagsDir, "", "1"},
   164  		{noTagsDir, "v9.9.9", "0"},
   165  		{aboveTagDir, "", "3"},
   166  		{aboveTagDir, "v0.0.1", "1"},
   167  		{onTagDir, "", "1"},
   168  		{onTagDir, "v0.0.1", "0"},
   169  	}
   170  
   171  	for _, repo := range table {
   172  		if e := os.Chdir(repo.dir); e != nil {
   173  			t.Fatal(e)
   174  		}
   175  		// Print that we're in the right directory.
   176  		cwd, e := os.Getwd()
   177  		if e != nil {
   178  			t.Error(e)
   179  		}
   180  		t.Log(cwd)
   181  
   182  		// Clear cache
   183  		cacheCommitCount = ""
   184  		cacheCommitCountFromTagName = ""
   185  
   186  		count := getCommitCountFrom(repo.fromTag, repo.dir)
   187  		if count != repo.want {
   188  			t.Errorf("got: %v, want: %v", count, repo.want)
   189  		}
   190  
   191  		if e := os.Chdir(baseProjectDir); e != nil {
   192  			t.Fatal(e)
   193  		}
   194  	}
   195  }
   196  
   197  func Test_getB(t *testing.T) {
   198  	table := []struct {
   199  		dir   string
   200  		wants string
   201  		wantb bool
   202  	}{
   203  		{noTagsDir, "", false},
   204  		{aboveTagDir, "101", true},
   205  		{onTagDir, "100", true},
   206  	}
   207  
   208  	for _, repo := range table {
   209  		if e := os.Chdir(repo.dir); e != nil {
   210  			t.Fatal(e)
   211  		}
   212  		// Print that we're in the right directory.
   213  		cwd, e := os.Getwd()
   214  		if e != nil {
   215  			t.Error(e)
   216  		}
   217  		t.Log(cwd)
   218  
   219  		// Clear cache
   220  		cacheCommitCount = ""
   221  		cacheCommitCountFromTagName = ""
   222  		cacheLastTagName = ""
   223  
   224  		b, ok := getB(repo.dir)
   225  		if ok != repo.wantb {
   226  			t.Errorf("got: %v, want: %v", ok, repo.wantb)
   227  		}
   228  		if b != repo.wants {
   229  			t.Errorf("got: %v, want: %v", b, repo.wants)
   230  		}
   231  
   232  		if e := os.Chdir(baseProjectDir); e != nil {
   233  			t.Fatal(e)
   234  		}
   235  	}
   236  }
   237  
   238  func TestGetVersion(t *testing.T) {
   239  	table := []struct {
   240  		dir    string
   241  		format string
   242  		want   string
   243  	}{
   244  		{noTagsDir, "v", "v"},
   245  		{onTagDir, "v", "v"},
   246  		{aboveTagDir, "v", "v"},
   247  
   248  		{noTagsDir, "v%M.%m.%P", "v?.?.?"},
   249  		{onTagDir, "v%M.%m.%P", "v0.0.1"},
   250  		{aboveTagDir, "v%M.%m.%P", "v0.0.1"},
   251  
   252  		{noTagsDir, "%C", "1"},
   253  		{onTagDir, "%C", "0"},
   254  		{aboveTagDir, "%C", "1"},
   255  
   256  		{noTagsDir, "%S", "8673a80f120d8e11d607f1580da41c717e13863f"[:defaultHashLength]},
   257  		{onTagDir, "%S", "e35b683e9d2b32c444976484472980582b4c68a9"[:defaultHashLength]},
   258  		{aboveTagDir, "%S", "fe53b1e838d2fa761b3ce11d9fec683209f093a4"[:defaultHashLength]},
   259  
   260  		{noTagsDir, "%S4", "8673a80f120d8e11d607f1580da41c717e13863f"[:4]},
   261  		{onTagDir, "%S5", "e35b683e9d2b32c444976484472980582b4c68a9"[:5]},
   262  		{aboveTagDir, "%S6", "fe53b1e838d2fa761b3ce11d9fec683209f093a4"[:6]},
   263  
   264  		{noTagsDir, "v%M.%m.%P+%C-%S5", "v?.?.?+1-8673a"},
   265  		{onTagDir, "v%M.%m.%P+%C-%S5", "v0.0.1+0-e35b6"},
   266  		{aboveTagDir, "v%M.%m.%P+%C-%S5", "v0.0.1+1-fe53b"},
   267  
   268  		{noTagsDir, "TAG_OR_NIGHTLY", "v?.?.?+1-8673a80"},
   269  		{onTagDir, "TAG_OR_NIGHTLY", "v0.0.1-e35b683"},
   270  		{aboveTagDir, "TAG_OR_NIGHTLY", "v0.0.1+1-fe53b1e"},
   271  
   272  		{noTagsDir, "v%M.%m.%B-%S5", "v?.?.?-8673a"},
   273  		{onTagDir, "v%M.%m.%B-%S5", "v0.0.100-e35b6"},
   274  		{aboveTagDir, "v%M.%m.%B-%S5", "v0.0.101-fe53b"},
   275  	}
   276  
   277  	for _, repo := range table {
   278  		if e := os.Chdir(repo.dir); e != nil {
   279  			t.Fatal(e)
   280  		}
   281  		// Print that we're in the right directory.
   282  		cwd, e := os.Getwd()
   283  		if e != nil {
   284  			t.Error(e)
   285  		}
   286  		t.Log(cwd)
   287  
   288  		// Clear cache
   289  		cacheHEADHash = ""
   290  		cacheCommitCount = ""
   291  		cacheLastTagName = ""
   292  		cacheCommitCountFromTagName = ""
   293  
   294  		got := GetVersion(repo.format, repo.dir)
   295  		if got != repo.want {
   296  			t.Errorf("got: %v, want: %v", got, repo.want)
   297  		}
   298  
   299  		if e := os.Chdir(baseProjectDir); e != nil {
   300  			t.Fatal(e)
   301  		}
   302  	}
   303  }
   304  
   305  // Test helpers
   306  //
   307  func Test_isHash(t *testing.T) {
   308  	table := []struct {
   309  		hash string
   310  		ok   bool
   311  	}{
   312  		{"asdf123", true},
   313  		{"zxcv123", false},
   314  		{"gasdf123", true},
   315  		{"gqwer123", false},
   316  		{"--nope", false},
   317  		{"v0.1.5", false},
   318  	}
   319  
   320  	for _, tt := range table {
   321  		if got := isHash(tt.hash); got != tt.ok {
   322  			t.Errorf("hash: %s, got: %v, want: %v", tt.hash, got, tt.ok)
   323  		}
   324  	}
   325  }
   326  
   327  func Test_parseSemverFromTag(t *testing.T) {
   328  	table := []struct {
   329  		s    string
   330  		want []string
   331  	}{
   332  		{"v0.1.7", []string{"0", "1", "7"}},
   333  		{"0.1.7", []string{"0", "1", "7"}},
   334  	}
   335  
   336  	for _, tt := range table {
   337  		if got := parseSemverFromTag(tt.s); strings.Join(got, "") != strings.Join(tt.want, "") {
   338  			t.Errorf("tag: %s, got: %v, want: %v", tt.s, got, tt.want)
   339  		}
   340  	}
   341  }
   342  
   343  func Test_parseHashLength(t *testing.T) {
   344  	table := []struct {
   345  		s     string
   346  		wantl int
   347  		wante error
   348  	}{
   349  		{"v%M.%m.%P+%C-%S", defaultHashLength, nil}, // test in big format string w/o specifier
   350  		{"v%M.%m.%P+%C-%S8", 8, nil},                // test in big format string with specifier
   351  		{"%S2", 2, nil},                             // test alone
   352  		{"v%S2", 2, nil},                            // test irrelevant prefix
   353  		{"%S109", 109, nil},                         // test not dependent on actual length of hash
   354  		{"%s109", defaultHashLength, nil},           // test not %S (no matching format)
   355  		{"%S17%S11", 17, nil},                       // test only works for single occurrence
   356  	}
   357  
   358  	for _, tt := range table {
   359  		i, e := parseHashLength(tt.s)
   360  		if e != tt.wante {
   361  			t.Fatalf("unexpected error: %v", e)
   362  		}
   363  		if i != tt.wantl {
   364  			t.Errorf("want: %v, got: %v", tt.wantl, i)
   365  		}
   366  	}
   367  }