github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/formatters/basic/anchors/check_test.go (about)

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package anchors_test
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/braydonk/yaml"
    22  	"github.com/google/yamlfmt/formatters/basic/anchors"
    23  )
    24  
    25  func TestCheck(t *testing.T) {
    26  	for _, c := range []struct {
    27  		desc    string
    28  		in      string
    29  		wantErr bool
    30  	}{{
    31  		desc:    "no anchors",
    32  		in:      "foo: bar",
    33  		wantErr: false,
    34  	}, {
    35  		desc: "anchor",
    36  		// From https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/
    37  		in: `
    38  definitions:
    39    steps:
    40      - step: &build-test
    41          name: Build and test
    42          script:
    43            - mvn package
    44          artifacts:
    45            - target/**
    46  
    47  pipelines:
    48    branches:
    49      develop:
    50        - step: *build-test
    51      main:
    52        - step: *build-test
    53  `,
    54  		wantErr: true,
    55  	}} {
    56  		t.Run(c.desc, func(t *testing.T) {
    57  			var docNode yaml.Node
    58  			if err := yaml.NewDecoder(strings.NewReader(c.in)).Decode(&docNode); err != nil {
    59  				t.Fatalf("parse error: %v", err)
    60  			}
    61  			if err := anchors.Check(docNode); (err != nil) != c.wantErr {
    62  				t.Errorf("Check() error = %v, wantErr %v", err, c.wantErr)
    63  			}
    64  		})
    65  	}
    66  }