github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/buildplan/raw/walk_test.go (about)

     1  package raw
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/ActiveState/cli/internal/errs"
     9  	"github.com/ActiveState/cli/internal/rtutils/ptr"
    10  	"github.com/go-openapi/strfmt"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestRawBuild_walkNodesViaSteps(t *testing.T) {
    16  	type walkCall struct {
    17  		nodeID         strfmt.UUID
    18  		nodeType       string
    19  		parentArtifact strfmt.UUID
    20  	}
    21  
    22  	tests := []struct {
    23  		name      string
    24  		nodeIDs   []strfmt.UUID
    25  		tag       StepInputTag
    26  		build     *Build
    27  		wantCalls []walkCall
    28  		wantErr   bool
    29  	}{
    30  		{
    31  			"Ingredient from step",
    32  			[]strfmt.UUID{"00000000-0000-0000-0000-000000000002"},
    33  			TagSource,
    34  			buildWithSourceFromStep,
    35  			[]walkCall{
    36  				{"00000000-0000-0000-0000-000000000002", "Artifact", ""},
    37  				{"00000000-0000-0000-0000-000000000004", "Artifact", strfmt.UUID("00000000-0000-0000-0000-000000000002")},
    38  				{"00000000-0000-0000-0000-000000000006", "Source", strfmt.UUID("00000000-0000-0000-0000-000000000004")},
    39  			},
    40  			false,
    41  		},
    42  		{
    43  			"Ingredient from generatedBy, multiple artifacts to same ingredient",
    44  			[]strfmt.UUID{"00000000-0000-0000-0000-000000000002", "00000000-0000-0000-0000-000000000003"},
    45  			TagSource,
    46  			buildWithSourceFromGeneratedBy,
    47  			[]walkCall{
    48  				{"00000000-0000-0000-0000-000000000002", "Artifact", ""},
    49  				{"00000000-0000-0000-0000-000000000004", "Source", strfmt.UUID("00000000-0000-0000-0000-000000000002")},
    50  				{"00000000-0000-0000-0000-000000000003", "Artifact", ""},
    51  				{"00000000-0000-0000-0000-000000000004", "Source", strfmt.UUID("00000000-0000-0000-0000-000000000003")},
    52  			},
    53  			false,
    54  		},
    55  		{
    56  			"Build time deps",
    57  			[]strfmt.UUID{"00000000-0000-0000-0000-000000000002"},
    58  			TagDependency,
    59  			buildWithBuildDeps,
    60  			[]walkCall{
    61  				{"00000000-0000-0000-0000-000000000002", "Artifact", ""},
    62  				{"00000000-0000-0000-0000-000000000004", "Artifact", strfmt.UUID("00000000-0000-0000-0000-000000000002")},
    63  				{"00000000-0000-0000-0000-000000000006", "Source", strfmt.UUID("00000000-0000-0000-0000-000000000004")},
    64  			},
    65  			false,
    66  		},
    67  	}
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  
    71  			calls := []walkCall{}
    72  			walk := func(node interface{}, parent *Artifact) error {
    73  				var parentID *strfmt.UUID
    74  				if parent != nil {
    75  					parentID = &parent.NodeID
    76  				}
    77  				var id strfmt.UUID
    78  				switch v := node.(type) {
    79  				case *Artifact:
    80  					id = v.NodeID
    81  				case *Source:
    82  					id = v.NodeID
    83  				default:
    84  					t.Fatalf("unexpected node type %T", v)
    85  				}
    86  				calls = append(calls, walkCall{
    87  					nodeID:         id,
    88  					nodeType:       strings.Split(fmt.Sprintf("%T", node), ".")[1],
    89  					parentArtifact: ptr.From(parentID, ""),
    90  				})
    91  				return nil
    92  			}
    93  
    94  			if err := tt.build.WalkViaSteps(tt.nodeIDs, tt.tag, walk); (err != nil) != tt.wantErr {
    95  				t.Errorf("walkNodes() error = %v, wantErr %v", errs.JoinMessage(err), tt.wantErr)
    96  			}
    97  
    98  			// Compare each individual call rather than the entire list of calls, so failures are easier to digest
    99  			for n, want := range tt.wantCalls {
   100  				if n > len(calls)-1 {
   101  					t.Fatalf("expected call %d, but it didn't happen. Missing: %#v", n, want)
   102  				}
   103  				got := calls[n]
   104  				require.Equal(t, want.nodeID, got.nodeID, fmt.Sprintf("call %d gave wrong nodeID", n))
   105  				require.Equal(t, want.nodeType, got.nodeType, fmt.Sprintf("call %d gave wrong nodeType", n))
   106  				require.Equal(t, want.parentArtifact, got.parentArtifact, fmt.Sprintf("call %d gave wrong parentArtifact", n))
   107  			}
   108  
   109  			// Final sanity check, in case we forgot to update the above
   110  			assert.Equal(t, tt.wantCalls, calls)
   111  		})
   112  	}
   113  }
   114  
   115  func TestRawBuild_walkNodesViaRuntimeDeps(t *testing.T) {
   116  	type walkCall struct {
   117  		nodeID         strfmt.UUID
   118  		nodeType       string
   119  		parentArtifact strfmt.UUID
   120  	}
   121  
   122  	tests := []struct {
   123  		name      string
   124  		nodeIDs   []strfmt.UUID
   125  		build     *Build
   126  		wantCalls []walkCall
   127  		wantErr   bool
   128  	}{
   129  		{
   130  			"Runtime deps",
   131  			buildWithRuntimeDeps.Terminals[0].NodeIDs,
   132  			buildWithRuntimeDeps,
   133  			[]walkCall{
   134  				{"00000000-0000-0000-0000-000000000002", "Artifact", ""},
   135  				{"00000000-0000-0000-0000-000000000007", "Artifact", "00000000-0000-0000-0000-000000000002"},
   136  			},
   137  			false,
   138  		},
   139  		{
   140  			"Runtime deps via src step",
   141  			buildWithRuntimeDepsViaSrc.Terminals[0].NodeIDs,
   142  			buildWithRuntimeDepsViaSrc,
   143  			[]walkCall{
   144  				{"00000000-0000-0000-0000-000000000007", "Artifact", "00000000-0000-0000-0000-000000000002"},
   145  			},
   146  			false,
   147  		},
   148  	}
   149  	for _, tt := range tests {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  
   152  			calls := []walkCall{}
   153  			walk := func(node interface{}, parent *Artifact) error {
   154  				var parentID *strfmt.UUID
   155  				if parent != nil {
   156  					parentID = &parent.NodeID
   157  				}
   158  				var id strfmt.UUID
   159  				switch v := node.(type) {
   160  				case *Artifact:
   161  					id = v.NodeID
   162  				case *Source:
   163  					id = v.NodeID
   164  				default:
   165  					t.Fatalf("unexpected node type %T", v)
   166  				}
   167  				calls = append(calls, walkCall{
   168  					nodeID:         id,
   169  					nodeType:       strings.Split(fmt.Sprintf("%T", node), ".")[1],
   170  					parentArtifact: ptr.From(parentID, ""),
   171  				})
   172  				return nil
   173  			}
   174  
   175  			if err := tt.build.WalkViaRuntimeDeps(tt.nodeIDs, walk); (err != nil) != tt.wantErr {
   176  				t.Errorf("walkNodes() error = %v, wantErr %v", errs.JoinMessage(err), tt.wantErr)
   177  			}
   178  
   179  			// Compare each individual call rather than the entire list of calls, so failures are easier to digest
   180  			for n, want := range tt.wantCalls {
   181  				if n > len(calls)-1 {
   182  					t.Fatalf("expected call %d, but it didn't happen. Missing: %#v", n, want)
   183  				}
   184  				got := calls[n]
   185  				require.Equal(t, want.nodeID, got.nodeID, fmt.Sprintf("call %d gave wrong nodeID", n))
   186  				require.Equal(t, want.nodeType, got.nodeType, fmt.Sprintf("call %d gave wrong nodeType", n))
   187  				require.Equal(t, want.parentArtifact, got.parentArtifact, fmt.Sprintf("call %d gave wrong parentArtifact", n))
   188  			}
   189  
   190  			// Final sanity check, in case we forgot to update the above
   191  			assert.Equal(t, tt.wantCalls, calls)
   192  		})
   193  	}
   194  }