github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/quarkus/create_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package quarkus 21 22 import ( 23 "fmt" 24 "os" 25 "os/exec" 26 "strconv" 27 "testing" 28 29 "github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common" 30 "github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata" 31 "github.com/spf13/afero" 32 ) 33 34 type testCreate struct { 35 input CreateQuarkusProjectConfig 36 existingProject bool 37 } 38 39 var testRunCreateSuccess = []testCreate{ 40 {input: CreateQuarkusProjectConfig{ProjectName: "new-project", Extensions: ""}}, 41 {input: CreateQuarkusProjectConfig{ 42 ProjectName: "second-project", 43 Extensions: "extension-name", 44 DependenciesVersion: metadata.DependenciesVersion{ 45 QuarkusPlatformGroupId: "io.quarkus.platform", 46 QuarkusVersion: "2.16.0.Final", 47 }, 48 }}, 49 } 50 var testRunCreateFail = []testCreate{ 51 {input: CreateQuarkusProjectConfig{ProjectName: "test-data"}, existingProject: true}, 52 {input: CreateQuarkusProjectConfig{ProjectName: "wrong*project/name"}}, 53 } 54 55 func fakeRunCreate(testIndex int) func(command string, args ...string) *exec.Cmd { 56 return func(command string, args ...string) *exec.Cmd { 57 cs := []string{"-test.run=TestHelperRunCreate", "--", command} 58 cs = append(cs, args...) 59 cmd := exec.Command(os.Args[0], cs...) 60 cmd.Env = []string{fmt.Sprintf("GO_TEST_HELPER_RUN_CREATE_IMAGE=%d", testIndex)} 61 return cmd 62 } 63 } 64 65 func TestHelperRunCreate(t *testing.T) { 66 testIndex, err := strconv.Atoi(os.Getenv("GO_TEST_HELPER_RUN_CREATE_IMAGE")) 67 if err != nil { 68 return 69 } 70 fmt.Fprintf(os.Stdout, "%v", testRunCreateSuccess[testIndex].input.ProjectName) 71 os.Exit(0) 72 } 73 74 func TestRunCreate_Success(t *testing.T) { 75 for testIndex, test := range testRunCreateSuccess { 76 common.ExecCommand = fakeRunCreate(testIndex) 77 defer func() { common.ExecCommand = exec.Command }() 78 79 err := runCreateProject(test.input) 80 if err != nil { 81 t.Errorf("Expected nil error, got %#v", err) 82 } 83 } 84 } 85 86 func TestRunCreate_Fail(t *testing.T) { 87 common.FS = afero.NewMemMapFs() 88 for testIndex, test := range testRunCreateFail { 89 if test.existingProject == true { 90 common.CreateFolderStructure(t, test.input.ProjectName) 91 } 92 common.ExecCommand = fakeRunCreate(testIndex) 93 defer func() { common.ExecCommand = exec.Command }() 94 95 err := runCreateProject(test.input) 96 if err == nil { 97 t.Errorf("Expected error, got pass") 98 } 99 if test.existingProject == true { 100 common.DeleteFolderStructure(t, test.input.ProjectName) 101 } 102 } 103 }