go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/isolate/isolateimpl/batch_archive_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package isolateimpl
    16  
    17  import (
    18  	"path/filepath"
    19  	"runtime"
    20  	"strings"
    21  	"testing"
    22  
    23  	"go.chromium.org/luci/common/flag/stringmapflag"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestConvertPyToGoArchiveCMDArgs(t *testing.T) {
    29  	Convey(`Archive command line arguments should be converted properly for Go.`, t, func() {
    30  		data := []struct {
    31  			input    []string
    32  			expected []string
    33  		}{
    34  			// Simple.
    35  			{
    36  				[]string{"--path-variable", "key=value"},
    37  				[]string{"--path-variable", "key=value"},
    38  			},
    39  			{
    40  				[]string{"--path-variable", "key", "value1"},
    41  				[]string{"--path-variable", "key=value1"},
    42  			},
    43  			{
    44  				[]string{"--path-variable", "key", "--even-this-value"},
    45  				[]string{"--path-variable", "key=--even-this-value"},
    46  			},
    47  			// Other args.
    48  			{
    49  				[]string{"-x", "--var", "--config-variable", "key", "value"},
    50  				[]string{"-x", "--var", "--config-variable", "key=value"},
    51  			},
    52  			{
    53  				[]string{"--path-variable", "key", "value", "posarg"},
    54  				[]string{"--path-variable", "key=value", "posarg"},
    55  			},
    56  			// Too few args are just ignored.
    57  			{
    58  				[]string{"--path-variable"},
    59  				[]string{"--path-variable"},
    60  			},
    61  			{
    62  				[]string{"--path-variable", "key-and-no-value"},
    63  				[]string{"--path-variable", "key-and-no-value"},
    64  			},
    65  		}
    66  		for _, line := range data {
    67  			So(convertPyToGoArchiveCMDArgs(line.input), ShouldResemble, line.expected)
    68  		}
    69  	})
    70  }
    71  
    72  func TestInvalidArchiveCMD(t *testing.T) {
    73  	Convey(`Archive should handle errors in command line arguments.`, t, func() {
    74  		_, err := parseArchiveCMD([]string(nil), absToOS("e:", "/tmp/bar"))
    75  		So(err.Error(), ShouldResemble, "-isolate must be specified")
    76  	})
    77  }
    78  
    79  func TestArchiveCMDParsing(t *testing.T) {
    80  	Convey(`Archive command line arguments should be parsed correctly.`, t, func() {
    81  		args := []string{
    82  			"--isolate", "../boz/bar.isolate",
    83  			"--path-variable", "DEPTH", "../..",
    84  			"--path-variable", "PRODUCT_DIR", "../../out/Release",
    85  			"--config-variable", "OS=linux",
    86  		}
    87  		root := absToOS("e:", "/tmp/bar")
    88  		opts, err := parseArchiveCMD(args, root)
    89  		base := filepath.Dir(root)
    90  		So(opts.Isolate, ShouldResemble, filepath.Join(base, "boz", "bar.isolate"))
    91  		So(err, ShouldBeNil)
    92  		So(stringmapflag.Value{"OS": "linux"}, ShouldResemble, opts.ConfigVariables)
    93  		if runtime.GOOS == "windows" {
    94  			So(stringmapflag.Value{"PRODUCT_DIR": "../../out/Release", "EXECUTABLE_SUFFIX": ".exe", "DEPTH": "../.."}, ShouldResemble, opts.PathVariables)
    95  		} else {
    96  			So(stringmapflag.Value{"PRODUCT_DIR": "../../out/Release", "EXECUTABLE_SUFFIX": "", "DEPTH": "../.."}, ShouldResemble, opts.PathVariables)
    97  		}
    98  	})
    99  }
   100  
   101  // Verify that if the isolate path is absolute, we don't
   102  // accidentally interpret them as relative to the cwd.
   103  func TestArchiveAbsolutePaths(t *testing.T) {
   104  	Convey(`Archive command line should correctly handle absolute paths.`, t, func() {
   105  		root := absToOS("e:", "/tmp/bar/")
   106  		args := []string{
   107  			"--isolate", root + "foo.isolate",
   108  		}
   109  		opts, err := parseArchiveCMD(args, absToOS("x:", "/var/lib"))
   110  		So(err, ShouldBeNil)
   111  		So(opts.Isolate, ShouldResemble, root+"foo.isolate")
   112  	})
   113  }
   114  
   115  // Private stuff.
   116  
   117  // absToOS converts a POSIX path to OS specific format.
   118  func absToOS(drive, p string) string {
   119  	if runtime.GOOS == "windows" {
   120  		return drive + strings.Replace(p, "/", "\\", -1)
   121  	}
   122  	return p
   123  }