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
5 changes: 4 additions & 1 deletion transform/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/compose-spec/compose-go/v2/tree"
)

func transformSSH(data any, p tree.Path, _ bool) (any, error) {
func transformSSH(data any, p tree.Path, ignoreParseError bool) (any, error) {
switch v := data.(type) {
case map[string]any:
return v, nil
Expand All @@ -37,6 +37,9 @@ func transformSSH(data any, p tree.Path, _ bool) (any, error) {
id, path, ok := strings.Cut(s, "=")
if !ok {
if id != "default" {
if ignoreParseError {
return data, nil
}
return nil, fmt.Errorf("invalid ssh key %q", s)
}
result[id] = nil
Expand Down
42 changes: 42 additions & 0 deletions transform/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package transform

import (
"reflect"
"testing"

"github.com/compose-spec/compose-go/v2/tree"
Expand All @@ -34,3 +35,44 @@ func TestSSHConfig(t *testing.T) {
"foo": "bar",
})
}

func Test_transformSSH_ignoreParseError(t *testing.T) {
tests := []struct {
name string
yaml any
ignoreParseError bool
want any
wantErr string
}{
{
name: "unresolved variable, error",
yaml: []any{
"${SSH_AUTH_SOCK}",
},
wantErr: `invalid ssh key "${SSH_AUTH_SOCK}"`,
},
{
name: "unresolved variable, ignored",
yaml: []any{
"${SSH_AUTH_SOCK}",
},
ignoreParseError: true,
want: []any{
"${SSH_AUTH_SOCK}",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := transformSSH(tt.yaml, tree.NewPath("test"), tt.ignoreParseError)
if tt.wantErr != "" {
assert.Error(t, err, tt.wantErr)
return
}
assert.NilError(t, err)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("transformSSH() got = %v, want %v", got, tt.want)
}
})
}
}
9 changes: 7 additions & 2 deletions transform/ulimits.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import (
"github.com/compose-spec/compose-go/v2/tree"
)

func transformUlimits(data any, p tree.Path, _ bool) (any, error) {
func transformUlimits(data any, p tree.Path, ignoreParseError bool) (any, error) {
switch v := data.(type) {
case map[string]any:
return v, nil
case int:
return v, nil
case string:
if ignoreParseError {
return v, nil
}
return data, fmt.Errorf("%s: invalid type %T for ulimits", p, v)
default:
return data, fmt.Errorf("%s: invalid type %T for external", p, v)
return data, fmt.Errorf("%s: invalid type %T for ulimits", p, v)
}
}
76 changes: 76 additions & 0 deletions transform/ulimits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2020 The Compose Specification Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transform

import (
"reflect"
"testing"

"github.com/compose-spec/compose-go/v2/tree"
"gotest.tools/v3/assert"
)

func Test_transformUlimits(t *testing.T) {
tests := []struct {
name string
yaml any
ignoreParseError bool
want any
wantErr string
}{
{
name: "int",
yaml: 65535,
want: 65535,
},
{
name: "long syntax",
yaml: map[string]any{
"soft": 20000,
"hard": 40000,
},
want: map[string]any{
"soft": 20000,
"hard": 40000,
},
},
{
name: "unresolved variable, error",
yaml: "${NOFILE}",
wantErr: `test: invalid type string for ulimits`,
},
{
name: "unresolved variable, ignored",
yaml: "${NOFILE}",
ignoreParseError: true,
want: "${NOFILE}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := transformUlimits(tt.yaml, tree.NewPath("test"), tt.ignoreParseError)
if tt.wantErr != "" {
assert.Error(t, err, tt.wantErr)
return
}
assert.NilError(t, err)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("transformUlimits() got = %v, want %v", got, tt.want)
}
})
}
}