summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Hassard <steve@hassard.net>2018-12-30 05:09:42 -0800
committerIbrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>2018-12-30 13:09:42 +0000
commit0b5c8b9cdc35cadc115edfb3e39c02fc894f0e60 (patch)
treea00d5e3c577dfc162b04b13044553d0042267020
parenthotfix on stashed items vol.3 (diff)
downloadgitbatch-0b5c8b9cdc35cadc115edfb3e39c02fc894f0e60.tar.gz
Fix RNG seeding and add a test. (#51)
* Fix seeding the RNG. * Add test for RandomString.
-rw-r--r--pkg/helpers/utils.go4
-rw-r--r--pkg/helpers/utils_test.go10
2 files changed, 12 insertions, 2 deletions
diff --git a/pkg/helpers/utils.go b/pkg/helpers/utils.go
index 1a3f411..35de2c3 100644
--- a/pkg/helpers/utils.go
+++ b/pkg/helpers/utils.go
@@ -7,7 +7,7 @@ import (
)
var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
-var src = rand.NewSource(time.Now().UnixNano())
+var r = rand.New(rand.NewSource(time.Now().UnixNano()))
// TrimTrailingNewline removes the trailing new line form a string. this method
// is used mostly on outputs of a command
@@ -30,7 +30,7 @@ func Min(x, y int) int {
func RandomString(n int) string {
b := make([]rune, n)
for i := range b {
- b[i] = characterRunes[rand.Intn(len(characterRunes))]
+ b[i] = characterRunes[r.Intn(len(characterRunes))]
}
return string(b)
}
diff --git a/pkg/helpers/utils_test.go b/pkg/helpers/utils_test.go
new file mode 100644
index 0000000..4b48d3c
--- /dev/null
+++ b/pkg/helpers/utils_test.go
@@ -0,0 +1,10 @@
+package helpers
+
+import "testing"
+
+func TestRandomString(t *testing.T) {
+ var rand_string = RandomString(8)
+ if len(rand_string) != 8 {
+ t.Errorf("The length of the string should be equal.")
+ }
+}