How to use NewCustomCodeLocation method of types Package

Best Ginkgo code snippet using types.NewCustomCodeLocation

Run Ginkgo automation tests on LambdaTest cloud grid

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

code_location_test.go

Source: code_location_test.go Github

copy
1package types_test
2
3import (
4	"runtime"
5
6	. "github.com/onsi/ginkgo/v2"
7	"github.com/onsi/ginkgo/v2/types"
8	. "github.com/onsi/gomega"
9)
10
11var _ = Describe("CodeLocation", func() {
12	var codeLocation types.CodeLocation
13	var expectedFileName string
14	var expectedLineNumber int
15
16	caller0 := func() {
17		codeLocation = types.NewCodeLocation(1)
18	}
19
20	caller1 := func() {
21		_, expectedFileName, expectedLineNumber, _ = runtime.Caller(0)
22		expectedLineNumber += 2
23		caller0()
24	}
25
26	BeforeEach(func() {
27		caller1()
28	})
29
30	It("should use the passed in skip parameter to pick out the correct file & line number", func() {
31		Ω(codeLocation.FileName).Should(Equal(expectedFileName))
32		Ω(codeLocation.LineNumber).Should(Equal(expectedLineNumber))
33		Ω(codeLocation.FullStackTrace).Should(BeZero())
34	})
35
36	Describe("stringer behavior", func() {
37		It("should stringify nicely", func() {
38			Ω(codeLocation.String()).Should(ContainSubstring("code_location_test.go:%d", expectedLineNumber))
39		})
40	})
41
42	Describe("with a custom message", func() {
43		BeforeEach(func() {
44			codeLocation = types.NewCustomCodeLocation("I'm right here.")
45		})
46
47		It("emits the custom message", func() {
48			Ω(codeLocation.String()).Should(Equal("I'm right here."))
49		})
50	})
51
52	Describe("Fetching the line from the file in question", func() {
53		It("works", func() {
54			codeLocation = types.NewCodeLocation(0)
55			codeLocation.LineNumber = codeLocation.LineNumber - 2
56			Ω(codeLocation.ContentsOfLine()).Should(Equal("\tDescribe(\"Fetching the line from the file in question\", func() {"))
57		})
58
59		It("returns empty string if the line is not found or is out of bounds", func() {
60			codeLocation = types.CodeLocation{
61				FileName:   "foo.go",
62				LineNumber: 0,
63			}
64			Ω(codeLocation.ContentsOfLine()).Should(BeZero())
65
66			codeLocation = types.NewCodeLocation(0)
67			codeLocation.LineNumber = codeLocation.LineNumber + 1000000
68			Ω(codeLocation.ContentsOfLine()).Should(BeZero())
69		})
70	})
71
72	Describe("PruneStack", func() {
73		It("should remove any references to ginkgo and pkg/testing and pkg/runtime", func() {
74			// Hard-coded string, loosely based on what debug.Stack() produces.
75			input := `Skip: skip()
76/Skip/me
77Skip: skip()
78/Skip/me
79Something: Func()
80/Users/whoever/gospace/src/github.com/onsi/ginkgo/whatever.go:10 (0x12314)
81SomethingInternalToGinkgo: Func()
82/Users/whoever/gospace/src/github.com/onsi/ginkgo/whatever_else.go:10 (0x12314)
83Oops: BlowUp()
84/usr/goroot/pkg/strings/oops.go:10 (0x12341)
85MyCode: Func()
86/Users/whoever/gospace/src/mycode/code.go:10 (0x12341)
87MyCodeTest: Func()
88/Users/whoever/gospace/src/mycode/code_test.go:10 (0x12341)
89TestFoo: RunSpecs(t, "Foo Suite")
90/Users/whoever/gospace/src/mycode/code_suite_test.go:12 (0x37f08)
91TestingT: Blah()
92/usr/goroot/pkg/testing/testing.go:12 (0x37f08)
93Something: Func()
94/usr/goroot/pkg/runtime/runtime.go:12 (0x37f08)
95`
96			prunedStack := types.PruneStack(input, 1)
97			Ω(prunedStack).Should(Equal(`Oops: BlowUp()
98/usr/goroot/pkg/strings/oops.go:10 (0x12341)
99MyCode: Func()
100/Users/whoever/gospace/src/mycode/code.go:10 (0x12341)
101MyCodeTest: Func()
102/Users/whoever/gospace/src/mycode/code_test.go:10 (0x12341)
103TestFoo: RunSpecs(t, "Foo Suite")
104/Users/whoever/gospace/src/mycode/code_suite_test.go:12 (0x37f08)`))
105		})
106	})
107})
108
Full Screen

