Skip to content

Commit 1b4a39b

Browse files
committed
Code-style improvements
Signed-off-by: Ilya <[email protected]>
1 parent 75fd705 commit 1b4a39b

File tree

6 files changed

+43
-31
lines changed

6 files changed

+43
-31
lines changed

pkg/pluginregistry/bundles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func (r *PluginRegistry) NewTestStepBundle(ctx xcontext.Context, testStepDescrip
3030
if len(label) == 0 {
3131
return nil, ErrStepLabelIsMandatory{TestStepDescriptor: testStepDescriptor}
3232
}
33-
if err := test.CheckVariableName(label); err != nil {
34-
return nil, InvalidVariableFormat{InvalidName: label, Err: err}
33+
if err := test.CheckIdentifier(label); err != nil {
34+
return nil, ErrInvalidStepLabelFormat{InvalidName: label, Err: err}
3535
}
3636

3737
testStepBundle := test.TestStepBundle{

pkg/pluginregistry/errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ func (err ErrStepLabelIsMandatory) Error() string {
1919
return fmt.Sprintf("step has no label, but it is mandatory (step: %+v)", err.TestStepDescriptor)
2020
}
2121

22-
// InvalidVariableFormat tells that a variable name doesn't fit the variable name format (alphanum + '_')
23-
type InvalidVariableFormat struct {
22+
// ErrInvalidStepLabelFormat tells that a variable name doesn't fit the variable name format (alphanum + '_')
23+
type ErrInvalidStepLabelFormat struct {
2424
InvalidName string
2525
Err error
2626
}
2727

28-
func (err InvalidVariableFormat) Error() string {
28+
func (err ErrInvalidStepLabelFormat) Error() string {
2929
return fmt.Sprintf("'%s' doesn't match variable name format: %v", err.InvalidName, err.Err)
3030
}
3131

32-
func (err InvalidVariableFormat) Unwrap() error {
32+
func (err ErrInvalidStepLabelFormat) Unwrap() error {
3333
return err.Err
3434
}

pkg/runner/test_runner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type TestRunner struct {
4343
steps []*stepState // The pipeline, in order of execution
4444
targets map[string]*targetState // Target state lookup map
4545

46-
stepsVariables *testStepsVariables
46+
stepOutputs *testStepsVariables // contains emitted steps variables
4747

4848
// One mutex to rule them all, used to serialize access to all the state above.
4949
// Could probably be split into several if necessary.
@@ -145,14 +145,14 @@ func (tr *TestRunner) Run(
145145
}
146146

147147
var err error
148-
tr.stepsVariables, err = newTestStepsVariables(t.TestStepsBundles)
148+
tr.stepOutputs, err = newTestStepsVariables(t.TestStepsBundles)
149149
if err != nil {
150150
ctx.Errorf("Failed to initialise test steps variables: %v", err)
151151
return nil, nil, err
152152
}
153153

154154
for targetID, targetState := range tr.targets {
155-
if err := tr.stepsVariables.initTargetStepsVariables(targetID, targetState.StepsVariables); err != nil {
155+
if err := tr.stepOutputs.initTargetStepsVariables(targetID, targetState.StepsVariables); err != nil {
156156
ctx.Errorf("Failed to initialise test steps variables for target: %s: %v", targetID, err)
157157
return nil, nil, err
158158
}
@@ -221,7 +221,7 @@ func (tr *TestRunner) Run(
221221
numInFlightTargets := 0
222222
for i, tgt := range targets {
223223
tgs := tr.targets[tgt.ID]
224-
tgs.StepsVariables, err = tr.stepsVariables.getTargetStepsVariables(tgt.ID)
224+
tgs.StepsVariables, err = tr.stepOutputs.getTargetStepsVariables(tgt.ID)
225225
if err != nil {
226226
ctx.Errorf("Failed to get steps variables: %v", err)
227227
return nil, nil, err

pkg/runner/test_runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func (s *TestRunnerSuite) TestRandomizedMultiStep() {
471471
}
472472

473473
func (s *TestRunnerSuite) TestVariables() {
474-
ctx, cancel := xcontext.WithCancel(logrusctx.NewContext(logger.LevelDebug))
474+
ctx, cancel := logrusctx.NewContext(logger.LevelDebug)
475475
defer cancel()
476476

477477
var (

pkg/test/step.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12+
"regexp"
13+
"strconv"
14+
1215
"github.com/linuxboot/contest/pkg/event"
1316
"github.com/linuxboot/contest/pkg/event/testevent"
1417
"github.com/linuxboot/contest/pkg/target"
1518
"github.com/linuxboot/contest/pkg/xcontext"
16-
"strconv"
17-
"unicode"
1819
)
1920

2021
// TestStepParameters represents the parameters that a TestStep should consume
@@ -109,6 +110,14 @@ type TestStepChannels struct {
109110
}
110111

111112
// StepsVariables represents a read/write access for step variables
113+
// Example:
114+
// var sv StepsVariables
115+
// intVar := 42
116+
// sv.Add("dummy-target-id", "varname", intVar)
117+
//
118+
// var recvIntVar int
119+
// sv.Get(("dummy-target-id", "varname", &recvIntVar)
120+
// assert recvIntVar == 42
112121
type StepsVariables interface {
113122
// Add adds a new or replaces existing variable associated with current test step and target
114123
Add(tgtID string, name string, in interface{}) error
@@ -131,21 +140,24 @@ type TestStep interface {
131140
ValidateParameters(ctx xcontext.Context, params TestStepParameters) error
132141
}
133142

134-
// CheckVariableName checks that input argument forms a valid variable name
135-
func CheckVariableName(s string) error {
143+
var identifierRegexPattern *regexp.Regexp
144+
145+
func init() {
146+
var err error
147+
identifierRegexPattern, err = regexp.Compile(`[a-zA-Z]\w*`)
148+
if err != nil {
149+
panic(fmt.Sprintf("invalid identifier matching regexp expression: %v", err))
150+
}
151+
}
152+
153+
// CheckIdentifier checks that input argument forms a valid identifier name
154+
func CheckIdentifier(s string) error {
136155
if len(s) == 0 {
137-
return fmt.Errorf("empty variable name")
156+
return fmt.Errorf("empty identifier")
138157
}
139-
for idx, ch := range s {
140-
if idx == 0 {
141-
if !unicode.IsLetter(ch) {
142-
return fmt.Errorf("first character should be alpha, got %c", ch)
143-
}
144-
}
145-
if unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '_' {
146-
continue
147-
}
148-
return fmt.Errorf("got unxpected character: %c", ch)
158+
match := identifierRegexPattern.FindString(s)
159+
if match != s {
160+
return fmt.Errorf("identifier should be an non-empty string that matches: %s", identifierRegexPattern.String())
149161
}
150162
return nil
151163
}

pkg/test/step_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func TestTestStepParametersUnmarshalNested(t *testing.T) {
4949
}
5050

5151
func TestCheckVariableName(t *testing.T) {
52-
require.NoError(t, CheckVariableName("Abc123_XYZ"))
53-
require.Error(t, CheckVariableName(""))
54-
require.Error(t, CheckVariableName("1AAA"))
55-
require.Error(t, CheckVariableName("a b"))
56-
require.Error(t, CheckVariableName("a()+b"))
52+
require.NoError(t, CheckIdentifier("Abc123_XYZ"))
53+
require.Error(t, CheckIdentifier(""))
54+
require.Error(t, CheckIdentifier("1AAA"))
55+
require.Error(t, CheckIdentifier("a b"))
56+
require.Error(t, CheckIdentifier("a()+b"))
5757
}

0 commit comments

Comments
 (0)