mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #24821 from thaJeztah/stringid-refactor
Minor refactor and beter coverage for pkg/stringid
This commit is contained in:
commit
d2eb5dc9e0
2 changed files with 21 additions and 7 deletions
|
@ -29,11 +29,10 @@ func TruncateID(id string) string {
|
||||||
if i := strings.IndexRune(id, ':'); i >= 0 {
|
if i := strings.IndexRune(id, ':'); i >= 0 {
|
||||||
id = id[i+1:]
|
id = id[i+1:]
|
||||||
}
|
}
|
||||||
trimTo := shortLen
|
if len(id) > shortLen {
|
||||||
if len(id) < shortLen {
|
id = id[:shortLen]
|
||||||
trimTo = len(id)
|
|
||||||
}
|
}
|
||||||
return id[:trimTo]
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateID(crypto bool) string {
|
func generateID(crypto bool) string {
|
||||||
|
@ -60,7 +59,6 @@ func generateID(crypto bool) string {
|
||||||
// GenerateRandomID returns a unique id.
|
// GenerateRandomID returns a unique id.
|
||||||
func GenerateRandomID() string {
|
func GenerateRandomID() string {
|
||||||
return generateID(true)
|
return generateID(true)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateNonCryptoID generates unique id without using cryptographically
|
// GenerateNonCryptoID generates unique id without using cryptographically
|
||||||
|
|
|
@ -13,10 +13,26 @@ func TestGenerateRandomID(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenerateNonCryptoID(t *testing.T) {
|
||||||
|
id := GenerateNonCryptoID()
|
||||||
|
|
||||||
|
if len(id) != 64 {
|
||||||
|
t.Fatalf("Id returned is incorrect: %s", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestShortenId(t *testing.T) {
|
func TestShortenId(t *testing.T) {
|
||||||
id := GenerateRandomID()
|
id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
|
||||||
truncID := TruncateID(id)
|
truncID := TruncateID(id)
|
||||||
if len(truncID) != 12 {
|
if truncID != "90435eec5c4e" {
|
||||||
|
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShortenSha256Id(t *testing.T) {
|
||||||
|
id := "sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba"
|
||||||
|
truncID := TruncateID(id)
|
||||||
|
if truncID != "4e38e38c8ce0" {
|
||||||
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue