...

Source file src/github.com/onsi/ginkgo/v2/internal/focus.go

Documentation: github.com/onsi/ginkgo/v2/internal

     1  package internal
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/onsi/ginkgo/v2/types"
     8  )
     9  
    10  /*
    11  If a container marked as focus has a descendant that is also marked as focus, Ginkgo's policy is to
    12  unmark the container's focus.  This gives developers a more intuitive experience when debugging specs.
    13  It is common to focus a container to just run a subset of specs, then identify the specific specs within the container to focus -
    14  this policy allows the developer to simply focus those specific specs and not need to go back and turn the focus off of the container:
    15  
    16  As a common example, consider:
    17  
    18  	FDescribe("something to debug", function() {
    19  		It("works", function() {...})
    20  		It("works", function() {...})
    21  		FIt("doesn't work", function() {...})
    22  		It("works", function() {...})
    23  	})
    24  
    25  here the developer's intent is to focus in on the `"doesn't work"` spec and not to run the adjacent specs in the focused `"something to debug"` container.
    26  The nested policy applied by this function enables this behavior.
    27  */
    28  func ApplyNestedFocusPolicyToTree(tree *TreeNode) {
    29  	var walkTree func(tree *TreeNode) bool
    30  	walkTree = func(tree *TreeNode) bool {
    31  		if tree.Node.MarkedPending {
    32  			return false
    33  		}
    34  		hasFocusedDescendant := false
    35  		for _, child := range tree.Children {
    36  			childHasFocus := walkTree(child)
    37  			hasFocusedDescendant = hasFocusedDescendant || childHasFocus
    38  		}
    39  		tree.Node.MarkedFocus = tree.Node.MarkedFocus && !hasFocusedDescendant
    40  		return tree.Node.MarkedFocus || hasFocusedDescendant
    41  	}
    42  
    43  	walkTree(tree)
    44  }
    45  
    46  /*
    47  Ginkgo supports focussing specs using `FIt`, `FDescribe`, etc. - this is called "programmatic focus"
    48  It also supports focussing specs using regular expressions on the command line (`-focus=`, `-skip=`) that match against spec text and file filters (`-focus-files=`, `-skip-files=`) that match against code locations for nodes in specs.
    49  
    50  When both programmatic and file filters are provided their results are ANDed together.  If multiple kinds of filters are provided, the file filters run first followed by the regex filters.
    51  
    52  This function sets the `Skip` property on specs by applying Ginkgo's focus policy:
    53  - If there are no CLI arguments and no programmatic focus, do nothing.
    54  - If a spec somewhere has programmatic focus skip any specs that have no programmatic focus.
    55  - If there are CLI arguments parse them and skip any specs that either don't match the focus filters or do match the skip filters.
    56  
    57  *Note:* specs with pending nodes are Skipped when created by NewSpec.
    58  */
    59  func ApplyFocusToSpecs(specs Specs, description string, suiteLabels Labels, suiteConfig types.SuiteConfig) (Specs, bool) {
    60  	focusString := strings.Join(suiteConfig.FocusStrings, "|")
    61  	skipString := strings.Join(suiteConfig.SkipStrings, "|")
    62  
    63  	type SkipCheck func(spec Spec) bool
    64  
    65  	// by default, skip any specs marked pending
    66  	skipChecks := []SkipCheck{func(spec Spec) bool { return spec.Nodes.HasNodeMarkedPending() }}
    67  	hasProgrammaticFocus := false
    68  
    69  	for _, spec := range specs {
    70  		if spec.Nodes.HasNodeMarkedFocus() && !spec.Nodes.HasNodeMarkedPending() {
    71  			hasProgrammaticFocus = true
    72  			break
    73  		}
    74  	}
    75  
    76  	if hasProgrammaticFocus {
    77  		skipChecks = append(skipChecks, func(spec Spec) bool { return !spec.Nodes.HasNodeMarkedFocus() })
    78  	}
    79  
    80  	if suiteConfig.LabelFilter != "" {
    81  		labelFilter, _ := types.ParseLabelFilter(suiteConfig.LabelFilter)
    82  		skipChecks = append(skipChecks, func(spec Spec) bool {
    83  			return !labelFilter(UnionOfLabels(suiteLabels, spec.Nodes.UnionOfLabels()))
    84  		})
    85  	}
    86  
    87  	if len(suiteConfig.FocusFiles) > 0 {
    88  		focusFilters, _ := types.ParseFileFilters(suiteConfig.FocusFiles)
    89  		skipChecks = append(skipChecks, func(spec Spec) bool { return !focusFilters.Matches(spec.Nodes.CodeLocations()) })
    90  	}
    91  
    92  	if len(suiteConfig.SkipFiles) > 0 {
    93  		skipFilters, _ := types.ParseFileFilters(suiteConfig.SkipFiles)
    94  		skipChecks = append(skipChecks, func(spec Spec) bool { return skipFilters.Matches(spec.Nodes.CodeLocations()) })
    95  	}
    96  
    97  	if focusString != "" {
    98  		// skip specs that don't match the focus string
    99  		re := regexp.MustCompile(focusString)
   100  		skipChecks = append(skipChecks, func(spec Spec) bool { return !re.MatchString(description + " " + spec.Text()) })
   101  	}
   102  
   103  	if skipString != "" {
   104  		// skip specs that match the skip string
   105  		re := regexp.MustCompile(skipString)
   106  		skipChecks = append(skipChecks, func(spec Spec) bool { return re.MatchString(description + " " + spec.Text()) })
   107  	}
   108  
   109  	// skip specs if shouldSkip() is true.  note that we do nothing if shouldSkip() is false to avoid overwriting skip status established by the node's pending status
   110  	processedSpecs := Specs{}
   111  	for _, spec := range specs {
   112  		for _, skipCheck := range skipChecks {
   113  			if skipCheck(spec) {
   114  				spec.Skip = true
   115  				break
   116  			}
   117  		}
   118  		processedSpecs = append(processedSpecs, spec)
   119  	}
   120  
   121  	return processedSpecs, hasProgrammaticFocus
   122  }
   123  

View as plain text