github.com/yourbase/yb@v0.7.1/package_test.go (about)

     1  // Copyright 2021 YourBase Inc.
     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  //		 https://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  // SPDX-License-Identifier: Apache-2.0
    16  
    17  package yb
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  func TestBuildOrder(t *testing.T) {
    28  	tests := []struct {
    29  		name       string
    30  		desired    []string
    31  		acceptable [][]string
    32  	}{
    33  		{
    34  			name:    "Empty",
    35  			desired: []string{},
    36  			acceptable: [][]string{
    37  				{},
    38  			},
    39  		},
    40  		{
    41  			name:    "Leaf",
    42  			desired: []string{"default"},
    43  			acceptable: [][]string{
    44  				{"default"},
    45  			},
    46  		},
    47  		{
    48  			name:    "DirectDep",
    49  			desired: []string{"a"},
    50  			acceptable: [][]string{
    51  				{"b", "a"},
    52  			},
    53  		},
    54  		{
    55  			name:    "SharedDep",
    56  			desired: []string{"a", "b"},
    57  			acceptable: [][]string{
    58  				{"c", "b", "a"},
    59  				{"c", "a", "b"},
    60  			},
    61  		},
    62  		{
    63  			name:    "NamedDep",
    64  			desired: []string{"a", "b"},
    65  			acceptable: [][]string{
    66  				{"b", "a"},
    67  			},
    68  		},
    69  		{
    70  			name:    "IndirectDep",
    71  			desired: []string{"a"},
    72  			acceptable: [][]string{
    73  				{"c", "b", "a"},
    74  			},
    75  		},
    76  		{
    77  			name:    "DirectAndIndirectDep",
    78  			desired: []string{"a"},
    79  			acceptable: [][]string{
    80  				{"c", "b", "a"},
    81  			},
    82  		},
    83  	}
    84  	for _, test := range tests {
    85  		t.Run(test.name, func(t *testing.T) {
    86  			dir := t.TempDir()
    87  			configData, err := os.ReadFile(filepath.Join("testdata", "BuildOrder", test.name+".yml"))
    88  			if err != nil {
    89  				t.Fatal(err)
    90  			}
    91  			configPath := filepath.Join(dir, PackageConfigFilename)
    92  			if err := os.WriteFile(configPath, configData, 0o666); err != nil {
    93  				t.Fatal(err)
    94  			}
    95  			pkg, err := LoadPackage(configPath)
    96  			if err != nil {
    97  				t.Fatal(err)
    98  			}
    99  
   100  			desired := make([]*Target, 0, len(test.desired))
   101  			for _, name := range test.desired {
   102  				target := pkg.Targets[name]
   103  				if target == nil {
   104  					t.Fatalf("target %q not found", name)
   105  				}
   106  				desired = append(desired, target)
   107  			}
   108  			got := BuildOrder(desired...)
   109  			gotNames := make([]string, 0, len(got))
   110  			for _, target := range got {
   111  				gotNames = append(gotNames, target.Name)
   112  			}
   113  			for _, wantNames := range test.acceptable {
   114  				if cmp.Equal(wantNames, gotNames) {
   115  					return
   116  				}
   117  			}
   118  			t.Error("Bad ordering:", gotNames)
   119  			t.Log("Wanted one of:")
   120  			for _, wantNames := range test.acceptable {
   121  				t.Log(wantNames)
   122  			}
   123  		})
   124  	}
   125  }