mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-02-13 21:00:00 +00:00
Add some tests (#6076)
This commit is contained in:
@@ -1,3 +1,17 @@
|
||||
// Copyright 2024 Woodpecker 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 output
|
||||
|
||||
import (
|
||||
|
||||
56
cli/output/output_test.go
Normal file
56
cli/output/output_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2026 Woodpecker 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 output
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseOutputOptions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := []struct {
|
||||
in string
|
||||
out string
|
||||
opts []string
|
||||
}{
|
||||
{
|
||||
in: "output",
|
||||
out: "output",
|
||||
},
|
||||
{
|
||||
in: "output=a",
|
||||
out: "output",
|
||||
opts: []string{"a"},
|
||||
},
|
||||
{
|
||||
in: "output=",
|
||||
out: "output",
|
||||
},
|
||||
{
|
||||
in: "output=a,b",
|
||||
out: "output",
|
||||
opts: []string{"a", "b"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
out, opts := ParseOutputOptions(tc.in)
|
||||
assert.Equal(t, tc.out, out)
|
||||
assert.Equal(t, tc.opts, opts)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,17 @@
|
||||
// Copyright 2024 Woodpecker 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 output
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,10 +1,25 @@
|
||||
// Copyright 2024 Woodpecker 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 output
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type writerFlusherStub struct {
|
||||
@@ -18,6 +33,7 @@ func (s writerFlusherStub) Flush() error {
|
||||
type testFieldsStruct struct {
|
||||
Name string
|
||||
Number int
|
||||
Bool bool
|
||||
}
|
||||
|
||||
func TestTableOutput(t *testing.T) {
|
||||
@@ -27,49 +43,44 @@ func TestTableOutput(t *testing.T) {
|
||||
|
||||
t.Run("AddAllowedFields", func(t *testing.T) {
|
||||
_, _ = to.AddAllowedFields(testFieldsStruct{})
|
||||
if _, ok := to.allowedFields["name"]; !ok {
|
||||
t.Error("name should be a allowed field")
|
||||
}
|
||||
_, ok := to.allowedFields["name"]
|
||||
assert.True(t, ok)
|
||||
})
|
||||
t.Run("AddFieldAlias", func(t *testing.T) {
|
||||
to.AddFieldAlias("WoodpeckerCI", "wp")
|
||||
if alias, ok := to.fieldAlias["wp"]; !ok || alias != "WoodpeckerCI" {
|
||||
t.Errorf("'wp' alias should resolve to 'WoodpeckerCI', is: %v", alias)
|
||||
}
|
||||
alias, ok := to.fieldAlias["wp"]
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "WoodpeckerCI", alias)
|
||||
})
|
||||
t.Run("AddFieldOutputFn", func(t *testing.T) {
|
||||
to.AddFieldFn("WoodpeckerCI", FieldFn(func(_ any) string {
|
||||
return "WOODPECKER CI!!!"
|
||||
}))
|
||||
if _, ok := to.fieldMapping["woodpeckerci"]; !ok {
|
||||
t.Errorf("'WoodpeckerCI' field output fn should be set")
|
||||
}
|
||||
_, ok := to.fieldMapping["woodpeckerci"]
|
||||
assert.True(t, ok)
|
||||
})
|
||||
t.Run("ValidateColumns", func(t *testing.T) {
|
||||
err := to.ValidateColumns([]string{"non-existent", "NAME"})
|
||||
if err == nil ||
|
||||
strings.Contains(err.Error(), "name") ||
|
||||
!strings.Contains(err.Error(), "non-existent") {
|
||||
t.Errorf("error should contain 'non-existent' but not 'name': %v", err)
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "non-existent")
|
||||
assert.NotContains(t, err.Error(), "name")
|
||||
|
||||
assert.NoError(t, to.ValidateColumns([]string{"name"}))
|
||||
})
|
||||
t.Run("WriteHeader", func(t *testing.T) {
|
||||
to.WriteHeader([]string{"wp", "name"})
|
||||
if wfs.String() != "WP\tNAME\n" {
|
||||
t.Errorf("written header should be 'WOODPECKER CI\\tNAME\\n', is: %q", wfs.String())
|
||||
}
|
||||
assert.Equal(t, "WP\tNAME\n", wfs.String())
|
||||
wfs.Reset()
|
||||
})
|
||||
t.Run("WriteLine", func(t *testing.T) {
|
||||
_ = to.Write([]string{"wp", "name", "number"}, &testFieldsStruct{"test123", 1000000000})
|
||||
if wfs.String() != "WOODPECKER CI!!!\ttest123\t1000000000\n" {
|
||||
t.Errorf("written line should be 'WOODPECKER CI!!!\\ttest123\\t1000000000\\n', is: %q", wfs.String())
|
||||
}
|
||||
err := to.Write([]string{"wp", "name", "number", "bool"}, &testFieldsStruct{"test123", 1000000000, true})
|
||||
assert.NoError(t, err)
|
||||
err = to.Write([]string{"wp", "name", "number", "bool"}, &testFieldsStruct{"", 1000000000, false})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "WOODPECKER CI!!!\ttest123\t1000000000\tyes\nWOODPECKER CI!!!\t-\t1000000000\tno\n", wfs.String())
|
||||
wfs.Reset()
|
||||
})
|
||||
t.Run("Columns", func(t *testing.T) {
|
||||
if len(to.Columns()) != 3 {
|
||||
t.Errorf("unexpected number of columns: %v", to.Columns())
|
||||
}
|
||||
assert.Len(t, to.Columns(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
47
pipeline/frontend/metadata/environment_test.go
Normal file
47
pipeline/frontend/metadata/environment_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2026 Woodpecker 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 metadata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEnviron(t *testing.T) {
|
||||
m := Metadata{
|
||||
Sys: System{Name: "wp"},
|
||||
Curr: Pipeline{
|
||||
Event: EventRelease,
|
||||
Commit: Commit{
|
||||
IsPrerelease: true,
|
||||
},
|
||||
},
|
||||
Prev: Pipeline{
|
||||
Event: EventPullMetadata,
|
||||
Commit: Commit{
|
||||
Refspec: "branch-a:branch-b",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
envs := m.Environ()
|
||||
assert.Equal(t, "wp", envs["CI"])
|
||||
assert.Equal(t, "release", envs["CI_PIPELINE_EVENT"])
|
||||
assert.Equal(t, "pull_request_metadata", envs["CI_PREV_PIPELINE_EVENT"])
|
||||
assert.Equal(t, "true", envs["CI_COMMIT_PRERELEASE"])
|
||||
assert.Equal(t, "branch-a", envs["CI_PREV_COMMIT_SOURCE_BRANCH"])
|
||||
assert.Equal(t, "branch-b", envs["CI_PREV_COMMIT_TARGET_BRANCH"])
|
||||
}
|
||||
@@ -21,27 +21,13 @@ import (
|
||||
)
|
||||
|
||||
func TestEnvVarSubst(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
yaml string
|
||||
environ map[string]string
|
||||
want string
|
||||
}{{
|
||||
name: "simple substitution",
|
||||
yaml: `steps:
|
||||
result, err := EnvVarSubst(`steps:
|
||||
step1:
|
||||
image: ${HELLO_IMAGE}`,
|
||||
environ: map[string]string{"HELLO_IMAGE": "hello-world"},
|
||||
want: `steps:
|
||||
image: ${HELLO_IMAGE}
|
||||
command: echo ${NEWLINE}`, map[string]string{"HELLO_IMAGE": "hello-world", "NEWLINE": "some env\nwith newline"})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, `steps:
|
||||
step1:
|
||||
image: hello-world`,
|
||||
}}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
result, err := EnvVarSubst(testCase.yaml, testCase.environ)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, testCase.want, result)
|
||||
})
|
||||
}
|
||||
image: hello-world
|
||||
command: echo "some env\nwith newline"`, result)
|
||||
}
|
||||
|
||||
@@ -53,6 +53,11 @@ func TestParse(t *testing.T) {
|
||||
assert.False(t, out.SkipClone)
|
||||
})
|
||||
|
||||
t.Run("Should fail on invalid yaml", func(t *testing.T) {
|
||||
_, err := ParseString("notvalid")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Should handle simple yaml anchors", func(t *testing.T) {
|
||||
out, err := ParseString(simpleYamlAnchors)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user