go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/version/version_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 version
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"runtime"
    22  	"testing"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func TestRecoverSymlinkPath(t *testing.T) {
    28  	if runtime.GOOS == "windows" {
    29  		t.Skip("Skipping test on Windows")
    30  	}
    31  
    32  	Convey("recoverSymlinkPath works", t, func(c C) {
    33  		So(recoverSymlinkPath("/a/b/.cipd/pkgs/cipd_mac-amd64_L8/08c6146/cipd"), ShouldEqual, "/a/b/cipd")
    34  		So(recoverSymlinkPath("/a/b/.cipd/pkgs/cipd_mac-amd64_L8/08c6146/c/d/cipd"), ShouldEqual, "/a/b/c/d/cipd")
    35  		So(recoverSymlinkPath("/a/b/.cipd/pkgs/0/_current/c/d/cipd"), ShouldEqual, "/a/b/c/d/cipd")
    36  		So(recoverSymlinkPath(".cipd/pkgs/cipd_mac-amd64_L8/08c6146/a"), ShouldEqual, "a")
    37  	})
    38  
    39  	Convey("recoverSymlinkPath handles bad paths", t, func(c C) {
    40  		So(recoverSymlinkPath(""), ShouldEqual, "")
    41  		So(recoverSymlinkPath("/a/b/c"), ShouldEqual, "")
    42  		So(recoverSymlinkPath("/a/b/c/d/e/f"), ShouldEqual, "")
    43  		So(recoverSymlinkPath("/a/b/c/.cipd/pkgs/d"), ShouldEqual, "")
    44  		So(recoverSymlinkPath("/a/b/c/.cipd/pkgs/abc/d"), ShouldEqual, "")
    45  	})
    46  }
    47  
    48  func TestEvalSymlinksAndAbs(t *testing.T) {
    49  	if runtime.GOOS == "windows" {
    50  		t.Skip("Skipping test on Windows")
    51  	}
    52  
    53  	Convey(`evalSymlinksAndAbs`, t, func() {
    54  		Convey(`works`, func() {
    55  			dir, err := ioutil.TempDir("", "")
    56  			So(err, ShouldBeNil)
    57  			defer os.RemoveAll(dir)
    58  
    59  			p := func(path string) string {
    60  				return fmt.Sprintf("%s/%s", dir, path)
    61  			}
    62  
    63  			err = os.MkdirAll(p(`.cipd/pkgs/0/deadbeef`), 0755)
    64  			So(err, ShouldBeNil)
    65  			err = os.MkdirAll(p(`a`), 0755)
    66  			So(err, ShouldBeNil)
    67  
    68  			f, err := os.Create(p(`.cipd/pkgs/0/deadbeef/foo`))
    69  			So(err, ShouldBeNil)
    70  			f.Close()
    71  
    72  			err = os.Symlink(p(`.cipd/pkgs/0/deadbeef/foo`), p(`.cipd/pkgs/0/deadbeef/bar`))
    73  			So(err, ShouldBeNil)
    74  
    75  			err = os.Symlink(p(`.cipd/pkgs/0/deadbeef`), p(`.cipd/pkgs/0/_current`))
    76  			So(err, ShouldBeNil)
    77  
    78  			err = os.Symlink(p(`.cipd/pkgs/0/_current/foo`), p(`a/foo`))
    79  			So(err, ShouldBeNil)
    80  
    81  			pth, err := evalSymlinksAndAbs(p(`a/foo`), nil)
    82  			So(err, ShouldBeNil)
    83  			So(pth, ShouldEndWith, `.cipd/pkgs/0/deadbeef/foo`)
    84  		})
    85  	})
    86  }