github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/plugin/install/install_test.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package install
    19  
    20  import (
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"fmt"
    25  
    26  	"github.com/getgauge/gauge/util"
    27  	"github.com/getgauge/gauge/version"
    28  	. "gopkg.in/check.v1"
    29  )
    30  
    31  func Test(t *testing.T) { TestingT(t) }
    32  
    33  type MySuite struct{}
    34  
    35  var _ = Suite(&MySuite{})
    36  
    37  func (s *MySuite) TestFindVersion(c *C) {
    38  	installDescription := createInstallDescriptionWithVersions("0.0.4", "0.6.7", "0.7.4", "3.6.5")
    39  	versionInstall, err := installDescription.getVersion("0.7.4")
    40  	c.Assert(err, Equals, nil)
    41  	c.Assert(versionInstall.Version, Equals, "0.7.4")
    42  }
    43  
    44  func (s *MySuite) TestFindVersionFailing(c *C) {
    45  	installDescription := createInstallDescriptionWithVersions("0.0.4", "0.6.7", "0.7.4", "3.6.5")
    46  	_, err := installDescription.getVersion("0.9.4")
    47  	c.Assert(err, NotNil)
    48  }
    49  
    50  func (s *MySuite) TestSortingVersionInstallDescriptionsInDecreasingVersionOrder(c *C) {
    51  	installDescription := createInstallDescriptionWithVersions("5.8.8", "1.7.8", "4.8.9", "0.7.6", "3.5.6")
    52  	installDescription.sortVersionInstallDescriptions()
    53  	c.Assert(installDescription.Versions[0].Version, Equals, "5.8.8")
    54  	c.Assert(installDescription.Versions[1].Version, Equals, "4.8.9")
    55  	c.Assert(installDescription.Versions[2].Version, Equals, "3.5.6")
    56  	c.Assert(installDescription.Versions[3].Version, Equals, "1.7.8")
    57  	c.Assert(installDescription.Versions[4].Version, Equals, "0.7.6")
    58  }
    59  
    60  func (s *MySuite) TestFindingLatestCompatibleVersionSuccess(c *C) {
    61  	installDescription := createInstallDescriptionWithVersions("5.8.8", "1.7.8", "4.8.9", "0.7.6")
    62  	addVersionSupportToInstallDescription(installDescription,
    63  		&version.VersionSupport{"0.0.2", "0.8.7"},
    64  		&version.VersionSupport{"1.2.4", "1.2.6"},
    65  		&version.VersionSupport{"0.9.8", "1.2.1"},
    66  		&version.VersionSupport{Minimum: "0.7.7"})
    67  	versionInstallDesc, err := installDescription.getLatestCompatibleVersionTo(&version.Version{1, 0, 0})
    68  	c.Assert(err, Equals, nil)
    69  	c.Assert(versionInstallDesc.Version, Equals, "4.8.9")
    70  }
    71  
    72  func (s *MySuite) TestFindingLatestCompatibleVersionFailing(c *C) {
    73  	installDescription := createInstallDescriptionWithVersions("2.8.8", "0.7.8", "4.8.9", "1.7.6")
    74  	addVersionSupportToInstallDescription(installDescription,
    75  		&version.VersionSupport{"0.0.2", "0.8.7"},
    76  		&version.VersionSupport{"1.2.4", "1.2.6"},
    77  		&version.VersionSupport{"0.9.8", "1.0.0"},
    78  		&version.VersionSupport{Minimum: "1.7.7"})
    79  	_, err := installDescription.getLatestCompatibleVersionTo(&version.Version{1, 1, 0})
    80  	c.Assert(err, NotNil)
    81  }
    82  
    83  func createInstallDescriptionWithVersions(versionNumbers ...string) *installDescription {
    84  	var versionInstallDescriptions []versionInstallDescription
    85  	for _, version := range versionNumbers {
    86  		versionInstallDescriptions = append(versionInstallDescriptions, versionInstallDescription{Version: version})
    87  	}
    88  	return &installDescription{Name: "my-plugin", Versions: versionInstallDescriptions}
    89  }
    90  
    91  func addVersionSupportToInstallDescription(installDescription *installDescription, versionSupportList ...*version.VersionSupport) {
    92  	for i := range installDescription.Versions {
    93  		installDescription.Versions[i].GaugeVersionSupport = *versionSupportList[i]
    94  	}
    95  }
    96  
    97  func (s *MySuite) TestInstallGaugePluginFromNonExistingZipFile(c *C) {
    98  	result := InstallPluginFromZipFile(filepath.Join("test_resources", "notPresent.zip"), "ruby")
    99  	c.Assert(result.Error.Error(), Equals, fmt.Sprintf("ZipFile %s does not exist", filepath.Join("test_resources", "notPresent.zip")))
   100  }
   101  
   102  func (s *MySuite) TestGetVersionedPluginDirName(c *C) {
   103  	name := getVersionedPluginDirName("abcd/foo/bar/html-report-2.0.1.nightly-2016-02-09-darwin.x86.zip")
   104  	c.Assert(name, Equals, "2.0.1.nightly-2016-02-09")
   105  
   106  	name = getVersionedPluginDirName("abcd/foo/bar/xml-report-2.0.1.nightly-2016-02-09-darwin.x86.zip")
   107  	c.Assert(name, Equals, "2.0.1.nightly-2016-02-09")
   108  
   109  	name = getVersionedPluginDirName("abcd/foo/bar/html-report-0.3.4-windows.x86_64.zip")
   110  	c.Assert(name, Equals, "0.3.4")
   111  
   112  	name = getVersionedPluginDirName("abcd/foo/bar/gauge-java-0.3.4.nightly-2016-02-09-linux.x86.zip")
   113  	c.Assert(name, Equals, "0.3.4.nightly-2016-02-09")
   114  
   115  	name = getVersionedPluginDirName("abcd/foo/bar/gauge-java-0.3.4-linux.x86_64.zip")
   116  	c.Assert(name, Equals, "0.3.4")
   117  
   118  	name = getVersionedPluginDirName("abcd/foo/gauge-ruby-0.1.2.nightly-2016-02-09-linux.x86.zip")
   119  	c.Assert(name, Equals, "0.1.2.nightly-2016-02-09")
   120  
   121  	if util.IsWindows() {
   122  		name = getVersionedPluginDirName("C:\\Users\\apoorvam\\AppData\\Local\\Temp\\gauge_temp1456130044460213700\\gauge-java-0.3.4-windows.x86_64.zip")
   123  		c.Assert(name, Equals, "0.3.4")
   124  	}
   125  }
   126  
   127  func (s *MySuite) TestGetGaugePluginForJava(c *C) {
   128  	path, _ := filepath.Abs(filepath.Join("_testdata", "java"))
   129  	p, err := parsePluginJSON(path, "java")
   130  	c.Assert(err, Equals, nil)
   131  	c.Assert(p.ID, Equals, "java")
   132  	c.Assert(p.Version, Equals, "0.3.4")
   133  	c.Assert(p.Description, Equals, "Java support for gauge")
   134  	c.Assert(p.PreInstall.Darwin[0], Equals, "pre install command")
   135  	c.Assert(p.PreUnInstall.Darwin[0], Equals, "pre uninstall command")
   136  	c.Assert(p.GaugeVersionSupport.Minimum, Equals, "0.3.0")
   137  	c.Assert(p.GaugeVersionSupport.Maximum, Equals, "")
   138  }
   139  
   140  func (s *MySuite) TestGetGaugePluginForReportPlugin(c *C) {
   141  	path, _ := filepath.Abs("_testdata")
   142  	p, err := parsePluginJSON(path, "html-report")
   143  	c.Assert(err, Equals, nil)
   144  	c.Assert(p.ID, Equals, "html-report")
   145  	c.Assert(p.Version, Equals, "2.0.1")
   146  	c.Assert(p.Description, Equals, "Html reporting plugin")
   147  	c.Assert(p.PreInstall.Darwin[0], Equals, "pre install command")
   148  	c.Assert(p.PreUnInstall.Darwin[0], Equals, "pre uninstall command")
   149  	c.Assert(p.GaugeVersionSupport.Minimum, Equals, "0.3.0")
   150  	c.Assert(p.GaugeVersionSupport.Maximum, Equals, "")
   151  }