Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Create test for root
Browse files Browse the repository at this point in the history
Signed-off-by: Kadu Artur Prussek <[email protected]>
  • Loading branch information
kaduartur committed Dec 8, 2020
1 parent 88a22ab commit 4a128ff
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
140 changes: 140 additions & 0 deletions pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/json"
"errors"
"os"
"testing"
"time"
Expand Down Expand Up @@ -79,3 +80,142 @@ func Test_rootCmd(t *testing.T) {
})
}
}

func TestConvertContextToEnv(t *testing.T) {
ctxFile := `{
"current_context": "prod",
"contexts": [
"prod",
"qa",
"dev"
]
}`

type in struct {
ritHome string
file stream.FileWriteReadExistRemover
}

tests := []struct {
name string
in in
want error
}{
{
name: "success",
in: in{
ritHome: os.TempDir(),
file: sMocks.FileManagerMock{
WriteFunc: func(path string, content []byte) error {
return nil
},
ReadFunc: func(path string) ([]byte, error) {
return []byte(ctxFile), nil
},
ExistsFunc: func(path string) bool {
return true
},
RemoveFunc: func(path string) error {
return nil
},
},
},
want: nil,
},
{
name: "success when contexts file does not exist",
in: in{
ritHome: os.TempDir(),
file: sMocks.FileManagerMock{
ExistsFunc: func(path string) bool {
return false
},
},
},
want: nil,
},
{
name: "read contexts file error",
in: in{
ritHome: os.TempDir(),
file: sMocks.FileManagerMock{
ExistsFunc: func(path string) bool {
return true
},
ReadFunc: func(path string) ([]byte, error) {
return nil, errors.New("error to read contexts file")
},
},
},
want: errors.New("error to read contexts file"),
},
{
name: "unmarshal error",
in: in{
ritHome: os.TempDir(),
file: sMocks.FileManagerMock{
ExistsFunc: func(path string) bool {
return true
},
ReadFunc: func(path string) ([]byte, error) {
return []byte("invalid"), nil
},
},
},
want: errors.New("invalid character 'i' looking for beginning of value"),
},
{
name: "write envs file error",
in: in{
ritHome: os.TempDir(),
file: sMocks.FileManagerMock{
WriteFunc: func(path string, content []byte) error {
return errors.New("error to write envs file")
},
ReadFunc: func(path string) ([]byte, error) {
return []byte(ctxFile), nil
},
ExistsFunc: func(path string) bool {
return true
},
},
},
want: errors.New("error to write envs file"),
},
{
name: "remove contexts file error",
in: in{
ritHome: os.TempDir(),
file: sMocks.FileManagerMock{
WriteFunc: func(path string, content []byte) error {
return nil
},
ReadFunc: func(path string) ([]byte, error) {
return []byte(ctxFile), nil
},
ExistsFunc: func(path string) bool {
return true
},
RemoveFunc: func(path string) error {
return errors.New("error to remove contexts file")
},
},
},
want: errors.New("error to remove contexts file"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := rootCmd{
ritchieHome: tt.in.ritHome,
file: tt.in.file,
}

got := cmd.convertContextsFileToEnvsFile()
if got != nil && got.Error() != tt.want.Error() {
t.Fatalf("convertContextsFileToEnvsFile(%s) got %v, want %v", tt.name, got, tt.want)
}
})
}
}
23 changes: 23 additions & 0 deletions pkg/stream/mocks/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,26 @@ func (f FileWriteReadExisterCustomMock) Exists(path string) bool {
func (f FileWriteReadExisterCustomMock) Write(path string, content []byte) error {
return f.WriteMock(path, content)
}

type FileManagerMock struct {
WriteFunc func(path string, content []byte) error
ReadFunc func(path string) ([]byte, error)
ExistsFunc func(path string) bool
RemoveFunc func(path string) error
}

func (f FileManagerMock) Read(path string) ([]byte, error) {
return f.ReadFunc(path)
}

func (f FileManagerMock) Exists(path string) bool {
return f.ExistsFunc(path)
}

func (f FileManagerMock) Write(path string, content []byte) error {
return f.WriteFunc(path, content)
}

func (f FileManagerMock) Remove(path string) error {
return f.RemoveFunc(path)
}

0 comments on commit 4a128ff

Please sign in to comment.