Skip to content

Commit

Permalink
fix: adds test filter to the serve to generate code coverage (keploy#…
Browse files Browse the repository at this point in the history
…1612)

* fix: adds test filter to the serve to generate code coverage

Signed-off-by: re-Tick <[email protected]>

* style: formats the resolver.go

Signed-off-by: re-Tick <[email protected]>

---------

Signed-off-by: re-Tick <[email protected]>
  • Loading branch information
re-Tick authored Feb 23, 2024
1 parent 6e18c49 commit 1e6183b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
16 changes: 8 additions & 8 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ReadTestConfig(configPath string) (*models.Test, error) {
return &doc.Test, nil
}

func (t *Test) getTestConfig(path *string, proxyPort *uint32, appCmd *string, tests *map[string][]string, appContainer, networkName *string, Delay *uint64, buildDelay *time.Duration, passThroughPorts *[]uint, apiTimeout *uint64, globalNoise *models.GlobalNoise, testSetNoise *models.TestsetNoise, coverageReportPath *string, withCoverage *bool, generateTestReport *bool, configPath string, ignoreOrdering *bool, passThroughHosts *[]models.Filters) error {
func (t *Test) getTestConfig(path *string, proxyPort *uint32, appCmd *string, testFilters *map[string][]string, appContainer, networkName *string, Delay *uint64, buildDelay *time.Duration, passThroughPorts *[]uint, apiTimeout *uint64, globalNoise *models.GlobalNoise, testSetNoise *models.TestsetNoise, coverageReportPath *string, withCoverage *bool, generateTestReport *bool, configPath string, ignoreOrdering *bool, passThroughHosts *[]models.Filters) error {
configFilePath := filepath.Join(configPath, "keploy-config.yaml")
if isExist := utils.CheckFileExists(configFilePath); !isExist {
return errFileNotFound
Expand All @@ -61,8 +61,8 @@ func (t *Test) getTestConfig(path *string, proxyPort *uint32, appCmd *string, te
*appCmd = confTest.Command
}
for testset, testcases := range confTest.SelectedTests {
if _, ok := (*tests)[testset]; !ok {
(*tests)[testset] = testcases
if _, ok := (*testFilters)[testset]; !ok {
(*testFilters)[testset] = testcases
}
}
if *appContainer == "" {
Expand Down Expand Up @@ -246,7 +246,7 @@ func (t *Test) GetCmd() *cobra.Command {
return err
}

tests := map[string][]string{}
testFilters := map[string][]string{}

testsets, err := cmd.Flags().GetStringSlice("testsets")
if err != nil {
Expand All @@ -255,14 +255,14 @@ func (t *Test) GetCmd() *cobra.Command {
}

for _, testset := range testsets {
tests[testset] = []string{}
testFilters[testset] = []string{}
}

globalNoise := make(models.GlobalNoise)
testsetNoise := make(models.TestsetNoise)

passThroughHosts := []models.Filters{}
err = t.getTestConfig(&path, &proxyPort, &appCmd, &tests, &appContainer, &networkName, &delay, &buildDelay, &ports, &apiTimeout, &globalNoise, &testsetNoise, &coverageReportPath, &withCoverage, &generateTestReport, configPath, &ignoreOrdering, &passThroughHosts)
err = t.getTestConfig(&path, &proxyPort, &appCmd, &testFilters, &appContainer, &networkName, &delay, &buildDelay, &ports, &apiTimeout, &globalNoise, &testsetNoise, &coverageReportPath, &withCoverage, &generateTestReport, configPath, &ignoreOrdering, &passThroughHosts)
if err != nil {
if err == errFileNotFound {
t.logger.Info("Keploy config not found, continuing without configuration")
Expand Down Expand Up @@ -433,11 +433,11 @@ func (t *Test) GetCmd() *cobra.Command {
t.logger.Debug("the configuration for mocking mongo connection", zap.Any("password", mongoPassword))
if coverage {
g := graph.NewGraph(t.logger)
g.Serve(path, proxyPort, mongoPassword, testReportPath, generateTestReport, delay, pid, port, lang, ports, apiTimeout, appCmd, enableTele)
g.Serve(path, proxyPort, mongoPassword, testReportPath, generateTestReport, delay, pid, port, lang, ports, apiTimeout, appCmd, enableTele, testFilters)
} else {

t.tester.StartTest(path, testReportPath, appCmd, test.TestOptions{
Tests: tests,
Tests: testFilters,
AppContainer: appContainer,
AppNetwork: networkName,
MongoPassword: mongoPassword,
Expand Down
1 change: 1 addition & 0 deletions pkg/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var Emoji = "\U0001F430" + " Keploy:"

type Resolver struct {
Tester test.Tester
TestFilter map[string][]string
TestReportFS platform.TestReportDB
Storage platform.TestCaseDB
LoadedHooks *hooks.Hook
Expand Down
18 changes: 16 additions & 2 deletions pkg/graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/graph/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewGraph(logger *zap.Logger) graphInterface {
const defaultPort = 6789

// Serve is called by the serve command and is used to run a graphql server, to run tests separately via apis.
func (g *graph) Serve(path string, proxyPort uint32, mongopassword, testReportPath string, generateTestReport bool, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string, enableTele bool) {
func (g *graph) Serve(path string, proxyPort uint32, mongopassword, testReportPath string, generateTestReport bool, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string, enableTele bool, testFilters map[string][]string) {
var ps *proxy.ProxySet

defer pkg.DeleteTestReports(g.logger, generateTestReport)
Expand Down Expand Up @@ -73,6 +73,7 @@ func (g *graph) Serve(path string, proxyPort uint32, mongopassword, testReportPa

srv := handler.NewDefaultServer(NewExecutableSchema(Config{
Resolvers: &Resolver{
TestFilter: testFilters,
Tester: tester,
TestReportFS: testReportFS,
Storage: ys,
Expand Down
2 changes: 1 addition & 1 deletion pkg/graph/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

type graphInterface interface {
Serve(path string, proxyPort uint32, mongoPassword, testReportPath string, generateTestReport bool, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string, enableTele bool)
Serve(path string, proxyPort uint32, mongoPassword, testReportPath string, generateTestReport bool, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string, enableTele bool, testFilters map[string][]string)
stopGraphqlServer(http *http.Server)
}
8 changes: 0 additions & 8 deletions pkg/service/test/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ func UnmarshallJson(s string, log *zap.Logger) (interface{}, error) {
}
}

func ArrayToMap(arr []string) map[string]bool {
res := map[string]bool{}
for i := range arr {
res[arr[i]] = true
}
return res
}

func InterfaceToString(val interface{}) string {
switch v := val.(type) {
case int:
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (t *tester) Test(path string, testReportPath string, appCmd string, options
}
for _, sessionIndex := range sessions {
// checking whether the provided testset match with a recorded testset.
testcases := ArrayToMap(options.Tests[sessionIndex])
testcases := utils.ArrayToMap(options.Tests[sessionIndex])
if _, ok := options.Tests[sessionIndex]; !ok && len(options.Tests) != 0 {
continue
}
Expand Down
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,12 @@ func UpdateKeployToDocker(cmdName string, isDockerCompose bool, flags interface{
}

var WarningSign = "\U000026A0"

// ArrayToMap converts an array of strings to a map[string]bool.
func ArrayToMap(arr []string) map[string]bool {
res := map[string]bool{}
for i := range arr {
res[arr[i]] = true
}
return res
}

0 comments on commit 1e6183b

Please sign in to comment.