How to use outlineFile method of outline Package

Best Ginkgo code snippet using outline.outlineFile

Run Ginkgo automation tests on LambdaTest cloud grid

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

outline_test.go

Source: outline_test.go Github

copy
1package outline
2
3import (
4	"encoding/json"
5	"go/parser"
6	"go/token"
7	"io/ioutil"
8	"log"
9	"path/filepath"
10	"testing"
11
12	"github.com/google/go-cmp/cmp"
13)
14
15func TestFromASTFile(t *testing.T) {
16	tcs := []struct {
17		srcFile     string
18		outlineFile string
19	}{
20		{
21			srcFile:     "normal_test.go",
22			outlineFile: "normal_test_outline.json",
23		},
24		// TODO: enable tests
25		// {
26		// 	srcFile:     "nodot_test.go",
27		// 	outlineFile: "normal_test_outline.json",
28		// },
29		// {
30		// 	srcFile:     "alias_test.go",
31		// 	outlineFile: "normal_test_outline.json",
32		// },
33		{
34			srcFile:     "focused_test.go",
35			outlineFile: "focused_test_outline.json",
36		},
37		{
38			srcFile:     "pending_test.go",
39			outlineFile: "pending_test_outline.json",
40		},
41		{
42			srcFile:     "suite_test.go",
43			outlineFile: "suite_test_outline.json",
44		},
45	}
46
47	for _, tc := range tcs {
48		t.Run(tc.srcFile, func(t *testing.T) {
49			fset := token.NewFileSet()
50			astFile, err := parser.ParseFile(fset, filepath.Join("testdata", tc.srcFile), nil, 0)
51			if err != nil {
52				log.Fatalf("error parsing source: %s", err)
53			}
54
55			o, err := FromASTFile(fset, astFile)
56			if err != nil {
57				t.Fatalf("error creating outline %s", err)
58			}
59
60			got, err := json.MarshalIndent(o, "", "  ")
61			if err != nil {
62				log.Fatalf("error marshalling outline to json: %s", err)
63			}
64			want, err := ioutil.ReadFile(filepath.Join("testdata", tc.outlineFile))
65			if err != nil {
66				log.Fatalf("error reading outline fixture: %s", err)
67			}
68			if diff := cmp.Diff(want, got); diff != "" {
69				t.Fatalf("output mismatch (-want, +got):\n%s", diff)
70			}
71		})
72	}
73}
74
Full Screen

addOutlines.go

Source: addOutlines.go Github

copy
1package CAJParser
2
3import (
4	"bufio"
5	"fmt"
6	"io"
7	"log"
8	"os"
9	"os/exec"
10	"path"
11)
12
13func addOutlines(source io.ReadSeeker, output io.Writer, toc []toc) error {
14	// write pdfData to File
15	tempFile, err := os.CreateTemp("", "caj2pdf")
16	if err != nil {
17		panic(err)
18	}
19	defer os.Remove(tempFile.Name())
20
21	// Copy source to temp file
22	_, err = io.Copy(tempFile, source)
23
24	tempFile.Close()
25
26	tempFolder, err := os.MkdirTemp("", "caj2pdfDir")
27	if err != nil {
28		panic(err)
29	}
30	defer os.RemoveAll(tempFolder)
31
32	outputFilename := path.Join(tempFolder, "temp.pdf")
33
34	outlineFile, err := os.CreateTemp("", "caj2pdf")
35	if err != nil {
36		panic(err)
37	}
38	defer os.Remove(tempFile.Name())
39
40	_generateOutlineFile(toc, outlineFile)
41
42	_addOutlines(tempFile.Name(), outputFilename, outlineFile.Name())
43
44	// Copy repaired file to target
45	outputFileBuffer, err := os.Open(outputFilename)
46	if err != nil {
47		panic(err)
48	}
49	defer outputFileBuffer.Close()
50
51	_, err = io.Copy(output, outputFileBuffer)
52
53	return nil
54}
55
56func _addOutlines(source string, target string, outline string) {
57	cmd := exec.Command("pdfoutline", source, outline, target)
58	stderr, _ := cmd.StderrPipe()
59	if err := cmd.Start(); err != nil {
60		log.Fatal(err)
61	}
62
63	scanner := bufio.NewScanner(stderr)
64	for scanner.Scan() {
65		fmt.Println(scanner.Text())
66	}
67}
68
69func _generateOutlineFile(toc []toc, file io.Writer) {
70	outline := ""
71	for _, item := range toc {
72		outline += fmt.Sprintf("%d %d %s\n", item.Level, item.Page, item.Title)
73	}
74	file.Write([]byte(outline))
75}
76
Full Screen

outline_command.go

Source: outline_command.go Github

copy
1package main
2
3import (
4	"encoding/json"
5	"flag"
6	"fmt"
7	"go/parser"
8	"go/token"
9	"os"
10
11	"github.com/onsi/ginkgo/ginkgo/outline"
12)
13
14const (
15	// indentWidth is the width used by the 'indent' output
16	indentWidth = 4
17	// stdinAlias is a portable alias for stdin. This convention is used in
18	// other CLIs, e.g., kubectl.
19	stdinAlias   = "-"
20	usageCommand = "ginkgo outline <filename>"
21)
22
23func BuildOutlineCommand() *Command {
24	const defaultFormat = "csv"
25	var format string
26	flagSet := flag.NewFlagSet("outline", flag.ExitOnError)
27	flagSet.StringVar(&format, "format", defaultFormat, "Format of outline. Accepted: 'csv', 'indent', 'json'")
28	return &Command{
29		Name:         "outline",
30		FlagSet:      flagSet,
31		UsageCommand: usageCommand,
32		Usage: []string{
33			"Create an outline of Ginkgo symbols for a file",
34			"To read from stdin, use: `ginkgo outline -`",
35			"Accepts the following flags:",
36		},
37		Command: func(args []string, additionalArgs []string) {
38			outlineFile(args, format)
39		},
40	}
41}
42
43func outlineFile(args []string, format string) {
44	if len(args) != 1 {
45		println(fmt.Sprintf("usage: %s", usageCommand))
46		os.Exit(1)
47	}
48
49	filename := args[0]
50	var src *os.File
51	if filename == stdinAlias {
52		src = os.Stdin
53	} else {
54		var err error
55		src, err = os.Open(filename)
56		if err != nil {
57			println(fmt.Sprintf("error opening file: %s", err))
58			os.Exit(1)
59		}
60	}
61
62	fset := token.NewFileSet()
63
64	parsedSrc, err := parser.ParseFile(fset, filename, src, 0)
65	if err != nil {
66		println(fmt.Sprintf("error parsing source: %s", err))
67		os.Exit(1)
68	}
69
70	o, err := outline.FromASTFile(fset, parsedSrc)
71	if err != nil {
72		println(fmt.Sprintf("error creating outline: %s", err))
73		os.Exit(1)
74	}
75
76	var oerr error
77	switch format {
78	case "csv":
79		_, oerr = fmt.Print(o)
80	case "indent":
81		_, oerr = fmt.Print(o.StringIndent(indentWidth))
82	case "json":
83		b, err := json.Marshal(o)
84		if err != nil {
85			println(fmt.Sprintf("error marshalling to json: %s", err))
86		}
87		_, oerr = fmt.Println(string(b))
88	default:
89		complainAndQuit(fmt.Sprintf("format %s not accepted", format))
90	}
91	if oerr != nil {
92		println(fmt.Sprintf("error writing outline: %s", oerr))
93		os.Exit(1)
94	}
95}
96
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

Trigger outlineFile code on LambdaTest Cloud Grid

Execute automation tests with outlineFile 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)