github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/project_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 6 "github.com/evergreen-ci/evergreen" 7 "github.com/evergreen-ci/evergreen/model/version" 8 "github.com/evergreen-ci/evergreen/testutil" 9 "github.com/evergreen-ci/evergreen/util" 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 func TestFindProject(t *testing.T) { 14 15 Convey("When finding a project", t, func() { 16 17 Convey("an error should be thrown if the project ref is nil", func() { 18 project, err := FindProject("", nil) 19 So(err, ShouldNotBeNil) 20 So(project, ShouldBeNil) 21 }) 22 23 Convey("an error should be thrown if the project ref's identifier is nil", func() { 24 projRef := &ProjectRef{ 25 Identifier: "", 26 } 27 project, err := FindProject("", projRef) 28 So(err, ShouldNotBeNil) 29 So(project, ShouldBeNil) 30 }) 31 32 Convey("if the project file exists and is valid, the project spec within"+ 33 "should be unmarshalled and returned", func() { 34 v := &version.Version{ 35 Owner: "fakeowner", 36 Repo: "fakerepo", 37 Branch: "fakebranch", 38 Identifier: "project_test", 39 Requester: evergreen.RepotrackerVersionRequester, 40 Config: "owner: fakeowner\nrepo: fakerepo\nbranch: fakebranch", 41 } 42 p := &ProjectRef{ 43 Identifier: "project_test", 44 Owner: "fakeowner", 45 Repo: "fakerepo", 46 Branch: "fakebranch", 47 } 48 testutil.HandleTestingErr(v.Insert(), t, "failed to insert test version: %v", v) 49 _, err := FindProject("", p) 50 So(err, ShouldBeNil) 51 52 }) 53 54 }) 55 56 } 57 58 func TestGetVariantMappings(t *testing.T) { 59 60 Convey("With a project", t, func() { 61 62 Convey("getting variant mappings should return a map of the build"+ 63 " variant names to their display names", func() { 64 65 project := &Project{ 66 BuildVariants: []BuildVariant{ 67 { 68 Name: "bv1", 69 DisplayName: "bv1", 70 }, 71 { 72 Name: "bv2", 73 DisplayName: "dsp2", 74 }, 75 { 76 Name: "blecch", 77 DisplayName: "blecchdisplay", 78 }, 79 }, 80 } 81 82 mappings := project.GetVariantMappings() 83 So(len(mappings), ShouldEqual, 3) 84 So(mappings["bv1"], ShouldEqual, "bv1") 85 So(mappings["bv2"], ShouldEqual, "dsp2") 86 So(mappings["blecch"], ShouldEqual, "blecchdisplay") 87 88 }) 89 90 }) 91 92 } 93 94 func TestGetVariantsWithTask(t *testing.T) { 95 96 Convey("With a project", t, func() { 97 98 project := &Project{ 99 BuildVariants: []BuildVariant{ 100 { 101 Name: "bv1", 102 Tasks: []BuildVariantTask{{Name: "suite1"}}, 103 }, 104 { 105 Name: "bv2", 106 Tasks: []BuildVariantTask{ 107 {Name: "suite1"}, 108 {Name: "suite2"}, 109 }, 110 }, 111 { 112 Name: "bv3", 113 Tasks: []BuildVariantTask{{Name: "suite2"}}, 114 }, 115 }, 116 } 117 118 Convey("when getting the build variants where a task applies", func() { 119 120 Convey("it should be run on any build variants where the test is"+ 121 " specified to run", func() { 122 123 variants := project.GetVariantsWithTask("suite1") 124 So(len(variants), ShouldEqual, 2) 125 So(util.SliceContains(variants, "bv1"), ShouldBeTrue) 126 So(util.SliceContains(variants, "bv2"), ShouldBeTrue) 127 128 variants = project.GetVariantsWithTask("suite2") 129 So(len(variants), ShouldEqual, 2) 130 So(util.SliceContains(variants, "bv2"), ShouldBeTrue) 131 So(util.SliceContains(variants, "bv3"), ShouldBeTrue) 132 133 }) 134 135 }) 136 137 }) 138 } 139 140 func TestGetModuleRepoName(t *testing.T) { 141 142 Convey("With a module", t, func() { 143 144 Convey("getting the repo owner and name should return the repo"+ 145 " field, split at the ':' and removing the .git from"+ 146 " the end", func() { 147 148 module := &Module{ 149 Repo: "blecch:owner/repo.git", 150 } 151 152 owner, name := module.GetRepoOwnerAndName() 153 So(owner, ShouldEqual, "owner") 154 So(name, ShouldEqual, "repo") 155 156 }) 157 158 }) 159 } 160 161 func TestPopulateBVT(t *testing.T) { 162 163 Convey("With a test Project and BuildVariantTask", t, func() { 164 165 project := &Project{ 166 Tasks: []ProjectTask{ 167 { 168 Name: "task1", 169 ExecTimeoutSecs: 500, 170 Stepback: new(bool), 171 DependsOn: []TaskDependency{{Name: "other"}}, 172 Priority: 1000, 173 Patchable: new(bool), 174 }, 175 }, 176 BuildVariants: []BuildVariant{ 177 { 178 Name: "test", 179 Tasks: []BuildVariantTask{{Name: "task1", Priority: 5}}, 180 }, 181 }, 182 } 183 184 Convey("updating a BuildVariantTask with unset fields", func() { 185 bvt := project.BuildVariants[0].Tasks[0] 186 spec := project.GetSpecForTask("task1") 187 So(spec.Name, ShouldEqual, "task1") 188 bvt.Populate(spec) 189 190 Convey("should inherit the unset fields from the Project", func() { 191 So(bvt.Name, ShouldEqual, "task1") 192 So(bvt.ExecTimeoutSecs, ShouldEqual, 500) 193 So(bvt.Stepback, ShouldNotBeNil) 194 So(bvt.Patchable, ShouldNotBeNil) 195 So(len(bvt.DependsOn), ShouldEqual, 1) 196 197 Convey("but not set fields", func() { So(bvt.Priority, ShouldEqual, 5) }) 198 }) 199 }) 200 201 Convey("updating a BuildVariantTask with set fields", func() { 202 bvt := BuildVariantTask{ 203 Name: "task1", 204 ExecTimeoutSecs: 2, 205 Stepback: boolPtr(true), 206 DependsOn: []TaskDependency{{Name: "task2"}, {Name: "task3"}}, 207 } 208 spec := project.GetSpecForTask("task1") 209 So(spec.Name, ShouldEqual, "task1") 210 bvt.Populate(spec) 211 212 Convey("should not inherit set fields from the Project", func() { 213 So(bvt.Name, ShouldEqual, "task1") 214 So(bvt.ExecTimeoutSecs, ShouldEqual, 2) 215 So(bvt.Stepback, ShouldNotBeNil) 216 So(*bvt.Stepback, ShouldBeTrue) 217 So(len(bvt.DependsOn), ShouldEqual, 2) 218 219 Convey("but unset fields should", func() { So(bvt.Priority, ShouldEqual, 1000) }) 220 }) 221 }) 222 }) 223 } 224 225 func TestIgnoresAllFiles(t *testing.T) { 226 Convey("With test Project.Ignore setups and a list of.py, .yml, and .md files", t, func() { 227 files := []string{ 228 "src/cool/test.py", 229 "etc/other_config.yml", 230 "README.md", 231 } 232 Convey("a project with an empty ignore field should never ignore files", func() { 233 p := &Project{Ignore: []string{}} 234 So(p.IgnoresAllFiles(files), ShouldBeFalse) 235 }) 236 Convey("a project with a * ignore field should always ignore files", func() { 237 p := &Project{Ignore: []string{"*"}} 238 So(p.IgnoresAllFiles(files), ShouldBeTrue) 239 }) 240 Convey("a project that ignores .py files should not ignore all files", func() { 241 p := &Project{Ignore: []string{"*.py"}} 242 So(p.IgnoresAllFiles(files), ShouldBeFalse) 243 }) 244 Convey("a project that ignores .py, .yml, and .md files should ignore all files", func() { 245 p := &Project{Ignore: []string{"*.py", "*.yml", "*.md"}} 246 So(p.IgnoresAllFiles(files), ShouldBeTrue) 247 }) 248 Convey("a project that ignores all files by name should ignore all files", func() { 249 p := &Project{Ignore: []string{ 250 "src/cool/test.py", 251 "etc/other_config.yml", 252 "README.md", 253 }} 254 So(p.IgnoresAllFiles(files), ShouldBeTrue) 255 }) 256 Convey("a project that ignores all files by dir should ignore all files", func() { 257 p := &Project{Ignore: []string{"src/*", "etc/*", "README.md"}} 258 So(p.IgnoresAllFiles(files), ShouldBeTrue) 259 }) 260 Convey("a project with negations should not ignore all files", func() { 261 p := &Project{Ignore: []string{"*", "!src/cool/*"}} 262 So(p.IgnoresAllFiles(files), ShouldBeFalse) 263 }) 264 Convey("a project with a negated filetype should not ignore all files", func() { 265 p := &Project{Ignore: []string{"src/*", "!*.py", "*yml", "*.md"}} 266 So(p.IgnoresAllFiles(files), ShouldBeFalse) 267 }) 268 }) 269 } 270 271 func boolPtr(b bool) *bool { 272 return &b 273 }