Best Gauge code snippet using install.getVersion
resolve.go
Source:resolve.go
1package resolve2import (3 "fmt"4 "github.com/hashicorp/go-getter"5 "os"6 "strings"7 "wio/internal/constants"8 "wio/internal/types"9 "wio/pkg/log"10 "wio/pkg/npm/semver"11 "wio/pkg/util"12 "wio/pkg/util/sys"13 s "github.com/blang/semver"14)15const (16 Latest = "latest"17)18func (i *Info) GetLatest(name string) (string, error) {19 data, err := i.GetData(name)20 if err != nil {21 return "", err22 }23 if ver, exists := data.DistTags[Latest]; exists {24 return ver, nil25 }26 list, err := i.GetList(name)27 if err != nil {28 return "", err29 }30 return list.Last().String(), nil31}32func (i *Info) Exists(name string, ver string) (bool, error) {33 if ret := semver.Parse(ver); ret == nil {34 return false, util.Error("invalid version %s", ver)35 }36 data, err := i.GetData(name)37 if err != nil {38 return false, err39 }40 _, exists := data.Versions[ver]41 return exists, nil42}43func (i *Info) customUrlResolve(name string, dep types.Dependency, customPath string, root *Node, install bool) error {44 givenVer := semver.Parse(dep.GetVersion())45 if givenVer == nil {46 return util.Error("%s dependency version %s specified is not valid", name, dep.GetVersion())47 }48 dst := sys.Path(customPath, name) + "__" + givenVer.String()49 subDir := "/"50 if install && !sys.Exists(dst) {51 // add options to the source url52 url := dep.GetUrl().GetName()53 if !util.IsEmptyString(dep.GetUrl().GetDir()) {54 subDir = dep.GetUrl().GetDir()55 url += "//" + subDir56 }57 options := dep.GetUrl().GetOptions()58 if options != nil {59 url += "?"60 for name, value := range options {61 url += name + "=" + value + "&"62 }63 url = strings.Trim(strings.Trim(url, "&"), "?")64 }65 log.Info(log.Cyan, "Installing ")66 log.Info(log.Green, "%s@%s ", name, givenVer.String())67 log.Info(log.Cyan, "from ")68 log.Info(log.Green, "%s [%s]", dep.GetUrl().GetName(), subDir)69 log.Info(log.Cyan, "... ")70 if err := getter.Get(dst, url, func(client *getter.Client) error {71 client.Pwd = i.dir72 return nil73 }); err != nil {74 log.WriteFailure()75 return err76 } else {77 log.WriteSuccess()78 }79 }80 node := &Node{Name: name, ConfigVersion: dep.GetVersion(), Vendor: false, CustomUrl: true}81 root.Dependencies = append(root.Dependencies, node)82 if sys.Exists(dst) {83 // read wio.yml config84 config, err := types.ReadWioConfig(dst, true)85 if err != nil {86 return err87 }88 downloadVer := semver.Parse(config.GetVersion())89 if downloadVer == nil {90 return util.Error("%s dependency cannot have invalid version: %s", name, config.GetVersion())91 }92 if downloadVer.NE(*givenVer) {93 err = os.RemoveAll(dst)94 return util.Error("%s version mismatch between specified and downloaded: %s != %s",95 name, givenVer.String(), downloadVer.String())96 }97 i.SetPkg(name, config.GetVersion(), &Package{98 Vendor: false,99 Path: dst,100 Config: config,101 Version: nil,102 })103 return i.resolveRemote(config, node, install)104 }105 return nil106}107func (i *Info) createNodesAndFetch(deps map[string]types.Dependency, root *Node, install bool) error {108 customPath := sys.Path(i.dir, sys.WioFolder, sys.Modules, sys.Custom)109 if err := os.MkdirAll(customPath, os.ModePerm); err != nil {110 return err111 }112 for name, dep := range deps {113 if dep == nil {114 return util.Error("%s dependency cannot be empty", name)115 }116 // custom url is not provided117 if dep.GetUrl() == nil {118 node := &Node{Name: name, ConfigVersion: dep.GetVersion(), Vendor: dep.IsVendor()}119 root.Dependencies = append(root.Dependencies, node)120 } else {121 if err := i.customUrlResolve(name, dep, customPath, root, install); err != nil {122 return err123 }124 }125 }126 return nil127}128func (i *Info) resolveRemote(config types.Config, root *Node, install bool) error {129 if err := i.createNodesAndFetch(config.GetDependencies(), root, install); err != nil {130 return err131 }132 for _, dep := range root.Dependencies {133 if !dep.CustomUrl {134 if err := i.ResolveTree(dep, install); err != nil {135 return err136 }137 }138 }139 return nil140}141func (i *Info) ResolveRemote(config types.Config, install bool) error {142 logResolveStart(config)143 if err := i.LoadLocal(); err != nil {144 return err145 }146 i.root = &Node{147 Name: config.GetName(),148 ConfigVersion: config.GetVersion(),149 ResolvedVersion: semver.Parse(config.GetVersion()),150 }151 if i.root.ResolvedVersion == nil {152 return util.Error("project has invalid version %s", i.root.ConfigVersion)153 }154 // adds pkg config for the initial package155 if config.GetType() == constants.Pkg {156 i.SetPkg(i.root.Name, i.root.ResolvedVersion.String(), &Package{157 Vendor: false,158 Path: i.dir,159 Config: config,160 })161 }162 if err := i.resolveRemote(config, i.root, install); err != nil {163 return err164 }165 logResolveDone(i.root)166 return nil167}168func (i *Info) ResolveTree(root *Node, install bool) error {169 logResolve(root)170 if ret := i.GetRes(root.Name, root.ConfigVersion); ret != nil {171 root.ResolvedVersion = ret172 return nil173 }174 ver, err := i.resolveVer(root.Name, root.ConfigVersion)175 if err != nil {176 return err177 }178 root.ResolvedVersion = ver179 i.SetRes(root.Name, root.ConfigVersion, ver)180 if root.Vendor {181 pkg, err := i.GetPkg(root.Name, ver.String())182 if err != nil {183 return err184 }185 if err := i.createNodesAndFetch(pkg.Config.GetDependencies(), root, install); err != nil {186 return err187 }188 } else {189 data, err := i.GetVersion(root.Name, ver.String())190 if err != nil {191 return err192 }193 for name, ver := range data.Dependencies {194 node := &Node{Name: name, ConfigVersion: ver, Vendor: false}195 root.Dependencies = append(root.Dependencies, node)196 }197 }198 for _, node := range root.Dependencies {199 if !node.CustomUrl {200 if err := i.ResolveTree(node, install); err != nil {201 return err202 }203 }204 }205 return nil206}207func (i *Info) resolveVer(name string, ver string) (*s.Version, error) {208 if ret := semver.Parse(ver); ret != nil {209 i.StoreVer(name, ret)210 return ret, nil211 }212 query := semver.MakeQuery(ver)213 if query == nil {214 return nil, util.Error("invalid version expression %s", ver)215 }216 if ret := i.resolve[name].Find(query); ret != nil {217 return ret, nil218 }219 list, err := i.GetList(name)220 if err != nil {221 return nil, err222 }223 if ret := query.FindBest(list); ret != nil {224 i.StoreVer(name, ret)225 return ret, nil226 } else {227 fmt.Println("")228 }229 return nil, util.Error("unable to find suitable version for %s", ver)230}...
selfupdate_test.go
Source:selfupdate_test.go
1//go:build !noselfupdate2// +build !noselfupdate3package selfupdate4import (5 "context"6 "io/ioutil"7 "os"8 "os/exec"9 "path/filepath"10 "regexp"11 "runtime"12 "testing"13 "time"14 "github.com/rclone/rclone/fs"15 "github.com/rclone/rclone/fstest/testy"16 "github.com/rclone/rclone/lib/random"17 "github.com/stretchr/testify/assert"18)19func TestGetVersion(t *testing.T) {20 testy.SkipUnreliable(t)21 ctx := context.Background()22 // a beta version can only have "v" prepended23 resultVer, _, err := GetVersion(ctx, true, "1.2.3.4")24 assert.NoError(t, err)25 assert.Equal(t, "v1.2.3.4", resultVer)26 // but a stable version syntax should be checked27 _, _, err = GetVersion(ctx, false, "1")28 assert.Error(t, err)29 _, _, err = GetVersion(ctx, false, "1.")30 assert.Error(t, err)31 _, _, err = GetVersion(ctx, false, "1.2.")32 assert.Error(t, err)33 _, _, err = GetVersion(ctx, false, "1.2.3.4")34 assert.Error(t, err)35 // incomplete stable version should have micro release added36 resultVer, _, err = GetVersion(ctx, false, "1.52")37 assert.NoError(t, err)38 assert.Equal(t, "v1.52.3", resultVer)39}40func makeTestDir() (testDir string, err error) {41 const maxAttempts = 542 testDirBase := filepath.Join(os.TempDir(), "rclone-test-selfupdate.")43 for attempt := 0; attempt < maxAttempts; attempt++ {44 testDir = testDirBase + random.String(4)45 err = os.MkdirAll(testDir, os.ModePerm)46 if err == nil {47 break48 }49 }50 return51}52func TestInstallOnLinux(t *testing.T) {53 testy.SkipUnreliable(t)54 if runtime.GOOS != "linux" {55 t.Skip("this is a Linux only test")56 }57 // Prepare for test58 ctx := context.Background()59 testDir, err := makeTestDir()60 assert.NoError(t, err)61 path := filepath.Join(testDir, "rclone")62 defer func() {63 _ = os.Chmod(path, 0644)64 _ = os.RemoveAll(testDir)65 }()66 regexVer := regexp.MustCompile(`v[0-9]\S+`)67 betaVer, _, err := GetVersion(ctx, true, "")68 assert.NoError(t, err)69 // Must do nothing if version isn't changing70 assert.NoError(t, InstallUpdate(ctx, &Options{Beta: true, Output: path, Version: fs.Version}))71 // Must fail on non-writable file72 assert.NoError(t, ioutil.WriteFile(path, []byte("test"), 0644))73 assert.NoError(t, os.Chmod(path, 0000))74 err = (InstallUpdate(ctx, &Options{Beta: true, Output: path}))75 assert.Error(t, err)76 assert.Contains(t, err.Error(), "run self-update as root")77 // Must keep non-standard permissions78 assert.NoError(t, os.Chmod(path, 0644))79 assert.NoError(t, InstallUpdate(ctx, &Options{Beta: true, Output: path}))80 info, err := os.Stat(path)81 assert.NoError(t, err)82 assert.Equal(t, os.FileMode(0644), info.Mode().Perm())83 // Must remove temporary files84 files, err := ioutil.ReadDir(testDir)85 assert.NoError(t, err)86 assert.Equal(t, 1, len(files))87 // Must contain valid executable88 assert.NoError(t, os.Chmod(path, 0755))89 cmd := exec.Command(path, "version")90 output, err := cmd.CombinedOutput()91 assert.NoError(t, err)92 assert.True(t, cmd.ProcessState.Success())93 assert.Equal(t, betaVer, regexVer.FindString(string(output)))94}95func TestRenameOnWindows(t *testing.T) {96 testy.SkipUnreliable(t)97 if runtime.GOOS != "windows" {98 t.Skip("this is a Windows only test")99 }100 // Prepare for test101 ctx := context.Background()102 testDir, err := makeTestDir()103 assert.NoError(t, err)104 defer func() {105 _ = os.RemoveAll(testDir)106 }()107 path := filepath.Join(testDir, "rclone.exe")108 regexVer := regexp.MustCompile(`v[0-9]\S+`)109 stableVer, _, err := GetVersion(ctx, false, "")110 assert.NoError(t, err)111 betaVer, _, err := GetVersion(ctx, true, "")112 assert.NoError(t, err)113 // Must not create temporary files when target doesn't exist114 assert.NoError(t, InstallUpdate(ctx, &Options{Beta: true, Output: path}))115 files, err := ioutil.ReadDir(testDir)116 assert.NoError(t, err)117 assert.Equal(t, 1, len(files))118 // Must save running executable as the "old" file119 cmdWait := exec.Command(path, "config")120 stdinWait, err := cmdWait.StdinPipe() // Make it run waiting for input121 assert.NoError(t, err)122 assert.NoError(t, cmdWait.Start())123 assert.NoError(t, InstallUpdate(ctx, &Options{Beta: false, Output: path}))124 files, err = ioutil.ReadDir(testDir)125 assert.NoError(t, err)126 assert.Equal(t, 2, len(files))127 pathOld := filepath.Join(testDir, "rclone.old.exe")128 _, err = os.Stat(pathOld)129 assert.NoError(t, err)130 cmd := exec.Command(path, "version")131 output, err := cmd.CombinedOutput()132 assert.NoError(t, err)133 assert.True(t, cmd.ProcessState.Success())134 assert.Equal(t, stableVer, regexVer.FindString(string(output)))135 cmdOld := exec.Command(pathOld, "version")136 output, err = cmdOld.CombinedOutput()137 assert.NoError(t, err)138 assert.True(t, cmdOld.ProcessState.Success())139 assert.Equal(t, betaVer, regexVer.FindString(string(output)))140 // Stop previous waiting executable, run new and saved executables141 _ = stdinWait.Close()142 _ = cmdWait.Wait()143 time.Sleep(100 * time.Millisecond)144 cmdWait = exec.Command(path, "config")145 stdinWait, err = cmdWait.StdinPipe()146 assert.NoError(t, err)147 assert.NoError(t, cmdWait.Start())148 cmdWaitOld := exec.Command(pathOld, "config")149 stdinWaitOld, err := cmdWaitOld.StdinPipe()150 assert.NoError(t, err)151 assert.NoError(t, cmdWaitOld.Start())152 // Updating when the "old" executable is running must produce a random "old" file153 assert.NoError(t, InstallUpdate(ctx, &Options{Beta: true, Output: path}))154 files, err = ioutil.ReadDir(testDir)155 assert.NoError(t, err)156 assert.Equal(t, 3, len(files))157 // Stop all waiting executables158 _ = stdinWait.Close()159 _ = cmdWait.Wait()160 _ = stdinWaitOld.Close()161 _ = cmdWaitOld.Wait()162 time.Sleep(100 * time.Millisecond)163}...
getVersion
Using AI Code Generation
1func main() {2 fmt.Println(install.GetVersion())3}4func main() {5 fmt.Println(install.GetVersion())6}7func main() {8 fmt.Println(install.GetVersion())9}10func main() {11 fmt.Println(install.GetVersion())12}13func main() {14 fmt.Println(install.GetVersion())15}16func main() {17 fmt.Println(install.GetVersion())18}19func main() {20 fmt.Println(install.GetVersion())21}22func main() {23 fmt.Println(install.GetVersion())24}25func main() {26 fmt.Println(install.GetVersion())27}28func main() {29 fmt.Println(install.GetVersion())30}31func main() {32 fmt.Println(install.GetVersion())33}34func main() {35 fmt.Println(install.GetVersion())36}37func main() {38 fmt.Println(install.GetVersion())39}40func main() {41 fmt.Println(install.GetVersion())42}43func main() {44 fmt.Println(install.GetVersion())45}46func main() {47 fmt.Println(install.GetVersion())48}49func main() {50 fmt.Println(install.GetVersion())51}
getVersion
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, world.")4 xlsx, err := excelize.OpenFile("Book1.xlsx")5 if err != nil {6 fmt.Println(err)7 }8 cell := xlsx.GetCellValue("Sheet1", "B2")9 fmt.Println(cell)10 index := xlsx.GetSheetIndex("Sheet1")11 fmt.Println(index)12 rows := xlsx.GetRows("Sheet1")13 for _, row := range rows {14 for _, colCell := range row {15 fmt.Print(colCell, "\t")16 }17 fmt.Println()18 }19 rows = xlsx.GetRows("Sheet2")20 for _, row := range rows {21 for _, colCell := range row {22 fmt.Print(colCell, "\t")23 }24 fmt.Println()25 }26 rows = xlsx.GetRows("Sheet3")27 for _, row := range rows {28 for _, colCell := range row {29 fmt.Print(colCell, "\t")30 }31 fmt.Println()32 }33}
getVersion
Using AI Code Generation
1import (2func main() {3 fmt.Println(install.GetVersion())4}5import (6func main() {7 fmt.Println(install.GetVersion())8}9import (10func main() {11 fmt.Println(install.GetVersion())12}13import (14func main() {15 fmt.Println(install.GetVersion())16}17import (18func main() {19 fmt.Println(install.GetVersion())20}21import (22func main() {23 fmt.Println(install.GetVersion())24}25import (26func main() {27 fmt.Println(install.GetVersion())28}29import (30func main() {31 fmt.Println(install.GetVersion())32}33import (34func main() {35 fmt.Println(install.GetVersion())36}
getVersion
Using AI Code Generation
1import (2func main() {3 fmt.Println(install.GetVersion())4}5import (6func main() {7 fmt.Println(GoLangBasics2.GetVersion())8}9import (10func main() {11 fmt.Println(GoLangBasics2.GetVersion())12}13import (14type install struct {15}16func (i *install) getVersion() string {17}18func GetVersion() string {19 i := install{20 }21 return i.getVersion()22}23import (24type install struct {25}26func (i *install) getVersion() string {27}28func GetVersion() string {29 i := install{
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!