From af854a13e36bdd94bc2aa10517bda28b4dd26db0 Mon Sep 17 00:00:00 2001 From: Sourabh Mehta <73165318+soumeh01@users.noreply.github.com> Date: Mon, 13 Nov 2023 11:27:30 +0100 Subject: [PATCH] Fix: Read set files only on -S,--context-set option --- pkg/builder/csolution/builder.go | 45 ++++++++++++++++++++++----- pkg/builder/csolution/builder_test.go | 45 +++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/pkg/builder/csolution/builder.go b/pkg/builder/csolution/builder.go index 270c9b89..9292525e 100644 --- a/pkg/builder/csolution/builder.go +++ b/pkg/builder/csolution/builder.go @@ -134,13 +134,26 @@ func (b CSolutionBuilder) getCprjFilePath(idxFile string, context string) (strin return cprjPath, err } -func (b CSolutionBuilder) getSelectedContexts(cbuildSetFile string) ([]string, error) { +func (b CSolutionBuilder) getSelectedContexts(filePath string) ([]string, error) { var contexts []string - data, err := utils.ParseCbuildSetFile(cbuildSetFile) - if err == nil { - contexts = append(contexts, data.ContextSet.Contexts...) + var retErr error + + if b.Options.UseContextSet { + data, err := utils.ParseCbuildSetFile(filePath) + if err == nil { + contexts = append(contexts, data.ContextSet.Contexts...) + } + retErr = err + } else { + data, err := utils.ParseCbuildIndexFile(filePath) + if err == nil { + for _, cbuild := range data.BuildIdx.Cbuilds { + contexts = append(contexts, cbuild.Project+cbuild.Configuration) + } + } + retErr = err } - return contexts, err + return contexts, retErr } func (b CSolutionBuilder) getCSolutionPath() (path string, err error) { @@ -165,6 +178,16 @@ func (b CSolutionBuilder) getIdxFilePath() (string, error) { return idxFilePath, nil } +func (b CSolutionBuilder) getCbuildSetFilePath() (string, error) { + projName, err := utils.GetProjectName(b.InputFile) + if err != nil { + return "", err + } + + setFilePath := utils.NormalizePath(filepath.Join(filepath.Dir(b.InputFile), projName+".cbuild-set.yml")) + return setFilePath, nil +} + func (b CSolutionBuilder) getCprjsBuilders(selectedContexts []string) (cprjBuilders []cproject.CprjBuilder, err error) { for _, context := range selectedContexts { infoMsg := "Retrieve build information for context: \"" + context + "\"" @@ -367,17 +390,23 @@ func (b CSolutionBuilder) Build() (err error) { return err } - projName, err := utils.GetProjectName(b.InputFile) + var filePath string + if b.Options.UseContextSet { + filePath, err = b.getCbuildSetFilePath() + } else { + filePath, err = b.getIdxFilePath() + } + if err != nil { return err } - setFile := utils.NormalizePath(filepath.Join(filepath.Dir(b.InputFile), projName+".cbuild-set.yml")) // get list of selected contexts - selectedContexts, err := b.getSelectedContexts(setFile) + selectedContexts, err := b.getSelectedContexts(filePath) if err != nil { return err } + totalContexts := strconv.Itoa(len(selectedContexts)) log.Info("Processing " + totalContexts + " context(s)") diff --git a/pkg/builder/csolution/builder_test.go b/pkg/builder/csolution/builder_test.go index bbcca1cf..d6cae426 100644 --- a/pkg/builder/csolution/builder_test.go +++ b/pkg/builder/csolution/builder_test.go @@ -365,23 +365,38 @@ func TestGetSelectedContexts(t *testing.T) { }, } - t.Run("test set file missing", func(t *testing.T) { + t.Run("test with missing set file", func(t *testing.T) { + b.Options.UseContextSet = true contexts, err := b.getSelectedContexts("missingfile.cbuild-set.yml") assert.Error(err) assert.Len(contexts, 0) }) - t.Run("test get cprj file path", func(t *testing.T) { + t.Run("test get contexts from set file", func(t *testing.T) { expectedContexts := []string{ "test2.Debug+CM0", "test2.Debug+CM3", "test1.Debug+CM0", "test1.Release+CM0", } + b.Options.UseContextSet = true contexts, err := b.getSelectedContexts(testSetFile) assert.Nil(err) assert.Equal(contexts, expectedContexts) }) + + t.Run("test get contexts from idx file", func(t *testing.T) { + expectedContexts := []string{ + "HelloWorld_cm0plus.Debug+FRDM-K32L3A6", + "HelloWorld_cm0plus.Release+FRDM-K32L3A6", + "HelloWorld_cm4.Debug+FRDM-K32L3A6", + "HelloWorld_cm4.Release+FRDM-K32L3A6", + } + b.Options.UseContextSet = false + contexts, err := b.getSelectedContexts(testRoot + "/run/Test.cbuild-idx.yml") + assert.Nil(err) + assert.Equal(contexts, expectedContexts) + }) } func TestGetIdxFilePath(t *testing.T) { @@ -447,3 +462,29 @@ func TestFormulateArg(t *testing.T) { assert.Equal("convert --solution=../../../test/run/Test.csolution.yml --no-check-schema --no-update-rte --context=test.Debug+Target --context=test.Release+Target --context-set", strArg) }) } + +func TestGetCbuildSetFilePath(t *testing.T) { + assert := assert.New(t) + + b := CSolutionBuilder{ + BuilderParams: builder.BuilderParams{ + Runner: RunnerMock{}, + }, + } + + t.Run("test invalid input file", func(t *testing.T) { + b.InputFile = "run/TestSolution/invalid_file.yml" + + path, err := b.getCbuildSetFilePath() + assert.Error(err) + assert.Equal(path, "") + }) + + t.Run("test get cbuild-set file path", func(t *testing.T) { + b.InputFile = "run/TestSolution/test.csolution.yml" + + path, err := b.getCbuildSetFilePath() + assert.Nil(err) + assert.Equal(path, "run/TestSolution/test.cbuild-set.yml") + }) +}