go.fuchsia.dev/jiri@v0.0.0-20240502161911-b66513b29486/cmd/jiri/manifest_test.go (about)

     1  // Copyright 2018 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"go.fuchsia.dev/jiri/cipd"
    14  	"go.fuchsia.dev/jiri/jiritest"
    15  )
    16  
    17  func TestManifest(t *testing.T) {
    18  	// Create a test manifest file.
    19  	testManifestFile, err := os.CreateTemp("", "test_manifest")
    20  	if err != nil {
    21  		t.Fatalf("failed to create test manifest: %s", err)
    22  	}
    23  	testManifestFile.Write([]byte(`
    24  <?xml version="1.0" encoding="UTF-8"?>
    25  <manifest>
    26  	<imports>
    27  		<import name="the_import"
    28  			manifest="the_import_manifest"
    29  			remote="https://fuchsia.googlesource.com/the_import"
    30  			revision="the_import_revision"
    31  			remotebranch="the_import_remotebranch"
    32  			root="the_import_root"/>
    33  	</imports>
    34  	<projects>
    35  		<project name="the_project"
    36  			path="path/to/the_project"
    37  			remote="https://fuchsia.googlesource.com/the_project"
    38  			remotebranch="the_project_remotebranch"
    39  			revision="the_project_revision"
    40  			githooks="the_project_githooks"
    41  			gerrithost="https://fuchsia-review.googlesource.com"
    42  			historydepth="2"/>
    43  	</projects>
    44  	<packages>
    45  		<package name="the_package/${platform}"
    46  			version="the_package_version"
    47  			path="path/to/the_package"
    48  			internal="false" />
    49  	</packages>
    50  </manifest>
    51  `))
    52  
    53  	runCommand := func(t *testing.T, args []string) (stdout string, stderr string) {
    54  		// Set up a fake Jiri root to pass to our command.
    55  		fake, cleanup := jiritest.NewFakeJiriRoot(t)
    56  		defer cleanup()
    57  
    58  		// Initialize flags for the command.
    59  		flagSet := flag.NewFlagSet("manifest-test", flag.ContinueOnError)
    60  		setManifestFlags(flagSet)
    61  
    62  		// Make sure flags parse correctly.
    63  		if err := flagSet.Parse(args); err != nil {
    64  			t.Error(err)
    65  		}
    66  
    67  		// Run the command.
    68  		runCmd := func() {
    69  			if err := runManifest(fake.X, flagSet.Args()); err != nil {
    70  				// Capture the error as stderr since Jiri subcommands don't
    71  				// intenionally print to stderr when they fail.
    72  				stderr = err.Error()
    73  			}
    74  		}
    75  
    76  		var err error
    77  		stdout, _, err = runfunc(runCmd)
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  
    82  		return stdout, stderr
    83  	}
    84  
    85  	// Expects manifest to return a specific value when given args.
    86  	expectAttributeValue := func(t *testing.T, args []string, expectedValue string) {
    87  		stdout, stderr := runCommand(t, args)
    88  
    89  		// If an error occurred, fail.
    90  		if stderr != "" {
    91  			t.Error("error:", stderr)
    92  			return
    93  		}
    94  
    95  		// Compare stdout to the expected value.
    96  		if strings.Trim(stdout, " \n") != expectedValue {
    97  			t.Errorf("expected %q, got %q", expectedValue, stdout)
    98  		}
    99  	}
   100  
   101  	// Expects manifest to error when given args.
   102  	expectError := func(t *testing.T, args []string) {
   103  		stdout, stderr := runCommand(t, args)
   104  
   105  		// Fail if no error was output.
   106  		if stderr == "" {
   107  			t.Errorf("expected an error, got %s", stdout)
   108  			return
   109  		}
   110  	}
   111  
   112  	t.Run("should fail if manifest file is missing", func(t *testing.T) {
   113  		expectError(t, []string{
   114  			"-element=the_import",
   115  			"-template={{.Name}}",
   116  		})
   117  
   118  		expectError(t, []string{
   119  			"-element=the_project",
   120  			"-template={{.Name}}",
   121  		})
   122  	})
   123  
   124  	t.Run("should fail if -attribute is missing", func(t *testing.T) {
   125  		expectError(t, []string{
   126  			"-element=the_import",
   127  			testManifestFile.Name(),
   128  		})
   129  
   130  		expectError(t, []string{
   131  			"-element=the_project",
   132  			testManifestFile.Name(),
   133  		})
   134  	})
   135  
   136  	t.Run("should fail if -element is missing", func(t *testing.T) {
   137  		expectError(t, []string{
   138  			"-template={{.Name}}",
   139  			testManifestFile.Name(),
   140  		})
   141  
   142  		expectError(t, []string{
   143  			"-template={{.Name}}",
   144  			testManifestFile.Name(),
   145  		})
   146  	})
   147  
   148  	t.Run("should read <project> attributes", func(t *testing.T) {
   149  		expectAttributeValue(t, []string{
   150  			"-element=the_project",
   151  			"-template={{.Name}}",
   152  			testManifestFile.Name(),
   153  		},
   154  			"the_project")
   155  
   156  		expectAttributeValue(t, []string{
   157  			"-element=the_project",
   158  			"-template={{.Remote}}",
   159  			testManifestFile.Name(),
   160  		},
   161  			"https://fuchsia.googlesource.com/the_project")
   162  
   163  		expectAttributeValue(t, []string{
   164  			"-element=the_project",
   165  			"-template={{.Revision}}",
   166  			testManifestFile.Name(),
   167  		},
   168  			"the_project_revision")
   169  
   170  		expectAttributeValue(t, []string{
   171  			"-element=the_project",
   172  			"-template={{.RemoteBranch}}",
   173  			testManifestFile.Name(),
   174  		},
   175  			"the_project_remotebranch")
   176  
   177  		expectAttributeValue(t, []string{
   178  			"-element=the_project",
   179  			"-template={{.Path}}",
   180  			testManifestFile.Name(),
   181  		},
   182  			"path/to/the_project")
   183  	})
   184  
   185  	t.Run("should read <import> attributes", func(t *testing.T) {
   186  		expectAttributeValue(t, []string{
   187  			"-element=the_import",
   188  			"-template={{.Name}}",
   189  			testManifestFile.Name(),
   190  		},
   191  			"the_import")
   192  
   193  		expectAttributeValue(t, []string{
   194  			"-element=the_import",
   195  			"-template={{.Remote}}",
   196  			testManifestFile.Name(),
   197  		},
   198  			"https://fuchsia.googlesource.com/the_import")
   199  
   200  		expectAttributeValue(t, []string{
   201  			"-element=the_import",
   202  			"-template={{.Manifest}}",
   203  			testManifestFile.Name(),
   204  		},
   205  			"the_import_manifest")
   206  
   207  		expectAttributeValue(t, []string{
   208  			"-element=the_import",
   209  			"-template={{.Revision}}",
   210  			testManifestFile.Name(),
   211  		},
   212  			"the_import_revision")
   213  
   214  		expectAttributeValue(t, []string{
   215  			"-element=the_import",
   216  			"-template={{.RemoteBranch}}",
   217  			testManifestFile.Name(),
   218  		},
   219  			"the_import_remotebranch")
   220  	})
   221  
   222  	t.Run("should read <package> attributes", func(t *testing.T) {
   223  		expectAttributeValue(t, []string{
   224  			"-element=the_package/${platform}",
   225  			"-template={{.Name}}",
   226  			testManifestFile.Name(),
   227  		},
   228  			"the_package/${platform}")
   229  
   230  		expectAttributeValue(t, []string{
   231  			"-element=the_package/${platform}",
   232  			"-template={{.Version}}",
   233  			testManifestFile.Name(),
   234  		},
   235  			"the_package_version")
   236  
   237  		expectAttributeValue(t, []string{
   238  			"-element=the_package/${platform}",
   239  			"-template={{.Path}}",
   240  			testManifestFile.Name(),
   241  		},
   242  			"path/to/the_package")
   243  
   244  		var defaultPlatforms []string
   245  		for _, p := range cipd.DefaultPlatforms() {
   246  			defaultPlatforms = append(defaultPlatforms, p.String())
   247  		}
   248  		expectAttributeValue(t, []string{
   249  			"-element=the_package/${platform}",
   250  			"-template={{.Platforms}}",
   251  			testManifestFile.Name(),
   252  		},
   253  			strings.Join(defaultPlatforms, ","))
   254  
   255  		expectAttributeValue(t, []string{
   256  			"-element=the_package/${platform}",
   257  			"-template={{.Internal}}",
   258  			testManifestFile.Name(),
   259  		},
   260  			"false")
   261  	})
   262  }