github.com/YousefHaggyHeroku/pack@v1.5.5/internal/builder/detection_order_calculator_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/buildpacks/lifecycle/api"
     7  
     8  	pubbldr "github.com/YousefHaggyHeroku/pack/builder"
     9  	"github.com/YousefHaggyHeroku/pack/internal/builder"
    10  	"github.com/YousefHaggyHeroku/pack/internal/dist"
    11  	h "github.com/YousefHaggyHeroku/pack/testhelpers"
    12  
    13  	"github.com/heroku/color"
    14  	"github.com/sclevine/spec"
    15  	"github.com/sclevine/spec/report"
    16  )
    17  
    18  func TestDetectionOrderCalculator(t *testing.T) {
    19  	color.Disable(true)
    20  	defer color.Disable(false)
    21  	spec.Run(t, "testDetectionOrderCalculator", testDetectionOrderCalculator, spec.Parallel(), spec.Report(report.Terminal{}))
    22  }
    23  
    24  func testDetectionOrderCalculator(t *testing.T, when spec.G, it spec.S) {
    25  	when("Order", func() {
    26  		var (
    27  			assert = h.NewAssertionManager(t)
    28  
    29  			testBuildpackOne = dist.BuildpackInfo{
    30  				ID:      "test.buildpack",
    31  				Version: "test.buildpack.version",
    32  			}
    33  			testBuildpackTwo = dist.BuildpackInfo{
    34  				ID:      "test.buildpack.2",
    35  				Version: "test.buildpack.2.version",
    36  			}
    37  			testTopNestedBuildpack = dist.BuildpackInfo{
    38  				ID:      "test.top.nested",
    39  				Version: "test.top.nested.version",
    40  			}
    41  			testLevelOneNestedBuildpack = dist.BuildpackInfo{
    42  				ID:      "test.nested.level.one",
    43  				Version: "test.nested.level.one.version",
    44  			}
    45  			testLevelOneNestedBuildpackTwo = dist.BuildpackInfo{
    46  				ID:      "test.nested.level.one.two",
    47  				Version: "test.nested.level.one.two.version",
    48  			}
    49  			testLevelOneNestedBuildpackThree = dist.BuildpackInfo{
    50  				ID:      "test.nested.level.one.three",
    51  				Version: "test.nested.level.one.three.version",
    52  			}
    53  			testLevelTwoNestedBuildpack = dist.BuildpackInfo{
    54  				ID:      "test.nested.level.two",
    55  				Version: "test.nested.level.two.version",
    56  			}
    57  			topLevelOrder = dist.Order{
    58  				{
    59  					Group: []dist.BuildpackRef{
    60  						{BuildpackInfo: testBuildpackOne},
    61  						{BuildpackInfo: testBuildpackTwo},
    62  						{BuildpackInfo: testTopNestedBuildpack},
    63  					},
    64  				},
    65  			}
    66  			buildpackLayers = dist.BuildpackLayers{
    67  				"test.buildpack": {
    68  					"test.buildpack.version": dist.BuildpackLayerInfo{
    69  						API:         api.MustParse("0.2"),
    70  						LayerDiffID: "layer:diff",
    71  					},
    72  				},
    73  				"test.top.nested": {
    74  					"test.top.nested.version": dist.BuildpackLayerInfo{
    75  						API: api.MustParse("0.2"),
    76  						Order: dist.Order{
    77  							{
    78  								Group: []dist.BuildpackRef{
    79  									{BuildpackInfo: testLevelOneNestedBuildpack},
    80  									{BuildpackInfo: testLevelOneNestedBuildpackTwo},
    81  									{BuildpackInfo: testLevelOneNestedBuildpackThree},
    82  								},
    83  							},
    84  						},
    85  						LayerDiffID: "layer:diff",
    86  					},
    87  				},
    88  				"test.nested.level.one": {
    89  					"test.nested.level.one.version": dist.BuildpackLayerInfo{
    90  						API: api.MustParse("0.2"),
    91  						Order: dist.Order{
    92  							{
    93  								Group: []dist.BuildpackRef{
    94  									{BuildpackInfo: testLevelTwoNestedBuildpack},
    95  								},
    96  							},
    97  						},
    98  						LayerDiffID: "layer:diff",
    99  					},
   100  				},
   101  				"test.nested.level.one.three": {
   102  					"test.nested.level.one.three.version": dist.BuildpackLayerInfo{
   103  						API: api.MustParse("0.2"),
   104  						Order: dist.Order{
   105  							{
   106  								Group: []dist.BuildpackRef{
   107  									{BuildpackInfo: testLevelTwoNestedBuildpack},
   108  									{BuildpackInfo: testTopNestedBuildpack},
   109  								},
   110  							},
   111  						},
   112  						LayerDiffID: "layer:diff",
   113  					},
   114  				},
   115  			}
   116  		)
   117  
   118  		when("called with no depth", func() {
   119  			it("returns detection order with top level order of buildpacks", func() {
   120  				calculator := builder.NewDetectionOrderCalculator()
   121  				order, err := calculator.Order(topLevelOrder, buildpackLayers, pubbldr.OrderDetectionNone)
   122  				assert.Nil(err)
   123  
   124  				expectedOrder := pubbldr.DetectionOrder{
   125  					{
   126  						GroupDetectionOrder: pubbldr.DetectionOrder{
   127  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testBuildpackOne}},
   128  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testBuildpackTwo}},
   129  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testTopNestedBuildpack}},
   130  						},
   131  					},
   132  				}
   133  
   134  				assert.Equal(order, expectedOrder)
   135  			})
   136  		})
   137  
   138  		when("called with max depth", func() {
   139  			it("returns detection order for nested buildpacks", func() {
   140  				calculator := builder.NewDetectionOrderCalculator()
   141  				order, err := calculator.Order(topLevelOrder, buildpackLayers, pubbldr.OrderDetectionMaxDepth)
   142  				assert.Nil(err)
   143  
   144  				expectedOrder := pubbldr.DetectionOrder{
   145  					{
   146  						GroupDetectionOrder: pubbldr.DetectionOrder{
   147  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testBuildpackOne}},
   148  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testBuildpackTwo}},
   149  							{
   150  								BuildpackRef: dist.BuildpackRef{BuildpackInfo: testTopNestedBuildpack},
   151  								GroupDetectionOrder: pubbldr.DetectionOrder{
   152  									{
   153  										BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpack},
   154  										GroupDetectionOrder: pubbldr.DetectionOrder{
   155  											{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelTwoNestedBuildpack}},
   156  										},
   157  									},
   158  									{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpackTwo}},
   159  									{
   160  										BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpackThree},
   161  										GroupDetectionOrder: pubbldr.DetectionOrder{
   162  											{
   163  												BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelTwoNestedBuildpack},
   164  												Cyclical:     false,
   165  											},
   166  											{
   167  												BuildpackRef: dist.BuildpackRef{BuildpackInfo: testTopNestedBuildpack},
   168  												Cyclical:     true,
   169  											},
   170  										},
   171  									},
   172  								},
   173  							},
   174  						},
   175  					},
   176  				}
   177  
   178  				assert.Equal(order, expectedOrder)
   179  			})
   180  		})
   181  
   182  		when("called with a depth of 1", func() {
   183  			it("returns detection order for first level of nested buildpacks", func() {
   184  				calculator := builder.NewDetectionOrderCalculator()
   185  				order, err := calculator.Order(topLevelOrder, buildpackLayers, 1)
   186  				assert.Nil(err)
   187  
   188  				expectedOrder := pubbldr.DetectionOrder{
   189  					{
   190  						GroupDetectionOrder: pubbldr.DetectionOrder{
   191  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testBuildpackOne}},
   192  							{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testBuildpackTwo}},
   193  							{
   194  								BuildpackRef: dist.BuildpackRef{BuildpackInfo: testTopNestedBuildpack},
   195  								GroupDetectionOrder: pubbldr.DetectionOrder{
   196  									{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpack}},
   197  									{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpackTwo}},
   198  									{BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpackThree}},
   199  								},
   200  							},
   201  						},
   202  					},
   203  				}
   204  
   205  				assert.Equal(order, expectedOrder)
   206  			})
   207  		})
   208  
   209  		when("a buildpack is referenced in a sub detection group", func() {
   210  			it("marks the buildpack is cyclic and doesn't attempt to calculate that buildpacks order", func() {
   211  				cyclicBuildpackLayers := dist.BuildpackLayers{
   212  					"test.top.nested": {
   213  						"test.top.nested.version": dist.BuildpackLayerInfo{
   214  							API: api.MustParse("0.2"),
   215  							Order: dist.Order{
   216  								{
   217  									Group: []dist.BuildpackRef{
   218  										{BuildpackInfo: testLevelOneNestedBuildpack},
   219  									},
   220  								},
   221  							},
   222  							LayerDiffID: "layer:diff",
   223  						},
   224  					},
   225  					"test.nested.level.one": {
   226  						"test.nested.level.one.version": dist.BuildpackLayerInfo{
   227  							API: api.MustParse("0.2"),
   228  							Order: dist.Order{
   229  								{
   230  									Group: []dist.BuildpackRef{
   231  										{BuildpackInfo: testTopNestedBuildpack},
   232  									},
   233  								},
   234  							},
   235  							LayerDiffID: "layer:diff",
   236  						},
   237  					},
   238  				}
   239  				cyclicOrder := dist.Order{
   240  					{
   241  						Group: []dist.BuildpackRef{{BuildpackInfo: testTopNestedBuildpack}},
   242  					},
   243  				}
   244  
   245  				calculator := builder.NewDetectionOrderCalculator()
   246  				order, err := calculator.Order(cyclicOrder, cyclicBuildpackLayers, pubbldr.OrderDetectionMaxDepth)
   247  				assert.Nil(err)
   248  
   249  				expectedOrder := pubbldr.DetectionOrder{
   250  					{
   251  						GroupDetectionOrder: pubbldr.DetectionOrder{
   252  							{
   253  								BuildpackRef: dist.BuildpackRef{BuildpackInfo: testTopNestedBuildpack},
   254  								GroupDetectionOrder: pubbldr.DetectionOrder{
   255  									{
   256  										BuildpackRef: dist.BuildpackRef{BuildpackInfo: testLevelOneNestedBuildpack},
   257  										GroupDetectionOrder: pubbldr.DetectionOrder{
   258  											{
   259  												BuildpackRef: dist.BuildpackRef{
   260  													BuildpackInfo: testTopNestedBuildpack,
   261  												},
   262  												Cyclical: true,
   263  											},
   264  										},
   265  									},
   266  								},
   267  							},
   268  						},
   269  					},
   270  				}
   271  
   272  				assert.Equal(order, expectedOrder)
   273  			})
   274  		})
   275  	})
   276  }