Skip to content

Commit

Permalink
Fix: Read set files only on -S,--context-set option
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 authored Nov 13, 2023
1 parent c2765e5 commit af854a1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
45 changes: 37 additions & 8 deletions pkg/builder/csolution/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 + "\""
Expand Down Expand Up @@ -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)")

Expand Down
45 changes: 43 additions & 2 deletions pkg/builder/csolution/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
})
}

0 comments on commit af854a1

Please sign in to comment.