-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version: add testing for version creation
- Loading branch information
1 parent
d970fb9
commit b33ea9e
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func Test_PluginVersionCreate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
coreVersion string | ||
preVersion string | ||
metaVersion string | ||
expectError bool | ||
expectVersionString string | ||
}{ | ||
{ | ||
"Valid semver core only version", | ||
"1.0.0", | ||
"", | ||
"", | ||
false, | ||
"1.0.0", | ||
}, | ||
{ | ||
"Valid semver, should get canonical version", | ||
"01.001.001", | ||
"", | ||
"", | ||
false, | ||
"1.1.1", | ||
}, | ||
{ | ||
"Valid semver with prerelease, should get canonical version", | ||
"1.001.010", | ||
"dev", | ||
"", | ||
false, | ||
"1.1.10-dev", | ||
}, | ||
{ | ||
"Valid semver with metadata, should get canonical version", | ||
"1.001.010", | ||
"", | ||
"123abcdef", | ||
false, | ||
"1.1.10+123abcdef", | ||
}, | ||
{ | ||
"Valid semver with prerelease and metadata, should get canonical version", | ||
"1.001.010", | ||
"dev", | ||
"123abcdef", | ||
false, | ||
"1.1.10-dev+123abcdef", | ||
}, | ||
{ | ||
"Invalid version, should fail", | ||
".1.1", | ||
"", | ||
"", | ||
true, | ||
"", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer func() { | ||
panicMsg := recover() | ||
if !tt.expectError && panicMsg != nil { | ||
t.Errorf("creating version panicked, should not have.") | ||
} | ||
|
||
if tt.expectError && panicMsg == nil { | ||
t.Errorf("creating version should have panicked, but did not.") | ||
} | ||
|
||
if panicMsg != nil { | ||
t.Logf("panic message was: %v", panicMsg) | ||
} | ||
}() | ||
|
||
ver := NewPluginVersion(tt.coreVersion, tt.preVersion, tt.metaVersion) | ||
verStr := ver.String() | ||
if verStr != tt.expectVersionString { | ||
t.Errorf("string format mismatch, version created is %q, expected %q", verStr, tt.expectVersionString) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFormattedVersionString(t *testing.T) { | ||
GitCommit = "abcdef12345" | ||
defer func() { | ||
GitCommit = "" | ||
}() | ||
|
||
expectedVersion := fmt.Sprintf("1.0.0-dev (%s)", GitCommit) | ||
|
||
ver := InitializePluginVersion("1.0.0", "dev") | ||
formatted := ver.FormattedVersion() | ||
if formatted != expectedVersion { | ||
t.Fatalf("Expected formatted version %q; got %q", expectedVersion, formatted) | ||
} | ||
} |