code_location.go

Source: code_location.go Github

copy
1package types
2
3import (
4	"fmt"
5	"os"
6	"regexp"
7	"runtime"
8	"runtime/debug"
9	"strings"
10)
11
12type CodeLocation struct {
13	FileName       string `json:",omitempty"`
14	LineNumber     int    `json:",omitempty"`
15	FullStackTrace string `json:",omitempty"`
16	CustomMessage  string `json:",omitempty"`
17}
18
19func (codeLocation CodeLocation) String() string {
20	if codeLocation.CustomMessage != "" {
21		return codeLocation.CustomMessage
22	}
23	return fmt.Sprintf("%s:%d", codeLocation.FileName, codeLocation.LineNumber)
24}
25
26func (codeLocation CodeLocation) ContentsOfLine() string {
27	if codeLocation.CustomMessage != "" {
28		return ""
29	}
30	contents, err := os.ReadFile(codeLocation.FileName)
31	if err != nil {
32		return ""
33	}
34	lines := strings.Split(string(contents), "\n")
35	if len(lines) < codeLocation.LineNumber {
36		return ""
37	}
38	return lines[codeLocation.LineNumber-1]
39}
40
41func NewCustomCodeLocation(message string) CodeLocation {
42	return CodeLocation{
43		CustomMessage: message,
44	}
45}
46
47func NewCodeLocation(skip int) CodeLocation {
48	_, file, line, _ := runtime.Caller(skip + 1)
49	return CodeLocation{FileName: file, LineNumber: line}
50}
51
52func NewCodeLocationWithStackTrace(skip int) CodeLocation {
53	_, file, line, _ := runtime.Caller(skip + 1)
54	stackTrace := PruneStack(string(debug.Stack()), skip+1)
55	return CodeLocation{FileName: file, LineNumber: line, FullStackTrace: stackTrace}
56}
57
58// PruneStack removes references to functions that are internal to Ginkgo
59// and the Go runtime from a stack string and a certain number of stack entries
60// at the beginning of the stack. The stack string has the format
61// as returned by runtime/debug.Stack. The leading goroutine information is
62// optional and always removed if present. Beware that runtime/debug.Stack
63// adds itself as first entry, so typically skip must be >= 1 to remove that
64// entry.
65func PruneStack(fullStackTrace string, skip int) string {
66	stack := strings.Split(fullStackTrace, "\n")
67	// Ensure that the even entries are the method names and the
68	// the odd entries the source code information.
69	if len(stack) > 0 && strings.HasPrefix(stack[0], "goroutine ") {
70		// Ignore "goroutine 29 [running]:" line.
71		stack = stack[1:]
72	}
73	// The "+1" is for skipping over the initial entry, which is
74	// runtime/debug.Stack() itself.
75	if len(stack) > 2*(skip+1) {
76		stack = stack[2*(skip+1):]
77	}
78	prunedStack := []string{}
79	if os.Getenv("GINKGO_PRUNE_STACK") == "FALSE" {
80		prunedStack = stack
81	} else {
82		re := regexp.MustCompile(`\/ginkgo\/|\/pkg\/testing\/|\/pkg\/runtime\/`)
83		for i := 0; i < len(stack)/2; i++ {
84			// We filter out based on the source code file name.
85			if !re.Match([]byte(stack[i*2+1])) {
86				prunedStack = append(prunedStack, stack[i*2])
87				prunedStack = append(prunedStack, stack[i*2+1])
88			}
89		}
90	}
91	return strings.Join(prunedStack, "\n")
92}
93
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in

Trigger NewCustomCodeLocation code on LambdaTest Cloud Grid

Execute automation tests with NewCustomCodeLocation on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)