@@ -1546,3 +1546,203 @@ func TestFeatureConfig_prereqsSatisfied(t *testing.T) {
1546
1546
})
1547
1547
}
1548
1548
}
1549
+
1550
+ func TestCheckPreReqsForPreReqs (t * testing.T ) {
1551
+ trueVariation := Variation {
1552
+ Name : stringPtr ("True" ),
1553
+ Value : "true" ,
1554
+ Identifier : "true" ,
1555
+ }
1556
+
1557
+ falseVariation := Variation {
1558
+ Name : stringPtr ("False" ),
1559
+ Value : "false" ,
1560
+ Identifier : "false" ,
1561
+ }
1562
+
1563
+ blueVariation := Variation {
1564
+ Name : stringPtr ("blue" ),
1565
+ Value : "blue" ,
1566
+ Identifier : "blue" ,
1567
+ }
1568
+
1569
+ redVariation := Variation {
1570
+ Name : stringPtr ("red" ),
1571
+ Value : "red" ,
1572
+ Identifier : "red" ,
1573
+ }
1574
+
1575
+ flags := map [string ]FeatureConfig {
1576
+ "flag1" : {
1577
+ DefaultServe : Serve {
1578
+ Variation : stringPtr ("true" ),
1579
+ },
1580
+ Environment : "dev" ,
1581
+ Feature : "flag1" ,
1582
+ Kind : "boolean" ,
1583
+ OffVariation : "false" ,
1584
+ Prerequisites : nil ,
1585
+ Project : "default" ,
1586
+ Rules : nil ,
1587
+ State : "on" ,
1588
+ VariationToTargetMap : nil ,
1589
+ Variations : Variations {
1590
+ trueVariation , falseVariation ,
1591
+ },
1592
+ Segments : nil ,
1593
+ },
1594
+ "flag2" : {
1595
+ DefaultServe : Serve {
1596
+ Variation : stringPtr ("true" ),
1597
+ },
1598
+ Environment : "dev" ,
1599
+ Feature : "flag2" ,
1600
+ Kind : "boolean" ,
1601
+ OffVariation : "false" ,
1602
+ Prerequisites : []Prerequisite {
1603
+ {Feature : "flag1" ,
1604
+ Variations : []string {"true" },
1605
+ },
1606
+ },
1607
+ Project : "default" ,
1608
+ Rules : nil ,
1609
+ State : "on" ,
1610
+ VariationToTargetMap : nil ,
1611
+ Variations : Variations {
1612
+ trueVariation , falseVariation ,
1613
+ },
1614
+ Segments : nil ,
1615
+ },
1616
+ "flag3" : {
1617
+ DefaultServe : Serve {
1618
+ Variation : stringPtr ("true" ),
1619
+ },
1620
+ Environment : "dev" ,
1621
+ Feature : "flag3" ,
1622
+ Kind : "boolean" ,
1623
+ OffVariation : "false" ,
1624
+ Prerequisites : []Prerequisite {
1625
+ {Feature : "flag2" ,
1626
+ Variations : []string {"true" },
1627
+ },
1628
+ },
1629
+ Project : "default" ,
1630
+ Rules : nil ,
1631
+ State : "on" ,
1632
+ VariationToTargetMap : nil ,
1633
+ Variations : Variations {
1634
+ trueVariation , falseVariation ,
1635
+ },
1636
+ Segments : nil ,
1637
+ },
1638
+ "mv1" : {
1639
+ DefaultServe : Serve {
1640
+ Variation : stringPtr ("red" ),
1641
+ },
1642
+ Environment : "dev" ,
1643
+ Feature : "mv1" ,
1644
+ Kind : "string" ,
1645
+ OffVariation : "blue" ,
1646
+ Prerequisites : []Prerequisite {
1647
+ {Feature : "flag1" ,
1648
+ Variations : []string {"true" },
1649
+ },
1650
+ },
1651
+ Project : "default" ,
1652
+ Rules : nil ,
1653
+ State : "on" ,
1654
+ VariationToTargetMap : nil ,
1655
+ Variations : Variations {
1656
+ redVariation , blueVariation ,
1657
+ },
1658
+ Segments : nil ,
1659
+ },
1660
+ }
1661
+ tests := []struct {
1662
+ name string
1663
+ expected bool
1664
+ preReqFlagPreReqs []Prerequisite
1665
+ flags map [string ]FeatureConfig
1666
+ target * Target
1667
+ }{
1668
+ {
1669
+ name : "Given I have no preReqFlagPreReqs" ,
1670
+ expected : true ,
1671
+ flags : nil ,
1672
+ preReqFlagPreReqs : nil ,
1673
+ target : nil ,
1674
+ },
1675
+ {
1676
+ name : "Given I have a preReqFlagPreReqs that has no nested preregs that will match" ,
1677
+ expected : true ,
1678
+ flags : flags ,
1679
+ preReqFlagPreReqs : []Prerequisite {
1680
+ {Feature : "flag1" ,
1681
+ Variations : []string {"true" },
1682
+ },
1683
+ },
1684
+ target : nil ,
1685
+ },
1686
+ {
1687
+ name : "Given I have a preReqFlagPreReqs that has no nested prereqs that will not match" ,
1688
+ expected : false ,
1689
+ flags : flags ,
1690
+ preReqFlagPreReqs : []Prerequisite {
1691
+ {Feature : "flag1" ,
1692
+ Variations : []string {"false" },
1693
+ },
1694
+ },
1695
+ target : nil ,
1696
+ },
1697
+ {
1698
+ name : "Given I have a preReqFlagPreReqs that has nested prereqs that will match i.e. flag 3 depends on flag 2, which depends on flag 1" ,
1699
+ expected : true ,
1700
+ flags : flags ,
1701
+ preReqFlagPreReqs : []Prerequisite {
1702
+ {Feature : "flag3" ,
1703
+ Variations : []string {"true" },
1704
+ },
1705
+ },
1706
+ target : nil ,
1707
+ },
1708
+ {
1709
+ name : "Given I have a preReqFlagPreReqs that has nested prereqs that will not match" ,
1710
+ expected : false ,
1711
+ flags : flags ,
1712
+ preReqFlagPreReqs : []Prerequisite {
1713
+ {Feature : "flag3" ,
1714
+ Variations : []string {"false" },
1715
+ },
1716
+ },
1717
+ target : nil ,
1718
+ },
1719
+ {
1720
+ name : "Given I have a preReqFlagPreReqs of a mv flag that has nested prereqs that will not match" ,
1721
+ expected : false ,
1722
+ flags : flags ,
1723
+ preReqFlagPreReqs : []Prerequisite {
1724
+ {Feature : "mv1" ,
1725
+ Variations : []string {"blue" },
1726
+ },
1727
+ },
1728
+ target : nil ,
1729
+ },
1730
+ {
1731
+ name : "Given I have a preReqFlagPreReqs of a mv flag that has nested prereqs that will match" ,
1732
+ expected : true ,
1733
+ flags : flags ,
1734
+ preReqFlagPreReqs : []Prerequisite {
1735
+ {Feature : "mv1" ,
1736
+ Variations : []string {"red" },
1737
+ },
1738
+ },
1739
+ target : nil ,
1740
+ },
1741
+ }
1742
+
1743
+ for _ , tt := range tests {
1744
+ t .Run (tt .name , func (t * testing.T ) {
1745
+ assert .Equal (t , tt .expected , checkPreReqsForPreReqs (tt .preReqFlagPreReqs , tt .flags , tt .target ))
1746
+ })
1747
+ }
1748
+ }
0 commit comments