github.com/getgauge/gauge@v1.6.9/plugin/install/install_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 install
     8  
     9  import (
    10  	"fmt"
    11  	"path/filepath"
    12  	"runtime"
    13  	"testing"
    14  
    15  	"github.com/getgauge/gauge/util"
    16  	"github.com/getgauge/gauge/version"
    17  	. "gopkg.in/check.v1"
    18  )
    19  
    20  func Test(t *testing.T) { TestingT(t) }
    21  
    22  type MySuite struct{}
    23  
    24  var _ = Suite(&MySuite{})
    25  
    26  func (s *MySuite) TestFindVersion(c *C) {
    27  	installDescription := createInstallDescriptionWithVersions("0.0.4", "0.6.7", "0.7.4", "3.6.5")
    28  	versionInstall, err := installDescription.getVersion("0.7.4")
    29  	c.Assert(err, Equals, nil)
    30  	c.Assert(versionInstall.Version, Equals, "0.7.4")
    31  }
    32  
    33  func (s *MySuite) TestFindVersionFailing(c *C) {
    34  	installDescription := createInstallDescriptionWithVersions("0.0.4", "0.6.7", "0.7.4", "3.6.5")
    35  	_, err := installDescription.getVersion("0.9.4")
    36  	c.Assert(err, NotNil)
    37  }
    38  
    39  func (s *MySuite) TestSortingVersionInstallDescriptionsInDecreasingVersionOrder(c *C) {
    40  	installDescription := createInstallDescriptionWithVersions("5.8.8", "1.7.8", "4.8.9", "0.7.6", "3.5.6")
    41  	installDescription.sortVersionInstallDescriptions()
    42  	c.Assert(installDescription.Versions[0].Version, Equals, "5.8.8")
    43  	c.Assert(installDescription.Versions[1].Version, Equals, "4.8.9")
    44  	c.Assert(installDescription.Versions[2].Version, Equals, "3.5.6")
    45  	c.Assert(installDescription.Versions[3].Version, Equals, "1.7.8")
    46  	c.Assert(installDescription.Versions[4].Version, Equals, "0.7.6")
    47  }
    48  
    49  func (s *MySuite) TestFindingLatestCompatibleVersionSuccess(c *C) {
    50  	installDescription := createInstallDescriptionWithVersions("5.8.8", "1.7.8", "4.8.9", "0.7.6")
    51  	addVersionSupportToInstallDescription(installDescription,
    52  		&version.VersionSupport{Minimum: "0.0.2", Maximum: "0.8.7"},
    53  		&version.VersionSupport{Minimum: "1.2.4", Maximum: "1.2.6"},
    54  		&version.VersionSupport{Minimum: "0.9.8", Maximum: "1.2.1"},
    55  		&version.VersionSupport{Minimum: "0.7.7"})
    56  	versionInstallDesc, err := installDescription.getLatestCompatibleVersionTo(&version.Version{Major: 1, Minor: 0, Patch: 0})
    57  	c.Assert(err, Equals, nil)
    58  	c.Assert(versionInstallDesc.Version, Equals, "4.8.9")
    59  }
    60  
    61  func (s *MySuite) TestFindingLatestCompatibleVersionFailing(c *C) {
    62  	installDescription := createInstallDescriptionWithVersions("2.8.8", "0.7.8", "4.8.9", "1.7.6")
    63  	addVersionSupportToInstallDescription(installDescription,
    64  		&version.VersionSupport{Minimum: "0.0.2", Maximum: "0.8.7"},
    65  		&version.VersionSupport{Minimum: "1.2.4", Maximum: "1.2.6"},
    66  		&version.VersionSupport{Minimum: "0.9.8", Maximum: "1.0.0"},
    67  		&version.VersionSupport{Minimum: "1.7.7"})
    68  	_, err := installDescription.getLatestCompatibleVersionTo(&version.Version{Major: 1, Minor: 1, Patch: 0})
    69  	c.Assert(err, NotNil)
    70  }
    71  
    72  func createInstallDescriptionWithVersions(versionNumbers ...string) *installDescription {
    73  	var versionInstallDescriptions []versionInstallDescription
    74  	for _, version := range versionNumbers {
    75  		versionInstallDescriptions = append(versionInstallDescriptions, versionInstallDescription{Version: version})
    76  	}
    77  	return &installDescription{Name: "my-plugin", Versions: versionInstallDescriptions}
    78  }
    79  
    80  func addVersionSupportToInstallDescription(installDescription *installDescription, versionSupportList ...*version.VersionSupport) {
    81  	for i := range installDescription.Versions {
    82  		installDescription.Versions[i].GaugeVersionSupport = *versionSupportList[i]
    83  	}
    84  }
    85  
    86  func (s *MySuite) TestInstallPluginFromZipFile(c *C) {
    87  	result := InstallPluginFromZipFile("zip_with_multiple-dot.s.zip", "ruby")
    88  	c.Assert(result.Error.Error(), Equals, fmt.Sprintf("provided plugin is not compatible with OS %s %s", runtime.GOOS, runtime.GOARCH))
    89  }
    90  
    91  func (s *MySuite) TestGetVersionedPluginDirName(c *C) {
    92  	name := getVersionedPluginDirName("abcd/foo/bar/html-report-2.0.1.nightly-2016-02-09-darwin.x86.zip")
    93  	c.Assert(name, Equals, "2.0.1.nightly-2016-02-09")
    94  
    95  	name = getVersionedPluginDirName("abcd/foo/bar/xml-report-2.0.1.nightly-2016-02-09-darwin.x86.zip")
    96  	c.Assert(name, Equals, "2.0.1.nightly-2016-02-09")
    97  
    98  	name = getVersionedPluginDirName("abcd/foo/bar/html-report-0.3.4-windows.x86_64.zip")
    99  	c.Assert(name, Equals, "0.3.4")
   100  
   101  	name = getVersionedPluginDirName("abcd/foo/bar/gauge-java-0.3.4.nightly-2016-02-09-linux.x86.zip")
   102  	c.Assert(name, Equals, "0.3.4.nightly-2016-02-09")
   103  
   104  	name = getVersionedPluginDirName("abcd/foo/bar/gauge-java-0.3.4-linux.x86_64.zip")
   105  	c.Assert(name, Equals, "0.3.4")
   106  
   107  	name = getVersionedPluginDirName("abcd/foo/gauge-ruby-0.1.2.nightly-2016-02-09-linux.x86.zip")
   108  	c.Assert(name, Equals, "0.1.2.nightly-2016-02-09")
   109  
   110  	if util.IsWindows() {
   111  		name = getVersionedPluginDirName("C:\\Users\\apoorvam\\AppData\\Local\\Temp\\gauge_temp1456130044460213700\\gauge-java-0.3.4-windows.x86_64.zip")
   112  		c.Assert(name, Equals, "0.3.4")
   113  	}
   114  }
   115  
   116  func (s *MySuite) TestGetGaugePluginForJava(c *C) {
   117  	path, _ := filepath.Abs(filepath.Join("_testdata", "java"))
   118  	p, err := parsePluginJSON(path, "java")
   119  	c.Assert(err, Equals, nil)
   120  	c.Assert(p.ID, Equals, "java")
   121  	c.Assert(p.Version, Equals, "0.3.4")
   122  	c.Assert(p.Description, Equals, "Java support for gauge")
   123  	c.Assert(p.PreInstall.Darwin[0], Equals, "pre install command")
   124  	c.Assert(p.PreUnInstall.Darwin[0], Equals, "pre uninstall command")
   125  	c.Assert(p.GaugeVersionSupport.Minimum, Equals, "0.3.0")
   126  	c.Assert(p.GaugeVersionSupport.Maximum, Equals, "")
   127  }
   128  
   129  func (s *MySuite) TestGetGaugePluginForReportPlugin(c *C) {
   130  	path, _ := filepath.Abs("_testdata")
   131  	p, err := parsePluginJSON(path, "html-report")
   132  	c.Assert(err, Equals, nil)
   133  	c.Assert(p.ID, Equals, "html-report")
   134  	c.Assert(p.Version, Equals, "2.0.1")
   135  	c.Assert(p.Description, Equals, "Html reporting plugin")
   136  	c.Assert(p.PreInstall.Darwin[0], Equals, "pre install command")
   137  	c.Assert(p.PreUnInstall.Darwin[0], Equals, "pre uninstall command")
   138  	c.Assert(p.GaugeVersionSupport.Minimum, Equals, "0.3.0")
   139  	c.Assert(p.GaugeVersionSupport.Maximum, Equals, "")
   140  }
   141  
   142  func (s *MySuite) TestMatchesUninstallVersionIfUninstallPluginVersionIsNotGiven(c *C) {
   143  	dirPath := "somepath"
   144  	uninstallVersion := ""
   145  
   146  	c.Assert(matchesUninstallVersion(dirPath, uninstallVersion), Equals, true)
   147  }
   148  
   149  func (s *MySuite) TestMatchesUninstallVersionIfUninstallPluginVersionMatches(c *C) {
   150  	dirPath := "0.1.1-nightly-2016-05-05"
   151  	uninstallVersion := "0.1.1-nightly-2016-05-05"
   152  
   153  	c.Assert(matchesUninstallVersion(dirPath, uninstallVersion), Equals, true)
   154  }
   155  
   156  func (s *MySuite) TestMatchesUninstallVersionIfUninstallPluginVersionDoesntMatches(c *C) {
   157  	dirPath := "0.1.1"
   158  	uninstallVersion := "0.1.1-nightly-2016-05-05"
   159  
   160  	c.Assert(matchesUninstallVersion(dirPath, uninstallVersion), Equals, false)
   161  }
   162  
   163  func (s *MySuite) TestIsPlatformIndependentZipFile(c *C) {
   164  	javaReleased := "java-3.1.0.nightly-2017-02-08-darwin.x86_64.zip"
   165  	csharpReleased := "gauge-csharp-0.10.1.zip"
   166  	javaNightly := "gauge-java-3.1.0.nightly-2017-02-08-darwin.x86_64.zip"
   167  	csharpNightly := "gauge-csharp-0.10.1.nightly-2017-02-17.zip"
   168  
   169  	c.Assert(isPlatformIndependent(javaReleased), Equals, false)
   170  	c.Assert(isPlatformIndependent(csharpReleased), Equals, true)
   171  	c.Assert(isPlatformIndependent(javaNightly), Equals, false)
   172  	c.Assert(isPlatformIndependent(csharpNightly), Equals, true)
   173  }