Skip to content

Commit

Permalink
Move ApplyPresets
Browse files Browse the repository at this point in the history
  • Loading branch information
5nord committed May 10, 2023
1 parent 55d0dd0 commit 304cd0d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 64 deletions.
8 changes: 0 additions & 8 deletions project/helpers_test.go

This file was deleted.

55 changes: 0 additions & 55 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/nokia/ntt/internal/yaml"
"github.com/nokia/ntt/project/internal/k3"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/syntax"
)

var (
Expand Down Expand Up @@ -478,60 +477,6 @@ func Files(c *Config) ([]string, error) {
return fs.TTCN3Files(files...)
}

// ApplyPresets returns a list of test case configurations with optional
// presets applied. The presets are applied in the order they are specified in
// the list.
//
// ApplyPresets reads test case configuration from environment variables, the
// parameters file, package.yml and from the TTCN-3 documentation tags.
func ApplyPresets(c *Config, presets ...string) (*Parameters, error) {
// Global configuration
gc := c.Parameters

// Presets override/extend parameters files
for _, preset := range presets {
tc, ok := gc.Presets[preset]
if !ok {
return nil, fmt.Errorf("preset %q not found", preset)
}
gc.TestConfig = MergeTestConfig(gc.TestConfig, tc)
}

files, err := fs.TTCN3Files(c.Sources...)
if err != nil {
return nil, err
}

list := acquireExecutables(&gc, files, presets)

gc.Execute = list
return &gc, nil
}

// acquireExecutables depending on the provided presets and on the availability
// inside the ttcn-3 code, a list of executable ttcn-3 entities (i.e. testcases,
// control parts) is returned
func acquireExecutables(gc *Parameters, files []string, presets []string) []TestConfig {
var list []TestConfig
for _, file := range files {
tree := ttcn3.ParseFile(file)
tree.Inspect(func(n syntax.Node) bool {
switch n := n.(type) {
case *syntax.FuncDecl:
if n.IsTest() || n.Modif != nil && n.Modif.String() == "@control" {
list = append(list, gc.Glob(tree.QualifiedName(n), presets...)...)
}
return false
case *syntax.ControlPart:
list = append(list, gc.Glob(tree.QualifiedName(n), presets...)...)
return false
}
return true
})
}
return list
}

// Glob matches a test case name against the list of test case configurations
// and presets and returns a list of matching test case configurations.
func (p *Parameters) Glob(name string, presets ...string) []TestConfig {
Expand Down
59 changes: 58 additions & 1 deletion show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"text/template"

"github.com/nokia/ntt/internal/env"
"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/internal/yaml"
"github.com/nokia/ntt/project"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/syntax"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -61,7 +64,7 @@ func printJSON(report *ConfigReport, keys []string) error {
presets = strings.Split(s, string(os.PathListSeparator))
}

params, err := project.ApplyPresets(report.Config, presets...)
params, err := ApplyPresets(report.Config, presets...)
if err != nil {
return err
}
Expand All @@ -75,6 +78,60 @@ func printJSON(report *ConfigReport, keys []string) error {
return report.err
}

// ApplyPresets returns a list of test case configurations with optional
// presets applied. The presets are applied in the order they are specified in
// the list.
//
// ApplyPresets reads test case configuration from environment variables, the
// parameters file, package.yml and from the TTCN-3 documentation tags.
func ApplyPresets(c *project.Config, presets ...string) (*project.Parameters, error) {
// Global configuration
gc := c.Parameters

// Presets override/extend parameters files
for _, preset := range presets {
tc, ok := gc.Presets[preset]
if !ok {
return nil, fmt.Errorf("preset %q not found", preset)
}
gc.TestConfig = project.MergeTestConfig(gc.TestConfig, tc)
}

files, err := fs.TTCN3Files(c.Sources...)
if err != nil {
return nil, err
}

list := acquireExecutables(&gc, files, presets)

gc.Execute = list
return &gc, nil
}

// acquireExecutables depending on the provided presets and on the availability
// inside the ttcn-3 code, a list of executable ttcn-3 entities (i.e. testcases,
// control parts) is returned
func acquireExecutables(gc *project.Parameters, files []string, presets []string) []project.TestConfig {
var list []project.TestConfig
for _, file := range files {
tree := ttcn3.ParseFile(file)
tree.Inspect(func(n syntax.Node) bool {
switch n := n.(type) {
case *syntax.FuncDecl:
if n.IsTest() || n.Modif != nil && n.Modif.String() == "@control" {
list = append(list, gc.Glob(tree.QualifiedName(n), presets...)...)
}
return false
case *syntax.ControlPart:
list = append(list, gc.Glob(tree.QualifiedName(n), presets...)...)
return false
}
return true
})
}
return list
}

func printShellScript(report *ConfigReport, keys []string) error {
const shellTemplate = `# This is a generated output of ntt show. Args: {{ .Args }}
Expand Down

0 comments on commit 304cd0d

Please sign in to comment.