How to use bumpPrerelease method of version Package

Best Testkube code snippet using version.bumpPrerelease

version_test.go

Source:version_test.go Github

copy

Full Screen

1package version2import (3 "fmt"4 "testing"5 "github.com/stretchr/testify/assert"6)7func TestPreReleaseIdentifiersEqual(t *testing.T) {8 assert := assert.New(t)9 testData := []struct {10 version string11 identifiers string12 expected bool13 }{14 {"0.1.0", "", true},15 {"v1.0.0-alpha.0", "alpha", true},16 {"v1.0.0-alpha.0", "alpha.0", true},17 {"v1.0.0-SNAPSHOT", "SNAPSHOT", true},18 }19 for _, tc := range testData {20 v, _ := NewVersion(tc.version)21 assert.Equal(tc.expected, v.PreReleaseIdentifiersEqual(tc.identifiers))22 }23}24func TestGetPreReleaseIncrement(t *testing.T) {25 assert := assert.New(t)26 testData := []struct {27 version string28 expected int29 expectedError bool30 }{31 {"0.1.0", -1, true},32 {"v1.0.0-alpha.0", 0, false},33 {"v1.0.0-alpha.1", 1, false},34 }35 for _, tc := range testData {36 v, _ := NewVersion(tc.version)37 inc, err := v.GetPreReleaseIncrement()38 if tc.expectedError {39 assert.Error(err)40 } else {41 assert.NoError(err)42 assert.Equal(tc.expected, inc)43 }44 }45}46func TestVersionBumpMajor(t *testing.T) {47 assert := assert.New(t)48 testData := []struct {49 data string50 expected string51 }{52 {"0.1.0", "1.0.0"},53 {"v1.0.0-alpha.0", "1.0.0"}, // pre-release < release54 {"1.1.0-alpha.0", "2.0.0"}, // but not if we want to bump major on a minor pre-release55 {"v1.1.1-alpha.0", "2.0.0"}, // but not if we want to bump major on a patch pre-release56 {"v1.0.1-alpha.0", "2.0.0"},57 }58 for _, tc := range testData {59 v1, _ := NewVersion(tc.data)60 expected, _ := NewVersion(tc.expected)61 actual := v1.BumpMajor()62 assert.Equal(expected, actual)63 }64}65func ExampleVersion_BumpMajor() {66 v := Version{Major: 1} // 1.0.067 v2 := v.BumpMajor()68 fmt.Println(v2.String())69 // Output: 2.0.070}71func TestVersionBumpMinor(t *testing.T) {72 assert := assert.New(t)73 testData := []struct {74 data string75 expected string76 }{77 {"0.1.0", "0.2.0"},78 {"1.0.1", "1.1.0"},79 {"v1.1.0-alpha.2", "1.1.0"}, // pre-release < release80 {"v1.0.1-alpha.2", "1.1.0"}, // but not if we want to bump minor on a patch pre-release81 {"1.1.1-alpha.2", "1.2.0"}, // same82 }83 for _, tc := range testData {84 v1, _ := NewVersion(tc.data)85 expected, _ := NewVersion(tc.expected)86 actual := v1.BumpMinor()87 assert.Equal(expected, actual)88 }89}90func ExampleVersion_BumpMinor() {91 v := Version{Major: 1} // 1.0.092 v2 := v.BumpMinor()93 fmt.Println(v2.String())94 // Output: 1.1.095}96func TestVersionBumpPatch(t *testing.T) {97 assert := assert.New(t)98 testData := []struct {99 data string100 expected string101 }{102 {"0.1.0", "0.1.1"},103 {"0.1.0-alpha.0", "0.1.0"}, // pre-release < release104 }105 for _, tc := range testData {106 v1, _ := NewVersion(tc.data)107 expected, _ := NewVersion(tc.expected)108 actual := v1.BumpPatch()109 assert.Equal(expected, actual)110 }111}112func ExampleVersion_BumpPatch() {113 v := Version{Major: 1} // 1.0.0114 v2 := v.BumpPatch()115 fmt.Println(v2.String())116 // Output: 1.0.1117}118func TestNewVersion(t *testing.T) {119 assert := assert.New(t)120 testData := []struct {121 version string122 err bool123 }{124 {"1.2.3", false},125 {"v1.2.3", false},126 {"1.0", true},127 {"v1.0", true},128 {"1", true},129 {"v1", true},130 {"1.2.beta", true},131 {"v1.2.beta", true},132 {"foo", true},133 {"1.2-5", true},134 {"v1.2-5", true},135 {"1.2-beta.5", true},136 {"v1.2-beta.5", true},137 {"\n1.2", true},138 {"\nv1.2", true},139 {"1.2.0-x.Y.0+metadata", false},140 {"v1.2.0-x.Y.0+metadata", false},141 {"1.2.0-x.Y.0+metadata-width-hypen", false},142 {"v1.2.0-x.Y.0+metadata-width-hypen", false},143 {"1.2.3-rc1-with-hypen", false},144 {"v1.2.3-rc1-with-hypen", false},145 {"1.2.3.4", true},146 {"v1.2.3.4", true},147 {"1.2.2147483648", false},148 {"1.2147483648.3", false},149 {"2147483648.3.0", false},150 }151 for _, tc := range testData {152 _, err := NewVersion(tc.version)153 if tc.err && err == nil {154 assert.Fail("expected error for version: %s", tc.version)155 } else if !tc.err && err != nil {156 assert.Fail("error for version %s: %s", tc.version, err)157 }158 }159}160func ExampleNewVersion() {161 NewVersion("1.2.3")162 NewVersion("v1.2.3") // with v prefix163 NewVersion("2.3.5-beta") // pre-release overwritable164 NewVersion("2.3.5-beta.5") // pre-release with index165 NewVersion("2.3.5+metadata") // build-metadata166}167func TestBumpPreRelease(t *testing.T) {168 t.Parallel()169 assert := assert.New(t)170 testData := []struct {171 version string172 preRelease string173 overridePreRelease bool174 expected string175 }{176 {"1.0.0", "", false, "1.1.0-0"},177 {"1.0.0-0", "", false, "1.0.0-1"},178 {"1.0.0", "alpha", false, "1.1.0-alpha.0"},179 {"1.1.0-alpha.0", "alpha", false, "1.1.0-alpha.1"},180 {"1.1.0-alpha.1", "beta", false, "1.1.0-beta.0"},181 {"1.0.0", "", true, "1.0.0"},182 {"1.0.0", "SNAPSHOT", true, "1.1.0-SNAPSHOT"},183 {"1.1.0-SNAPSHOT", "SNAPSHOT", true, "1.1.0-SNAPSHOT"},184 }185 for _, tc := range testData {186 version, err := NewVersion(tc.version)187 assert.Nil(err)188 actual := version.BumpPreRelease(tc.preRelease, tc.overridePreRelease, nil)189 assert.Equal(tc.expected, actual.String())190 }191}192func ExampleVersion_BumpPreRelease() {193 v1 := Version{Major: 1} // 1.0.0194 // Parameters: pre-release, pre-release overwrite, versionBumper (default to Version.BumpMinor)195 v2 := v1.BumpPreRelease("alpha", false, nil)196 // The current version is not a pre-release, so it will use the versionBumper to first bump the minor (default) and then set the pre-release to alpha.0197 fmt.Println(v2.String())198 // However if the current is already a pre-release of the same class (alpha here), then it just increments the pre-release id199 v3 := v2.BumpPreRelease("alpha", false, nil)200 fmt.Println(v3.String())201 // Output:202 // 1.1.0-alpha.0203 // 1.1.0-alpha.1204}205func ExampleVersion_BumpPreRelease_overwrite() {206 v1 := Version{Major: 1} // 1.0.0207 // If you don't want to have pre-release index, you can pass true for pre-release overwrite as parameter208 v2 := v1.BumpPreRelease("alpha", true, nil)209 fmt.Println(v2.String())210 // But then it means your version can be overwritten if you perform again the same operation211 v3 := v2.BumpPreRelease("alpha", true, nil)212 fmt.Println(v3.String())213 // Output:214 // 1.1.0-alpha215 // 1.1.0-alpha216}217func ExampleVersion_BumpPreRelease_versionBumper() {218 v1 := Version{Major: 1} // 1.0.0219 // It is also possible to overwrite the default version bumper220 v2 := v1.BumpPreRelease("alpha", false, Version.BumpMajor)221 fmt.Println(v2.String())222 // But if the previous is already a pre-release of the same class (alpha here), then it will not be used223 v3 := v2.BumpPreRelease("alpha", false, Version.BumpMajor)224 fmt.Println(v3.String())225 // Output:226 // 2.0.0-alpha.0227 // 2.0.0-alpha.1228}229func TestWithBuildMetadata(t *testing.T) {230 t.Parallel()231 assert := assert.New(t)232 testData := []struct {233 version string234 buildMetadata string235 expected string236 }{237 {"1.0.0", "build.8", "1.0.0+build.8"},238 {"1.0.0", "3.abcdkd", "1.0.0+3.abcdkd"},239 }240 for _, tc := range testData {241 version, err := NewVersion(tc.version)242 assert.Nil(err)243 actual := version.WithBuildMetadata(tc.buildMetadata)244 assert.Equal(tc.expected, actual.String())245 }246}247func ExampleVersion_WithBuildMetadata() {248 v := Version{Major: 1} // 1.0.0249 v2 := v.WithBuildMetadata("build.1") // this simply set the build metadata to the version250 fmt.Println(v2.String())251 // Output: 1.0.0+build.1252}...

Full Screen

Full Screen

bumper.go

Source:bumper.go Github

copy

Full Screen

...50 }51 if nextReleaseIsGreaterThanLastPrerelease(nextRelease, latestPrerelease) {52 return prerelease1(esti, commits, nextRelease)53 }54 return bumpPrerelease(esti, latestPrerelease, commits)55}56func bumpRelease(repo GitRepo, esti Estimator) (*semver.Version, []*object.Commit, error) {57 latestRelease, err := repo.LatestTaggedRelease()58 if err != nil {59 return nil, nil, err60 }61 commits, err := repo.CommitMessagesSince(latestRelease)62 if err != nil {63 return nil, nil, err64 }65 if latestRelease == nil {66 return nil, commits, err67 }68 nextRelease := bump(latestRelease, esti.BumpLevelFrom(messagesFrom(commits)))69 return &nextRelease, commits, nil70}71func fakePrerelease(conf Config) (*semver.Version, bool, error) {72 prerelease, ok := conf.ShouldFakePrerelease()73 if !ok {74 return nil, false, nil75 }76 version, err := semver.StrictNewVersion(prerelease)77 if err != nil {78 return nil, true, fmt.Errorf("cannot parse given prerelease version: %w", err)79 }80 return version, true, nil81}82func prerelease1(esti Estimator, commits []*object.Commit, v *semver.Version) (*semver.Version, []*object.Commit, error) {83 if nextPrerelease, err := esti.NextPrerelease(""); err != nil {84 return nil, nil, err85 } else if ver, err := v.SetPrerelease(nextPrerelease); err != nil {86 return nil, nil, err87 } else {88 return &ver, commits, nil89 }90}91func bumpPrerelease(esti Estimator, latestPrerelease *semver.Version, commits []*object.Commit) (*semver.Version, []*object.Commit, error) {92 pre, err := esti.NextPrerelease(latestPrerelease.Prerelease())93 if err != nil {94 return nil, nil, err95 }96 nextPrerelease, err := latestPrerelease.SetPrerelease(pre)97 if err != nil {98 return nil, nil, err99 }100 return &nextPrerelease, commits, nil101}102func bump(v *semver.Version, lvl BumpLevel) semver.Version {103 switch lvl {104 case BumpLevelMajor:105 return v.IncMajor()...

Full Screen

Full Screen

version.go

Source:version.go Github

copy

Full Screen

...21 if err != nil {22 return "", err23 }24 if version.Prerelease() != "" {25 version = bumpPrerelease(version)26 return version.String(), nil27 }28 return "", nil29}30// IsPrerelease detects if release is prerelease31func IsPrerelease(currentVersion string) bool {32 version, err := semver.NewVersion(currentVersion)33 if err != nil {34 return false35 }36 return version.Prerelease() != ""37}38// Next returns next generated semver based on version position39func Next(currentVersion, kind string) (string, error) {40 err := validateVersionPostion(kind)41 if err != nil {42 return "", err43 }44 version, err := semver.NewVersion(currentVersion)45 if err != nil {46 return "", err47 }48 var inc semver.Version49 switch kind {50 case Major:51 inc = version.IncMajor()52 case Minor:53 inc = version.IncMinor()54 case Patch:55 inc = version.IncPatch()56 default:57 return "", fmt.Errorf("invalid position" + kind)58 }59 return inc.String(), nil60}61// bumpPrerelease bumps number in versions like 0.0.1-alpha2 or 0.0.3-omega462func bumpPrerelease(version *semver.Version) *semver.Version {63 prerelease := version.Prerelease()64 r := regexp.MustCompile("[0-9]+$")65 matches := r.FindStringSubmatch(prerelease)66 if len(matches) == 1 {67 num, err := strconv.Atoi(matches[0])68 if err == nil {69 num = num + 170 prerelease = strings.Replace(prerelease, matches[0], fmt.Sprintf("%03d", num), -1)71 v, _ := version.SetPrerelease(prerelease)72 return &v73 }74 }75 return version76}...

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v, err := semver.Parse("v1.2.3-alpha.1")4 if err != nil {5 panic(err)6 }7 v = v.BumpPrerelease("beta")8 fmt.Println(v)9}

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v, _ := version.NewVersion("1.2.3-beta.1")4 v = v.IncPrerelease("beta")5 fmt.Println(v)6}7Recommended Posts: Golang | version.NewVersion() method8Golang | version.NewSemver() method

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v, _ := version.NewVersion("1.2.3-alpha.1")4 v = v.IncPrerelease("beta")5 v = v.IncPrerelease("beta")6}

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v, err := semver.Parse("v1.2.3-alpha.1")4 if err != nil {5 fmt.Println(err)6 }7}

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 version1, _ := version.NewVersion("1.2.3")4 fmt.Println(version1.String())5 version1.BumpPrerelease("alpha")6 fmt.Println(version1.String())7}

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import(2func main() {3 v, _ := semver.Parse("1.2.3-alpha.1")4 v.BumpPrerelease("beta")5 fmt.Println(v)6}

Full Screen

Full Screen

bumpPrerelease

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v, _ := version.NewVersion("1.2.3")4 v = v.IncPrerelease("alpha")5 fmt.Println(v)6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful