github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/focus_test.go (about)

     1  package internal_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo"
     5  	. "github.com/onsi/gomega"
     6  
     7  	"github.com/onsi/ginkgo/internal"
     8  	"github.com/onsi/ginkgo/types"
     9  )
    10  
    11  var _ = Describe("Focus", func() {
    12  	Describe("ApplyNestedFocusToTree", func() {
    13  		It("unfocuses parent nodes that have a focused child node somewhere in their tree", func() {
    14  			tree := TN(N(ntCon, "root", Focus), //should lose focus
    15  				TN(N(ntCon, "A", Focus), //should stay focused
    16  					TN(N(ntIt)),
    17  					TN(N(ntIt)),
    18  				),
    19  				TN(N(ntCon),
    20  					TN(N(ntIt)),
    21  					TN(N(ntIt, "B", Focus)), //should stay focused
    22  				),
    23  				TN(N(ntCon, "C", Focus), //should lose focus
    24  					TN(N(ntIt)),
    25  					TN(N(ntIt, "D", Focus)), //should stay focused
    26  				),
    27  				TN(N(ntCon, "E", Focus), //should lose focus
    28  					TN(N(ntIt)),
    29  					TN(N(ntCon),
    30  						TN(N(ntIt)),
    31  						TN(N(ntIt, "F", Focus)), // should stay focused
    32  					),
    33  				),
    34  				TN(N(ntCon, "G", Focus), //should lose focus
    35  					TN(N(ntIt)),
    36  					TN(N(ntCon, "H", Focus), //should lose focus
    37  						TN(N(ntIt)),
    38  						TN(N(ntIt, "I", Focus)), //should stay focused
    39  					),
    40  					TN(N(ntCon, "J", Focus), // should stay focused
    41  						TN(N(ntIt)),
    42  					),
    43  				),
    44  			)
    45  
    46  			internal.ApplyNestedFocusPolicyToTree(tree)
    47  			Ω(mustFindNodeWithText(tree, "root").MarkedFocus).Should(BeFalse())
    48  			Ω(mustFindNodeWithText(tree, "A").MarkedFocus).Should(BeTrue())
    49  			Ω(mustFindNodeWithText(tree, "B").MarkedFocus).Should(BeTrue())
    50  			Ω(mustFindNodeWithText(tree, "C").MarkedFocus).Should(BeFalse())
    51  			Ω(mustFindNodeWithText(tree, "D").MarkedFocus).Should(BeTrue())
    52  			Ω(mustFindNodeWithText(tree, "E").MarkedFocus).Should(BeFalse())
    53  			Ω(mustFindNodeWithText(tree, "F").MarkedFocus).Should(BeTrue())
    54  			Ω(mustFindNodeWithText(tree, "G").MarkedFocus).Should(BeFalse())
    55  			Ω(mustFindNodeWithText(tree, "H").MarkedFocus).Should(BeFalse())
    56  			Ω(mustFindNodeWithText(tree, "I").MarkedFocus).Should(BeTrue())
    57  			Ω(mustFindNodeWithText(tree, "J").MarkedFocus).Should(BeTrue())
    58  		})
    59  
    60  		It("does not unfocus parent nodes if a focused child is the child of a pending child", func() {
    61  			tree := TN(N(ntCon),
    62  				TN(N(ntCon, "A", Focus), //should stay focused
    63  					TN(N(ntIt)),
    64  					TN(N(ntCon, "B", Pending), //should stay pending
    65  						TN(N(ntIt)),
    66  						TN(N(ntIt, "C", Focus)), //should stay focused
    67  					),
    68  				),
    69  			)
    70  
    71  			internal.ApplyNestedFocusPolicyToTree(tree)
    72  			Ω(mustFindNodeWithText(tree, "A").MarkedFocus).Should(BeTrue())
    73  			Ω(mustFindNodeWithText(tree, "B").MarkedPending).Should(BeTrue())
    74  			Ω(mustFindNodeWithText(tree, "C").MarkedFocus).Should(BeTrue())
    75  		})
    76  	})
    77  
    78  	Describe("ApplyFocusToSpecs", func() {
    79  		var specs Specs
    80  		var description string
    81  		var suiteLabels Labels
    82  		var conf types.SuiteConfig
    83  
    84  		harvestSkips := func(specs Specs) []bool {
    85  			out := []bool{}
    86  			for _, spec := range specs {
    87  				out = append(out, spec.Skip)
    88  			}
    89  			return out
    90  		}
    91  
    92  		BeforeEach(func() {
    93  			description = "Silmarillion Suite"
    94  			suiteLabels = Labels{"SuiteLabel", "TopLevelLabel"}
    95  			conf = types.SuiteConfig{}
    96  		})
    97  
    98  		Context("when there are specs with nodes marked pending", func() {
    99  			BeforeEach(func() {
   100  				specs = Specs{
   101  					S(N(), N()),
   102  					S(N(), N()),
   103  					S(N(), N(Pending)),
   104  					S(N(), N()),
   105  					S(N(Pending)),
   106  				}
   107  			})
   108  
   109  			It("skips those specs", func() {
   110  				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   111  				Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, true, false, true}))
   112  				Ω(hasProgrammaticFocus).Should(BeFalse())
   113  			})
   114  		})
   115  
   116  		Context("when there are specs with nodes marked focused", func() {
   117  			BeforeEach(func() {
   118  				specs = Specs{
   119  					S(N(), N()),
   120  					S(N(), N()),
   121  					S(N(), N(Focus)),
   122  					S(N()),
   123  					S(N(Focus)),
   124  				}
   125  			})
   126  			It("skips any other specs and notes that it has programmatic focus", func() {
   127  				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   128  				Ω(harvestSkips(specs)).Should(Equal([]bool{true, true, false, true, false}))
   129  				Ω(hasProgrammaticFocus).Should(BeTrue())
   130  			})
   131  
   132  			Context("when the specs with nodes marked focused also have nodes marked pending ", func() {
   133  				BeforeEach(func() {
   134  					specs = Specs{
   135  						S(N(), N()),
   136  						S(N(), N()),
   137  						S(N(Pending), N(Focus)),
   138  						S(N()),
   139  					}
   140  				})
   141  				It("does not skip any other specs and notes that it does not have programmatic focus", func() {
   142  					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   143  					Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, true, false}))
   144  					Ω(hasProgrammaticFocus).Should(BeFalse())
   145  				})
   146  			})
   147  		})
   148  
   149  		Context("when there are focus strings and/or skip strings configured", func() {
   150  			BeforeEach(func() {
   151  				specs = Specs{
   152  					S(N("blue"), N("dragon")),
   153  					S(N("blue"), N("Dragon")),
   154  					S(N("red dragon"), N()),
   155  					S(N("green dragon"), N()),
   156  					S(N(Pending), N("blue Dragon")),
   157  					S(N("yellow dragon")),
   158  					S(N(Focus, "yellow dragon")),
   159  				}
   160  			})
   161  
   162  			Context("when there are focus strings configured", func() {
   163  				BeforeEach(func() {
   164  					conf.FocusStrings = []string{"blue [dD]ra", "(red|green) dragon"}
   165  				})
   166  
   167  				It("overrides any programmatic focus, runs only specs that match the focus string, and continues to skip specs with nodes marked pending", func() {
   168  					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   169  					Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, false, false, true, true, true}))
   170  					Ω(hasProgrammaticFocus).Should(BeFalse())
   171  				})
   172  
   173  				It("includes the description string in the search", func() {
   174  					conf.FocusStrings = []string{"Silmaril"}
   175  					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   176  					Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, false, false, true, false, false}))
   177  					Ω(hasProgrammaticFocus).Should(BeFalse())
   178  				})
   179  			})
   180  
   181  			Context("when there are skip strings configured", func() {
   182  				BeforeEach(func() {
   183  					conf.SkipStrings = []string{"blue [dD]ragon", "red dragon"}
   184  				})
   185  
   186  				It("overrides any programmatic focus, and runs specs that don't match the skip strings, and continues to skip specs with nodes marked pending", func() {
   187  					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   188  					Ω(harvestSkips(specs)).Should(Equal([]bool{true, true, true, false, true, false, false}))
   189  					Ω(hasProgrammaticFocus).Should(BeFalse())
   190  				})
   191  
   192  				It("includes the description string in the search", func() {
   193  					conf.SkipStrings = []string{"Silmaril"}
   194  					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   195  					Ω(harvestSkips(specs)).Should(Equal([]bool{true, true, true, true, true, true, true}))
   196  					Ω(hasProgrammaticFocus).Should(BeFalse())
   197  				})
   198  			})
   199  
   200  			Context("when skip and focus are configured", func() {
   201  				BeforeEach(func() {
   202  					conf.FocusStrings = []string{"blue [dD]ragon", "(red|green) dragon"}
   203  					conf.SkipStrings = []string{"red dragon", "Dragon"}
   204  				})
   205  
   206  				It("ORs both together", func() {
   207  					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   208  					Ω(harvestSkips(specs)).Should(Equal([]bool{false, true, true, false, true, true, true}))
   209  					Ω(hasProgrammaticFocus).Should(BeFalse())
   210  				})
   211  			})
   212  		})
   213  
   214  		Context("when configured to focus/skip files", func() {
   215  			BeforeEach(func() {
   216  				specs = Specs{
   217  					S(N(CL("file_a", 1))),               //include because "file_:1" is in FocusFiles
   218  					S(N(CL("file_b", 3, "file_b", 15))), //include becasue "file_:15-21" is in FocusFiles
   219  					S(N(CL("file_b", 17))),              //skip because "_b:17" is in SkipFiles
   220  					S(N(CL("file_b", 20), Pending)),     //skip because spec is flagged pending
   221  					S(N(CL("c", 3), Focus)),             //skip because "c" is not in FocusFiles - override programmatic focus
   222  					S(N(CL("d", 17))),                   //include because "d " is in FocusFiles
   223  				}
   224  
   225  				conf.FocusFiles = []string{"file_:1,15-21", "d"}
   226  				conf.SkipFiles = []string{"_b:17"}
   227  			})
   228  
   229  			It("applies a file-based focus and skip filter", func() {
   230  				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   231  				Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, true, true, true, false}))
   232  				Ω(hasProgrammaticFocus).Should(BeFalse())
   233  			})
   234  		})
   235  
   236  		Context("when configured with a label filter", func() {
   237  			BeforeEach(func() {
   238  				conf.LabelFilter = "(cat || cow) && !fish"
   239  				specs = Specs{
   240  					S(N(ntCon, Label("cat", "dog")), N(ntIt, "A", Label("fish"))),  //skip because fish
   241  					S(N(ntCon, Label("cat", "dog")), N(ntIt, "B", Label("apple"))), //include because has cat and not fish
   242  					S(N(ntCon, Label("dog")), N(ntIt, "C", Label("apple"))),        //skip because no cat or cow
   243  					S(N(ntCon, Label("cow")), N(ntIt, "D", Label("fish"), Focus)),  //skip because fish, override focus
   244  					S(N(ntCon, Label("cow")), N(ntIt, "E")),                        //include because cow and no fish
   245  					S(N(ntCon, Label("cow")), N(ntIt, "F", Pending)),               //skip because pending
   246  				}
   247  			})
   248  
   249  			It("applies the label filters", func() {
   250  				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   251  				Ω(harvestSkips(specs)).Should(Equal([]bool{true, false, true, true, false, true}))
   252  				Ω(hasProgrammaticFocus).Should(BeFalse())
   253  
   254  			})
   255  		})
   256  
   257  		Context("when configured with a label filter that filters on the suite level label", func() {
   258  			BeforeEach(func() {
   259  				conf.LabelFilter = "cat && TopLevelLabel"
   260  				specs = Specs{
   261  					S(N(ntCon, Label("cat", "dog")), N(ntIt, "A", Label("fish"))), //include because cat and suite has TopLevelLabel
   262  					S(N(ntCon, Label("dog")), N(ntIt, "B", Label("apple"))),       //skip because no cat
   263  				}
   264  			})
   265  			It("honors the suite level label", func() {
   266  				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   267  				Ω(harvestSkips(specs)).Should(Equal([]bool{false, true}))
   268  				Ω(hasProgrammaticFocus).Should(BeFalse())
   269  			})
   270  		})
   271  
   272  		Context("when configured with focus/skip files, focus/skip strings, and label filters", func() {
   273  			BeforeEach(func() {
   274  				specs = Specs{
   275  					S(N("dog", CL("file_a", 1), Label("brown"))),                   //include because "file_:1" is in FocusFiles and "dog" is in FocusStrings and has "brown" label
   276  					S(N("dog", CL("file_a", 1), Label("white"))),                   //skip because does not have "brown" label
   277  					S(N("dog cat", CL("file_b", 3, "file_b", 15), Label("brown"))), //skip because "file_:15-21" is in FocusFiles but "cat" is in SkipStirngs
   278  					S(N("fish", CL("file_b", 17), Label("brown"))),                 //skip because "_b:17" is in SkipFiles, even though "fish" is in FocusStrings
   279  					S(N("biscuit", CL("file_b", 20), Pending, Label("brown"))),     //skip because spec is flagged pending
   280  					S(N("pony", CL("c", 3), Focus, Label("brown"))),                //skip because "c" is not in FocusFiles or FocusStrings - override programmatic focus
   281  					S(N("goat", CL("d", 17), Label("brown"))),                      //skip because "goat" is in FocusStrings but "d" is not in FocusFiles
   282  				}
   283  
   284  				conf.FocusFiles = []string{"file_:1,15-21"}
   285  				conf.SkipFiles = []string{"_b:17"}
   286  				conf.FocusStrings = []string{"goat", "dog", "fish", "biscuit"}
   287  				conf.SkipStrings = []string{"cat"}
   288  				conf.LabelFilter = "brown"
   289  			})
   290  
   291  			It("applies all filters", func() {
   292  				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
   293  				Ω(harvestSkips(specs)).Should(Equal([]bool{false, true, true, true, true, true, true}))
   294  				Ω(hasProgrammaticFocus).Should(BeFalse())
   295  			})
   296  		})
   297  	})
   298  })