github.com/Comcast/plax@v0.8.32/dsl/include_test.go (about)

     1  /*
     2   * Copyright 2021 Comcast Cable Communications Management, LLC
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * SPDX-License-Identifier: Apache-2.0
    17   */
    18  
    19  package dsl
    20  
    21  import (
    22  	"io/ioutil"
    23  	"os"
    24  	"testing"
    25  
    26  	"gopkg.in/yaml.v3"
    27  )
    28  
    29  func chdir(t *testing.T, dir string) {
    30  	cwd, err := os.Getwd()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	if err = os.Chdir(dir); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	t.Cleanup(func() {
    38  		if err = os.Chdir(cwd); err != nil {
    39  			t.Fatal(err)
    40  		}
    41  	})
    42  }
    43  
    44  func TestInclude(t *testing.T) {
    45  	ctx := NewCtx(nil)
    46  
    47  	bs, err := ioutil.ReadFile("../demos/include.yaml")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	ctx.IncludeDirs = []string{"../demos"}
    53  
    54  	bs, err = IncludeYAML(ctx, bs)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	var tst Test
    60  	if err = yaml.Unmarshal(bs, &tst); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	if tst.Doc == "" {
    65  		t.Fatal("Preamble not included")
    66  	}
    67  
    68  	p, have := tst.Spec.Phases["receive"]
    69  	if !have {
    70  		t.Fatal("receive not included")
    71  	}
    72  
    73  	if 0 == len(p.Steps) {
    74  		t.Fatal("receive empty")
    75  	}
    76  }