github.com/getgauge/gauge@v1.6.9/config/properties_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 config
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"reflect"
    13  	"sort"
    14  	"strings"
    15  	"sync"
    16  	"testing"
    17  
    18  	"github.com/getgauge/common"
    19  	"github.com/getgauge/gauge/version"
    20  )
    21  
    22  type dummyFormatter struct {
    23  	p []string
    24  }
    25  
    26  func (f *dummyFormatter) Format(p []Property) (string, error) {
    27  	for _, prop := range p {
    28  		f.p = append(f.p, prop.Value)
    29  	}
    30  	return "", nil
    31  }
    32  
    33  func TestPropertiesSetValue(t *testing.T) {
    34  	p := defaults()
    35  	want := "https://gauge.trepo.url"
    36  
    37  	err := p.set(gaugeRepositoryURL, want)
    38  	if err != nil {
    39  		t.Error(err)
    40  	}
    41  
    42  	got, err := p.get(gaugeRepositoryURL)
    43  	if err != nil {
    44  		t.Errorf("Expected error == nil when setting property, got %s", err.Error())
    45  	}
    46  	if got != want {
    47  		t.Errorf("Setting Property `%s` failed, want: `%s`, got `%s`", gaugeRepositoryURL, want, got)
    48  	}
    49  }
    50  
    51  func TestPropertiesFormat(t *testing.T) {
    52  	p := defaults()
    53  	var want []string
    54  	for _, p := range p.p {
    55  		want = append(want, (*p).Value)
    56  	}
    57  	sort.Strings(want)
    58  	f := &dummyFormatter{}
    59  
    60  	_, err := p.Format(f)
    61  	if err != nil {
    62  		t.Error(err)
    63  	}
    64  	sort.Strings(f.p)
    65  	if !reflect.DeepEqual(f.p, want) {
    66  		t.Errorf("Properties Format failed, want: `%s`, got `%s`", want, f.p)
    67  	}
    68  }
    69  
    70  func TestMergedProperties(t *testing.T) {
    71  	want := "false"
    72  	idFile := filepath.Join("_testData", "config", "gauge.properties")
    73  	err := os.WriteFile(idFile, []byte("check_updates=false"), common.NewFilePermissions)
    74  	if err != nil {
    75  		t.Error(err)
    76  	}
    77  
    78  	s, err := filepath.Abs("_testData")
    79  	if err != nil {
    80  		t.Error(err)
    81  	}
    82  	os.Setenv("GAUGE_HOME", s)
    83  
    84  	p, err := mergedProperties()
    85  	if err != nil {
    86  		t.Errorf("Unable to read MergedProperties: %s", err.Error())
    87  	}
    88  
    89  	got, err := p.get(checkUpdates)
    90  
    91  	if err != nil {
    92  		t.Errorf("Expected error == nil when getting property after merge, got %s", err.Error())
    93  	}
    94  	if got != want {
    95  		t.Errorf("Properties Merge failed, want: %s == `%s`, got `%s`", checkUpdates, want, got)
    96  	}
    97  	os.Setenv("GAUGE_HOME", "")
    98  	err = os.Remove(idFile)
    99  	if err != nil {
   100  		t.Error(err)
   101  	}
   102  }
   103  
   104  var propertiesContent = "# Version " + version.CurrentGaugeVersion.String() + `
   105  # This file contains Gauge specific internal configurations. Do not delete
   106  
   107  # Allow Gauge to download template from insecure URLs.
   108  allow_insecure_download = false
   109  
   110  # Allow Gauge and its plugin updates to be notified.
   111  check_updates = true
   112  
   113  # Url to get plugin versions
   114  gauge_repository_url = https://downloads.gauge.org/plugin
   115  
   116  # Timeout in milliseconds for requests from runner when invoked for ide.
   117  ide_request_timeout = 30000
   118  
   119  # Timeout in milliseconds for making a connection to plugins.
   120  plugin_connection_timeout = 10000
   121  
   122  # Timeout in milliseconds for a plugin to stop after a kill message has been sent.
   123  plugin_kill_timeout = 4000
   124  
   125  # Timeout in milliseconds for making a connection to the language runner.
   126  runner_connection_timeout = 30000
   127  
   128  # Timeout in milliseconds for requests from the language runner.
   129  runner_request_timeout = 30000
   130  `
   131  
   132  func TestPropertiesString(t *testing.T) {
   133  	want := strings.Split(propertiesContent, "\n\n")
   134  
   135  	s, err := defaults().String()
   136  	if err != nil {
   137  		t.Error(err)
   138  	}
   139  	got := strings.Split(s, "\n\n")
   140  
   141  	if len(got) != len(want) {
   142  		t.Errorf("Expected %d properties, got %d", len(want), len(got))
   143  	}
   144  
   145  	for i, x := range want {
   146  		if got[i] != x {
   147  			t.Errorf("Expected property no %d = %s, got %s", i, x, got[i])
   148  		}
   149  	}
   150  }
   151  
   152  func TestPropertiesStringConcurrent(t *testing.T) {
   153  	want := strings.Split(propertiesContent, "\n\n")
   154  
   155  	writeFunc := func(wg *sync.WaitGroup) {
   156  		err := Merge()
   157  		if err != nil {
   158  			t.Log(err)
   159  		}
   160  		wg.Done()
   161  	}
   162  
   163  	wg := &sync.WaitGroup{}
   164  
   165  	for i := 0; i < 1000; i++ {
   166  		wg.Add(1)
   167  		go writeFunc(wg)
   168  	}
   169  
   170  	wg.Wait()
   171  
   172  	s, err := defaults().String()
   173  	if err != nil {
   174  		t.Error(err)
   175  	}
   176  	got := strings.Split(s, "\n\n")
   177  
   178  	if len(got) != len(want) {
   179  		t.Errorf("Expected %d properties, got %d", len(want), len(got))
   180  	}
   181  
   182  	for i, x := range want {
   183  		if got[i] != x {
   184  			t.Errorf("Expected property no %d = %s, got %s", i, x, got[i])
   185  		}
   186  	}
   187  }
   188  
   189  func TestWriteGaugePropertiesOnlyForNewVersion(t *testing.T) {
   190  	oldEnv := os.Getenv("GAUGE_HOME")
   191  	os.Setenv("GAUGE_HOME", filepath.Join(".", "_testData"))
   192  	propFile := filepath.Join("_testData", "config", "gauge.properties")
   193  	err := os.WriteFile(propFile, []byte("# Version 0.8.0"), common.NewFilePermissions)
   194  	if err != nil {
   195  		t.Error(err)
   196  	}
   197  
   198  	err = Merge()
   199  	if err != nil {
   200  		t.Error(err)
   201  	}
   202  
   203  	want := version.FullVersion()
   204  	got, err := GaugeVersionInPropertiesFile(common.GaugePropertiesFile)
   205  	if err != nil {
   206  		t.Error(err)
   207  	}
   208  	if got.String() != want {
   209  		t.Errorf("Expected Gauge Version in gauge.properties %s, got %s", want, got)
   210  	}
   211  	os.Setenv("GAUGE_HOME", oldEnv)
   212  	os.Remove(propFile)
   213  }