github.com/getgauge/gauge@v1.6.9/plugin/plugin_test.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package plugin
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"reflect"
    15  	"testing"
    16  
    17  	gm "github.com/getgauge/gauge-proto/go/gauge_messages"
    18  
    19  	"github.com/getgauge/common"
    20  	"github.com/getgauge/gauge/config"
    21  	"github.com/getgauge/gauge/plugin/pluginInfo"
    22  	"github.com/getgauge/gauge/version"
    23  	"google.golang.org/grpc"
    24  )
    25  
    26  type mockResultClient struct {
    27  	invoked bool
    28  }
    29  
    30  func (client *mockResultClient) NotifySuiteResult(c context.Context, r *gm.SuiteExecutionResult, opts ...grpc.CallOption) (*gm.Empty, error) {
    31  	client.invoked = true
    32  	return nil, nil
    33  }
    34  
    35  func (client *mockResultClient) NotifyExecutionStarting(c context.Context, r *gm.ExecutionStartingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    36  	return nil, nil
    37  }
    38  
    39  func (client *mockResultClient) NotifyExecutionEnding(c context.Context, r *gm.ExecutionEndingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    40  	return nil, nil
    41  }
    42  
    43  func (client *mockResultClient) NotifySpecExecutionStarting(c context.Context, r *gm.SpecExecutionStartingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    44  	return nil, nil
    45  }
    46  
    47  func (client *mockResultClient) NotifySpecExecutionEnding(c context.Context, r *gm.SpecExecutionEndingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    48  	return nil, nil
    49  }
    50  
    51  func (client *mockResultClient) NotifyScenarioExecutionStarting(c context.Context, r *gm.ScenarioExecutionStartingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    52  	return nil, nil
    53  }
    54  
    55  func (client *mockResultClient) NotifyScenarioExecutionEnding(c context.Context, r *gm.ScenarioExecutionEndingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    56  	return nil, nil
    57  }
    58  
    59  func (client *mockResultClient) NotifyStepExecutionStarting(c context.Context, r *gm.StepExecutionStartingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    60  	return nil, nil
    61  }
    62  
    63  func (client *mockResultClient) NotifyStepExecutionEnding(c context.Context, r *gm.StepExecutionEndingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    64  	return nil, nil
    65  }
    66  
    67  func (client *mockResultClient) NotifyConceptExecutionStarting(c context.Context, r *gm.ConceptExecutionStartingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    68  	return nil, nil
    69  }
    70  
    71  func (client *mockResultClient) NotifyConceptExecutionEnding(c context.Context, r *gm.ConceptExecutionEndingRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    72  	return nil, nil
    73  }
    74  
    75  func (client *mockResultClient) Kill(c context.Context, r *gm.KillProcessRequest, opts ...grpc.CallOption) (*gm.Empty, error) {
    76  	return nil, nil
    77  }
    78  
    79  func TestGetPluginDescriptorFromJSON(t *testing.T) {
    80  	testData := "_testdata"
    81  	path, _ := filepath.Abs(testData)
    82  
    83  	pd, err := GetPluginDescriptorFromJSON(filepath.Join(path, "_test.json"))
    84  
    85  	if err != nil {
    86  		t.Errorf("error: %s", err.Error())
    87  	}
    88  	t.Run("ID", func(t *testing.T) {
    89  		if pd.ID != "html-report" {
    90  			t.Errorf("expected %s, got %s", "html-report", pd.ID)
    91  		}
    92  	})
    93  	t.Run("Version", func(t *testing.T) {
    94  		if pd.Version != "1.1.0" {
    95  			t.Errorf("expected %s, got %s", "1.1.0", pd.Version)
    96  		}
    97  	})
    98  	t.Run("Name", func(t *testing.T) {
    99  		if pd.Name != "Html Report" {
   100  			t.Errorf("expected %s, got %s", "Html Report", pd.Name)
   101  		}
   102  	})
   103  	t.Run("Description", func(t *testing.T) {
   104  		if pd.Description != "Html reporting plugin" {
   105  			t.Errorf("expected %s, got %s", "Html reporting plugin", pd.Name)
   106  		}
   107  	})
   108  	t.Run("Path", func(t *testing.T) {
   109  		if pd.pluginPath != path {
   110  			t.Errorf("expected %s, got %s", path, pd.pluginPath)
   111  		}
   112  	})
   113  	t.Run("Min Version", func(t *testing.T) {
   114  		if pd.GaugeVersionSupport.Minimum != "0.2.0" {
   115  			t.Errorf("expected %s, got %s", "0.2.0", pd.GaugeVersionSupport.Minimum)
   116  		}
   117  	})
   118  	t.Run("Max Version", func(t *testing.T) {
   119  		if pd.GaugeVersionSupport.Maximum != "0.4.0" {
   120  			t.Errorf("expected %s, got %s", "0.4.0", pd.GaugeVersionSupport.Maximum)
   121  		}
   122  	})
   123  	t.Run("Scope", func(t *testing.T) {
   124  		if !reflect.DeepEqual(pd.Scope, []string{"Execution"}) {
   125  			t.Errorf("expected %s, got %s", []string{"Execution"}, pd.Scope)
   126  		}
   127  	})
   128  	htmlCommand := []string{"bin/html-report"}
   129  	t.Run("Windows Command", func(t *testing.T) {
   130  		if !reflect.DeepEqual(pd.Command.Windows, htmlCommand) {
   131  			t.Errorf("expected %s, got %s", htmlCommand, pd.Command.Windows)
   132  		}
   133  	})
   134  	t.Run("Darwin Command", func(t *testing.T) {
   135  		if !reflect.DeepEqual(pd.Command.Darwin, htmlCommand) {
   136  			t.Errorf("expected %s, got %s", htmlCommand, pd.Command.Darwin)
   137  		}
   138  	})
   139  	t.Run("Linux Command", func(t *testing.T) {
   140  		if !reflect.DeepEqual(pd.Command.Linux, htmlCommand) {
   141  			t.Errorf("expected %s, got %s", htmlCommand, pd.Command.Linux)
   142  		}
   143  	})
   144  }
   145  
   146  func TestGetPluginDescriptorFromNonExistingJSON(t *testing.T) {
   147  	testData := "_testdata"
   148  	path, _ := filepath.Abs(testData)
   149  	JSONPath := filepath.Join(path, "_test1.json")
   150  	_, err := GetPluginDescriptorFromJSON(JSONPath)
   151  
   152  	expected := fmt.Errorf("File %s doesn't exist.", JSONPath)
   153  	if err.Error() != expected.Error() {
   154  		t.Errorf("expected %s, got %s", expected, err)
   155  	}
   156  }
   157  
   158  func TestGetLanguageQueryParamWhenProjectRootNotSet(t *testing.T) {
   159  	config.ProjectRoot = ""
   160  
   161  	l := language()
   162  
   163  	if l != "" {
   164  		t.Errorf("expected empty language, got %s", l)
   165  	}
   166  }
   167  
   168  func TestGetLanguageQueryParam(t *testing.T) {
   169  	path, _ := filepath.Abs(filepath.Join("_testdata", "sample"))
   170  	config.ProjectRoot = path
   171  
   172  	l := language()
   173  
   174  	if l != "java" {
   175  		t.Errorf("expected java, got %s", l)
   176  	}
   177  }
   178  
   179  func TestGetPluginsWithoutScope(t *testing.T) {
   180  	path, _ := filepath.Abs(filepath.Join("_testdata"))
   181  	os.Setenv(common.GaugeHome, path)
   182  
   183  	got := PluginsWithoutScope()
   184  
   185  	want := []pluginInfo.PluginInfo{
   186  		{
   187  			Name:    "noscope",
   188  			Version: &version.Version{Major: 1, Minor: 0, Patch: 0},
   189  			Path:    filepath.Join(path, "plugins", "noscope", "1.0.0"),
   190  		},
   191  	}
   192  	if !reflect.DeepEqual(got, want) {
   193  		t.Errorf("Failed GetPluginWithoutScope.\n\tWant: %v\n\tGot: %v", want, got)
   194  	}
   195  }
   196  
   197  func TestSendMessageShouldUseGRPCConnectionIfAvailable(t *testing.T) {
   198  	c := &mockResultClient{}
   199  	p := &plugin{
   200  		gRPCConn:       &grpc.ClientConn{},
   201  		ReporterClient: c,
   202  	}
   203  
   204  	e := p.sendMessage(&gm.Message{MessageType: gm.Message_SuiteExecutionResult, SuiteExecutionResult: &gm.SuiteExecutionResult{}})
   205  
   206  	if e != nil {
   207  		t.Errorf("Expected error to be nil. Got : %v", e)
   208  	}
   209  
   210  	if !c.invoked {
   211  		t.Errorf("Expected grpc client to be invoked")
   212  	}
   213  }