github.com/Zenithar/prototool@v1.3.0/internal/protoc/downloader_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package protoc 22 23 import ( 24 "fmt" 25 "io/ioutil" 26 "os" 27 "path/filepath" 28 "strings" 29 "testing" 30 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 "github.com/uber/prototool/internal/settings" 34 ) 35 36 func TestGetDefaultBasePath(t *testing.T) { 37 tests := []struct { 38 goos string 39 goarch string 40 xdgCacheHome string 41 home string 42 expectedBasePath string 43 expectError bool 44 }{ 45 { 46 goos: "darwin", 47 goarch: "amd64", 48 xdgCacheHome: "/foo", 49 home: "/Users/alice", 50 expectedBasePath: "/foo/prototool/Darwin/x86_64", 51 }, 52 { 53 goos: "darwin", 54 goarch: "amd64", 55 home: "/Users/alice", 56 expectedBasePath: "/Users/alice/Library/Caches/prototool/Darwin/x86_64", 57 }, 58 { 59 goos: "linux", 60 goarch: "amd64", 61 xdgCacheHome: "/foo", 62 home: "/home/alice", 63 expectedBasePath: "/foo/prototool/Linux/x86_64", 64 }, 65 { 66 goos: "linux", 67 goarch: "amd64", 68 home: "/home/alice", 69 expectedBasePath: "/home/alice/.cache/prototool/Linux/x86_64", 70 }, 71 { 72 goos: "foo", 73 goarch: "amd64", 74 xdgCacheHome: "/foo", 75 home: "/home/alice", 76 expectError: true, 77 }, 78 { 79 goos: "linux", 80 goarch: "foo", 81 xdgCacheHome: "/foo", 82 home: "/home/alice", 83 expectError: true, 84 }, 85 { 86 goos: "linux", 87 goarch: "amd64", 88 expectError: true, 89 }, 90 } 91 for _, tt := range tests { 92 t.Run(strings.Join([]string{tt.goos, tt.goarch, tt.xdgCacheHome, tt.home}, " "), func(t *testing.T) { 93 basePath, err := getDefaultBasePathInternal(tt.goos, tt.goarch, newTestGetenvFunc(tt.xdgCacheHome, tt.home)) 94 if tt.expectError { 95 assert.Error(t, err) 96 } 97 assert.Equal(t, tt.expectedBasePath, basePath) 98 }) 99 } 100 } 101 102 func TestNewDownloaderProtocValidation(t *testing.T) { 103 tests := []struct { 104 desc string 105 url string 106 binPath string 107 wktPath string 108 createBinPath bool 109 createWKTPath bool 110 err error 111 }{ 112 { 113 desc: "No options", 114 }, 115 { 116 desc: "protocURL option", 117 url: "http://example.com", 118 }, 119 { 120 desc: "protocBinPath with protocWKTPath", 121 binPath: "protoc", 122 wktPath: "include", 123 createBinPath: true, 124 createWKTPath: true, 125 }, 126 { 127 desc: "protocURL set with protocBinPath and protocWKTPath", 128 url: "http://example.com", 129 binPath: "protoc", 130 wktPath: "include", 131 err: fmt.Errorf("cannot use protoc-url in combination with either protoc-bin-path or protoc-wkt-path"), 132 }, 133 { 134 desc: "protocBinPath set without protocWKTPath", 135 binPath: "protoc", 136 err: fmt.Errorf("both protoc-bin-path and protoc-wkt-path must be set"), 137 }, 138 { 139 desc: "protocBinPath does not exist", 140 binPath: "protoc", 141 wktPath: "include", 142 err: fmt.Errorf("stat protoc: no such file or directory"), 143 }, 144 { 145 desc: "protocWKTPath does not exist", 146 binPath: "protoc", 147 wktPath: "include", 148 createBinPath: true, 149 err: fmt.Errorf("stat include: no such file or directory"), 150 }, 151 } 152 for _, tt := range tests { 153 t.Run(tt.desc, func(t *testing.T) { 154 tmpRoot, err := ioutil.TempDir("", "test") 155 require.NoError(t, err) 156 157 // Clean up all the created test directories. 158 defer os.RemoveAll(tmpRoot) 159 160 if tt.createBinPath { 161 tt.binPath, err = ioutil.TempDir(tmpRoot, tt.binPath) 162 require.NoError(t, err) 163 } 164 165 if tt.createWKTPath { 166 tt.wktPath, err = ioutil.TempDir(tmpRoot, tt.wktPath) 167 require.NoError(t, err) 168 } 169 170 if tt.createBinPath && tt.createWKTPath { 171 require.NoError(t, os.MkdirAll(filepath.Join(tt.wktPath, "google", "protobuf"), 0755)) 172 } 173 174 _, err = newDownloader( 175 settings.Config{}, 176 DownloaderWithProtocURL(tt.url), 177 DownloaderWithProtocBinPath(tt.binPath), 178 DownloaderWithProtocWKTPath(tt.wktPath), 179 ) 180 181 if tt.err != nil { 182 require.Error(t, err) 183 assert.EqualError(t, err, tt.err.Error()) 184 } else { 185 assert.Nil(t, err) 186 } 187 }) 188 } 189 } 190 191 func newTestGetenvFunc(xdgCacheHome string, home string) func(string) string { 192 m := make(map[string]string) 193 if xdgCacheHome != "" { 194 m["XDG_CACHE_HOME"] = xdgCacheHome 195 } 196 if home != "" { 197 m["HOME"] = home 198 } 199 return func(key string) string { return m[key] } 200 }