Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support to print list of implemented steps #2433

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import (
"fmt"
supersort "sort"

gm "github.com/getgauge/gauge-proto/go/gauge_messages"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/manifest"
"github.com/getgauge/gauge/runner"
"github.com/getgauge/gauge/util"

"github.com/getgauge/gauge/filter"
"github.com/getgauge/gauge/gauge"
Expand All @@ -22,8 +26,8 @@ import (
var (
listCmd = &cobra.Command{
Use: "list [flags] [args]",
Short: "List specifications, scenarios or tags for a gauge project",
Long: `List specifications, scenarios or tags for a gauge project`,
Short: "List specifications, scenarios, steps or tags for a gauge project",
Long: `List specifications, scenarios, steps or tags for a gauge project`,
Example: ` gauge list --tags specs`,
Run: func(cmd *cobra.Command, args []string) {
if err := config.SetProjectRoot(args); err != nil {
Expand All @@ -46,7 +50,11 @@ var (
logger.Info(true, "[Tags]")
listTags(specs, print)
}
if !specsFlag && !scenariosFlag && !tagsFlag {
if stepsFlag {
logger.Info(true, "[Steps]")
listSteps(specs, print)
}
if !specsFlag && !scenariosFlag && !tagsFlag && !stepsFlag {
exit(fmt.Errorf("Missing flag, nothing to list"), cmd.UsageString())
}
},
Expand All @@ -55,13 +63,15 @@ var (
tagsFlag bool
specsFlag bool
scenariosFlag bool
stepsFlag bool
)

func init() {
GaugeCmd.AddCommand(listCmd)
listCmd.Flags().BoolVarP(&tagsFlag, "tags", "", false, "List the tags in projects")
listCmd.Flags().BoolVarP(&specsFlag, "specs", "", false, "List the specifications in projects")
listCmd.Flags().BoolVarP(&scenariosFlag, "scenarios", "", false, "List the scenarios in projects")
listCmd.Flags().BoolVarP(&stepsFlag, "steps", "", false, "List all the steps in projects (including concept steps). Does not include unused steps.")
}

type handleResult func([]string)
Expand Down Expand Up @@ -96,6 +106,10 @@ func listSpecifications(s []*gauge.Specification, f handleResult) {
f(sortedDistinctElements(allSpecs))
}

func listSteps(s []*gauge.Specification, f handleResult) {
f(sortedDistinctElements(getImplementedStepsWithAliases()))
}

func sortedDistinctElements(s []string) []string {
unique := uniqueNonEmptyElementsOf(s)
supersort.Strings(unique)
Expand All @@ -120,5 +134,31 @@ func uniqueNonEmptyElementsOf(input []string) []string {
}

return us
}

func getImplementedStepsWithAliases() []string {
r, err := connectToRunner()
defer func() { _ = r.Kill() }()
if err != nil {
panic(err)
}
getAllStepsRequest := &gm.Message{MessageType: gm.Message_StepNamesRequest, StepNamesRequest: &gm.StepNamesRequest{}}
response, err := r.ExecuteMessageWithTimeout(getAllStepsRequest)
if err != nil {
exit(fmt.Errorf("error while connecting to runner : %s", err.Error()), "unable to get steps from runner")
}
return response.GetStepNamesResponse().GetSteps()
}

var connectToRunner = func() (runner.Runner, error) {
outFile, err := util.OpenFile(logger.ActiveLogFile)
if err != nil {
return nil, err
}
manifest, err := manifest.ProjectManifest()
if err != nil {
return nil, err
}

return runner.StartGrpcRunner(manifest, outFile, outFile, config.IdeRequestTimeout(), false)
}
28 changes: 28 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"reflect"
"testing"

"github.com/getgauge/gauge-proto/go/gauge_messages"
"github.com/getgauge/gauge/gauge"
"github.com/getgauge/gauge/runner"
)

func TestOnlyUniqueTagsAreReturned(t *testing.T) {
Expand All @@ -35,6 +37,32 @@ func TestOnlyUniqueSceanriosAreReturned(t *testing.T) {
})
}

func TestOnlyUniqueStepsAreReturned(t *testing.T) {
connectToRunner = func() (runner.Runner, error) {
return &mockRunner{
response: &gauge_messages.Message{
StepNamesResponse: &gauge_messages.StepNamesResponse{
Steps: []string{
"Vowels are <vowelString>.",
"Vowels in English language are <vowelString>.",
"The word <word> has <expectedCount> vowels.",
"Almost all words have vowels <wordsTable>",
},
},
},
}, nil
}
expected := []string{
"Almost all words have vowels <wordsTable>",
"The word <word> has <expectedCount> vowels.",
"Vowels are <vowelString>.",
"Vowels in English language are <vowelString>.",
}
listSteps([]*gauge.Specification{buildTestSpecification()}, func(res []string) {
verifyUniqueness(res, expected, t)
})
}

func buildTestSpecification() *gauge.Specification {
return &gauge.Specification{
Heading: &gauge.Heading{
Expand Down
50 changes: 50 additions & 0 deletions cmd/mockRunner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE in the project root for license information.
*----------------------------------------------------------------*/

package cmd

import (
"net"

"github.com/getgauge/gauge-proto/go/gauge_messages"
"github.com/getgauge/gauge/runner"
)

type mockRunner struct {
response *gauge_messages.Message
}

func (r *mockRunner) ExecuteAndGetStatus(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
return nil
}

func (r *mockRunner) ExecuteMessageWithTimeout(m *gauge_messages.Message) (*gauge_messages.Message, error) {
return r.response, nil
}

func (r *mockRunner) Alive() bool {
return true
}

func (r *mockRunner) Kill() error {
return nil
}

func (r *mockRunner) Connection() net.Conn {
return nil
}

func (r *mockRunner) IsMultithreaded() bool {
return false
}

func (r *mockRunner) Info() *runner.RunnerInfo {
return &runner.RunnerInfo{Killed: false}
}

func (r *mockRunner) Pid() int {
return 0
}
3 changes: 2 additions & 1 deletion filter/specItemFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ package filter

import (
"errors"
"github.com/getgauge/gauge/env"
"go/constant"
"go/token"
"go/types"
"regexp"
"strconv"
"strings"

"github.com/getgauge/gauge/env"

"fmt"

"github.com/getgauge/gauge/gauge"
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// CurrentGaugeVersion represents the current version of Gauge
var CurrentGaugeVersion = &Version{1, 6, 6}
var CurrentGaugeVersion = &Version{1, 6, 7}

// BuildMetadata represents build information of current release (e.g, nightly build information)
var BuildMetadata = ""
Expand Down