| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "strings" |
| "testing" |
| ) |
| |
| func TestResolveRecipesPackage(t *testing.T) { |
| t.Parallel() |
| |
| var tests = []struct { |
| prebuiltsXML string |
| expectedName string |
| expectedVersion string |
| expectedErr bool |
| }{ |
| { |
| ` |
| <manifest> |
| <packages> |
| <package name="fuchsia/infra/recipe_bundles/fuchsia.googlesource.com/infra/recipes" |
| path="foo/bar" |
| version="git_revision:foo" |
| attributes="recipes"/> |
| </packages> |
| </manifest> |
| `, |
| "fuchsia/infra/recipe_bundles/fuchsia.googlesource.com/infra/recipes", |
| "git_revision:foo", |
| false, |
| }, |
| { |
| ` |
| <manifest> |
| <packages> |
| <package name="nonsense" |
| path="foo/nonsense" |
| version="git_revision:nonsense"/> |
| <package name="fuchsia/infra/recipe_bundles/fuchsia.googlesource.com/infra/recipes" |
| path="foo/bar" |
| version="git_revision:foo" |
| attributes="recipes"/> |
| <package name="other" |
| path="foo/other" |
| version="version:other_version"/> |
| </packages> |
| </manifest> |
| `, |
| "fuchsia/infra/recipe_bundles/fuchsia.googlesource.com/infra/recipes", |
| "git_revision:foo", |
| false, |
| }, |
| { |
| ` |
| <manifest> |
| <packages> |
| <package name="nonsense" |
| path="foo/bar" |
| version="git_revision:foo"/> |
| </packages> |
| </manifest> |
| `, |
| "", |
| "", |
| true, |
| }, |
| } |
| |
| for _, test := range tests { |
| pkg, err := resolveRecipesPackage(strings.NewReader(test.prebuiltsXML)) |
| if err == nil { |
| if test.expectedErr { |
| t.Fatalf("expected error, got nil") |
| } |
| if pkg.Name != test.expectedName { |
| t.Fatalf("package %q does not match expected %q", pkg.Name, test.expectedName) |
| } |
| if pkg.Version != test.expectedVersion { |
| t.Fatalf("version %q does not match expected %q", pkg.Version, test.expectedVersion) |
| } |
| } else if !test.expectedErr { |
| t.Fatalf("got unexpected error %v", err) |
| } |
| } |
| } |