go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/vpython/application/application_test.go (about)

     1  // Copyright 2023 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  package application
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"fmt"
    20  	"io/fs"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  	"time"
    25  
    26  	"go.chromium.org/luci/cipd/client/cipd"
    27  	"go.chromium.org/luci/common/logging"
    28  	"go.chromium.org/luci/common/system/filesystem"
    29  
    30  	"go.chromium.org/luci/vpython/api/vpython"
    31  
    32  	. "github.com/smartystreets/goconvey/convey"
    33  )
    34  
    35  func TestParseArguments(t *testing.T) {
    36  	Convey("Test parse arguments", t, func() {
    37  		ctx := context.Background()
    38  
    39  		app := &Application{}
    40  		app.Initialize(ctx)
    41  
    42  		parseArgs := func(args ...string) error {
    43  			app.Arguments = args
    44  			So(app.ParseEnvs(ctx), ShouldBeNil)
    45  			return app.ParseArgs(ctx)
    46  		}
    47  
    48  		Convey("Test log level", func() {
    49  			err := parseArgs(
    50  				"-vpython-log-level",
    51  				"warning",
    52  			)
    53  			So(err, ShouldBeNil)
    54  			ctx = app.SetLogLevel(ctx)
    55  			So(logging.GetLevel(ctx), ShouldEqual, logging.Warning)
    56  		})
    57  
    58  		Convey("Test unknown argument", func() {
    59  			const unknownErr = "failed to extract flags: unknown flag: vpython-test"
    60  
    61  			// Care but only care arguments begin with "-" or "--".
    62  			err := parseArgs("-vpython-test")
    63  			So(err, ShouldBeError, unknownErr)
    64  			err = parseArgs("--vpython-test")
    65  			So(err, ShouldBeError, unknownErr)
    66  			err = parseArgs("-vpython-root", "root", "vpython-test")
    67  			So(err, ShouldBeNil)
    68  
    69  			// All arguments after the script file should be bypassed.
    70  			err = parseArgs("-vpython-test", "test.py")
    71  			So(err, ShouldBeError, unknownErr)
    72  			err = parseArgs("test.py", "-vpython-test")
    73  			So(err, ShouldBeNil)
    74  
    75  			// Stop parsing arguments when seen --
    76  			err = parseArgs("--", "-vpython-test")
    77  			So(err, ShouldBeNil)
    78  		})
    79  
    80  		Convey("Test no user cache dir", func() {
    81  			app.userCacheDir = func() (string, error) { return "", errors.New("error") }
    82  
    83  			err := app.ParseEnvs(ctx)
    84  			So(err, ShouldBeNil)
    85  			err = app.ParseArgs(ctx)
    86  			So(err, ShouldBeNil)
    87  			So(app.VpythonRoot, ShouldNotBeEmpty)
    88  		})
    89  
    90  		Convey("Test cipd cache dir", func() {
    91  			err := parseArgs("-vpython-root", "root", "vpython-test")
    92  			So(err, ShouldBeNil)
    93  			wd, err := os.Getwd()
    94  			So(err, ShouldBeNil)
    95  			So(app.CIPDCacheDir, ShouldStartWith, filepath.Join(wd, "root"))
    96  		})
    97  
    98  		Convey("Test cipd cache dir with env", func() {
    99  			// Don't set cipd cache dir if env provides one
   100  			app.Environments = append(app.Environments, fmt.Sprintf("%s=%s", cipd.EnvCacheDir, "something"))
   101  			err := parseArgs("-vpython-root", "root", "vpython-test")
   102  			So(err, ShouldBeNil)
   103  			So(app.CIPDCacheDir, ShouldStartWith, "something")
   104  		})
   105  
   106  		Convey("Test spec load", func() {
   107  			Convey("not found", func() {
   108  				wd, err := os.Getwd()
   109  				So(err, ShouldBeNil)
   110  				defer os.Chdir(wd)
   111  				err = os.Chdir(t.TempDir())
   112  				So(err, ShouldBeNil)
   113  
   114  				// CommonFilesystemBarrier for spec loader
   115  				err = filesystem.Touch(".gclient", time.Time{}, fs.ModePerm)
   116  				So(err, ShouldBeNil)
   117  
   118  				err = parseArgs()
   119  				So(err, ShouldBeNil)
   120  
   121  				Convey("default", func() {
   122  					app.VpythonSpec = &vpython.Spec{PythonVersion: "something"}
   123  					err = app.LoadSpec(ctx)
   124  					So(err, ShouldBeNil)
   125  					So(app.VpythonSpec.GetPythonVersion(), ShouldEqual, "something")
   126  				})
   127  
   128  				Convey("no default", func() {
   129  					err = app.LoadSpec(ctx)
   130  					So(err, ShouldBeNil)
   131  					So(app.VpythonSpec, ShouldNotBeNil)
   132  				})
   133  			})
   134  		})
   135  	})
   136  }