Skip to content

Commit

Permalink
Adds 'ginkgo outline' command
Browse files Browse the repository at this point in the history
Implements feature request in onsi#753

Signed-off-by: Daniel Lipovetsky <daniel.lipovetsky@gmail.com>
  • Loading branch information
dlipovetsky committed Dec 26, 2020
1 parent b08408c commit cc7c3f2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ginkgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ will output an executable file named `package.test`. This can be run directly o
ginkgo <path-to-package.test>
To print an outline of Ginkgo specs and containers in a file:
gingko outline <filename>
To print out Ginkgo's version:
ginkgo version
Expand Down Expand Up @@ -172,6 +177,7 @@ func init() {
Commands = append(Commands, BuildUnfocusCommand())
Commands = append(Commands, BuildVersionCommand())
Commands = append(Commands, BuildHelpCommand())
Commands = append(Commands, BuildOutlineCommand())
}

func main() {
Expand Down
70 changes: 70 additions & 0 deletions ginkgo/outline_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"go/parser"
"go/token"
"os"

"github.com/onsi/ginkgo/ginkgo/outline"
)

func BuildOutlineCommand() *Command {
const defaultFormat = "csv"
var format string
flagSet := flag.NewFlagSet("outline", flag.ExitOnError)
flagSet.StringVar(&format, "format", defaultFormat, "Format of outline. Accepted: 'csv', 'json'")
return &Command{
Name: "outline",
FlagSet: flagSet,
UsageCommand: "ginkgo outline <filename>",
Usage: []string{
"Outline of Ginkgo symbols for the file",
},
Command: func(args []string, additionalArgs []string) {
outlineFile(args, format)
},
}
}

func outlineFile(args []string, format string) {
if len(args) != 1 {
println("usage: ginkgo outline <filename>")
os.Exit(1)
}

filename := args[0]
fset := token.NewFileSet()

src, err := parser.ParseFile(fset, filename, nil, 0)
if err != nil {
println(fmt.Sprintf("error parsing source: %s", err))
os.Exit(1)
}

o, err := outline.FromASTFile(fset, src)
if err != nil {
println(fmt.Sprintf("error creating outline: %s", err))
os.Exit(1)
}

var oerr error
switch format {
case "csv":
_, oerr = fmt.Print(o)
case "json":
b, err := json.Marshal(o)
if err != nil {
println(fmt.Sprintf("error marshalling to json: %s", err))
}
_, oerr = fmt.Print(string(b))
default:
complainAndQuit(fmt.Sprintf("format %s not accepted", format))
}
if oerr != nil {
println(fmt.Sprintf("error writing outline: %s", oerr))
os.Exit(1)
}
}

0 comments on commit cc7c3f2

Please sign in to comment.