github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/plugin/plugin_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 plugin 19 20 import ( 21 "fmt" 22 "path/filepath" 23 "testing" 24 25 "github.com/getgauge/gauge/config" 26 "github.com/getgauge/gauge/version" 27 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) TestSortingOfPluginInfos(c *C) { 38 plugins := make(map[string]PluginInfo) 39 plugins["e"] = PluginInfo{Name: "e"} 40 plugins["b"] = PluginInfo{Name: "b"} 41 plugins["c"] = PluginInfo{Name: "c"} 42 plugins["d"] = PluginInfo{Name: "d"} 43 plugins["a"] = PluginInfo{Name: "a"} 44 45 actual := sortPlugins(plugins) 46 47 var expected []PluginInfo 48 expected = append(expected, PluginInfo{Name: "a"}) 49 expected = append(expected, PluginInfo{Name: "b"}) 50 expected = append(expected, PluginInfo{Name: "c"}) 51 expected = append(expected, PluginInfo{Name: "d"}) 52 expected = append(expected, PluginInfo{Name: "e"}) 53 54 c.Assert(len(expected), Equals, len(plugins)) 55 for i := range expected { 56 c.Assert(expected[i], Equals, actual[i]) 57 } 58 } 59 60 func (s *MySuite) TestGetLatestPluginPath(c *C) { 61 path, _ := filepath.Abs(filepath.Join("_testdata", "java")) 62 63 latestVersion, err := getLatestInstalledPlugin(path) 64 65 c.Assert(err, Equals, nil) 66 c.Assert(latestVersion.Version.String(), Equals, "1.2.0") 67 c.Assert(latestVersion.Name, Equals, "java") 68 c.Assert(latestVersion.Path, Equals, filepath.Join(path, "1.2.0")) 69 } 70 71 func (s *MySuite) TestGetLatestPluginPathIfNoPluginsFound(c *C) { 72 testData := "_testdata" 73 path, _ := filepath.Abs(testData) 74 75 _, err := getLatestInstalledPlugin(path) 76 77 c.Assert(err.Error(), Equals, fmt.Sprintf("No valid versions of plugin %s found in %s", testData, path)) 78 } 79 80 func (s *MySuite) TestGetLatestInstalledPlugin(c *C) { 81 path, _ := filepath.Abs(filepath.Join("_testdata", "java")) 82 83 latestPlugin, err := getLatestInstalledPlugin(path) 84 85 c.Assert(err, Equals, nil) 86 c.Assert(latestPlugin.Path, Equals, filepath.Join(path, "1.2.0")) 87 } 88 89 func (s *MySuite) TestGetLatestInstalledPluginIfNoPluginsFound(c *C) { 90 testData := "_testdata" 91 path, _ := filepath.Abs(testData) 92 93 _, err := getLatestInstalledPlugin(path) 94 95 c.Assert(err.Error(), Equals, fmt.Sprintf("No valid versions of plugin %s found in %s", testData, path)) 96 } 97 98 func (s *MySuite) TestGetPluginDescriptorFromJSON(c *C) { 99 testData := "_testdata" 100 path, _ := filepath.Abs(testData) 101 102 pd, err := GetPluginDescriptorFromJSON(filepath.Join(path, "_test.json")) 103 104 c.Assert(err, Equals, nil) 105 c.Assert(pd.ID, Equals, "html-report") 106 c.Assert(pd.Version, Equals, "1.1.0") 107 c.Assert(pd.Name, Equals, "Html Report") 108 c.Assert(pd.Description, Equals, "Html reporting plugin") 109 c.Assert(pd.pluginPath, Equals, path) 110 c.Assert(pd.GaugeVersionSupport.Minimum, Equals, "0.2.0") 111 c.Assert(pd.GaugeVersionSupport.Maximum, Equals, "0.4.0") 112 c.Assert(pd.Scope, DeepEquals, []string{"Execution"}) 113 htmlCommand := []string{"bin/html-report"} 114 c.Assert(pd.Command.Windows, DeepEquals, htmlCommand) 115 c.Assert(pd.Command.Darwin, DeepEquals, htmlCommand) 116 c.Assert(pd.Command.Linux, DeepEquals, htmlCommand) 117 } 118 119 func (s *MySuite) TestGetPluginDescriptorFromNonExistingJSON(c *C) { 120 testData := "_testdata" 121 path, _ := filepath.Abs(testData) 122 JSONPath := filepath.Join(path, "_test1.json") 123 _, err := GetPluginDescriptorFromJSON(JSONPath) 124 125 c.Assert(err, DeepEquals, fmt.Errorf("File %s doesn't exist.", JSONPath)) 126 } 127 128 func (s *MySuite) TestGetStablePluginAmongGivenPluginsOfAVersion(c *C) { 129 v, _ := version.ParseVersion("0.2.2") 130 131 pluginInfo1 := PluginInfo{Version: v, Path: "0.2.2"} 132 plugins := []PluginInfo{pluginInfo1} 133 latestBuild := getLatestOf(plugins, v) 134 c.Assert(latestBuild.Version, Equals, v) 135 136 pluginInfo2 := PluginInfo{Version: v, Path: "0.2.2.nightly-2016-02-09"} 137 plugins = []PluginInfo{pluginInfo2} 138 latestBuild = getLatestOf(plugins, v) 139 c.Assert(latestBuild.Path, Equals, pluginInfo2.Path) 140 c.Assert(latestBuild.Version, Equals, v) 141 142 pluginInfo1.Path = "0.2.2.nightly-2015-02-03" 143 pluginInfo2.Path = "0.2.2.nightly-2016-02-09" 144 pluginInfo3 := PluginInfo{Version: v, Path: "0.2.2.nightly-2017-02-09"} 145 plugins = []PluginInfo{pluginInfo1, pluginInfo3, pluginInfo2} 146 latestBuild = getLatestOf(plugins, v) 147 c.Assert(latestBuild.Path, Equals, pluginInfo3.Path) 148 c.Assert(latestBuild.Version, Equals, v) 149 150 pluginInfo1.Path = "0.2.2.nightly-2015-02-03" 151 pluginInfo2.Path = "0.2.2.nightly-2016-02-04" 152 plugins = []PluginInfo{pluginInfo1, pluginInfo2} 153 latestBuild = getLatestOf(plugins, v) 154 c.Assert(latestBuild.Path, Equals, pluginInfo2.Path) 155 c.Assert(latestBuild.Version, Equals, v) 156 157 pluginInfo1.Path = "0.2.2.nightly-2015-01-03" 158 pluginInfo2.Path = "0.2.2.nightly-2015-02-03" 159 plugins = []PluginInfo{pluginInfo1, pluginInfo2} 160 latestBuild = getLatestOf(plugins, v) 161 c.Assert(latestBuild.Path, Equals, pluginInfo2.Path) 162 163 pluginInfo1.Path = "0.2.2.nightly-2015-01-03" 164 pluginInfo2.Path = "0.2.2.nightly-2016-02-03" 165 plugins = []PluginInfo{pluginInfo1, pluginInfo2} 166 latestBuild = getLatestOf(plugins, v) 167 c.Assert(latestBuild.Path, Equals, pluginInfo2.Path) 168 169 pluginInfo1.Path = "0.2.2.nightly-2015-01-03" 170 pluginInfo2.Path = "0.2.2.nightly-2017-02-03" 171 pluginInfo2.Path = "0.2.2.nightly-2016-02-03" 172 plugins = []PluginInfo{pluginInfo1, pluginInfo2} 173 latestBuild = getLatestOf(plugins, v) 174 c.Assert(latestBuild.Path, Equals, pluginInfo2.Path) 175 176 pluginInfo1.Path = "0.2.2.nightly-2017-01-03" 177 pluginInfo2.Path = "0.2.2.nightly-2017-01-05" 178 pluginInfo2.Path = "0.2.2.nightly-2017-01-04" 179 plugins = []PluginInfo{pluginInfo1, pluginInfo2} 180 latestBuild = getLatestOf(plugins, v) 181 c.Assert(latestBuild.Path, Equals, pluginInfo2.Path) 182 } 183 184 func (s *MySuite) TestGetLanguageQueryParamWhenProjectRootNotSet(c *C) { 185 config.ProjectRoot = "" 186 187 l := language() 188 189 c.Assert(l, Equals, "") 190 } 191 192 func (s *MySuite) TestGetLanguageQueryParam(c *C) { 193 path, _ := filepath.Abs(filepath.Join("_testdata", "sample")) 194 config.ProjectRoot = path 195 196 l := language() 197 198 c.Assert(l, Equals, "java") 199 }