-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
scenario_state.go
99 lines (83 loc) · 3.41 KB
/
scenario_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cucumber
import (
"fmt"
"sort"
"strings"
"github.com/cucumber/messages-go/v10"
"github.com/git-town/git-town/v9/src/stringslice"
"github.com/git-town/git-town/v9/test/datatable"
"github.com/git-town/git-town/v9/test/fixture"
"github.com/git-town/git-town/v9/test/helpers"
)
// ScenarioState constains the state that is shared by all steps within a scenario.
type ScenarioState struct {
// the Fixture used in the current scenario
fixture fixture.Fixture
// initialLocalBranches contains the local branches before the WHEN steps run
initialLocalBranches []string
// initialRemoteBranches contains the remote branches before the WHEN steps run
initialRemoteBranches []string
// initialCommits describes the commits in this Git environment before the WHEN steps ran.
initialCommits *messages.PickleStepArgument_PickleTable
// initialBranchHierarchy describes the branch hierarchy before the WHEN steps ran.
initialBranchHierarchy datatable.DataTable
// initialCurrentBranch contains the name of the branch that was checked out before the WHEN steps ran
initialCurrentBranch string
// the error of the last run of Git Town
runExitCode int
// indicates whether the scenario has verified the error
runExitCodeChecked bool
// the output of the last run of Git Town
runOutput string
// content of the uncommitted file in the workspace
uncommittedContent string
// name of the uncommitted file in the workspace
uncommittedFileName string
}
// Reset restores the null value of this ScenarioState.
func (state *ScenarioState) Reset(gitEnv fixture.Fixture) {
state.fixture = gitEnv
state.initialLocalBranches = []string{"main"}
state.initialRemoteBranches = []string{"main"}
state.initialCommits = nil
state.initialBranchHierarchy = datatable.DataTable{Cells: [][]string{{"BRANCH", "PARENT"}}}
state.initialCurrentBranch = ""
state.runOutput = ""
state.runExitCode = 0
state.runExitCodeChecked = false
state.uncommittedFileName = ""
state.uncommittedContent = ""
}
// InitialBranches provides the branches in this Scenario before the WHEN steps ran.
func (state *ScenarioState) InitialBranches() datatable.DataTable {
result := datatable.DataTable{}
result.AddRow("REPOSITORY", "BRANCHES")
sort.Strings(state.initialLocalBranches)
state.initialLocalBranches = stringslice.Hoist(state.initialLocalBranches, "main")
sort.Strings(state.initialRemoteBranches)
state.initialRemoteBranches = stringslice.Hoist(state.initialRemoteBranches, "main")
localBranchesJoined := strings.Join(state.initialLocalBranches, ", ")
remoteBranchesJoined := strings.Join(state.initialRemoteBranches, ", ")
if localBranchesJoined == remoteBranchesJoined {
result.AddRow("local, origin", localBranchesJoined)
} else {
result.AddRow("local", localBranchesJoined)
if remoteBranchesJoined != "" {
result.AddRow("origin", remoteBranchesJoined)
}
}
return result
}
// compareExistingCommits compares the commits in the Git environment of the given ScenarioState
// against the given Gherkin table.
func (state *ScenarioState) compareTable(table *messages.PickleStepArgument_PickleTable) error {
fields := helpers.TableFields(table)
commitTable := state.fixture.CommitTable(fields)
diff, errorCount := commitTable.EqualGherkin(table)
if errorCount != 0 {
fmt.Printf("\nERROR! Found %d differences in the existing commits\n\n", errorCount)
fmt.Println(diff)
return fmt.Errorf("mismatching commits found, see diff above")
}
return nil
}