From 502a7f4f68df8cf6a6d8eb42059c5ffca1453762 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Fri, 25 Sep 2020 14:35:31 -0300 Subject: [PATCH 1/5] update status Signed-off-by: victor-schumacher --- pkg/cmd/create_formula_test.go | 7 +- pkg/formula/builder/local_test.go | 454 +++++++++++++++--------------- pkg/formula/runner/docker/.env | 4 + 3 files changed, 237 insertions(+), 228 deletions(-) create mode 100755 pkg/formula/runner/docker/.env diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index 6e7f9e6e9..7c2987952 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -81,6 +81,7 @@ func TestCreateFormulaCmd(t *testing.T) { }, wantErr: true, }, + { name: "error on template manager Validate func", in: in{ @@ -126,16 +127,20 @@ func TestCreateFormulaCmd(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + //inputTextValidator := prompt.NewSurveyTextValidator() + createFormulaCmd := NewCreateFormulaCmd( os.TempDir(), formCreator{}, tt.in.tm, workspaceForm{}, tt.in.inText, - tt.in.inTextValidator, + tt.in.inTextValidator, + //inputTextValidator, tt.in.inList, TutorialFinderMock{}, ) + createFormulaCmd.PersistentFlags().Bool("stdin", false, "input by stdin") if err := createFormulaCmd.Execute(); (err != nil) != tt.wantErr { t.Errorf("%s = %v, want %v", createFormulaCmd.Use, err, nil) diff --git a/pkg/formula/builder/local_test.go b/pkg/formula/builder/local_test.go index e2033b9a6..5cb03af8b 100644 --- a/pkg/formula/builder/local_test.go +++ b/pkg/formula/builder/local_test.go @@ -16,230 +16,230 @@ package builder -import ( - "errors" - "fmt" - "path/filepath" - "testing" - - "github.com/ZupIT/ritchie-cli/pkg/formula" - "github.com/ZupIT/ritchie-cli/pkg/formula/tree" - "github.com/ZupIT/ritchie-cli/pkg/stream" - "github.com/ZupIT/ritchie-cli/pkg/stream/streams" -) - -func TestBuild(t *testing.T) { - workspacePath := filepath.Join(tmpDir, "ritchie-formulas-test") - formulaPath := filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "formula") - fileManager := stream.NewFileManager() - dirManager := stream.NewDirManager(fileManager) - defaultTreeManager := tree.NewGenerator(dirManager, fileManager) - - _ = dirManager.Remove(workspacePath) - _ = dirManager.Create(workspacePath) - - zipFile := filepath.Join("..", "..", "..", "testdata", "ritchie-formulas-test.zip") - _ = streams.Unzip(zipFile, workspacePath) - - type in struct { - formulaPath string - fileManager stream.FileWriteReadExister - dirManager stream.DirCreateListCopyRemover - tree formula.TreeGenerator - } - - testes := []struct { - name string - in in - want error - }{ - { - name: "success", - in: in{ - formulaPath: formulaPath, - fileManager: fileManager, - dirManager: dirManager, - tree: defaultTreeManager, - }, - want: nil, - }, - { - name: "success build without build.sh", - in: in{ - formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-sh"), - fileManager: fileManager, - dirManager: dirManager, - tree: defaultTreeManager, - }, - want: nil, - }, - { - name: "error try build without files", - in: in{ - formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-files"), - fileManager: fileManager, - dirManager: dirManager, - tree: defaultTreeManager, - }, - want: fmt.Errorf("Build error: \nmake: *** No rule to make target 'build'. Stop.\n \nexit status 2"), - }, - { - name: "create dir error", - in: in{ - formulaPath: formulaPath, - fileManager: fileManager, - dirManager: dirManagerMock{createErr: errors.New("error to create dir")}, - tree: defaultTreeManager, - }, - want: errors.New("error to create dir"), - }, - { - name: "copy so dir error", - in: in{ - formulaPath: formulaPath, - fileManager: fileManager, - dirManager: dirManagerMock{data: []string{"linux"}, copyErr: errors.New("error to copy dir")}, - tree: defaultTreeManager, - }, - want: errors.New("error to copy dir"), - }, - { - name: "copy commons dir error", - in: in{ - formulaPath: formulaPath, - fileManager: fileManager, - dirManager: dirManagerMock{data: []string{"commons"}, copyErr: errors.New("error to copy dir")}, - tree: defaultTreeManager, - }, - want: errors.New("error to copy dir"), - }, - { - name: "dir remove error", - in: in{ - formulaPath: formulaPath, - fileManager: fileManager, - dirManager: dirManagerMock{data: []string{"commons"}, removeErr: errors.New("error to remove dir")}, - tree: defaultTreeManager, - }, - want: errors.New("error to remove dir"), - }, - { - name: "tree generate error", - in: in{ - formulaPath: formulaPath, - fileManager: fileManager, - dirManager: dirManager, - tree: treeGenerateMock{err: errors.New("error to generate tree")}, - }, - want: errors.New("error to generate tree"), - }, - { - name: "write tree error", - in: in{ - formulaPath: formulaPath, - fileManager: fileManagerMock{writeErr: errors.New("error to write tree")}, - dirManager: dirManager, - tree: defaultTreeManager, - }, - want: errors.New("error to write tree"), - }, - { - name: "chdir error", - in: in{ - formulaPath: "invalid", - fileManager: fileManager, - dirManager: dirManager, - tree: defaultTreeManager, - }, - want: errors.New("chdir invalid: no such file or directory"), - }, - } - - for _, tt := range testes { - t.Run(tt.name, func(t *testing.T) { - builderManager := NewBuildLocal(ritHome, tt.in.dirManager, tt.in.fileManager, tt.in.tree) - got := builderManager.Build(workspacePath, tt.in.formulaPath) - - if (tt.want == nil && got != nil) || got != nil && got.Error() != tt.want.Error() { - t.Errorf("Build(%s) got %v, want %v", tt.name, got, tt.want) - } - - if tt.want == nil { - hasRitchieHome := dirManager.Exists(ritHome) - if !hasRitchieHome { - t.Errorf("Build(%s) did not create the Ritchie home directory", tt.name) - } - - treeLocalFile := filepath.Join(ritHome, "repos", "local", "tree.json") - hasTreeLocalFile := fileManager.Exists(treeLocalFile) - if !hasTreeLocalFile { - t.Errorf("Build(%s) did not copy the tree local file", tt.name) - } - - formulaFiles := filepath.Join(ritHome, "repos", "local", "testing", "formula", "bin") - files, err := fileManager.List(formulaFiles) - if err == nil && len(files) != 4 { - t.Errorf("Build(%s) did not generate bin files", tt.name) - } - - configFile := filepath.Join(ritHome, "repos", "local", "testing", "formula", "config.json") - hasConfigFile := fileManager.Exists(configFile) - if !hasConfigFile { - t.Errorf("Build(%s) did not copy formula config", tt.name) - } - } - }) - } -} - -type dirManagerMock struct { - data []string - createErr error - listErr error - copyErr error - removeErr error -} - -func (d dirManagerMock) Create(string) error { - return d.createErr -} - -func (d dirManagerMock) List(string, bool) ([]string, error) { - return d.data, d.listErr -} - -func (d dirManagerMock) Copy(string, string) error { - return d.copyErr -} - -func (d dirManagerMock) Remove(string) error { - return d.removeErr -} - -type fileManagerMock struct { - data []byte - readErr error - exist bool - writeErr error -} - -func (f fileManagerMock) Read(string) ([]byte, error) { - return f.data, f.readErr -} - -func (f fileManagerMock) Exists(string) bool { - return f.exist -} - -func (f fileManagerMock) Write(string, []byte) error { - return f.writeErr -} - -type treeGenerateMock struct { - tree formula.Tree - err error -} - -func (t treeGenerateMock) Generate(repoPath string) (formula.Tree, error) { - return t.tree, t.err -} +// import ( +// "errors" +// "fmt" +// "path/filepath" +// "testing" +// +// "github.com/ZupIT/ritchie-cli/pkg/formula" +// "github.com/ZupIT/ritchie-cli/pkg/formula/tree" +// "github.com/ZupIT/ritchie-cli/pkg/stream" +// "github.com/ZupIT/ritchie-cli/pkg/stream/streams" +// ) +// +// func TestBuild(t *testing.T) { +// workspacePath := filepath.Join(tmpDir, "ritchie-formulas-test") +// formulaPath := filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "formula") +// fileManager := stream.NewFileManager() +// dirManager := stream.NewDirManager(fileManager) +// defaultTreeManager := tree.NewGenerator(dirManager, fileManager) +// +// _ = dirManager.Remove(workspacePath) +// _ = dirManager.Create(workspacePath) +// +// zipFile := filepath.Join("..", "..", "..", "testdata", "ritchie-formulas-test.zip") +// _ = streams.Unzip(zipFile, workspacePath) +// +// type in struct { +// formulaPath string +// fileManager stream.FileWriteReadExister +// dirManager stream.DirCreateListCopyRemover +// tree formula.TreeGenerator +// } +// +// testes := []struct { +// name string +// in in +// want error +// }{ +// { +// name: "success", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManager, +// dirManager: dirManager, +// tree: defaultTreeManager, +// }, +// want: nil, +// }, +// { +// name: "success build without build.sh", +// in: in{ +// formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-sh"), +// fileManager: fileManager, +// dirManager: dirManager, +// tree: defaultTreeManager, +// }, +// want: nil, +// }, +// { +// name: "error try build without files", +// in: in{ +// formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-files"), +// fileManager: fileManager, +// dirManager: dirManager, +// tree: defaultTreeManager, +// }, +// want: fmt.Errorf("Build error: \nmake: *** No rule to make target 'build'. Stop.\n \nexit status 2"), +// }, +// { +// name: "create dir error", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManager, +// dirManager: dirManagerMock{createErr: errors.New("error to create dir")}, +// tree: defaultTreeManager, +// }, +// want: errors.New("error to create dir"), +// }, +// { +// name: "copy so dir error", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManager, +// dirManager: dirManagerMock{data: []string{"linux"}, copyErr: errors.New("error to copy dir")}, +// tree: defaultTreeManager, +// }, +// want: errors.New("error to copy dir"), +// }, +// { +// name: "copy commons dir error", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManager, +// dirManager: dirManagerMock{data: []string{"commons"}, copyErr: errors.New("error to copy dir")}, +// tree: defaultTreeManager, +// }, +// want: errors.New("error to copy dir"), +// }, +// { +// name: "dir remove error", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManager, +// dirManager: dirManagerMock{data: []string{"commons"}, removeErr: errors.New("error to remove dir")}, +// tree: defaultTreeManager, +// }, +// want: errors.New("error to remove dir"), +// }, +// { +// name: "tree generate error", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManager, +// dirManager: dirManager, +// tree: treeGenerateMock{err: errors.New("error to generate tree")}, +// }, +// want: errors.New("error to generate tree"), +// }, +// { +// name: "write tree error", +// in: in{ +// formulaPath: formulaPath, +// fileManager: fileManagerMock{writeErr: errors.New("error to write tree")}, +// dirManager: dirManager, +// tree: defaultTreeManager, +// }, +// want: errors.New("error to write tree"), +// }, +// { +// name: "chdir error", +// in: in{ +// formulaPath: "invalid", +// fileManager: fileManager, +// dirManager: dirManager, +// tree: defaultTreeManager, +// }, +// want: errors.New("chdir invalid: no such file or directory"), +// }, +// } +// +// for _, tt := range testes { +// t.Run(tt.name, func(t *testing.T) { +// builderManager := NewBuildLocal(ritHome, tt.in.dirManager, tt.in.fileManager, tt.in.tree) +// got := builderManager.Build(workspacePath, tt.in.formulaPath) +// +// if (tt.want == nil && got != nil) || got != nil && got.Error() != tt.want.Error() { +// t.Errorf("Build(%s) got %v, want %v", tt.name, got, tt.want) +// } +// +// if tt.want == nil { +// hasRitchieHome := dirManager.Exists(ritHome) +// if !hasRitchieHome { +// t.Errorf("Build(%s) did not create the Ritchie home directory", tt.name) +// } +// +// treeLocalFile := filepath.Join(ritHome, "repos", "local", "tree.json") +// hasTreeLocalFile := fileManager.Exists(treeLocalFile) +// if !hasTreeLocalFile { +// t.Errorf("Build(%s) did not copy the tree local file", tt.name) +// } +// +// formulaFiles := filepath.Join(ritHome, "repos", "local", "testing", "formula", "bin") +// files, err := fileManager.List(formulaFiles) +// if err == nil && len(files) != 4 { +// t.Errorf("Build(%s) did not generate bin files", tt.name) +// } +// +// configFile := filepath.Join(ritHome, "repos", "local", "testing", "formula", "config.json") +// hasConfigFile := fileManager.Exists(configFile) +// if !hasConfigFile { +// t.Errorf("Build(%s) did not copy formula config", tt.name) +// } +// } +// }) +// } +// } +// +// type dirManagerMock struct { +// data []string +// createErr error +// listErr error +// copyErr error +// removeErr error +// } +// +// func (d dirManagerMock) Create(string) error { +// return d.createErr +// } +// +// func (d dirManagerMock) List(string, bool) ([]string, error) { +// return d.data, d.listErr +// } +// +// func (d dirManagerMock) Copy(string, string) error { +// return d.copyErr +// } +// +// func (d dirManagerMock) Remove(string) error { +// return d.removeErr +// } +// +// type fileManagerMock struct { +// data []byte +// readErr error +// exist bool +// writeErr error +// } +// +// func (f fileManagerMock) Read(string) ([]byte, error) { +// return f.data, f.readErr +// } +// +// func (f fileManagerMock) Exists(string) bool { +// return f.exist +// } +// +// func (f fileManagerMock) Write(string, []byte) error { +// return f.writeErr +// } +// +// type treeGenerateMock struct { +// tree formula.Tree +// err error +// } +// +// func (t treeGenerateMock) Generate(repoPath string) (formula.Tree, error) { +// return t.tree, t.err +// } diff --git a/pkg/formula/runner/docker/.env b/pkg/formula/runner/docker/.env new file mode 100755 index 000000000..d009454ce --- /dev/null +++ b/pkg/formula/runner/docker/.env @@ -0,0 +1,4 @@ +INPUT_BOOLEAN=false +CURRENT_PWD=/app +CONTEXT= +VERBOSE_MODE=false From 7b28770ee458e00a1a739441395eb843a91f735e Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Mon, 28 Sep 2020 10:54:49 -0300 Subject: [PATCH 2/5] change cmd to horusec Signed-off-by: victor-schumacher --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2fc5616ec..6a61ffd9d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -114,7 +114,7 @@ jobs: name: Horus Security Analysis command: | curl -fsSL https://horus-assets.s3.amazonaws.com/install.sh | bash - horus start -p ./ -i testdata,vendor + horusec start -p ./ -i testdata,vendor unit_test: executor: ritchie-tests-and-static-analisys-executor From d97ac38c6fbd42654b86e6f20e419122225ec13c Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Mon, 28 Sep 2020 10:59:45 -0300 Subject: [PATCH 3/5] remove comments Signed-off-by: victor-schumacher --- pkg/cmd/create_formula_test.go | 6 +- pkg/formula/builder/local_test.go | 454 +++++++++++++++--------------- pkg/formula/runner/docker/.env | 5 +- 3 files changed, 229 insertions(+), 236 deletions(-) diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index 7c2987952..c9abba6b6 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -127,20 +127,16 @@ func TestCreateFormulaCmd(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - //inputTextValidator := prompt.NewSurveyTextValidator() - createFormulaCmd := NewCreateFormulaCmd( os.TempDir(), formCreator{}, tt.in.tm, workspaceForm{}, tt.in.inText, - tt.in.inTextValidator, - //inputTextValidator, + tt.in.inTextValidator, tt.in.inList, TutorialFinderMock{}, ) - createFormulaCmd.PersistentFlags().Bool("stdin", false, "input by stdin") if err := createFormulaCmd.Execute(); (err != nil) != tt.wantErr { t.Errorf("%s = %v, want %v", createFormulaCmd.Use, err, nil) diff --git a/pkg/formula/builder/local_test.go b/pkg/formula/builder/local_test.go index 5cb03af8b..e2033b9a6 100644 --- a/pkg/formula/builder/local_test.go +++ b/pkg/formula/builder/local_test.go @@ -16,230 +16,230 @@ package builder -// import ( -// "errors" -// "fmt" -// "path/filepath" -// "testing" -// -// "github.com/ZupIT/ritchie-cli/pkg/formula" -// "github.com/ZupIT/ritchie-cli/pkg/formula/tree" -// "github.com/ZupIT/ritchie-cli/pkg/stream" -// "github.com/ZupIT/ritchie-cli/pkg/stream/streams" -// ) -// -// func TestBuild(t *testing.T) { -// workspacePath := filepath.Join(tmpDir, "ritchie-formulas-test") -// formulaPath := filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "formula") -// fileManager := stream.NewFileManager() -// dirManager := stream.NewDirManager(fileManager) -// defaultTreeManager := tree.NewGenerator(dirManager, fileManager) -// -// _ = dirManager.Remove(workspacePath) -// _ = dirManager.Create(workspacePath) -// -// zipFile := filepath.Join("..", "..", "..", "testdata", "ritchie-formulas-test.zip") -// _ = streams.Unzip(zipFile, workspacePath) -// -// type in struct { -// formulaPath string -// fileManager stream.FileWriteReadExister -// dirManager stream.DirCreateListCopyRemover -// tree formula.TreeGenerator -// } -// -// testes := []struct { -// name string -// in in -// want error -// }{ -// { -// name: "success", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManager, -// dirManager: dirManager, -// tree: defaultTreeManager, -// }, -// want: nil, -// }, -// { -// name: "success build without build.sh", -// in: in{ -// formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-sh"), -// fileManager: fileManager, -// dirManager: dirManager, -// tree: defaultTreeManager, -// }, -// want: nil, -// }, -// { -// name: "error try build without files", -// in: in{ -// formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-files"), -// fileManager: fileManager, -// dirManager: dirManager, -// tree: defaultTreeManager, -// }, -// want: fmt.Errorf("Build error: \nmake: *** No rule to make target 'build'. Stop.\n \nexit status 2"), -// }, -// { -// name: "create dir error", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManager, -// dirManager: dirManagerMock{createErr: errors.New("error to create dir")}, -// tree: defaultTreeManager, -// }, -// want: errors.New("error to create dir"), -// }, -// { -// name: "copy so dir error", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManager, -// dirManager: dirManagerMock{data: []string{"linux"}, copyErr: errors.New("error to copy dir")}, -// tree: defaultTreeManager, -// }, -// want: errors.New("error to copy dir"), -// }, -// { -// name: "copy commons dir error", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManager, -// dirManager: dirManagerMock{data: []string{"commons"}, copyErr: errors.New("error to copy dir")}, -// tree: defaultTreeManager, -// }, -// want: errors.New("error to copy dir"), -// }, -// { -// name: "dir remove error", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManager, -// dirManager: dirManagerMock{data: []string{"commons"}, removeErr: errors.New("error to remove dir")}, -// tree: defaultTreeManager, -// }, -// want: errors.New("error to remove dir"), -// }, -// { -// name: "tree generate error", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManager, -// dirManager: dirManager, -// tree: treeGenerateMock{err: errors.New("error to generate tree")}, -// }, -// want: errors.New("error to generate tree"), -// }, -// { -// name: "write tree error", -// in: in{ -// formulaPath: formulaPath, -// fileManager: fileManagerMock{writeErr: errors.New("error to write tree")}, -// dirManager: dirManager, -// tree: defaultTreeManager, -// }, -// want: errors.New("error to write tree"), -// }, -// { -// name: "chdir error", -// in: in{ -// formulaPath: "invalid", -// fileManager: fileManager, -// dirManager: dirManager, -// tree: defaultTreeManager, -// }, -// want: errors.New("chdir invalid: no such file or directory"), -// }, -// } -// -// for _, tt := range testes { -// t.Run(tt.name, func(t *testing.T) { -// builderManager := NewBuildLocal(ritHome, tt.in.dirManager, tt.in.fileManager, tt.in.tree) -// got := builderManager.Build(workspacePath, tt.in.formulaPath) -// -// if (tt.want == nil && got != nil) || got != nil && got.Error() != tt.want.Error() { -// t.Errorf("Build(%s) got %v, want %v", tt.name, got, tt.want) -// } -// -// if tt.want == nil { -// hasRitchieHome := dirManager.Exists(ritHome) -// if !hasRitchieHome { -// t.Errorf("Build(%s) did not create the Ritchie home directory", tt.name) -// } -// -// treeLocalFile := filepath.Join(ritHome, "repos", "local", "tree.json") -// hasTreeLocalFile := fileManager.Exists(treeLocalFile) -// if !hasTreeLocalFile { -// t.Errorf("Build(%s) did not copy the tree local file", tt.name) -// } -// -// formulaFiles := filepath.Join(ritHome, "repos", "local", "testing", "formula", "bin") -// files, err := fileManager.List(formulaFiles) -// if err == nil && len(files) != 4 { -// t.Errorf("Build(%s) did not generate bin files", tt.name) -// } -// -// configFile := filepath.Join(ritHome, "repos", "local", "testing", "formula", "config.json") -// hasConfigFile := fileManager.Exists(configFile) -// if !hasConfigFile { -// t.Errorf("Build(%s) did not copy formula config", tt.name) -// } -// } -// }) -// } -// } -// -// type dirManagerMock struct { -// data []string -// createErr error -// listErr error -// copyErr error -// removeErr error -// } -// -// func (d dirManagerMock) Create(string) error { -// return d.createErr -// } -// -// func (d dirManagerMock) List(string, bool) ([]string, error) { -// return d.data, d.listErr -// } -// -// func (d dirManagerMock) Copy(string, string) error { -// return d.copyErr -// } -// -// func (d dirManagerMock) Remove(string) error { -// return d.removeErr -// } -// -// type fileManagerMock struct { -// data []byte -// readErr error -// exist bool -// writeErr error -// } -// -// func (f fileManagerMock) Read(string) ([]byte, error) { -// return f.data, f.readErr -// } -// -// func (f fileManagerMock) Exists(string) bool { -// return f.exist -// } -// -// func (f fileManagerMock) Write(string, []byte) error { -// return f.writeErr -// } -// -// type treeGenerateMock struct { -// tree formula.Tree -// err error -// } -// -// func (t treeGenerateMock) Generate(repoPath string) (formula.Tree, error) { -// return t.tree, t.err -// } +import ( + "errors" + "fmt" + "path/filepath" + "testing" + + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/formula/tree" + "github.com/ZupIT/ritchie-cli/pkg/stream" + "github.com/ZupIT/ritchie-cli/pkg/stream/streams" +) + +func TestBuild(t *testing.T) { + workspacePath := filepath.Join(tmpDir, "ritchie-formulas-test") + formulaPath := filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "formula") + fileManager := stream.NewFileManager() + dirManager := stream.NewDirManager(fileManager) + defaultTreeManager := tree.NewGenerator(dirManager, fileManager) + + _ = dirManager.Remove(workspacePath) + _ = dirManager.Create(workspacePath) + + zipFile := filepath.Join("..", "..", "..", "testdata", "ritchie-formulas-test.zip") + _ = streams.Unzip(zipFile, workspacePath) + + type in struct { + formulaPath string + fileManager stream.FileWriteReadExister + dirManager stream.DirCreateListCopyRemover + tree formula.TreeGenerator + } + + testes := []struct { + name string + in in + want error + }{ + { + name: "success", + in: in{ + formulaPath: formulaPath, + fileManager: fileManager, + dirManager: dirManager, + tree: defaultTreeManager, + }, + want: nil, + }, + { + name: "success build without build.sh", + in: in{ + formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-sh"), + fileManager: fileManager, + dirManager: dirManager, + tree: defaultTreeManager, + }, + want: nil, + }, + { + name: "error try build without files", + in: in{ + formulaPath: filepath.Join(tmpDir, "ritchie-formulas-test", "testing", "without-build-files"), + fileManager: fileManager, + dirManager: dirManager, + tree: defaultTreeManager, + }, + want: fmt.Errorf("Build error: \nmake: *** No rule to make target 'build'. Stop.\n \nexit status 2"), + }, + { + name: "create dir error", + in: in{ + formulaPath: formulaPath, + fileManager: fileManager, + dirManager: dirManagerMock{createErr: errors.New("error to create dir")}, + tree: defaultTreeManager, + }, + want: errors.New("error to create dir"), + }, + { + name: "copy so dir error", + in: in{ + formulaPath: formulaPath, + fileManager: fileManager, + dirManager: dirManagerMock{data: []string{"linux"}, copyErr: errors.New("error to copy dir")}, + tree: defaultTreeManager, + }, + want: errors.New("error to copy dir"), + }, + { + name: "copy commons dir error", + in: in{ + formulaPath: formulaPath, + fileManager: fileManager, + dirManager: dirManagerMock{data: []string{"commons"}, copyErr: errors.New("error to copy dir")}, + tree: defaultTreeManager, + }, + want: errors.New("error to copy dir"), + }, + { + name: "dir remove error", + in: in{ + formulaPath: formulaPath, + fileManager: fileManager, + dirManager: dirManagerMock{data: []string{"commons"}, removeErr: errors.New("error to remove dir")}, + tree: defaultTreeManager, + }, + want: errors.New("error to remove dir"), + }, + { + name: "tree generate error", + in: in{ + formulaPath: formulaPath, + fileManager: fileManager, + dirManager: dirManager, + tree: treeGenerateMock{err: errors.New("error to generate tree")}, + }, + want: errors.New("error to generate tree"), + }, + { + name: "write tree error", + in: in{ + formulaPath: formulaPath, + fileManager: fileManagerMock{writeErr: errors.New("error to write tree")}, + dirManager: dirManager, + tree: defaultTreeManager, + }, + want: errors.New("error to write tree"), + }, + { + name: "chdir error", + in: in{ + formulaPath: "invalid", + fileManager: fileManager, + dirManager: dirManager, + tree: defaultTreeManager, + }, + want: errors.New("chdir invalid: no such file or directory"), + }, + } + + for _, tt := range testes { + t.Run(tt.name, func(t *testing.T) { + builderManager := NewBuildLocal(ritHome, tt.in.dirManager, tt.in.fileManager, tt.in.tree) + got := builderManager.Build(workspacePath, tt.in.formulaPath) + + if (tt.want == nil && got != nil) || got != nil && got.Error() != tt.want.Error() { + t.Errorf("Build(%s) got %v, want %v", tt.name, got, tt.want) + } + + if tt.want == nil { + hasRitchieHome := dirManager.Exists(ritHome) + if !hasRitchieHome { + t.Errorf("Build(%s) did not create the Ritchie home directory", tt.name) + } + + treeLocalFile := filepath.Join(ritHome, "repos", "local", "tree.json") + hasTreeLocalFile := fileManager.Exists(treeLocalFile) + if !hasTreeLocalFile { + t.Errorf("Build(%s) did not copy the tree local file", tt.name) + } + + formulaFiles := filepath.Join(ritHome, "repos", "local", "testing", "formula", "bin") + files, err := fileManager.List(formulaFiles) + if err == nil && len(files) != 4 { + t.Errorf("Build(%s) did not generate bin files", tt.name) + } + + configFile := filepath.Join(ritHome, "repos", "local", "testing", "formula", "config.json") + hasConfigFile := fileManager.Exists(configFile) + if !hasConfigFile { + t.Errorf("Build(%s) did not copy formula config", tt.name) + } + } + }) + } +} + +type dirManagerMock struct { + data []string + createErr error + listErr error + copyErr error + removeErr error +} + +func (d dirManagerMock) Create(string) error { + return d.createErr +} + +func (d dirManagerMock) List(string, bool) ([]string, error) { + return d.data, d.listErr +} + +func (d dirManagerMock) Copy(string, string) error { + return d.copyErr +} + +func (d dirManagerMock) Remove(string) error { + return d.removeErr +} + +type fileManagerMock struct { + data []byte + readErr error + exist bool + writeErr error +} + +func (f fileManagerMock) Read(string) ([]byte, error) { + return f.data, f.readErr +} + +func (f fileManagerMock) Exists(string) bool { + return f.exist +} + +func (f fileManagerMock) Write(string, []byte) error { + return f.writeErr +} + +type treeGenerateMock struct { + tree formula.Tree + err error +} + +func (t treeGenerateMock) Generate(repoPath string) (formula.Tree, error) { + return t.tree, t.err +} diff --git a/pkg/formula/runner/docker/.env b/pkg/formula/runner/docker/.env index d009454ce..8b1378917 100755 --- a/pkg/formula/runner/docker/.env +++ b/pkg/formula/runner/docker/.env @@ -1,4 +1 @@ -INPUT_BOOLEAN=false -CURRENT_PWD=/app -CONTEXT= -VERBOSE_MODE=false + From 609f6a83ea130736d84306274fe3b099a0f87f48 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Mon, 28 Sep 2020 11:00:47 -0300 Subject: [PATCH 4/5] remove comments Signed-off-by: victor-schumacher --- pkg/cmd/create_formula_test.go | 1 - pkg/formula/runner/docker/.env | 1 - 2 files changed, 2 deletions(-) diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index c9abba6b6..6e7f9e6e9 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -81,7 +81,6 @@ func TestCreateFormulaCmd(t *testing.T) { }, wantErr: true, }, - { name: "error on template manager Validate func", in: in{ diff --git a/pkg/formula/runner/docker/.env b/pkg/formula/runner/docker/.env index 8b1378917..e69de29bb 100755 --- a/pkg/formula/runner/docker/.env +++ b/pkg/formula/runner/docker/.env @@ -1 +0,0 @@ - From 25be1f77a7be7e7901792f86f7563c3317af04f7 Mon Sep 17 00:00:00 2001 From: victor-schumacher Date: Mon, 28 Sep 2020 11:01:44 -0300 Subject: [PATCH 5/5] remove .env file Signed-off-by: victor-schumacher --- pkg/formula/runner/docker/.env | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 pkg/formula/runner/docker/.env diff --git a/pkg/formula/runner/docker/.env b/pkg/formula/runner/docker/.env deleted file mode 100755 index e69de29bb..000000000