github.com/getgauge/gauge@v1.6.9/template/template_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 template
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/getgauge/common"
    17  	"github.com/getgauge/gauge/config"
    18  )
    19  
    20  var templatesContent = `
    21  # Template for gauge dotnet projects
    22  dotnet = https://github.com/getgauge/template-dotnet/releases/latest/download/dotnet.zip
    23  
    24  # Template for gauge java projects
    25  java = https://github.com/getgauge/template-java/releases/latest/download/java.zip
    26  
    27  # Template for gauge java_gradle projects
    28  java_gradle = https://github.com/getgauge/template-java-gradle/releases/latest/download/java_gradle.zip
    29  
    30  # Template for gauge java_maven projects
    31  java_maven = https://github.com/getgauge/template-java-maven/releases/latest/download/java_maven.zip
    32  
    33  # Template for gauge java_maven_selenium projects
    34  java_maven_selenium = https://github.com/getgauge/template-java-maven-selenium/releases/latest/download/java_maven_selenium.zip
    35  
    36  # Template for gauge js projects
    37  js = https://github.com/getgauge/template-js/releases/latest/download/js.zip
    38  
    39  # Template for gauge js_simple projects
    40  js_simple = https://github.com/getgauge/template-js-simple/releases/latest/download/js_simple.zip
    41  
    42  # Template for gauge python projects
    43  python = https://github.com/getgauge/template-python/releases/latest/download/python.zip
    44  
    45  # Template for gauge python_selenium projects
    46  python_selenium = https://github.com/getgauge/template-python-selenium/releases/latest/download/python_selenium.zip
    47  
    48  # Template for gauge ruby projects
    49  ruby = https://github.com/getgauge/template-ruby/releases/latest/download/ruby.zip
    50  
    51  # Template for gauge ruby_selenium projects
    52  ruby_selenium = https://github.com/getgauge/template-ruby-selenium/releases/latest/download/ruby_selenium.zip
    53  
    54  # Template for gauge ts projects
    55  ts = https://github.com/getgauge/template-ts/releases/latest/download/ts.zip
    56  `
    57  
    58  func TestMain(m *testing.M) {
    59  	oldHome := os.Getenv("GAUGE_HOME")
    60  	var assertNil = func(err error) {
    61  		if err != nil {
    62  			fmt.Println("Failed to setup test data")
    63  			os.Exit(1)
    64  		}
    65  	}
    66  	home, err := filepath.Abs("_testdata")
    67  	assertNil(err)
    68  	assertNil(os.Setenv("GAUGE_HOME", "_testdata"))
    69  	assertNil(os.MkdirAll(filepath.Join(home, "config"), common.NewDirectoryPermissions))
    70  	assertNil(Generate())
    71  	e := m.Run()
    72  	assertNil(os.Setenv("GAUGE_HOME", oldHome))
    73  	assertNil(os.RemoveAll(home))
    74  	os.Exit(e)
    75  }
    76  
    77  func TestUpdateShouldAddTemplateIfDoesNotExistss(t *testing.T) {
    78  	temp := &templates{
    79  		t: map[string]*config.Property{},
    80  	}
    81  	e := temp.update("hello", "https://templates.org/foo.zip", false)
    82  	if e != nil {
    83  		t.Errorf("expected error to be nil. got '%s'", e.Error())
    84  	}
    85  }
    86  
    87  func TestUpdateShouldNotAddInValidURL(t *testing.T) {
    88  	temp := &templates{
    89  		t: map[string]*config.Property{},
    90  	}
    91  	e := temp.update("hello", "foo/bar", true)
    92  	if e == nil {
    93  		t.Errorf("expected error to not be nil. got '%s'", e.Error())
    94  	}
    95  	expected := "Failed to add template 'hello'. The template location must be a valid (https) URI"
    96  	if e.Error() != expected {
    97  		t.Errorf("expected error to be '%s'. got '%s'", expected, e.Error())
    98  	}
    99  }
   100  
   101  func TestGetShouldGetTemplateIfDoesNotExists(t *testing.T) {
   102  	temp := &templates{
   103  		t: map[string]*config.Property{},
   104  	}
   105  	_, e := temp.get("hello")
   106  	if e == nil {
   107  		t.Errorf("expected error. got nil")
   108  	}
   109  	expected := "Cannot find Gauge template 'hello'"
   110  	if e.Error() != expected {
   111  		t.Errorf("expected error to be \n'%s'\nGot:\n'%s'", expected, e.Error())
   112  	}
   113  }
   114  
   115  func TestGetShouldGetSimilarTemplatesIfDoesNotExists(t *testing.T) {
   116  	temp := defaults()
   117  	_, e := temp.get("jaba")
   118  	if e == nil {
   119  		t.Errorf("expected error. got nil")
   120  	}
   121  	expected := `Cannot find Gauge template 'jaba'.
   122  The most similar template names are
   123  
   124  	java
   125  	java_gradle
   126  	java_maven
   127  	java_maven_selenium`
   128  
   129  	if e.Error() != expected {
   130  		t.Errorf("expected error to be \n'%s'\nGot:\n'%s'", expected, e.Error())
   131  	}
   132  }
   133  
   134  func TestGetShouldGetTemplateIfExists(t *testing.T) {
   135  	temp := &templates{
   136  		t: map[string]*config.Property{
   137  			"hello": config.NewProperty("hello", "/foo/bar", ""),
   138  		},
   139  	}
   140  
   141  	v, e := temp.get("hello")
   142  	if e != nil {
   143  		t.Errorf("expected error to be nil. got '%s'", e.Error())
   144  	}
   145  
   146  	if v != "/foo/bar" {
   147  		t.Errorf("Expected: '/foo/bar'\nGot: '%s'", v)
   148  	}
   149  }
   150  
   151  func TestTemplateAll(t *testing.T) {
   152  	want := defaults().names
   153  	s, err := All()
   154  	if err != nil {
   155  		t.Error(err)
   156  	}
   157  	got := strings.Split(s, "\n")
   158  
   159  	for i, x := range want {
   160  		if got[i] != x {
   161  			fmt.Printf("'%s'\n", got[i])
   162  			fmt.Printf("'%s'\n", x)
   163  			t.Errorf("Expected property no %d = %s, got %s", i, x, got[i])
   164  		}
   165  	}
   166  }
   167  
   168  func TestTemplateString(t *testing.T) {
   169  	want := strings.Split(templatesContent, "\n\n")
   170  
   171  	s, err := defaults().String()
   172  	if err != nil {
   173  		t.Error(err)
   174  	}
   175  	got := strings.Split(s, "\n\n")
   176  
   177  	if len(got) != len(want) {
   178  		t.Errorf("Expected %d properties, got %d", len(want), len(got))
   179  	}
   180  
   181  	for i, x := range want {
   182  		if got[i] != x {
   183  			fmt.Printf("'%s'\n", got[i])
   184  			fmt.Printf("'%s'\n", x)
   185  			t.Errorf("Expected property no %d = %s, got %s", i, x, got[i])
   186  		}
   187  	}
   188  }
   189  
   190  func TestTemplateList(t *testing.T) {
   191  	want := []string{
   192  		"Template Name                 \tLocation                           ",
   193  		"----------------------------------------------------------------------------------------------------------------------------------------",
   194  		"dotnet                        \thttps://github.com/getgauge/template-dotnet/releases/latest/download/dotnet.zip",
   195  		"java                          \thttps://github.com/getgauge/template-java/releases/latest/download/java.zip",
   196  		"java_gradle                   \thttps://github.com/getgauge/template-java-gradle/releases/latest/download/java_gradle.zip",
   197  		"java_maven                    \thttps://github.com/getgauge/template-java-maven/releases/latest/download/java_maven.zip",
   198  		"java_maven_selenium           \thttps://github.com/getgauge/template-java-maven-selenium/releases/latest/download/java_maven_selenium.zip",
   199  		"js                            \thttps://github.com/getgauge/template-js/releases/latest/download/js.zip",
   200  		"js_simple                     \thttps://github.com/getgauge/template-js-simple/releases/latest/download/js_simple.zip",
   201  		"python                        \thttps://github.com/getgauge/template-python/releases/latest/download/python.zip",
   202  		"python_selenium               \thttps://github.com/getgauge/template-python-selenium/releases/latest/download/python_selenium.zip",
   203  		"ruby                          \thttps://github.com/getgauge/template-ruby/releases/latest/download/ruby.zip",
   204  		"ruby_selenium                 \thttps://github.com/getgauge/template-ruby-selenium/releases/latest/download/ruby_selenium.zip",
   205  		"ts                            \thttps://github.com/getgauge/template-ts/releases/latest/download/ts.zip",
   206  	}
   207  	s, err := List(false)
   208  	if err != nil {
   209  		t.Error(err)
   210  	}
   211  	got := strings.Split(s, "\n")
   212  	if len(got) != len(want) {
   213  		t.Errorf("Expected %d entries, got %d", len(want), len(got))
   214  	}
   215  	for i, x := range want {
   216  		if got[i] != x {
   217  			t.Errorf("Properties text Format failed\nwant:`%s`\ngot: `%s`", x, got[i])
   218  		}
   219  	}
   220  }
   221  
   222  func TestTemplateListAfterUpdate(t *testing.T) {
   223  
   224  	want := []string{
   225  		"Template Name                 \tLocation                           ",
   226  		"----------------------------------------------------------------------------------------------------------------------------------------",
   227  		"dotnet                        \thttps://github.com/getgauge/template-dotnet/releases/latest/download/dotnet.zip",
   228  		"foo                           \thttps://github.com/getgauge/template-foo/releases/latest/download/foo.zip",
   229  		"java                          \thttps://github.com/getgauge/template-java/releases/latest/download/java.zip",
   230  		"java_gradle                   \thttps://github.com/getgauge/template-java-gradle/releases/latest/download/java_gradle.zip",
   231  		"java_maven                    \thttps://github.com/getgauge/template-java-maven/releases/latest/download/java_maven.zip",
   232  		"java_maven_selenium           \thttps://github.com/getgauge/template-java-maven-selenium/releases/latest/download/java_maven_selenium.zip",
   233  		"js                            \thttps://github.com/getgauge/template-js/releases/latest/download/js.zip",
   234  		"js_simple                     \thttps://github.com/getgauge/template-js-simple/releases/latest/download/js_simple.zip",
   235  		"python                        \thttps://github.com/getgauge/template-python/releases/latest/download/python.zip",
   236  		"python_selenium               \thttps://github.com/getgauge/template-python-selenium/releases/latest/download/python_selenium.zip",
   237  		"ruby                          \thttps://github.com/getgauge/template-ruby/releases/latest/download/ruby.zip",
   238  		"ruby_selenium                 \thttps://github.com/getgauge/template-ruby-selenium/releases/latest/download/ruby_selenium.zip",
   239  		"ts                            \thttps://github.com/getgauge/template-ts/releases/latest/download/ts.zip",
   240  	}
   241  	err := Update("foo", "https://github.com/getgauge/template-foo/releases/latest/download/foo.zip")
   242  	if err != nil {
   243  		t.Error(err)
   244  	}
   245  	s, err := List(false)
   246  	got := strings.Split(s, "\n")
   247  	if err != nil {
   248  		t.Error(err)
   249  	}
   250  	if len(got) != len(want) {
   251  		t.Errorf("Expected %d entries, got %d", len(want), len(got))
   252  	}
   253  	for i, x := range want {
   254  		if got[i] != x {
   255  			t.Errorf("Properties text Format failed\nwant:`%s`\ngot: `%s`", x, got[i])
   256  		}
   257  	}
   258  }