2023-08-10 11:06:00 +02:00
|
|
|
// Copyright 2023 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.
|
|
|
|
|
2021-09-27 02:38:15 +02:00
|
|
|
package schema_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-19 10:40:48 +02:00
|
|
|
"os"
|
2021-09-27 02:38:15 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-12 09:25:13 +02:00
|
|
|
|
2023-12-08 09:15:08 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter/schema"
|
2021-09-27 02:38:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSchema(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testTable := []struct {
|
|
|
|
name string
|
|
|
|
testFile string
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Branches",
|
|
|
|
testFile: ".woodpecker/test-branches.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Branches Array",
|
|
|
|
testFile: ".woodpecker/test-branches-array.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Branches exclude & include",
|
|
|
|
testFile: ".woodpecker/test-branches-exclude-include.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Clone",
|
|
|
|
testFile: ".woodpecker/test-clone.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Clone skip",
|
|
|
|
testFile: ".woodpecker/test-clone-skip.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Matrix",
|
|
|
|
testFile: ".woodpecker/test-matrix.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multi Pipeline",
|
|
|
|
testFile: ".woodpecker/test-multi.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin",
|
|
|
|
testFile: ".woodpecker/test-plugin.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Run on",
|
|
|
|
testFile: ".woodpecker/test-run-on.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service",
|
|
|
|
testFile: ".woodpecker/test-service.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Step",
|
|
|
|
testFile: ".woodpecker/test-step.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "When",
|
|
|
|
testFile: ".woodpecker/test-when.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Workspace",
|
|
|
|
testFile: ".woodpecker/test-workspace.yml",
|
|
|
|
},
|
2022-05-31 01:12:18 +02:00
|
|
|
{
|
|
|
|
name: "Platform",
|
|
|
|
testFile: ".woodpecker/test-platform.yml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Labels",
|
|
|
|
testFile: ".woodpecker/test-labels.yml",
|
|
|
|
},
|
2023-04-29 14:49:41 +02:00
|
|
|
{
|
|
|
|
name: "Map and Sequence Merge", // https://woodpecker-ci.org/docs/next/usage/advanced-yaml-syntax
|
|
|
|
testFile: ".woodpecker/test-merge-map-and-sequence.yml",
|
|
|
|
},
|
2021-09-27 02:38:15 +02:00
|
|
|
{
|
|
|
|
name: "Broken Config",
|
|
|
|
testFile: ".woodpecker/test-broken.yml",
|
|
|
|
fail: true,
|
|
|
|
},
|
2023-12-07 17:56:13 +02:00
|
|
|
{
|
|
|
|
name: "Array syntax",
|
|
|
|
testFile: ".woodpecker/test-array-syntax.yml",
|
|
|
|
fail: false,
|
|
|
|
},
|
2021-09-27 02:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testTable {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-19 10:40:48 +02:00
|
|
|
fi, err := os.Open(tt.testFile)
|
|
|
|
assert.NoError(t, err, "could not open test file")
|
|
|
|
defer fi.Close()
|
|
|
|
configErrors, err := schema.Lint(fi)
|
2021-09-27 02:38:15 +02:00
|
|
|
if tt.fail {
|
|
|
|
if len(configErrors) == 0 {
|
|
|
|
assert.Error(t, err, "Expected config errors but got none")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, fmt.Sprintf("Validation failed: %v", configErrors))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|