github.com/scaleway/scaleway-cli@v1.11.1/pkg/utils/utils_test.go (about) 1 package utils 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "sort" 8 "testing" 9 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 func TestWordify(t *testing.T) { 14 Convey("Testing Wordify()", t, func() { 15 So(Wordify("Hello World 42 !!"), ShouldEqual, "Hello_World_42") 16 So(Wordify(" Hello World 42 !! "), ShouldEqual, "Hello_World_42") 17 So(Wordify("Hello_World_42"), ShouldEqual, "Hello_World_42") 18 So(Wordify(""), ShouldEqual, "") 19 }) 20 } 21 22 func TestTruncIf(t *testing.T) { 23 Convey("Testing TruncIf()", t, func() { 24 So(TruncIf("Hello World", 5, false), ShouldEqual, "Hello World") 25 So(TruncIf("Hello World", 5, true), ShouldEqual, "Hello") 26 So(TruncIf("Hello World", 50, false), ShouldEqual, "Hello World") 27 So(TruncIf("Hello World", 50, true), ShouldEqual, "Hello World") 28 }) 29 } 30 31 func TestPathToTARPathparts(t *testing.T) { 32 Convey("Testing PathToTARPathparts()", t, func() { 33 dir, base := PathToTARPathparts("/etc/passwd") 34 So([]string{"/etc", "passwd"}, ShouldResemble, []string{dir, base}) 35 36 dir, base = PathToTARPathparts("/etc") 37 So([]string{"/", "etc"}, ShouldResemble, []string{dir, base}) 38 39 dir, base = PathToTARPathparts("/etc/") 40 So([]string{"/", "etc"}, ShouldResemble, []string{dir, base}) 41 42 dir, base = PathToTARPathparts("/long/path/to/file") 43 So([]string{"/long/path/to", "file"}, ShouldResemble, []string{dir, base}) 44 45 dir, base = PathToTARPathparts("/long/path/to/dir/") 46 So([]string{"/long/path/to", "dir"}, ShouldResemble, []string{dir, base}) 47 }) 48 } 49 50 func TestRemoveDuplicates(t *testing.T) { 51 Convey("Testing RemoveDuplicates()", t, func() { 52 slice := RemoveDuplicates([]string{"a", "b", "c", "a"}) 53 sort.Strings(slice) 54 So(slice, ShouldResemble, []string{"a", "b", "c"}) 55 56 slice = RemoveDuplicates([]string{"a", "b", "c", "a"}) 57 sort.Strings(slice) 58 So(slice, ShouldResemble, []string{"a", "b", "c"}) 59 60 slice = RemoveDuplicates([]string{"a", "b", "c", "a", "a", "b", "d"}) 61 sort.Strings(slice) 62 So(slice, ShouldResemble, []string{"a", "b", "c", "d"}) 63 64 slice = RemoveDuplicates([]string{"a", "b", "c", "a", ""}) 65 sort.Strings(slice) 66 So(slice, ShouldResemble, []string{"", "a", "b", "c"}) 67 }) 68 } 69 70 func TestGeneratingAnSSHKey(t *testing.T) { 71 Convey("Testing GeneratingAnSSHKey()", t, func() { 72 streams := SpawnRedirection{ 73 Stdout: os.Stdout, 74 Stderr: os.Stderr, 75 Stdin: os.Stdin, 76 } 77 78 tmpDir, err := ioutil.TempDir("/tmp", "scaleway-test") 79 So(err, ShouldBeNil) 80 81 tmpFile, err := ioutil.TempFile(tmpDir, "ssh-key") 82 So(err, ShouldBeNil) 83 84 err = os.Remove(tmpFile.Name()) 85 So(err, ShouldBeNil) 86 87 filePath, err := GeneratingAnSSHKey(streams, tmpDir, filepath.Base(tmpFile.Name())) 88 So(err, ShouldBeNil) 89 So(filePath, ShouldEqual, tmpFile.Name()) 90 91 os.Remove(tmpFile.Name()) 92 }) 93 }