Skip to content

Commit

Permalink
Added unit test for covarage (flyteorg#83)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuvraj <code@evalsocket.dev>
  • Loading branch information
yindia committed Jun 5, 2021
1 parent 2d2c145 commit caa38e5
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 96 deletions.
5 changes: 3 additions & 2 deletions flytectl/.github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: build

on:
pull_request:
push:
branches:
- master

jobs:
build:
Expand All @@ -23,7 +24,7 @@ jobs:
with:
args: make install && make test_unit_codecov
- name: Push CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v1.0.5
with:
file: coverage.txt
flags: unittests
Expand Down
3 changes: 1 addition & 2 deletions flytectl/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ bin
.DS_Store
_test
./config.yaml
docs/build/*
cmd/get/temp-output-file
docs/build/*
32 changes: 32 additions & 0 deletions flytectl/cmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package config

import (
"testing"

"github.com/flyteorg/flytectl/pkg/printer"
"github.com/stretchr/testify/assert"
)

func TestOutputFormat(t *testing.T) {
c := &Config{
Output: "json",
}
result, err := c.OutputFormat()
assert.Nil(t, err)
assert.Equal(t, printer.OutputFormat(1), result)
}

func TestInvalidOutputFormat(t *testing.T) {
c := &Config{
Output: "test",
}
var result printer.OutputFormat
defer func() {
if r := recover(); r != nil {
assert.Equal(t, printer.OutputFormat(0), result)
assert.NotNil(t, r)
}
}()
result = c.MustOutputFormat()

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func WriteConfigToFile(matchableAttrConfig interface{}, fileName string) error {
return fmt.Errorf("error: %v", err)
}
if _, err = os.Stat(fileName); err == nil {
if !cmdUtil.AskForConfirmation(fmt.Sprintf("warning file %v will be overwritten", fileName)) {
if !cmdUtil.AskForConfirmation(fmt.Sprintf("warning file %v will be overwritten", fileName), os.Stdin) {
return fmt.Errorf("backup the file before continuing")
}
}
Expand Down
2 changes: 1 addition & 1 deletion flytectl/cmd/get/execution_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func WriteExecConfigToFile(executionConfig ExecutionConfig, fileName string) err
fmt.Printf("error: %v", err)
}
if _, err = os.Stat(fileName); err == nil {
if !cmdUtil.AskForConfirmation(fmt.Sprintf("warning file %v will be overwritten", fileName)) {
if !cmdUtil.AskForConfirmation(fmt.Sprintf("warning file %v will be overwritten", fileName), os.Stdin) {
return errors.New("backup the file before continuing")
}
}
Expand Down
5 changes: 1 addition & 4 deletions flytectl/cmd/get/launch_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Retrieves a launch plans within project and domain for a version and generate th
::
flytectl get launchplan -d development -p flytectldemo core.advanced.run_merge_sort.merge_sort --execFile execution_spec.yam
flytectl get launchplan -d development -p flytectldemo core.advanced.run_merge_sort.merge_sort --execFile execution_spec.yaml
The generated file would look similar to this
Expand Down Expand Up @@ -152,9 +152,6 @@ func getLaunchPlanFunc(ctx context.Context, args []string, cmdCtx cmdCore.Comman
return err
}
launchPlans := launchPlanList.LaunchPlans
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved %v launch plans", len(launchPlans))
return launchPlanPrinter.Print(config.GetConfig().MustOutputFormat(), launchplansColumns,
LaunchplanToProtoMessages(launchPlans)...)
Expand Down
7 changes: 4 additions & 3 deletions flytectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

f "github.com/flyteorg/flytectl/pkg/filesystemutils"

"github.com/flyteorg/flytectl/cmd/config"
cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flytectl/cmd/create"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/flyteorg/flytectl/cmd/register"
"github.com/flyteorg/flytectl/cmd/update"
"github.com/flyteorg/flytectl/cmd/version"
f "github.com/flyteorg/flytectl/pkg/filesystemutils"
"github.com/flyteorg/flytectl/pkg/printer"
stdConfig "github.com/flyteorg/flytestdlib/config"
"github.com/flyteorg/flytestdlib/config/viper"
Expand Down Expand Up @@ -41,7 +42,7 @@ func newRootCmd() *cobra.Command {
DisableAutoGenTag: true,
}

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", f.FilePathJoin(f.UserHomeDir(), configFileDir, configFileName),
"config file (default is $HOME/.flyte/config.yaml)")

configAccessor.InitializePflags(rootCmd.PersistentFlags())
Expand Down Expand Up @@ -69,7 +70,7 @@ func newRootCmd() *cobra.Command {
func initConfig(_ *cobra.Command, _ []string) error {
configAccessor = viper.NewAccessor(stdConfig.Options{
StrictMode: true,
SearchPaths: []string{cfgFile, f.FilePathJoin(f.UserHomeDir(), configFileDir, configFileName)},
SearchPaths: []string{cfgFile},
})

err := configAccessor.UpdateConfig(context.TODO())
Expand Down
17 changes: 17 additions & 0 deletions flytectl/pkg/adminutils/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package adminutils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetConfig(t *testing.T) {
defaultConfig = &Config{
MaxRecords: 500,
BatchSize: 100,
}
c := GetConfig()
assert.Equal(t, defaultConfig.BatchSize, c.BatchSize)
assert.Equal(t, defaultConfig.MaxRecords, c.MaxRecords)
}
71 changes: 0 additions & 71 deletions flytectl/pkg/adminutils/iterator.go

This file was deleted.

20 changes: 8 additions & 12 deletions flytectl/pkg/commandutils/command_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ package commandutils
import (
"bufio"
"fmt"
"log"
"os"
"io"
"strings"
)

func AskForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.ToLower(strings.TrimSpace(response))
func AskForConfirmation(s string, reader io.Reader) bool {
fmt.Printf("%s [y/n]: ", s)
r := bufio.NewScanner(reader)
for r.Scan() {
response := strings.ToLower(strings.TrimSpace(r.Text()))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
} else if response == "n" || response == "No" {
return false
}
}
return false
}
51 changes: 51 additions & 0 deletions flytectl/pkg/commandutils/command_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package commandutils

import (
"io"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

type TestCase struct {
Input io.Reader `json:"input"`
Output bool `json:"output"`
}

func TestAskForConfirmation(t *testing.T) {
tests := []TestCase{
{
Input: strings.NewReader("yes"),
Output: true,
},
{
Input: strings.NewReader("y"),
Output: true,
},
{
Input: strings.NewReader("no"),
Output: false,
},
{
Input: strings.NewReader("n"),
Output: false,
},
{
Input: strings.NewReader("No"),
Output: false,
},
{
Input: strings.NewReader("Yes"),
Output: true,
},
{
Input: strings.NewReader(""),
Output: false,
},
}
for _, test := range tests {
answer := AskForConfirmation("Testing for yes", test.Input)
assert.Equal(t, test.Output, answer)
}
}
Loading

0 comments on commit caa38e5

Please sign in to comment.