Skip to content

Commit

Permalink
Create spec from variables
Browse files Browse the repository at this point in the history
  • Loading branch information
oshratZairi committed Nov 19, 2024
1 parent b6efb65 commit 534f258
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
18 changes: 17 additions & 1 deletion common/spec/specfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package spec

import (
"encoding/json"

"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
clientutils "github.com/jfrog/jfrog-client-go/utils"
Expand Down Expand Up @@ -39,6 +38,23 @@ func CreateSpecFromFile(specFilePath string, specVars map[string]string) (spec *
return
}

func CreateSpecFromVariables(buildName string, buildNumber string) (*SpecFiles, error) {
if buildName == "" || buildNumber == "" {
return nil, errorutils.CheckErrorf("build name and build number must be provided")
}

buildString := buildName + "/" + buildNumber
specFile := &SpecFiles{
Files: []File{
{
Build: buildString,
},
},
}

return specFile, nil
}

type File struct {
Aql utils.Aql
PathMapping utils.PathMapping
Expand Down
43 changes: 43 additions & 0 deletions common/spec/specfiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spec

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCreateSpecFromVariables(t *testing.T) {
t.Run("Valid Inputs", func(t *testing.T) {
spec, err := CreateSpecFromVariables("Common-builds", "1.2.0")

assert.NoError(t, err)
assert.NotNil(t, spec)
assert.Equal(t, "Common-builds/1.2.0", spec.Files[0].Build)
})

t.Run("Missing Build Name", func(t *testing.T) {
spec, err := CreateSpecFromVariables("", "1.2.0")

assert.Error(t, err)
assert.Nil(t, spec)
assert.EqualError(t, err, "build name and build number must be provided")
})

t.Run("Missing Build Number", func(t *testing.T) {
spec, err := CreateSpecFromVariables("Common-builds", "")

// Assert that an error is returned and the spec is nil
assert.Error(t, err)
assert.Nil(t, spec)
assert.EqualError(t, err, "build name and build number must be provided")
})

// Test case when both buildName and buildNumber are empty
t.Run("Empty Build Name and Build Number", func(t *testing.T) {
spec, err := CreateSpecFromVariables("", "")

// Assert that an error is returned and the spec is nil
assert.Error(t, err)
assert.Nil(t, spec)
assert.EqualError(t, err, "build name and build number must be provided")
})
}

0 comments on commit 534f258

Please sign in to comment.