How to use runOncePairsForSpec method of internal Package

Best Ginkgo code snippet using internal.runOncePairsForSpec

group.go

Source:group.go Github

copy

Full Screen

...21		containerID: containerID,22	}23}24type runOncePairs []runOncePair25func runOncePairsForSpec(spec Spec) runOncePairs {26	pairs := runOncePairs{}27	containers := spec.Nodes.WithType(types.NodeTypeContainer)28	for _, node := range spec.Nodes {29		if node.NodeType.Is(types.NodeTypeBeforeAll | types.NodeTypeAfterAll) {30			pairs = append(pairs, runOncePairForNode(node, containers.FirstWithNestingLevel(node.NestingLevel-1).ID))31		} else if node.NodeType.Is(types.NodeTypeBeforeEach|types.NodeTypeJustBeforeEach|types.NodeTypeAfterEach|types.NodeTypeJustAfterEach) && node.MarkedOncePerOrdered {32			passedIntoAnOrderedContainer := false33			firstOrderedContainerDeeperThanNode := containers.FirstSatisfying(func(container Node) bool {34				passedIntoAnOrderedContainer = passedIntoAnOrderedContainer || container.MarkedOrdered35				return container.NestingLevel >= node.NestingLevel && passedIntoAnOrderedContainer36			})37			if firstOrderedContainerDeeperThanNode.IsZero() {38				continue39			}40			pairs = append(pairs, runOncePairForNode(node, firstOrderedContainerDeeperThanNode.ID))41		}42	}43	return pairs44}45func (pairs runOncePairs) runOncePairFor(nodeID uint) runOncePair {46	for i := range pairs {47		if pairs[i].nodeID == nodeID {48			return pairs[i]49		}50	}51	return runOncePair{}52}53func (pairs runOncePairs) hasRunOncePair(pair runOncePair) bool {54	for i := range pairs {55		if pairs[i] == pair {56			return true57		}58	}59	return false60}61func (pairs runOncePairs) withType(nodeTypes types.NodeType) runOncePairs {62	count := 063	for i := range pairs {64		if pairs[i].nodeType.Is(nodeTypes) {65			count++66		}67	}68	out, j := make(runOncePairs, count), 069	for i := range pairs {70		if pairs[i].nodeType.Is(nodeTypes) {71			out[j] = pairs[i]72			j++73		}74	}75	return out76}77type group struct {78	suite          *Suite79	specs          Specs80	runOncePairs   map[uint]runOncePairs81	runOnceTracker map[runOncePair]types.SpecState82	succeeded bool83}84func newGroup(suite *Suite) *group {85	return &group{86		suite:          suite,87		runOncePairs:   map[uint]runOncePairs{},88		runOnceTracker: map[runOncePair]types.SpecState{},89		succeeded:      true,90	}91}92func (g *group) initialReportForSpec(spec Spec) types.SpecReport {93	return types.SpecReport{94		ContainerHierarchyTexts:     spec.Nodes.WithType(types.NodeTypeContainer).Texts(),95		ContainerHierarchyLocations: spec.Nodes.WithType(types.NodeTypeContainer).CodeLocations(),96		ContainerHierarchyLabels:    spec.Nodes.WithType(types.NodeTypeContainer).Labels(),97		LeafNodeLocation:            spec.FirstNodeWithType(types.NodeTypeIt).CodeLocation,98		LeafNodeType:                types.NodeTypeIt,99		LeafNodeText:                spec.FirstNodeWithType(types.NodeTypeIt).Text,100		LeafNodeLabels:              []string(spec.FirstNodeWithType(types.NodeTypeIt).Labels),101		ParallelProcess:             g.suite.config.ParallelProcess,102		IsSerial:                    spec.Nodes.HasNodeMarkedSerial(),103		IsInOrderedContainer:        !spec.Nodes.FirstNodeMarkedOrdered().IsZero(),104	}105}106func (g *group) evaluateSkipStatus(spec Spec) (types.SpecState, types.Failure) {107	if spec.Nodes.HasNodeMarkedPending() {108		return types.SpecStatePending, types.Failure{}109	}110	if spec.Skip {111		return types.SpecStateSkipped, types.Failure{}112	}113	if g.suite.interruptHandler.Status().Interrupted || g.suite.skipAll {114		return types.SpecStateSkipped, types.Failure{}115	}116	if !g.succeeded {117		return types.SpecStateSkipped, g.suite.failureForLeafNodeWithMessage(spec.FirstNodeWithType(types.NodeTypeIt),118			"Spec skipped because an earlier spec in an ordered container failed")119	}120	beforeOncePairs := g.runOncePairs[spec.SubjectID()].withType(types.NodeTypeBeforeAll | types.NodeTypeBeforeEach | types.NodeTypeJustBeforeEach)121	for _, pair := range beforeOncePairs {122		if g.runOnceTracker[pair].Is(types.SpecStateSkipped) {123			return types.SpecStateSkipped, g.suite.failureForLeafNodeWithMessage(spec.FirstNodeWithType(types.NodeTypeIt),124				fmt.Sprintf("Spec skipped because Skip() was called in %s", pair.nodeType))125		}126	}127	if g.suite.config.DryRun {128		return types.SpecStatePassed, types.Failure{}129	}130	return g.suite.currentSpecReport.State, g.suite.currentSpecReport.Failure131}132func (g *group) isLastSpecWithPair(specID uint, pair runOncePair) bool {133	lastSpecID := uint(0)134	for idx := range g.specs {135		if g.specs[idx].Skip {136			continue137		}138		sID := g.specs[idx].SubjectID()139		if g.runOncePairs[sID].hasRunOncePair(pair) {140			lastSpecID = sID141		}142	}143	return lastSpecID == specID144}145func (g *group) attemptSpec(isFinalAttempt bool, spec Spec) {146	interruptStatus := g.suite.interruptHandler.Status()147	pairs := g.runOncePairs[spec.SubjectID()]148	nodes := spec.Nodes.WithType(types.NodeTypeBeforeAll)149	nodes = append(nodes, spec.Nodes.WithType(types.NodeTypeBeforeEach)...).SortedByAscendingNestingLevel()150	nodes = append(nodes, spec.Nodes.WithType(types.NodeTypeJustBeforeEach).SortedByAscendingNestingLevel()...)151	nodes = append(nodes, spec.Nodes.FirstNodeWithType(types.NodeTypeIt))152	terminatingNode, terminatingPair := Node{}, runOncePair{}153	for _, node := range nodes {154		oncePair := pairs.runOncePairFor(node.ID)155		if !oncePair.isZero() && g.runOnceTracker[oncePair].Is(types.SpecStatePassed) {156			continue157		}158		g.suite.currentSpecReport.State, g.suite.currentSpecReport.Failure = g.suite.runNode(node, interruptStatus.Channel, spec.Nodes.BestTextFor(node))159		g.suite.currentSpecReport.RunTime = time.Since(g.suite.currentSpecReport.StartTime)160		if !oncePair.isZero() {161			g.runOnceTracker[oncePair] = g.suite.currentSpecReport.State162		}163		if g.suite.currentSpecReport.State != types.SpecStatePassed {164			terminatingNode, terminatingPair = node, oncePair165			break166		}167	}168	afterNodeWasRun := map[uint]bool{}169	includeDeferCleanups := false170	for {171		nodes := spec.Nodes.WithType(types.NodeTypeAfterEach)172		nodes = append(nodes, spec.Nodes.WithType(types.NodeTypeAfterAll)...).SortedByDescendingNestingLevel()173		nodes = append(spec.Nodes.WithType(types.NodeTypeJustAfterEach).SortedByDescendingNestingLevel(), nodes...)174		if !terminatingNode.IsZero() {175			nodes = nodes.WithinNestingLevel(terminatingNode.NestingLevel)176		}177		if includeDeferCleanups {178			nodes = append(nodes, g.suite.cleanupNodes.WithType(types.NodeTypeCleanupAfterEach).Reverse()...)179			nodes = append(nodes, g.suite.cleanupNodes.WithType(types.NodeTypeCleanupAfterAll).Reverse()...)180		}181		nodes = nodes.Filter(func(node Node) bool {182			if afterNodeWasRun[node.ID] {183				//this node has already been run on this attempt, don't rerun it184				return false185			}186			pair := runOncePair{}187			switch node.NodeType {188			case types.NodeTypeCleanupAfterEach, types.NodeTypeCleanupAfterAll:189				// check if we were generated in an AfterNode that has already run190				if afterNodeWasRun[node.NodeIDWhereCleanupWasGenerated] {191					return true // we were, so we should definitely run this cleanup now192				}193				// looks like this cleanup nodes was generated by a before node or it.194				// the run-once status of a cleanup node is governed by the run-once status of its generator195				pair = pairs.runOncePairFor(node.NodeIDWhereCleanupWasGenerated)196			default:197				pair = pairs.runOncePairFor(node.ID)198			}199			if pair.isZero() {200				// this node is not governed by any run-once policy, we should run it201				return true202			}203			// it's our last chance to run if we're the last spec for our oncePair204			isLastSpecWithPair := g.isLastSpecWithPair(spec.SubjectID(), pair)205			switch g.suite.currentSpecReport.State {206			case types.SpecStatePassed: //this attempt is passing...207				return isLastSpecWithPair //...we should run-once if we'this is our last chance208			case types.SpecStateSkipped: //the spec was skipped by the user...209				if isLastSpecWithPair {210					return true //...we're the last spec, so we should run the AfterNode211				}212				if !terminatingPair.isZero() && terminatingNode.NestingLevel == node.NestingLevel {213					return true //...or, a run-once node at our nesting level was skipped which means this is our last chance to run214				}215			case types.SpecStateFailed, types.SpecStatePanicked: // the spec has failed...216				if isFinalAttempt {217					return true //...if this was the last attempt then we're the last spec to run and so the AfterNode should run218				}219				if !terminatingPair.isZero() { // ...and it failed in a run-once.  which will be running again220					if node.NodeType.Is(types.NodeTypeCleanupAfterEach | types.NodeTypeCleanupAfterAll) {221						return terminatingNode.ID == node.NodeIDWhereCleanupWasGenerated // we should run this node if we're a clean-up generated by it222					} else {223						return terminatingNode.NestingLevel == node.NestingLevel // ...or if we're at the same nesting level224					}225				}226			case types.SpecStateInterrupted, types.SpecStateAborted: // ...we've been interrupted and/or aborted227				return true //...that means the test run is over and we should clean up the stack.  Run the AfterNode228			}229			return false230		})231		if len(nodes) == 0 && includeDeferCleanups {232			break233		}234		for _, node := range nodes {235			afterNodeWasRun[node.ID] = true236			state, failure := g.suite.runNode(node, g.suite.interruptHandler.Status().Channel, spec.Nodes.BestTextFor(node))237			g.suite.currentSpecReport.RunTime = time.Since(g.suite.currentSpecReport.StartTime)238			if g.suite.currentSpecReport.State == types.SpecStatePassed || state == types.SpecStateAborted {239				g.suite.currentSpecReport.State = state240				g.suite.currentSpecReport.Failure = failure241			}242		}243		includeDeferCleanups = true244	}245}246func (g *group) run(specs Specs) {247	g.specs = specs248	for _, spec := range g.specs {249		g.runOncePairs[spec.SubjectID()] = runOncePairsForSpec(spec)250	}251	for _, spec := range g.specs {252		g.suite.currentSpecReport = g.initialReportForSpec(spec)253		g.suite.currentSpecReport.State, g.suite.currentSpecReport.Failure = g.evaluateSkipStatus(spec)254		g.suite.reporter.WillRun(g.suite.currentSpecReport)255		g.suite.reportEach(spec, types.NodeTypeReportBeforeEach)256		skip := g.suite.config.DryRun || g.suite.currentSpecReport.State.Is(types.SpecStateFailureStates|types.SpecStateSkipped|types.SpecStatePending)257		g.suite.currentSpecReport.StartTime = time.Now()258		if !skip {259			maxAttempts := max(1, spec.FlakeAttempts())260			if g.suite.config.FlakeAttempts > 0 {261				maxAttempts = g.suite.config.FlakeAttempts262			}263			for attempt := 0; attempt < maxAttempts; attempt++ {...

Full Screen

Full Screen

runOncePairsForSpec

Using AI Code Generation

copy

Full Screen

1func (s *S) TestRunOncePairsForSpec(c *C) {2	s.specs = []string{"a", "b", "c", "d", "e"}3	s.specsRan = []string{"a", "b", "c", "d", "e"}4	s.specsToRun = []string{"a", "b", "c", "d", "e"}5	s.runOncePairsForSpec("a", "b", "c", "d", "e")6	c.Assert(s.runOncePairs, DeepEquals, [][]string{7		{"a", "b"},8		{"b", "c"},9		{"c", "d"},10		{"d", "e"},11	})12}13func (s *S) TestRunOncePairsForSpecWithSkip(c *C) {14	s.specs = []string{"a", "b", "c", "d", "e"}15	s.specsRan = []string{"a", "b", "d"}16	s.specsToRun = []string{"a", "b", "c", "d", "e"}17	s.runOncePairsForSpec("a", "b", "c", "d", "e")18	c.Assert(s.runOncePairs, DeepEquals, [][]string{19		{"a", "b"},20		{"b", "c"},21		{"c", "d"},22		{"d", "e"},23	})24}25func (s *S) TestRunOncePairsForSpecWithSkipAndRun(c *C) {26	s.specs = []string{"a", "b", "c", "d", "e"}27	s.specsRan = []string{"a", "b", "d"}28	s.specsToRun = []string{"a", "b", "c", "d", "e"}29	s.runOncePairsForSpec("a", "b", "d", "c", "e")30	c.Assert(s.runOncePairs, DeepEquals, [][]string{31		{"a", "b"},32		{"d", "c"},

Full Screen

Full Screen

runOncePairsForSpec

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println("Hello, playground")4	spec := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}5	pairs := runOncePairsForSpec(spec)6	fmt.Println(pairs)7}8func runOncePairsForSpec(spec []string) [][]string {9	rType := reflect.TypeOf(r)10	method, _ := rType.MethodByName("runOncePairsForSpec")11	inputs := make([]reflect.Value, 1)12	specSlice := make([]reflect.Value, len(spec))13	for i, v := range spec {14		specSlice[i] = reflect.ValueOf(v)15	}16	inputs[0] = reflect.ValueOf(specSlice)17	outputs := method.Func.Call(inputs)18	pairs := outputs[0].Interface().([][]string)19}20type runOncePairs struct {21}22func (r runOncePairs) runOncePairsForSpec(spec []string) [][]string {23	specLen := len(spec)24	for i := 0; i < specLen; i++ {25		for j := 0; j < len(remainingSpec); j++ {

Full Screen

Full Screen

runOncePairsForSpec

Using AI Code Generation

copy

Full Screen

1func (s *Spec) RunOnce(pairs ...Pair) (interface{}, error) {2  return s.internal.runOncePairsForSpec(pairs, s)3}4func (s *Spec) RunOnce(pairs ...Pair) (interface{}, error) {5  return s.internal.runOncePairsForSpec(pairs, s)6}7func (s *Spec) RunOnce(pairs ...Pair) (interface{}, error) {8  return s.internal.runOncePairsForSpec(pairs, s)9}10func (s *Spec) RunOnce(pairs ...Pair) (interface{}, error) {11  return s.internal.runOncePairsForSpec(pairs, s)12}13func (s *Spec) RunOnce(pairs ...Pair) (interface{}, error) {14  return s.internal.runOncePairsForSpec(pairs, s)15}16func (s *Spec) RunOnce(pairs ...Pair) (interface{}, error) {17  return s.internal.runOncePairsForSpec(pairs,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful