go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/recipe_wrapper/manifest/manifest_test.go (about)

     1  // Copyright 2019 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package manifest
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestResolveRecipesProject(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	tests := []struct {
    16  		projectsXML      string
    17  		expectedRemote   string
    18  		expectedRevision string
    19  		expectErr        bool
    20  	}{
    21  		{
    22  			projectsXML: `
    23  			<manifest>
    24  				<projects>
    25  					<project remote="https://fuchsia.googlesource.com/infra/recipes"
    26  					 path="a/b/c"
    27  					 revision="foo"/>
    28  				</projects>
    29  			</manifest>
    30  			`,
    31  			expectedRemote:   "https://fuchsia.googlesource.com/infra/recipes",
    32  			expectedRevision: "foo",
    33  		},
    34  		{
    35  			projectsXML: `
    36  			<manifest>
    37  				<projects>
    38  					<project remote="https://fuchsia.googlesource.com/dunno"
    39  					 path="a/b/c"
    40  					 revision="foo"/>
    41  					<project remote="https://fuchsia.googlesource.com/infra/recipes"
    42  					 path="a/b/c"
    43  					 revision="bar"/>
    44  					<project remote="https://fuchsia.googlesource.com/other"
    45  					 path="a/b/c"
    46  					 revision="baz"/>
    47  				</projects>
    48  			</manifest>
    49  			`,
    50  			expectedRemote:   "https://fuchsia.googlesource.com/infra/recipes",
    51  			expectedRevision: "bar",
    52  		},
    53  		{
    54  			projectsXML: `
    55  			<manifest>
    56  				<projects>
    57  					<project remote="https://fuchsia.googlesource.com/dunno"
    58  					 path="a/b/c"
    59  					 revision="foo"/>
    60  				</projects>
    61  			</manifest>
    62  			`,
    63  			expectErr: true,
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		proj, err := ResolveRecipesProject(strings.NewReader(test.projectsXML), "https://fuchsia.googlesource.com/infra/recipes")
    69  		if err == nil {
    70  			if test.expectErr {
    71  				t.Fatalf("expected error, got nil")
    72  			}
    73  			if proj.Remote != test.expectedRemote {
    74  				t.Fatalf("project %q does not match expected %q", proj.Remote, test.expectedRemote)
    75  			}
    76  			if proj.Revision != test.expectedRevision {
    77  				t.Fatalf("revision %q does not match expected %q", proj.Revision, test.expectedRevision)
    78  			}
    79  		} else if !test.expectErr {
    80  			t.Fatalf("got unexpected error: %s", err)
    81  		}
    82  	}
    83  }