Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ type ConfigValidationIgnoreItem struct {
Identifier string `yaml:"identifier"`
// Optional to ignore only a specific path.
Path string `yaml:"path,omitempty"`
// Optional to ignore only a specific message.
Message string `yaml:"message,omitempty"`
}

func (c *ConfigValidationIgnoreItem) UnmarshalYAML(value *yaml.Node) error {
Expand All @@ -184,6 +186,7 @@ func (c *ConfigValidationIgnoreItem) UnmarshalYAML(value *yaml.Node) error {
type objectFormat struct {
Identifier string `yaml:"identifier"`
Path string `yaml:"path,omitempty"`
Message string `yaml:"message,omitempty"`
}
var obj objectFormat
if err := value.Decode(&obj); err != nil {
Expand All @@ -192,6 +195,7 @@ func (c *ConfigValidationIgnoreItem) UnmarshalYAML(value *yaml.Node) error {

c.Identifier = obj.Identifier
c.Path = obj.Path
c.Message = obj.Message

return nil
}
Expand Down
30 changes: 27 additions & 3 deletions extension/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,36 @@ func (c *ValidationContext) Warnings() []ValidationMessage {
return c.warnings
}

func (c *ValidationContext) ApplyIgnores(ignores []string) {
func (c *ValidationContext) ApplyIgnores(ignores []ConfigValidationIgnoreItem) {
for _, ignore := range ignores {
for i := 0; i < len(c.errors); i++ {
if c.errors[i].Identifier == ignore {
if c.errors[i].Identifier == ignore.Identifier && ignore.Message == "" {
c.errors = append(c.errors[:i], c.errors[i+1:]...)
i--
continue
}

if c.errors[i].Identifier == ignore.Identifier && ignore.Message != "" && strings.Contains(c.errors[i].Message, ignore.Message) {
c.errors = append(c.errors[:i], c.errors[i+1:]...)
i--
continue
}
}
}

// Apply the same logic to warnings
for _, ignore := range ignores {
for i := 0; i < len(c.warnings); i++ {
if c.warnings[i].Identifier == ignore.Identifier && ignore.Message == "" {
c.warnings = append(c.warnings[:i], c.warnings[i+1:]...)
i--
continue
}

if c.warnings[i].Identifier == ignore.Identifier && ignore.Message != "" && strings.Contains(c.warnings[i].Message, ignore.Message) {
c.warnings = append(c.warnings[:i], c.warnings[i+1:]...)
i--
continue
}
}
}
Expand All @@ -67,7 +91,7 @@ func RunValidation(ctx context.Context, ext Extension) *ValidationContext {
ext.Validate(ctx, vc)
validateAdministrationSnippets(vc)
validateStorefrontSnippets(vc)
vc.ApplyIgnores(ext.GetExtensionConfig().Validation.Ignore.Identifiers())
vc.ApplyIgnores(ext.GetExtensionConfig().Validation.Ignore)

return vc
}
Expand Down
18 changes: 17 additions & 1 deletion extension/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ func TestIgnores(t *testing.T) {
ctx.AddError("metadata.name", "Key `name` is required")
assert.True(t, ctx.HasErrors())

ctx.ApplyIgnores([]string{"metadata.name"})
ctx.ApplyIgnores([]ConfigValidationIgnoreItem{
{Identifier: "metadata.name"},
})
assert.False(t, ctx.HasErrors())
}

func TestIgnoresWithMessage(t *testing.T) {
app := getAppForValidation()

ctx := newValidationContext(app)

ctx.AddError("metadata.name", "Key `name` is required")
assert.True(t, ctx.HasErrors())

ctx.ApplyIgnores([]ConfigValidationIgnoreItem{
{Identifier: "metadata.name", Message: "Key `name` is required"},
})
assert.False(t, ctx.HasErrors())
}
109 changes: 109 additions & 0 deletions internal/curl/curl_wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package curl

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestInitCurlCommand(t *testing.T) {
t.Run("empty command", func(t *testing.T) {
cmd := InitCurlCommand()
assert.Empty(t, cmd.options)
assert.Empty(t, cmd.args)
})

t.Run("with method", func(t *testing.T) {
cmd := InitCurlCommand(Method("POST"))
assert.Len(t, cmd.options, 1)
assert.Equal(t, "-X", cmd.options[0].flag)
assert.Equal(t, "POST", cmd.options[0].value)
})

t.Run("with bearer token", func(t *testing.T) {
cmd := InitCurlCommand(BearerToken("test-token"))
assert.Len(t, cmd.options, 1)
assert.Equal(t, "--header", cmd.options[0].flag)
assert.Equal(t, "Authorization: test-token", cmd.options[0].value)
})

t.Run("with custom args", func(t *testing.T) {
cmd := InitCurlCommand(Args([]string{"-v"}))
assert.Empty(t, cmd.options)
assert.Equal(t, []string{"-v"}, cmd.args)
})

t.Run("with URL", func(t *testing.T) {
u, _ := url.Parse("https://example.com")
cmd := InitCurlCommand(Url(u))
assert.Empty(t, cmd.options)
assert.Equal(t, []string{"https://example.com"}, cmd.args)
})

t.Run("with header", func(t *testing.T) {
cmd := InitCurlCommand(Header("Content-Type", "application/json"))
assert.Len(t, cmd.options, 1)
assert.Equal(t, "--header", cmd.options[0].flag)
assert.Equal(t, "Content-Type: application/json", cmd.options[0].value)
})

t.Run("with multiple options", func(t *testing.T) {
u, _ := url.Parse("https://example.com")
cmd := InitCurlCommand(
Method("POST"),
Url(u),
Header("Content-Type", "application/json"),
BearerToken("test-token"),
)

assert.Len(t, cmd.options, 3)
assert.Equal(t, []string{"https://example.com"}, cmd.args)

cmdOptions := cmd.getCmdOptions()
expectedOptions := []string{
"-X", "POST",
"--header", "Content-Type: application/json",
"--header", "Authorization: test-token",
"https://example.com",
}
assert.Equal(t, expectedOptions, cmdOptions)
})
}

func TestCommand_getCmdOptions(t *testing.T) {
t.Run("empty command", func(t *testing.T) {
cmd := &Command{}
assert.Empty(t, cmd.getCmdOptions())
})

t.Run("with options and args", func(t *testing.T) {
cmd := &Command{
options: []curlOption{
{flag: "-X", value: "POST"},
{flag: "--header", value: "Content-Type: application/json"},
},
args: []string{"https://example.com"},
}

expected := []string{
"-X", "POST",
"--header", "Content-Type: application/json",
"https://example.com",
}
assert.Equal(t, expected, cmd.getCmdOptions())
})
}

func TestArgs_WithExistingArgs(t *testing.T) {
cmd := &Command{args: []string{"existing"}}
Args([]string{"new"})(cmd)
assert.Equal(t, []string{"existing", "new"}, cmd.args)
}

func TestUrl_WithExistingArgs(t *testing.T) {
cmd := &Command{args: []string{"existing"}}
u, _ := url.Parse("https://example.com")
Url(u)(cmd)
assert.Equal(t, []string{"https://example.com", "existing"}, cmd.args)
}