github.com/SAP/cloud-mta-build-tool@v1.2.27/internal/buildops/modules_deps_test.go (about)

     1  package buildops
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/pkg/errors"
    14  
    15  	"github.com/SAP/cloud-mta-build-tool/internal/archive"
    16  	"github.com/SAP/cloud-mta/mta"
    17  	"strings"
    18  )
    19  
    20  var _ = Describe("ModulesDeps", func() {
    21  
    22  	var _ = Describe("Process Dependencies test", func() {
    23  		AfterEach(func() {
    24  			os.RemoveAll(getTestPath("result"))
    25  		})
    26  
    27  		It("Sanity", func() {
    28  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getTestPath("result"), MtaFilename: "mtaWithBuildParams.yaml"}
    29  			Ω(ProcessDependencies(&ep, &ep, "ui5app")).Should(Succeed())
    30  		})
    31  		It("Invalid artifacts", func() {
    32  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getTestPath("result"), MtaFilename: "mtaWithBuildParamsWithWrongArtifacts.yaml"}
    33  			Ω(ProcessDependencies(&ep, &ep, "ui5app")).Should(HaveOccurred())
    34  		})
    35  		It("Invalid mta", func() {
    36  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), MtaFilename: "mta1.yaml"}
    37  			Ω(ProcessDependencies(&ep, &ep, "ui5app")).Should(HaveOccurred())
    38  		})
    39  		It("Invalid module name", func() {
    40  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5")}
    41  			Ω(ProcessDependencies(&ep, &ep, "xxx")).Should(HaveOccurred())
    42  		})
    43  		It("Invalid module name", func() {
    44  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), MtaFilename: "mtaWithWrongBuildParams.yaml"}
    45  			Ω(ProcessDependencies(&ep, &ep, "ui5app")).Should(HaveOccurred())
    46  		})
    47  	})
    48  
    49  	It("Resolve dependencies - Valid case", func() {
    50  		wd, _ := os.Getwd()
    51  		ep := dir.Loc{SourcePath: filepath.Join(wd, "testdata"), MtaFilename: "mta_multiapps.yaml"}
    52  		mtaStr, _ := ep.ParseFile()
    53  		actual, _ := getModulesOrder(mtaStr)
    54  		// last module depends on others
    55  		Ω(actual[len(actual)-1]).Should(Equal("eb-uideployer"))
    56  	})
    57  
    58  	It("Resolve dependencies - cyclic dependencies", func() {
    59  		wd, _ := os.Getwd()
    60  		ep := dir.Loc{SourcePath: filepath.Join(wd, "testdata"), MtaFilename: "mta_multiapps_cyclic_deps.yaml"}
    61  		mtaStr, _ := ep.ParseFile()
    62  		_, err := getModulesOrder(mtaStr)
    63  		Ω(err).Should(HaveOccurred())
    64  		Ω(err.Error()).Should(ContainSubstring("eb-ui-conf-eb"))
    65  	})
    66  
    67  	var _ = Describe("GetModulesNames", func() {
    68  		It("Sanity", func() {
    69  			mtaStr := &mta.MTA{Modules: []*mta.Module{{Name: "someproj-db"}, {Name: "someproj-java"}}}
    70  			Ω(GetModulesNames(mtaStr)).Should(Equal([]string{"someproj-db", "someproj-java"}))
    71  		})
    72  		It("Required module not defined", func() {
    73  			mtaContent := readFile(getTestPath("mtahtml5", "mtaRequiredModuleNotDefined.yaml"))
    74  			mtaStr, _ := mta.Unmarshal(mtaContent)
    75  			_, err := GetModulesNames(mtaStr)
    76  			Ω(err.Error()).Should(Equal(`the "abc" module is not defined`))
    77  		})
    78  	})
    79  })
    80  
    81  func readFile(file string) []byte {
    82  	content, err := ioutil.ReadFile(file)
    83  	Ω(err).Should(Succeed())
    84  	s := string(content)
    85  	s = strings.Replace(s, "\r\n", "\r", -1)
    86  	content = []byte(s)
    87  	return content
    88  }
    89  
    90  func executeAndProvideOutput(execute func() error) (string, error) {
    91  	old := os.Stdout // keep backup of the real stdout
    92  	r, w, err := os.Pipe()
    93  	if err != nil {
    94  		return "", err
    95  	}
    96  	os.Stdout = w
    97  
    98  	err = execute()
    99  
   100  	outC := make(chan string)
   101  	// copy the output in a separate goroutine so printing can't block indefinitely
   102  	go func() {
   103  		var buf bytes.Buffer
   104  		_, err := io.Copy(&buf, r)
   105  		if err != nil {
   106  			fmt.Println(err)
   107  		}
   108  		outC <- buf.String()
   109  	}()
   110  
   111  	os.Stdout = old // restoring the real stdout
   112  	// back to normal state
   113  	_ = w.Close()
   114  	out := <-outC
   115  	return out, err
   116  }
   117  
   118  var _ = Describe("Provide", func() {
   119  	It("Valid path to yaml", func() {
   120  
   121  		out, err := executeAndProvideOutput(func() error {
   122  			return ProvideModules(filepath.Join("testdata", "mtahtml5"), "dev", nil, os.Getwd)
   123  		})
   124  		Ω(err).Should(Succeed())
   125  		Ω(out).Should(ContainSubstring("[ui5app ui5app2]"))
   126  	})
   127  
   128  	It("Invalid path to yaml", func() {
   129  		Ω(ProvideModules(filepath.Join("testdata", "mtahtml6"), "dev", nil, os.Getwd)).Should(HaveOccurred())
   130  	})
   131  
   132  	It("Invalid modules dependencies", func() {
   133  		Ω(ProvideModules(filepath.Join("testdata", "testWithWrongBuildParams"), "dev", nil, os.Getwd)).Should(HaveOccurred())
   134  	})
   135  
   136  	It("Invalid working folder getter", func() {
   137  		Ω(ProvideModules("", "dev", nil, func() (string, error) {
   138  			return "", errors.New("err")
   139  		})).Should(HaveOccurred())
   140  	})
   141  
   142  })