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

refactor: deprecated dynamic input #848

Merged
67 changes: 67 additions & 0 deletions internal/mocks/post_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mocks

import "github.com/stretchr/testify/mock"

type DirManagerMock struct {
mock.Mock
}

func (d *DirManagerMock) Remove(dir string) error {
args := d.Called(dir)
return args.Error(0)
}

type FileManagerMock struct {
mock.Mock
}

func (fi *FileManagerMock) Write(string, []byte) error {
args := fi.Called()
return args.Error(0)
}

func (fi *FileManagerMock) Read(string) ([]byte, error) {
args := fi.Called()
return args.Get(0).([]byte), args.Error(1)
}

func (fi *FileManagerMock) Exists(string) bool {
args := fi.Called()
return args.Bool(0)
}

func (fi *FileManagerMock) Append(path string, content []byte) error {
args := fi.Called(path, content)
return args.Error(0)
}

func (fi *FileManagerMock) Move(oldPath, newPath string, files []string) error {
args := fi.Called(oldPath, newPath, files)
return args.Error(0)
}

func (fi *FileManagerMock) Remove(path string) error {
args := fi.Called(path)
return args.Error(0)
}

func (fi *FileManagerMock) ListNews(oldPath, newPath string) ([]string, error) {
args := fi.Called(oldPath, newPath)
return args.Get(0).([]string), args.Error(1)
}
1 change: 1 addition & 0 deletions pkg/formula/runner/config_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestFind(t *testing.T) {
},
{
name: "fail invalid runType",
ritHome: ritHome,
fileContent: "error",
err: "strconv.Atoi: parsing \"error\": invalid syntax",
},
Expand Down
16 changes: 16 additions & 0 deletions pkg/formula/runner/post_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/input"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

Expand All @@ -28,6 +30,9 @@ type PostRunnerManager struct {
dir stream.DirRemover
}

const deprecatedMsg = "\nWarning: dynamic input are deprecated and will no " +
"longer be supported in future versions\n"

func NewPostRunner(file stream.FileNewListMoveRemover, dir stream.DirRemover) PostRunnerManager {
return PostRunnerManager{file: file, dir: dir}
}
Expand All @@ -50,6 +55,8 @@ func (po PostRunnerManager) PostRun(p formula.Setup, docker bool) error {
return err
}

po.deprecatedInput(p)

return nil
}

Expand All @@ -58,3 +65,12 @@ func (po PostRunnerManager) removeWorkDir(tmpDir string) {
fmt.Sprintln("Error in remove dir")
}
}

func (po PostRunnerManager) deprecatedInput(p formula.Setup) {
for _, in := range p.Config.Inputs {
if in.Type == input.DynamicType {
prompt.Warning(deprecatedMsg)
break
}
}
}
110 changes: 43 additions & 67 deletions pkg/formula/runner/post_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ import (
"errors"
"testing"

"github.com/ZupIT/ritchie-cli/internal/mocks"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/stream"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestPostRun(t *testing.T) {
type in struct {
file stream.FileNewListMoveRemover
dir stream.DirRemover
wrErr error
rdErr error
apErr error
mvErr error
rmErr error
lnErr error
exist bool
dir error
setup formula.Setup
docker bool
}
Expand All @@ -40,8 +48,6 @@ func TestPostRun(t *testing.T) {
{
name: "success",
in: in{
file: fileManagerMock{},
dir: dirManagerMock{},
setup: formula.Setup{},
docker: false,
},
Expand All @@ -50,8 +56,6 @@ func TestPostRun(t *testing.T) {
{
name: "success docker",
in: in{
file: fileManagerMock{},
dir: dirManagerMock{},
setup: formula.Setup{},
docker: true,
},
Expand All @@ -60,8 +64,7 @@ func TestPostRun(t *testing.T) {
{
name: "error remove .env file docker",
in: in{
file: fileManagerMock{rmErr: errors.New("error to remove .env file")},
dir: dirManagerMock{},
rmErr: errors.New("error to remove .env file"),
setup: formula.Setup{},
docker: true,
},
Expand All @@ -70,8 +73,7 @@ func TestPostRun(t *testing.T) {
{
name: "error list new files",
in: in{
file: fileManagerMock{lErr: errors.New("error to list new files")},
dir: dirManagerMock{},
lnErr: errors.New("error to list new files"),
setup: formula.Setup{},
docker: false,
},
Expand All @@ -80,8 +82,7 @@ func TestPostRun(t *testing.T) {
{
name: "error move new files",
in: in{
file: fileManagerMock{mErr: errors.New("error to move new files")},
dir: dirManagerMock{},
mvErr: errors.New("error to move new files"),
setup: formula.Setup{},
docker: false,
},
Expand All @@ -90,72 +91,47 @@ func TestPostRun(t *testing.T) {
{
name: "error remove work dir",
in: in{
file: fileManagerMock{},
dir: dirManagerMock{rmErr: errors.New("error to remove workdir")},
dir: errors.New("error to remove workdir"),
setup: formula.Setup{},
docker: false,
},
want: nil,
},
{
name: "input deprecated",
in: in{
setup: formula.Setup{
Config: formula.Config{
Inputs: formula.Inputs{
formula.Input{
Type: "dynamic",
},
},
},
},
docker: false,
},
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
runner := NewPostRunner(tt.in.file, tt.in.dir)
dm := &mocks.DirManagerMock{}
dm.On("Remove", mock.Anything).Return(tt.in.dir)
fm := &mocks.FileManagerMock{}
fm.On("Write", mock.Anything, mock.Anything).Return(tt.in.wrErr)
fm.On("Read", mock.Anything).Return([]byte{}, tt.in.rdErr)
fm.On("Exists", mock.Anything).Return(tt.in.exist)
fm.On("Append", mock.Anything, mock.Anything).Return(tt.in.apErr)
fm.On("Move", mock.Anything, mock.Anything, mock.Anything).Return(tt.in.mvErr)
fm.On("Remove", mock.Anything).Return(tt.in.rmErr)
fm.On("ListNews", mock.Anything, mock.Anything).Return([]string{}, tt.in.lnErr)
runner := NewPostRunner(fm, dm)
got := runner.PostRun(tt.in.setup, tt.in.docker)

if (tt.want != nil && got == nil) || got != nil && got.Error() != tt.want.Error() {
t.Errorf("PostRun(%s) got %v, want %v", tt.name, got, tt.want)
}
assert.Equal(t, tt.want, got)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful, right 🌟

})
}

}

type dirManagerMock struct {
rmErr error
}

func (d dirManagerMock) Remove(dir string) error {
return d.rmErr
}

type fileManagerMock struct {
rBytes []byte
rErr error
wErr error
aErr error
mErr error
rmErr error
lErr error
exist bool
listNews []string
}

func (fi fileManagerMock) Write(string, []byte) error {
return fi.wErr
}

func (fi fileManagerMock) Read(string) ([]byte, error) {
return fi.rBytes, fi.rErr
}

func (fi fileManagerMock) Exists(string) bool {
return fi.exist
}

func (fi fileManagerMock) Append(path string, content []byte) error {
return fi.aErr
}

func (fi fileManagerMock) Move(oldPath, newPath string, files []string) error {
return fi.mErr
}

func (fi fileManagerMock) Remove(path string) error {
return fi.rmErr
}

func (fi fileManagerMock) ListNews(oldPath, newPath string) ([]string, error) {
return fi.listNews, fi.lErr
}