github.com/mhristof/semver@v0.17.1/tag/main_test.go (about)

     1  package tag
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/MakeNowJust/heredoc"
    11  	log "github.com/sirupsen/logrus"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestGet(t *testing.T) {
    17  	cases := []struct {
    18  		name string
    19  		tags []string
    20  		exp  []string
    21  	}{
    22  		{
    23  			name: "simple tags",
    24  			tags: []string{
    25  				"v1.0.0",
    26  				"v1.2.0",
    27  			},
    28  			exp: []string{
    29  				"v1.0.0",
    30  				"v1.2.0",
    31  			},
    32  		},
    33  		{
    34  			name: "skip non semver tags",
    35  			tags: []string{
    36  				"v1.0.0",
    37  				"not-semver",
    38  				"v1.2.0",
    39  			},
    40  			exp: []string{
    41  				"v1.0.0",
    42  				"v1.2.0",
    43  			},
    44  		},
    45  	}
    46  
    47  	for _, test := range cases {
    48  		folder, err := ioutil.TempDir("", "sampledir")
    49  		if err != nil {
    50  			log.Fatal(err)
    51  		}
    52  		defer os.Remove(folder)
    53  
    54  		Eval(fmt.Sprintf("git -C %s init", folder))
    55  		Eval(fmt.Sprintf("touch %s/test", folder))
    56  		Eval(fmt.Sprintf("git -C %s add .", folder))
    57  		Eval(fmt.Sprintf("git -C %s commit -m init", folder))
    58  
    59  		for _, tag := range test.tags {
    60  			Eval(fmt.Sprintf("git -C %s tag %s", folder, tag))
    61  		}
    62  
    63  		tags := Get(folder)
    64  
    65  		assert.Equal(t, tags, test.exp, test.name)
    66  
    67  	}
    68  }
    69  
    70  func TestIncrement(t *testing.T) {
    71  	cases := []struct {
    72  		name    string
    73  		version string
    74  		major   bool
    75  		minor   bool
    76  		patch   bool
    77  		exp     string
    78  	}{
    79  		{
    80  			name:    "increase major",
    81  			version: "v1.1.1",
    82  			major:   true,
    83  			exp:     "v2.0.0",
    84  		},
    85  		{
    86  			name:    "increase minor",
    87  			version: "v1.1.1",
    88  			minor:   true,
    89  			exp:     "v1.2.0",
    90  		},
    91  		{
    92  			name:    "increase patch",
    93  			version: "v1.1.1",
    94  			patch:   true,
    95  			exp:     "v1.1.2",
    96  		},
    97  	}
    98  
    99  	for _, test := range cases {
   100  		assert.Equal(t, test.exp, Increment(test.version, test.major, test.minor, test.patch), test.name)
   101  	}
   102  }
   103  
   104  func tmpDir(t *testing.T) string {
   105  	dir, err := ioutil.TempDir("", "semver")
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	return dir
   110  }
   111  
   112  func TestFindNext(t *testing.T) {
   113  	cases := []struct {
   114  		name  string
   115  		cmds  []string
   116  		repo  string
   117  		patch bool
   118  		minor bool
   119  		major bool
   120  	}{
   121  		{
   122  			name: "next bug",
   123  			cmds: strings.Split(heredoc.Doc(`
   124  				git init
   125  				git commit --allow-empty -m initial.import
   126  				git tag v0.1.0
   127  				git commit --allow-empty -m bug:-test`), "\n"),
   128  			repo:  tmpDir(t),
   129  			patch: true,
   130  		},
   131  		{
   132  			name: "next minor",
   133  			cmds: strings.Split(heredoc.Doc(`
   134  				git init
   135  				git commit --allow-empty -m initial.import
   136  				git tag v0.1.0
   137  				git commit --allow-empty -m feature:-test
   138  				git commit --allow-empty -m bug:-test`), "\n"),
   139  			repo:  tmpDir(t),
   140  			minor: true,
   141  		},
   142  		{
   143  			name: "next major",
   144  			cmds: strings.Split(heredoc.Doc(`
   145  				git init
   146  				git commit --allow-empty -m initial.import
   147  				git tag v0.1.0
   148  				git commit --allow-empty -m feat!:test
   149  				git commit --allow-empty -m 'bug:-test`), "\n"),
   150  			repo:  tmpDir(t),
   151  			major: true,
   152  		},
   153  		{
   154  			name: "next major with scope",
   155  			cmds: strings.Split(heredoc.Doc(`
   156  				git init
   157  				git commit --allow-empty -m initial.import
   158  				git tag v0.1.0
   159  				git commit --allow-empty -m feat(foo)!:test
   160  				git commit --allow-empty -m bug:-test`), "\n"),
   161  			repo:  tmpDir(t),
   162  			major: true,
   163  		},
   164  		{
   165  			name: "cannot determine next version",
   166  			cmds: strings.Split(heredoc.Doc(`
   167  				git init
   168  				git commit --allow-empty -m initial.import
   169  				git tag v0.1.0
   170  				git commit --allow-empty -m test
   171  				git commit --allow-empty -m test`), "\n"),
   172  			repo: tmpDir(t),
   173  		},
   174  	}
   175  
   176  	for _, test := range cases {
   177  		err := os.Chdir(test.repo)
   178  		if err != nil {
   179  			t.Fatal(err)
   180  		}
   181  
   182  		for _, c := range test.cmds {
   183  			_, err := Eval(c)
   184  			if err != nil {
   185  				t.Fatal(err, c)
   186  			}
   187  		}
   188  
   189  		major, minor, patch := FindNext("v0.1.0")
   190  
   191  		assert.Equal(t, test.patch, patch, test.name+" patch")
   192  		assert.Equal(t, test.minor, minor, test.name+" minor")
   193  		assert.Equal(t, test.major, major, test.name+" major")
   194  	}
   195  }