| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // OS-agnostic helper functions which are called from run(). |
| 6 |
| 7 package firstrun |
| 8 |
| 9 import ( |
| 10 "errors" |
| 11 "os" |
| 12 "path/filepath" |
| 13 "strings" |
| 14 "testing" |
| 15 "time" |
| 16 |
| 17 . "github.com/smartystreets/goconvey/convey" |
| 18 ) |
| 19 |
| 20 type fakeFileInfo struct { |
| 21 name string |
| 22 size int64 |
| 23 mode os.FileMode |
| 24 modTime time.Time |
| 25 isDir bool |
| 26 } |
| 27 |
| 28 func (f fakeFileInfo) Name() string { return f.name } |
| 29 func (f fakeFileInfo) Size() int64 { return f.size } |
| 30 func (f fakeFileInfo) Mode() os.FileMode { return f.mode } |
| 31 func (f fakeFileInfo) ModTime() time.Time { return f.modTime } |
| 32 func (f fakeFileInfo) IsDir() bool { return f.isDir } |
| 33 func (f fakeFileInfo) Sys() interface{} { return nil } |
| 34 |
| 35 func TestCheckNotInstalled(t *testing.T) { |
| 36 t.Parallel() |
| 37 Convey("When checking that cr isn't already installed", t, func() { |
| 38 Convey("an empty $PATH results in an error", func() { |
| 39 getenv = func(s string) string { return "" } |
| 40 path, err := firstrunCheckNotInstalled() |
| 41 So(path, ShouldEqual, "") |
| 42 So(err, ShouldNotBeNil) |
| 43 }) |
| 44 |
| 45 Convey("paths not ending in cr/bin are ignored", func() { |
| 46 getenv = func(s string) string { |
| 47 paths := []string{ |
| 48 filepath.Join("foo", "bar"), |
| 49 filepath.Join("foo", "cr", "bin", "bar")
, |
| 50 } |
| 51 return strings.Join(paths, string(os.PathListSep
arator)) |
| 52 } |
| 53 path, err := firstrunCheckNotInstalled() |
| 54 So(path, ShouldEqual, "") |
| 55 So(err, ShouldBeNil) |
| 56 }) |
| 57 |
| 58 Convey("multiple candidates are all checked", func() { |
| 59 getenv = func(s string) string { |
| 60 paths := []string{ |
| 61 filepath.Join("foo", "cr", "bin"), |
| 62 filepath.Join("foo", "bar", "cr", "bin")
, |
| 63 } |
| 64 return strings.Join(paths, string(os.PathListSep
arator)) |
| 65 } |
| 66 |
| 67 Convey("and missing executables are handled", func() { |
| 68 stat = func(s string) (os.FileInfo, error) { |
| 69 return nil, os.ErrNotExist |
| 70 } |
| 71 path, err := firstrunCheckNotInstalled() |
| 72 So(path, ShouldEqual, "") |
| 73 So(err, ShouldBeNil) |
| 74 }) |
| 75 |
| 76 Convey("and non-executables are ignored", func() { |
| 77 stat = func(s string) (os.FileInfo, error) { |
| 78 return fakeFileInfo{executableName, 64,
0666, time.Now(), false}, nil |
| 79 } |
| 80 path, err := firstrunCheckNotInstalled() |
| 81 So(err, ShouldBeNil) |
| 82 So(path, ShouldEqual, "") |
| 83 }) |
| 84 |
| 85 Convey("and the only executable is returned", func() { |
| 86 stat = func(s string) (os.FileInfo, error) { |
| 87 if strings.Contains(s, "bar") { |
| 88 return fakeFileInfo{executableNa
me, 64, 0777, time.Now(), false}, nil |
| 89 } |
| 90 return fakeFileInfo{executableName, 64,
0666, time.Now(), false}, nil |
| 91 } |
| 92 path, err := firstrunCheckNotInstalled() |
| 93 So(path, ShouldEqual, filepath.Join("foo", "bar"
, "cr", "bin", executableName)) |
| 94 So(err, ShouldBeNil) |
| 95 }) |
| 96 |
| 97 Convey("and the first executable is returned", func() { |
| 98 stat = func(s string) (os.FileInfo, error) { |
| 99 return fakeFileInfo{executableName, 64,
0777, time.Now(), false}, nil |
| 100 } |
| 101 path, err := firstrunCheckNotInstalled() |
| 102 So(path, ShouldEqual, filepath.Join("foo", "cr",
"bin", executableName)) |
| 103 So(err, ShouldBeNil) |
| 104 }) |
| 105 |
| 106 Convey("and errors are propagated forward", func() { |
| 107 stat = func(s string) (os.FileInfo, error) { |
| 108 if strings.Contains(s, "bar") { |
| 109 return fakeFileInfo{executableNa
me, 64, 0666, time.Now(), false}, nil |
| 110 } |
| 111 return nil, errors.New("some crazy error
") |
| 112 } |
| 113 path, err := firstrunCheckNotInstalled() |
| 114 So(path, ShouldEqual, "") |
| 115 So(err, ShouldResemble, errors.New("some crazy e
rror")) |
| 116 }) |
| 117 }) |
| 118 }) |
| 119 } |
| OLD | NEW |