| 1 |
// Copyright 2019 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package zip_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"archive/zip" |
| 9 |
"bytes" |
| 10 |
"crypto/sha256" |
| 11 |
"encoding/hex" |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"go/build" |
| 15 |
"go/build/constraint" |
| 16 |
"io" |
| 17 |
"maps" |
| 18 |
"os" |
| 19 |
"os/exec" |
| 20 |
"path" |
| 21 |
"path/filepath" |
| 22 |
"runtime" |
| 23 |
"slices" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"sync/atomic" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
"golang.org/x/mod/module" |
| 31 |
"golang.org/x/mod/sumdb/dirhash" |
| 32 |
modzip "golang.org/x/mod/zip" |
| 33 |
"golang.org/x/tools/txtar" |
| 34 |
) |
| 35 |
|
| 36 |
const emptyHash = "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" |
| 37 |
|
| 38 |
var gitOnce struct { |
| 39 |
path string |
| 40 |
err error |
| 41 |
sync.Once |
| 42 |
} |
| 43 |
|
| 44 |
func init() { |
| 45 |
if os.Getenv("GO_BUILDER_NAME") != "" || os.Getenv("GIT_TRACE_CURL") == "1" { |
| 46 |
// Enable extra Git logging to diagnose networking issues. |
| 47 |
// (These environment variables will be inherited by subprocesses.) |
| 48 |
os.Setenv("GIT_TRACE_CURL", "1") |
| 49 |
os.Setenv("GIT_TRACE_CURL_NO_DATA", "1") |
| 50 |
os.Setenv("GIT_REDACT_COOKIES", "o,SSO,GSSO_Uberproxy") |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// gitPath returns the path to a usable "git" command, |
| 55 |
// or a non-nil error. |
| 56 |
func gitPath() (string, error) { |
| 57 |
gitOnce.Do(func() { |
| 58 |
path, err := exec.LookPath("git") |
| 59 |
if err != nil { |
| 60 |
gitOnce.err = err |
| 61 |
return |
| 62 |
} |
| 63 |
if runtime.GOOS == "plan9" { |
| 64 |
gitOnce.err = errors.New("plan9 git does not support the full git command line") |
| 65 |
} |
| 66 |
gitOnce.path = path |
| 67 |
}) |
| 68 |
|
| 69 |
return gitOnce.path, gitOnce.err |
| 70 |
} |
| 71 |
|
| 72 |
func mustHaveGit(t testing.TB) { |
| 73 |
if _, err := gitPath(); err != nil { |
| 74 |
t.Helper() |
| 75 |
t.Skipf("skipping: %v", err) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
type testParams struct { |
| 80 |
path, version, wantErr, hash string |
| 81 |
archive *txtar.Archive |
| 82 |
} |
| 83 |
|
| 84 |
// readTest loads a test from a txtar file. The comment section of the file |
| 85 |
// should contain lines with key=value pairs. Valid keys are the field names |
| 86 |
// from testParams. |
| 87 |
func readTest(file string) (testParams, error) { |
| 88 |
var test testParams |
| 89 |
var err error |
| 90 |
test.archive, err = txtar.ParseFile(file) |
| 91 |
if err != nil { |
| 92 |
return testParams{}, err |
| 93 |
} |
| 94 |
|
| 95 |
lines := strings.Split(string(test.archive.Comment), "\n") |
| 96 |
for n, line := range lines { |
| 97 |
n++ // report line numbers starting with 1 |
| 98 |
if i := strings.IndexByte(line, '#'); i >= 0 { |
| 99 |
line = line[:i] |
| 100 |
} |
| 101 |
line = strings.TrimSpace(line) |
| 102 |
if line == "" { |
| 103 |
continue |
| 104 |
} |
| 105 |
before, after, ok := strings.Cut(line, "=") |
| 106 |
if !ok { |
| 107 |
return testParams{}, fmt.Errorf("%s:%d: missing = separator", file, n) |
| 108 |
} |
| 109 |
key, value := strings.TrimSpace(before), strings.TrimSpace(after) |
| 110 |
switch key { |
| 111 |
case "path": |
| 112 |
test.path = value |
| 113 |
case "version": |
| 114 |
test.version = value |
| 115 |
case "wantErr": |
| 116 |
test.wantErr = value |
| 117 |
case "hash": |
| 118 |
test.hash = value |
| 119 |
default: |
| 120 |
return testParams{}, fmt.Errorf("%s:%d: unknown key %q", file, n, key) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return test, nil |
| 125 |
} |
| 126 |
|
| 127 |
func extractTxtarToTempDir(t testing.TB, arc *txtar.Archive) (dir string, err error) { |
| 128 |
dir = t.TempDir() |
| 129 |
for _, f := range arc.Files { |
| 130 |
filePath := filepath.Join(dir, f.Name) |
| 131 |
if err := os.MkdirAll(filepath.Dir(filePath), 0777); err != nil { |
| 132 |
return "", err |
| 133 |
} |
| 134 |
if err := os.WriteFile(filePath, f.Data, 0666); err != nil { |
| 135 |
return "", err |
| 136 |
} |
| 137 |
} |
| 138 |
return dir, nil |
| 139 |
} |
| 140 |
|
| 141 |
func extractTxtarToTempZip(t *testing.T, arc *txtar.Archive) (zipPath string, err error) { |
| 142 |
zipPath = filepath.Join(t.TempDir(), "txtar.zip") |
| 143 |
|
| 144 |
zipFile, err := os.Create(zipPath) |
| 145 |
if err != nil { |
| 146 |
return "", err |
| 147 |
} |
| 148 |
defer func() { |
| 149 |
if cerr := zipFile.Close(); err == nil && cerr != nil { |
| 150 |
err = cerr |
| 151 |
} |
| 152 |
}() |
| 153 |
|
| 154 |
zw := zip.NewWriter(zipFile) |
| 155 |
for _, f := range arc.Files { |
| 156 |
zf, err := zw.Create(f.Name) |
| 157 |
if err != nil { |
| 158 |
return "", err |
| 159 |
} |
| 160 |
if _, err := zf.Write(f.Data); err != nil { |
| 161 |
return "", err |
| 162 |
} |
| 163 |
} |
| 164 |
if err := zw.Close(); err != nil { |
| 165 |
return "", err |
| 166 |
} |
| 167 |
return zipFile.Name(), nil |
| 168 |
} |
| 169 |
|
| 170 |
type fakeFile struct { |
| 171 |
name string |
| 172 |
size uint64 |
| 173 |
data []byte // if nil, Open will access a sequence of 0-bytes |
| 174 |
} |
| 175 |
|
| 176 |
func (f fakeFile) Path() string { return f.name } |
| 177 |
func (f fakeFile) Lstat() (os.FileInfo, error) { return fakeFileInfo{f}, nil } |
| 178 |
func (f fakeFile) Open() (io.ReadCloser, error) { |
| 179 |
if f.data != nil { |
| 180 |
return io.NopCloser(bytes.NewReader(f.data)), nil |
| 181 |
} |
| 182 |
if f.size >= uint64(modzip.MaxZipFile<<1) { |
| 183 |
return nil, fmt.Errorf("cannot open fakeFile of size %d", f.size) |
| 184 |
} |
| 185 |
return io.NopCloser(io.LimitReader(zeroReader{}, int64(f.size))), nil |
| 186 |
} |
| 187 |
|
| 188 |
type fakeFileInfo struct { |
| 189 |
f fakeFile |
| 190 |
} |
| 191 |
|
| 192 |
func (fi fakeFileInfo) Name() string { return path.Base(fi.f.name) } |
| 193 |
func (fi fakeFileInfo) Size() int64 { return int64(fi.f.size) } |
| 194 |
func (fi fakeFileInfo) Mode() os.FileMode { return 0644 } |
| 195 |
func (fi fakeFileInfo) ModTime() time.Time { return time.Time{} } |
| 196 |
func (fi fakeFileInfo) IsDir() bool { return false } |
| 197 |
func (fi fakeFileInfo) Sys() any { return nil } |
| 198 |
|
| 199 |
type zeroReader struct{} |
| 200 |
|
| 201 |
func (r zeroReader) Read(b []byte) (int, error) { |
| 202 |
for i := range b { |
| 203 |
b[i] = 0 |
| 204 |
} |
| 205 |
return len(b), nil |
| 206 |
} |
| 207 |
|
| 208 |
func formatCheckedFiles(cf modzip.CheckedFiles) string { |
| 209 |
buf := &bytes.Buffer{} |
| 210 |
fmt.Fprintf(buf, "valid:\n") |
| 211 |
for _, f := range cf.Valid { |
| 212 |
fmt.Fprintln(buf, f) |
| 213 |
} |
| 214 |
fmt.Fprintf(buf, "\nomitted:\n") |
| 215 |
for _, f := range cf.Omitted { |
| 216 |
fmt.Fprintf(buf, "%s: %v\n", f.Path, f.Err) |
| 217 |
} |
| 218 |
fmt.Fprintf(buf, "\ninvalid:\n") |
| 219 |
for _, f := range cf.Invalid { |
| 220 |
fmt.Fprintf(buf, "%s: %v\n", f.Path, f.Err) |
| 221 |
} |
| 222 |
return buf.String() |
| 223 |
} |
| 224 |
|
| 225 |
// TestCheckFiles verifies behavior of CheckFiles. Note that CheckFiles is also |
| 226 |
// covered by TestCreate, TestCreateDir, and TestCreateSizeLimits, so this test |
| 227 |
// focuses on how multiple errors and omissions are reported, rather than trying |
| 228 |
// to cover every case. |
| 229 |
func TestCheckFiles(t *testing.T) { |
| 230 |
testPaths, err := filepath.Glob(filepath.FromSlash("testdata/check_files/*.txt")) |
| 231 |
if err != nil { |
| 232 |
t.Fatal(err) |
| 233 |
} |
| 234 |
for _, testPath := range testPaths { |
| 235 |
name := strings.TrimSuffix(filepath.Base(testPath), ".txt") |
| 236 |
t.Run(name, func(t *testing.T) { |
| 237 |
t.Parallel() |
| 238 |
|
| 239 |
// Load the test. |
| 240 |
test, err := readTest(testPath) |
| 241 |
if err != nil { |
| 242 |
t.Fatal(err) |
| 243 |
} |
| 244 |
files := make([]modzip.File, 0, len(test.archive.Files)) |
| 245 |
var want string |
| 246 |
for _, tf := range test.archive.Files { |
| 247 |
if tf.Name == "want" { |
| 248 |
want = string(tf.Data) |
| 249 |
continue |
| 250 |
} |
| 251 |
files = append(files, fakeFile{ |
| 252 |
name: tf.Name, |
| 253 |
size: uint64(len(tf.Data)), |
| 254 |
data: tf.Data, |
| 255 |
}) |
| 256 |
} |
| 257 |
|
| 258 |
// Check the files. |
| 259 |
cf, _ := modzip.CheckFiles(files) |
| 260 |
got := formatCheckedFiles(cf) |
| 261 |
if got != want { |
| 262 |
t.Errorf("got:\n%s\n\nwant:\n%s", got, want) |
| 263 |
} |
| 264 |
|
| 265 |
// Check that the error (if any) is just a list of invalid files. |
| 266 |
// SizeError is not covered in this test. |
| 267 |
var gotErr, wantErr string |
| 268 |
if len(cf.Invalid) > 0 { |
| 269 |
wantErr = modzip.FileErrorList(cf.Invalid).Error() |
| 270 |
} |
| 271 |
if err := cf.Err(); err != nil { |
| 272 |
gotErr = err.Error() |
| 273 |
} |
| 274 |
if gotErr != wantErr { |
| 275 |
t.Errorf("got error:\n%s\n\nwant error:\n%s", gotErr, wantErr) |
| 276 |
} |
| 277 |
}) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// TestCheckDir verifies behavior of the CheckDir function. Note that CheckDir |
| 282 |
// relies on CheckFiles and listFilesInDir (called by CreateFromDir), so this |
| 283 |
// test focuses on how multiple errors and omissions are reported, rather than |
| 284 |
// trying to cover every case. |
| 285 |
func TestCheckDir(t *testing.T) { |
| 286 |
testPaths, err := filepath.Glob(filepath.FromSlash("testdata/check_dir/*.txt")) |
| 287 |
if err != nil { |
| 288 |
t.Fatal(err) |
| 289 |
} |
| 290 |
for _, testPath := range testPaths { |
| 291 |
name := strings.TrimSuffix(filepath.Base(testPath), ".txt") |
| 292 |
t.Run(name, func(t *testing.T) { |
| 293 |
t.Parallel() |
| 294 |
|
| 295 |
// Load the test and extract the files to a temporary directory. |
| 296 |
test, err := readTest(testPath) |
| 297 |
if err != nil { |
| 298 |
t.Fatal(err) |
| 299 |
} |
| 300 |
var want string |
| 301 |
for i, f := range test.archive.Files { |
| 302 |
if f.Name == "want" { |
| 303 |
want = string(f.Data) |
| 304 |
test.archive.Files = append(test.archive.Files[:i], test.archive.Files[i+1:]...) |
| 305 |
break |
| 306 |
} |
| 307 |
} |
| 308 |
tmpDir, err := extractTxtarToTempDir(t, test.archive) |
| 309 |
if err != nil { |
| 310 |
t.Fatal(err) |
| 311 |
} |
| 312 |
|
| 313 |
// Check the directory. |
| 314 |
cf, err := modzip.CheckDir(tmpDir) |
| 315 |
if err != nil && err.Error() != cf.Err().Error() { |
| 316 |
// I/O error |
| 317 |
t.Fatal(err) |
| 318 |
} |
| 319 |
rep := strings.NewReplacer(tmpDir, "$work", `'\''`, `'\''`, string(os.PathSeparator), "/") |
| 320 |
got := rep.Replace(formatCheckedFiles(cf)) |
| 321 |
if got != want { |
| 322 |
t.Errorf("got:\n%s\n\nwant:\n%s", got, want) |
| 323 |
} |
| 324 |
|
| 325 |
// Check that the error (if any) is just a list of invalid files. |
| 326 |
// SizeError is not covered in this test. |
| 327 |
var gotErr, wantErr string |
| 328 |
if len(cf.Invalid) > 0 { |
| 329 |
wantErr = modzip.FileErrorList(cf.Invalid).Error() |
| 330 |
} |
| 331 |
if err := cf.Err(); err != nil { |
| 332 |
gotErr = err.Error() |
| 333 |
} |
| 334 |
if gotErr != wantErr { |
| 335 |
t.Errorf("got error:\n%s\n\nwant error:\n%s", gotErr, wantErr) |
| 336 |
} |
| 337 |
}) |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// TestCheckZip verifies behavior of CheckZip. Note that CheckZip is also |
| 342 |
// covered by TestUnzip, so this test focuses on how multiple errors are |
| 343 |
// reported, rather than trying to cover every case. |
| 344 |
func TestCheckZip(t *testing.T) { |
| 345 |
testPaths, err := filepath.Glob(filepath.FromSlash("testdata/check_zip/*.txt")) |
| 346 |
if err != nil { |
| 347 |
t.Fatal(err) |
| 348 |
} |
| 349 |
for _, testPath := range testPaths { |
| 350 |
name := strings.TrimSuffix(filepath.Base(testPath), ".txt") |
| 351 |
t.Run(name, func(t *testing.T) { |
| 352 |
t.Parallel() |
| 353 |
|
| 354 |
// Load the test and extract the files to a temporary zip file. |
| 355 |
test, err := readTest(testPath) |
| 356 |
if err != nil { |
| 357 |
t.Fatal(err) |
| 358 |
} |
| 359 |
var want string |
| 360 |
for i, f := range test.archive.Files { |
| 361 |
if f.Name == "want" { |
| 362 |
want = string(f.Data) |
| 363 |
test.archive.Files = append(test.archive.Files[:i], test.archive.Files[i+1:]...) |
| 364 |
break |
| 365 |
} |
| 366 |
} |
| 367 |
tmpZipPath, err := extractTxtarToTempZip(t, test.archive) |
| 368 |
if err != nil { |
| 369 |
t.Fatal(err) |
| 370 |
} |
| 371 |
|
| 372 |
// Check the zip. |
| 373 |
m := module.Version{Path: test.path, Version: test.version} |
| 374 |
cf, err := modzip.CheckZip(m, tmpZipPath) |
| 375 |
if err != nil && err.Error() != cf.Err().Error() { |
| 376 |
// I/O error |
| 377 |
t.Fatal(err) |
| 378 |
} |
| 379 |
got := formatCheckedFiles(cf) |
| 380 |
if got != want { |
| 381 |
t.Errorf("got:\n%s\n\nwant:\n%s", got, want) |
| 382 |
} |
| 383 |
|
| 384 |
// Check that the error (if any) is just a list of invalid files. |
| 385 |
// SizeError is not covered in this test. |
| 386 |
var gotErr, wantErr string |
| 387 |
if len(cf.Invalid) > 0 { |
| 388 |
wantErr = modzip.FileErrorList(cf.Invalid).Error() |
| 389 |
} |
| 390 |
if err := cf.Err(); err != nil { |
| 391 |
gotErr = err.Error() |
| 392 |
} |
| 393 |
if gotErr != wantErr { |
| 394 |
t.Errorf("got error:\n%s\n\nwant error:\n%s", gotErr, wantErr) |
| 395 |
} |
| 396 |
}) |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
func TestCreate(t *testing.T) { |
| 401 |
testDir := filepath.FromSlash("testdata/create") |
| 402 |
testEntries, err := os.ReadDir(testDir) |
| 403 |
if err != nil { |
| 404 |
t.Fatal(err) |
| 405 |
} |
| 406 |
for _, testEntry := range testEntries { |
| 407 |
base := filepath.Base(testEntry.Name()) |
| 408 |
if filepath.Ext(base) != ".txt" { |
| 409 |
continue |
| 410 |
} |
| 411 |
t.Run(base[:len(base)-len(".txt")], func(t *testing.T) { |
| 412 |
t.Parallel() |
| 413 |
|
| 414 |
// Load the test. |
| 415 |
testPath := filepath.Join(testDir, testEntry.Name()) |
| 416 |
test, err := readTest(testPath) |
| 417 |
if err != nil { |
| 418 |
t.Fatal(err) |
| 419 |
} |
| 420 |
|
| 421 |
// Write zip to temporary file. |
| 422 |
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreate-*.zip") |
| 423 |
if err != nil { |
| 424 |
t.Fatal(err) |
| 425 |
} |
| 426 |
tmpZipPath := tmpZip.Name() |
| 427 |
defer tmpZip.Close() |
| 428 |
m := module.Version{Path: test.path, Version: test.version} |
| 429 |
files := make([]modzip.File, len(test.archive.Files)) |
| 430 |
for i, tf := range test.archive.Files { |
| 431 |
files[i] = fakeFile{ |
| 432 |
name: tf.Name, |
| 433 |
size: uint64(len(tf.Data)), |
| 434 |
data: tf.Data, |
| 435 |
} |
| 436 |
} |
| 437 |
if err := modzip.Create(tmpZip, m, files); err != nil { |
| 438 |
if test.wantErr == "" { |
| 439 |
t.Fatalf("unexpected error: %v", err) |
| 440 |
} else if !strings.Contains(err.Error(), test.wantErr) { |
| 441 |
t.Fatalf("got error %q; want error containing %q", err.Error(), test.wantErr) |
| 442 |
} else { |
| 443 |
return |
| 444 |
} |
| 445 |
} else if test.wantErr != "" { |
| 446 |
t.Fatalf("unexpected success; wanted error containing %q", test.wantErr) |
| 447 |
} |
| 448 |
if err := tmpZip.Close(); err != nil { |
| 449 |
t.Fatal(err) |
| 450 |
} |
| 451 |
|
| 452 |
// Hash zip file, compare with known value. |
| 453 |
if hash, err := dirhash.HashZip(tmpZipPath, dirhash.Hash1); err != nil { |
| 454 |
t.Fatal(err) |
| 455 |
} else if hash != test.hash { |
| 456 |
t.Fatalf("got hash: %q\nwant: %q", hash, test.hash) |
| 457 |
} |
| 458 |
}) |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
func TestCreateFromDir(t *testing.T) { |
| 463 |
testDir := filepath.FromSlash("testdata/create_from_dir") |
| 464 |
testEntries, err := os.ReadDir(testDir) |
| 465 |
if err != nil { |
| 466 |
t.Fatal(err) |
| 467 |
} |
| 468 |
for _, testEntry := range testEntries { |
| 469 |
base := filepath.Base(testEntry.Name()) |
| 470 |
if filepath.Ext(base) != ".txt" { |
| 471 |
continue |
| 472 |
} |
| 473 |
t.Run(base[:len(base)-len(".txt")], func(t *testing.T) { |
| 474 |
t.Parallel() |
| 475 |
|
| 476 |
// Load the test. |
| 477 |
testPath := filepath.Join(testDir, testEntry.Name()) |
| 478 |
test, err := readTest(testPath) |
| 479 |
if err != nil { |
| 480 |
t.Fatal(err) |
| 481 |
} |
| 482 |
|
| 483 |
// Write files to a temporary directory. |
| 484 |
tmpDir, err := extractTxtarToTempDir(t, test.archive) |
| 485 |
if err != nil { |
| 486 |
t.Fatal(err) |
| 487 |
} |
| 488 |
|
| 489 |
// Create zip from the directory. |
| 490 |
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip") |
| 491 |
if err != nil { |
| 492 |
t.Fatal(err) |
| 493 |
} |
| 494 |
tmpZipPath := tmpZip.Name() |
| 495 |
defer tmpZip.Close() |
| 496 |
m := module.Version{Path: test.path, Version: test.version} |
| 497 |
if err := modzip.CreateFromDir(tmpZip, m, tmpDir); err != nil { |
| 498 |
if test.wantErr == "" { |
| 499 |
t.Fatalf("unexpected error: %v", err) |
| 500 |
} else if !strings.Contains(err.Error(), test.wantErr) { |
| 501 |
t.Fatalf("got error %q; want error containing %q", err, test.wantErr) |
| 502 |
} else { |
| 503 |
return |
| 504 |
} |
| 505 |
} else if test.wantErr != "" { |
| 506 |
t.Fatalf("unexpected success; want error containing %q", test.wantErr) |
| 507 |
} |
| 508 |
|
| 509 |
// Hash zip file, compare with known value. |
| 510 |
if hash, err := dirhash.HashZip(tmpZipPath, dirhash.Hash1); err != nil { |
| 511 |
t.Fatal(err) |
| 512 |
} else if hash != test.hash { |
| 513 |
t.Fatalf("got hash: %q\nwant: %q", hash, test.hash) |
| 514 |
} |
| 515 |
}) |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
func TestCreateFromDirSpecial(t *testing.T) { |
| 520 |
for _, test := range []struct { |
| 521 |
desc string |
| 522 |
setup func(t *testing.T, tmpDir string) string |
| 523 |
wantHash string |
| 524 |
}{ |
| 525 |
{ |
| 526 |
desc: "ignore_empty_dir", |
| 527 |
setup: func(t *testing.T, tmpDir string) string { |
| 528 |
if err := os.Mkdir(filepath.Join(tmpDir, "empty"), 0777); err != nil { |
| 529 |
t.Fatal(err) |
| 530 |
} |
| 531 |
return tmpDir |
| 532 |
}, |
| 533 |
wantHash: emptyHash, |
| 534 |
}, { |
| 535 |
desc: "ignore_symlink", |
| 536 |
setup: func(t *testing.T, tmpDir string) string { |
| 537 |
if err := os.Symlink(tmpDir, filepath.Join(tmpDir, "link")); err != nil { |
| 538 |
switch runtime.GOOS { |
| 539 |
case "aix", "android", "darwin", "dragonfly", "freebsd", "illumos", "ios", "js", "linux", "netbsd", "openbsd", "solaris": |
| 540 |
// Symlinks in tmpDir are always expected to work on these platforms. |
| 541 |
t.Fatal(err) |
| 542 |
default: |
| 543 |
t.Skipf("could not create symlink: %v", err) |
| 544 |
} |
| 545 |
} |
| 546 |
return tmpDir |
| 547 |
}, |
| 548 |
wantHash: emptyHash, |
| 549 |
}, { |
| 550 |
desc: "dir_is_vendor", |
| 551 |
setup: func(t *testing.T, tmpDir string) string { |
| 552 |
vendorDir := filepath.Join(tmpDir, "vendor") |
| 553 |
if err := os.Mkdir(vendorDir, 0777); err != nil { |
| 554 |
t.Fatal(err) |
| 555 |
} |
| 556 |
goModData := []byte("module example.com/m\n\ngo 1.13\n") |
| 557 |
if err := os.WriteFile(filepath.Join(vendorDir, "go.mod"), goModData, 0666); err != nil { |
| 558 |
t.Fatal(err) |
| 559 |
} |
| 560 |
return vendorDir |
| 561 |
}, |
| 562 |
wantHash: "h1:XduFAgX/GaspZa8Jv4pfzoGEzNaU/r88PiCunijw5ok=", |
| 563 |
}, |
| 564 |
} { |
| 565 |
t.Run(test.desc, func(t *testing.T) { |
| 566 |
dir := test.setup(t, t.TempDir()) |
| 567 |
|
| 568 |
tmpZipFile, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip") |
| 569 |
if err != nil { |
| 570 |
t.Fatal(err) |
| 571 |
} |
| 572 |
tmpZipPath := tmpZipFile.Name() |
| 573 |
defer tmpZipFile.Close() |
| 574 |
|
| 575 |
m := module.Version{Path: "example.com/m", Version: "v1.0.0"} |
| 576 |
if err := modzip.CreateFromDir(tmpZipFile, m, dir); err != nil { |
| 577 |
t.Fatal(err) |
| 578 |
} |
| 579 |
if err := tmpZipFile.Close(); err != nil { |
| 580 |
t.Fatal(err) |
| 581 |
} |
| 582 |
|
| 583 |
if hash, err := dirhash.HashZip(tmpZipPath, dirhash.Hash1); err != nil { |
| 584 |
t.Fatal(err) |
| 585 |
} else if hash != test.wantHash { |
| 586 |
t.Fatalf("got hash %q; want %q", hash, emptyHash) |
| 587 |
} |
| 588 |
}) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
func TestUnzip(t *testing.T) { |
| 593 |
testDir := filepath.FromSlash("testdata/unzip") |
| 594 |
testEntries, err := os.ReadDir(testDir) |
| 595 |
if err != nil { |
| 596 |
t.Fatal(err) |
| 597 |
} |
| 598 |
for _, testEntry := range testEntries { |
| 599 |
base := filepath.Base(testEntry.Name()) |
| 600 |
if filepath.Ext(base) != ".txt" { |
| 601 |
continue |
| 602 |
} |
| 603 |
t.Run(base[:len(base)-len(".txt")], func(t *testing.T) { |
| 604 |
// Load the test. |
| 605 |
testPath := filepath.Join(testDir, testEntry.Name()) |
| 606 |
test, err := readTest(testPath) |
| 607 |
if err != nil { |
| 608 |
t.Fatal(err) |
| 609 |
} |
| 610 |
|
| 611 |
// Convert txtar to temporary zip file. |
| 612 |
tmpZipPath, err := extractTxtarToTempZip(t, test.archive) |
| 613 |
if err != nil { |
| 614 |
t.Fatal(err) |
| 615 |
} |
| 616 |
|
| 617 |
// Extract to a temporary directory. |
| 618 |
tmpDir := t.TempDir() |
| 619 |
m := module.Version{Path: test.path, Version: test.version} |
| 620 |
if err := modzip.Unzip(tmpDir, m, tmpZipPath); err != nil { |
| 621 |
if test.wantErr == "" { |
| 622 |
t.Fatalf("unexpected error: %v", err) |
| 623 |
} else if !strings.Contains(err.Error(), test.wantErr) { |
| 624 |
t.Fatalf("got error %q; want error containing %q", err.Error(), test.wantErr) |
| 625 |
} else { |
| 626 |
return |
| 627 |
} |
| 628 |
} else if test.wantErr != "" { |
| 629 |
t.Fatalf("unexpected success; wanted error containing %q", test.wantErr) |
| 630 |
} |
| 631 |
|
| 632 |
// Hash the directory, compare to known value. |
| 633 |
prefix := fmt.Sprintf("%s@%s/", test.path, test.version) |
| 634 |
if hash, err := dirhash.HashDir(tmpDir, prefix, dirhash.Hash1); err != nil { |
| 635 |
t.Fatal(err) |
| 636 |
} else if hash != test.hash { |
| 637 |
t.Fatalf("got hash %q\nwant: %q", hash, test.hash) |
| 638 |
} |
| 639 |
}) |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
type sizeLimitTest struct { |
| 644 |
desc string |
| 645 |
files []modzip.File |
| 646 |
wantErr string |
| 647 |
wantCheckFilesErr string |
| 648 |
wantCreateErr string |
| 649 |
wantCheckZipErr string |
| 650 |
wantUnzipErr string |
| 651 |
} |
| 652 |
|
| 653 |
// sizeLimitTests is shared by TestCreateSizeLimits and TestUnzipSizeLimits. |
| 654 |
var sizeLimitTests = [...]sizeLimitTest{ |
| 655 |
{ |
| 656 |
desc: "one_large", |
| 657 |
files: []modzip.File{fakeFile{ |
| 658 |
name: "large.go", |
| 659 |
size: modzip.MaxZipFile, |
| 660 |
}}, |
| 661 |
}, { |
| 662 |
desc: "one_too_large", |
| 663 |
files: []modzip.File{fakeFile{ |
| 664 |
name: "large.go", |
| 665 |
size: modzip.MaxZipFile + 1, |
| 666 |
}}, |
| 667 |
wantCheckFilesErr: "module source tree too large", |
| 668 |
wantCreateErr: "module source tree too large", |
| 669 |
wantCheckZipErr: "total uncompressed size of module contents too large", |
| 670 |
wantUnzipErr: "total uncompressed size of module contents too large", |
| 671 |
}, { |
| 672 |
desc: "total_large", |
| 673 |
files: []modzip.File{ |
| 674 |
fakeFile{ |
| 675 |
name: "small.go", |
| 676 |
size: 10, |
| 677 |
}, |
| 678 |
fakeFile{ |
| 679 |
name: "large.go", |
| 680 |
size: modzip.MaxZipFile - 10, |
| 681 |
}, |
| 682 |
}, |
| 683 |
}, { |
| 684 |
desc: "total_too_large", |
| 685 |
files: []modzip.File{ |
| 686 |
fakeFile{ |
| 687 |
name: "small.go", |
| 688 |
size: 10, |
| 689 |
}, |
| 690 |
fakeFile{ |
| 691 |
name: "large.go", |
| 692 |
size: modzip.MaxZipFile - 9, |
| 693 |
}, |
| 694 |
}, |
| 695 |
wantCheckFilesErr: "module source tree too large", |
| 696 |
wantCreateErr: "module source tree too large", |
| 697 |
wantCheckZipErr: "total uncompressed size of module contents too large", |
| 698 |
wantUnzipErr: "total uncompressed size of module contents too large", |
| 699 |
}, { |
| 700 |
desc: "large_gomod", |
| 701 |
files: []modzip.File{fakeFile{ |
| 702 |
name: "go.mod", |
| 703 |
size: modzip.MaxGoMod, |
| 704 |
}}, |
| 705 |
}, { |
| 706 |
desc: "too_large_gomod", |
| 707 |
files: []modzip.File{fakeFile{ |
| 708 |
name: "go.mod", |
| 709 |
size: modzip.MaxGoMod + 1, |
| 710 |
}}, |
| 711 |
wantErr: "go.mod file too large", |
| 712 |
}, { |
| 713 |
desc: "large_license", |
| 714 |
files: []modzip.File{fakeFile{ |
| 715 |
name: "LICENSE", |
| 716 |
size: modzip.MaxLICENSE, |
| 717 |
}}, |
| 718 |
}, { |
| 719 |
desc: "too_large_license", |
| 720 |
files: []modzip.File{fakeFile{ |
| 721 |
name: "LICENSE", |
| 722 |
size: modzip.MaxLICENSE + 1, |
| 723 |
}}, |
| 724 |
wantErr: "LICENSE file too large", |
| 725 |
}, |
| 726 |
} |
| 727 |
|
| 728 |
var sizeLimitVersion = module.Version{Path: "example.com/large", Version: "v1.0.0"} |
| 729 |
|
| 730 |
func TestCreateSizeLimits(t *testing.T) { |
| 731 |
if testing.Short() { |
| 732 |
t.Skip("creating large files takes time") |
| 733 |
} |
| 734 |
tests := append(sizeLimitTests[:], sizeLimitTest{ |
| 735 |
// negative file size may happen when size is represented as uint64 |
| 736 |
// but is cast to int64, as is the case in zip files. |
| 737 |
desc: "negative", |
| 738 |
files: []modzip.File{fakeFile{ |
| 739 |
name: "neg.go", |
| 740 |
size: 0x8000000000000000, |
| 741 |
}}, |
| 742 |
wantErr: "module source tree too large", |
| 743 |
}, sizeLimitTest{ |
| 744 |
desc: "size_is_a_lie", |
| 745 |
files: []modzip.File{fakeFile{ |
| 746 |
name: "lie.go", |
| 747 |
size: 1, |
| 748 |
data: []byte(`package large`), |
| 749 |
}}, |
| 750 |
wantCreateErr: "larger than declared size", |
| 751 |
}) |
| 752 |
|
| 753 |
for _, test := range tests { |
| 754 |
t.Run(test.desc, func(t *testing.T) { |
| 755 |
t.Parallel() |
| 756 |
|
| 757 |
wantCheckFilesErr := test.wantCheckFilesErr |
| 758 |
if wantCheckFilesErr == "" { |
| 759 |
wantCheckFilesErr = test.wantErr |
| 760 |
} |
| 761 |
if _, err := modzip.CheckFiles(test.files); err == nil && wantCheckFilesErr != "" { |
| 762 |
t.Fatalf("CheckFiles: unexpected success; want error containing %q", wantCheckFilesErr) |
| 763 |
} else if err != nil && wantCheckFilesErr == "" { |
| 764 |
t.Fatalf("CheckFiles: got error %q; want success", err) |
| 765 |
} else if err != nil && !strings.Contains(err.Error(), wantCheckFilesErr) { |
| 766 |
t.Fatalf("CheckFiles: got error %q; want error containing %q", err, wantCheckFilesErr) |
| 767 |
} |
| 768 |
|
| 769 |
wantCreateErr := test.wantCreateErr |
| 770 |
if wantCreateErr == "" { |
| 771 |
wantCreateErr = test.wantErr |
| 772 |
} |
| 773 |
if err := modzip.Create(io.Discard, sizeLimitVersion, test.files); err == nil && wantCreateErr != "" { |
| 774 |
t.Fatalf("Create: unexpected success; want error containing %q", wantCreateErr) |
| 775 |
} else if err != nil && wantCreateErr == "" { |
| 776 |
t.Fatalf("Create: got error %q; want success", err) |
| 777 |
} else if err != nil && !strings.Contains(err.Error(), wantCreateErr) { |
| 778 |
t.Fatalf("Create: got error %q; want error containing %q", err, wantCreateErr) |
| 779 |
} |
| 780 |
}) |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
func TestUnzipSizeLimits(t *testing.T) { |
| 785 |
if testing.Short() { |
| 786 |
t.Skip("creating large files takes time") |
| 787 |
} |
| 788 |
for _, test := range sizeLimitTests { |
| 789 |
t.Run(test.desc, func(t *testing.T) { |
| 790 |
t.Parallel() |
| 791 |
tmpZipFile, err := os.CreateTemp(t.TempDir(), "TestUnzipSizeLimits-*.zip") |
| 792 |
if err != nil { |
| 793 |
t.Fatal(err) |
| 794 |
} |
| 795 |
tmpZipPath := tmpZipFile.Name() |
| 796 |
defer tmpZipFile.Close() |
| 797 |
|
| 798 |
zw := zip.NewWriter(tmpZipFile) |
| 799 |
prefix := fmt.Sprintf("%s@%s/", sizeLimitVersion.Path, sizeLimitVersion.Version) |
| 800 |
for _, tf := range test.files { |
| 801 |
zf, err := zw.Create(prefix + tf.Path()) |
| 802 |
if err != nil { |
| 803 |
t.Fatal(err) |
| 804 |
} |
| 805 |
rc, err := tf.Open() |
| 806 |
if err != nil { |
| 807 |
t.Fatal(err) |
| 808 |
} |
| 809 |
_, err = io.Copy(zf, rc) |
| 810 |
rc.Close() |
| 811 |
if err != nil { |
| 812 |
t.Fatal(err) |
| 813 |
} |
| 814 |
} |
| 815 |
if err := zw.Close(); err != nil { |
| 816 |
t.Fatal(err) |
| 817 |
} |
| 818 |
if err := tmpZipFile.Close(); err != nil { |
| 819 |
t.Fatal(err) |
| 820 |
} |
| 821 |
|
| 822 |
wantCheckZipErr := test.wantCheckZipErr |
| 823 |
if wantCheckZipErr == "" { |
| 824 |
wantCheckZipErr = test.wantErr |
| 825 |
} |
| 826 |
cf, err := modzip.CheckZip(sizeLimitVersion, tmpZipPath) |
| 827 |
if err == nil { |
| 828 |
err = cf.Err() |
| 829 |
} |
| 830 |
if err == nil && wantCheckZipErr != "" { |
| 831 |
t.Fatalf("CheckZip: unexpected success; want error containing %q", wantCheckZipErr) |
| 832 |
} else if err != nil && wantCheckZipErr == "" { |
| 833 |
t.Fatalf("CheckZip: got error %q; want success", err) |
| 834 |
} else if err != nil && !strings.Contains(err.Error(), wantCheckZipErr) { |
| 835 |
t.Fatalf("CheckZip: got error %q; want error containing %q", err, wantCheckZipErr) |
| 836 |
} |
| 837 |
|
| 838 |
wantUnzipErr := test.wantUnzipErr |
| 839 |
if wantUnzipErr == "" { |
| 840 |
wantUnzipErr = test.wantErr |
| 841 |
} |
| 842 |
if err := modzip.Unzip(t.TempDir(), sizeLimitVersion, tmpZipPath); err == nil && wantUnzipErr != "" { |
| 843 |
t.Fatalf("Unzip: unexpected success; want error containing %q", wantUnzipErr) |
| 844 |
} else if err != nil && wantUnzipErr == "" { |
| 845 |
t.Fatalf("Unzip: got error %q; want success", err) |
| 846 |
} else if err != nil && !strings.Contains(err.Error(), wantUnzipErr) { |
| 847 |
t.Fatalf("Unzip: got error %q; want error containing %q", err, wantUnzipErr) |
| 848 |
} |
| 849 |
}) |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
func TestUnzipSizeLimitsSpecial(t *testing.T) { |
| 854 |
if testing.Short() { |
| 855 |
t.Skip("skipping test; creating large files takes time") |
| 856 |
} |
| 857 |
|
| 858 |
for _, test := range []struct { |
| 859 |
desc string |
| 860 |
wantErr1, wantErr2 string |
| 861 |
m module.Version |
| 862 |
writeZip func(t *testing.T, zipFile *os.File) |
| 863 |
}{ |
| 864 |
{ |
| 865 |
desc: "large_zip", |
| 866 |
m: module.Version{Path: "example.com/m", Version: "v1.0.0"}, |
| 867 |
writeZip: func(t *testing.T, zipFile *os.File) { |
| 868 |
if err := zipFile.Truncate(modzip.MaxZipFile); err != nil { |
| 869 |
t.Fatal(err) |
| 870 |
} |
| 871 |
}, |
| 872 |
// this is not an error we care about; we're just testing whether |
| 873 |
// Unzip checks the size of the file before opening. |
| 874 |
// It's harder to create a valid zip file of exactly the right size. |
| 875 |
wantErr1: "not a valid zip file", |
| 876 |
}, { |
| 877 |
desc: "too_large_zip", |
| 878 |
m: module.Version{Path: "example.com/m", Version: "v1.0.0"}, |
| 879 |
writeZip: func(t *testing.T, zipFile *os.File) { |
| 880 |
if err := zipFile.Truncate(modzip.MaxZipFile + 1); err != nil { |
| 881 |
t.Fatal(err) |
| 882 |
} |
| 883 |
}, |
| 884 |
wantErr1: "module zip file is too large", |
| 885 |
}, { |
| 886 |
desc: "size_is_a_lie", |
| 887 |
m: module.Version{Path: "example.com/m", Version: "v1.0.0"}, |
| 888 |
writeZip: func(t *testing.T, zipFile *os.File) { |
| 889 |
// Create a normal zip file in memory containing one file full of zero |
| 890 |
// bytes. Use a distinctive size so we can find it later. |
| 891 |
zipBuf := &bytes.Buffer{} |
| 892 |
zw := zip.NewWriter(zipBuf) |
| 893 |
f, err := zw.Create("example.com/[email protected]/go.mod") |
| 894 |
if err != nil { |
| 895 |
t.Fatal(err) |
| 896 |
} |
| 897 |
realSize := 0x0BAD |
| 898 |
buf := make([]byte, realSize) |
| 899 |
if _, err := f.Write(buf); err != nil { |
| 900 |
t.Fatal(err) |
| 901 |
} |
| 902 |
if err := zw.Close(); err != nil { |
| 903 |
t.Fatal(err) |
| 904 |
} |
| 905 |
|
| 906 |
// Replace the uncompressed size of the file. As a shortcut, we just |
| 907 |
// search-and-replace the byte sequence. It should occur twice because |
| 908 |
// the 32- and 64-byte sizes are stored separately. All multi-byte |
| 909 |
// values are little-endian. |
| 910 |
zipData := zipBuf.Bytes() |
| 911 |
realSizeData := []byte{0xAD, 0x0B} |
| 912 |
fakeSizeData := []byte{0xAC, 0x00} |
| 913 |
s := zipData |
| 914 |
n := 0 |
| 915 |
for { |
| 916 |
if i := bytes.Index(s, realSizeData); i < 0 { |
| 917 |
break |
| 918 |
} else { |
| 919 |
s = s[i:] |
| 920 |
} |
| 921 |
copy(s[:len(fakeSizeData)], fakeSizeData) |
| 922 |
n++ |
| 923 |
} |
| 924 |
if n != 2 { |
| 925 |
t.Fatalf("replaced size %d times; expected 2", n) |
| 926 |
} |
| 927 |
|
| 928 |
// Write the modified zip to the actual file. |
| 929 |
if _, err := zipFile.Write(zipData); err != nil { |
| 930 |
t.Fatal(err) |
| 931 |
} |
| 932 |
}, |
| 933 |
// wantErr1 is for 1.18 and earlier, |
| 934 |
// wantErr2 is for 1.19 and later. |
| 935 |
wantErr1: "uncompressed size of file example.com/[email protected]/go.mod is larger than declared size", |
| 936 |
wantErr2: "not a valid zip file", |
| 937 |
}, |
| 938 |
} { |
| 939 |
t.Run(test.desc, func(t *testing.T) { |
| 940 |
t.Parallel() |
| 941 |
tmpZipFile, err := os.CreateTemp(t.TempDir(), "TestUnzipSizeLimitsSpecial-*.zip") |
| 942 |
if err != nil { |
| 943 |
t.Fatal(err) |
| 944 |
} |
| 945 |
tmpZipPath := tmpZipFile.Name() |
| 946 |
defer tmpZipFile.Close() |
| 947 |
|
| 948 |
test.writeZip(t, tmpZipFile) |
| 949 |
if err := tmpZipFile.Close(); err != nil { |
| 950 |
t.Fatal(err) |
| 951 |
} |
| 952 |
|
| 953 |
want := func() string { |
| 954 |
s := fmt.Sprintf("%q", test.wantErr1) |
| 955 |
if test.wantErr2 != "" { |
| 956 |
s = fmt.Sprintf("%q or %q", test.wantErr1, test.wantErr2) |
| 957 |
} |
| 958 |
return s |
| 959 |
} |
| 960 |
|
| 961 |
if err := modzip.Unzip(t.TempDir(), test.m, tmpZipPath); err == nil && test.wantErr1 != "" { |
| 962 |
t.Fatalf("unexpected success; want error containing %s", want()) |
| 963 |
} else if err != nil && test.wantErr1 == "" { |
| 964 |
t.Fatalf("got error %q; want success", err) |
| 965 |
} else if err != nil && !strings.Contains(err.Error(), test.wantErr1) && (test.wantErr2 == "" || !strings.Contains(err.Error(), test.wantErr2)) { |
| 966 |
t.Fatalf("got error %q; want error containing %s", err, want()) |
| 967 |
} |
| 968 |
}) |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
// TestVCS clones a repository, creates a zip for a known version, |
| 973 |
// and verifies the zip file itself has the same SHA-256 hash as the one |
| 974 |
// 'go mod download' produces. |
| 975 |
// |
| 976 |
// This test is intended to build confidence that this implementation produces |
| 977 |
// the same output as the go command, given the same VCS zip input. This is |
| 978 |
// not intended to be a complete conformance test. The code that produces zip |
| 979 |
// archives from VCS repos is based on the go command, but it's for testing |
| 980 |
// only, and we don't export it. |
| 981 |
// |
| 982 |
// Note that we test the hash of the zip file itself. This is stricter than |
| 983 |
// testing the hash of the content, which is what we've promised users. |
| 984 |
// It's okay if the zip hash changes without changing the content hash, but |
| 985 |
// we should not let that happen accidentally. |
| 986 |
func TestVCS(t *testing.T) { |
| 987 |
if testing.Short() { |
| 988 |
t.Skip("skipping VCS cloning in -short mode") |
| 989 |
} |
| 990 |
|
| 991 |
var downloadErrorCount int32 |
| 992 |
const downloadErrorLimit = 3 |
| 993 |
|
| 994 |
_, gitErr := gitPath() |
| 995 |
_, hgErr := exec.LookPath("hg") |
| 996 |
haveVCS := map[string]bool{ |
| 997 |
"git": gitErr == nil, |
| 998 |
"hg": hgErr == nil, |
| 999 |
} |
| 1000 |
|
| 1001 |
for _, test := range []struct { |
| 1002 |
m module.Version |
| 1003 |
vcs, url, subdir, rev string |
| 1004 |
wantContentHash, wantZipHash string |
| 1005 |
}{ |
| 1006 |
// Simple tests: all versions of rsc.io/quote + newer major versions |
| 1007 |
{ |
| 1008 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.0.0"}, |
| 1009 |
vcs: "git", |
| 1010 |
url: "https://github.com/rsc/quote", |
| 1011 |
rev: "v1.0.0", |
| 1012 |
wantContentHash: "h1:haUSojyo3j2M9g7CEUFG8Na09dtn7QKxvPGaPVQdGwM=", |
| 1013 |
wantZipHash: match(t, map[string]string{ |
| 1014 |
"//go:build go1.27": "da4f6166581725af8622e28d77791121f35ce86f8e1f96b4eb4730a9d21280d1", |
| 1015 |
"//go:build !go1.27": "5c08ba2c09a364f93704aaa780e7504346102c6ef4fe1333a11f09904a732078", // Zip hash prior to CL 707355 ("compress/flate: improve compression speed"). Can be deleted when Go 1.26 becomes unsupported. |
| 1016 |
}), |
| 1017 |
}, |
| 1018 |
{ |
| 1019 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.1.0"}, |
| 1020 |
vcs: "git", |
| 1021 |
url: "https://github.com/rsc/quote", |
| 1022 |
rev: "v1.1.0", |
| 1023 |
wantContentHash: "h1:n/ElL9GOlVEwL0mVjzaYj0UxTI/TX9aQ7lR5LHqP/Rw=", |
| 1024 |
wantZipHash: match(t, map[string]string{ |
| 1025 |
"//go:build go1.27": "04ddc8cbf27cc1bf87f971c5b1c53e24c0e35d2556048aa1df7c7a7b2ed6e394", |
| 1026 |
"//go:build !go1.27": "730a5ae6e5c4e216e4f84bb93aa9785a85630ad73f96954ebb5f9daa123dcaa9", |
| 1027 |
}), |
| 1028 |
}, |
| 1029 |
{ |
| 1030 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.2.0"}, |
| 1031 |
vcs: "git", |
| 1032 |
url: "https://github.com/rsc/quote", |
| 1033 |
rev: "v1.2.0", |
| 1034 |
wantContentHash: "h1:fFMCNi0A97hfNrtUZVQKETbuc3h7bmfFQHnjutpPYCg=", |
| 1035 |
wantZipHash: match(t, map[string]string{ |
| 1036 |
"//go:build go1.27": "eb464bd19a59393165618e0a31a9c9627da8af72ea066619d1b2a493444d0e5b", |
| 1037 |
"//go:build !go1.27": "fe1bd62652e9737a30d6b7fd396ea13e54ad13fb05f295669eb63d6d33290b06", |
| 1038 |
}), |
| 1039 |
}, |
| 1040 |
{ |
| 1041 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.2.1"}, |
| 1042 |
vcs: "git", |
| 1043 |
url: "https://github.com/rsc/quote", |
| 1044 |
rev: "v1.2.1", |
| 1045 |
wantContentHash: "h1:l+HtgC05eds8qgXNApuv6g1oK1q3B144BM5li1akqXY=", |
| 1046 |
wantZipHash: match(t, map[string]string{ |
| 1047 |
"//go:build go1.27": "3e202f48ea14950a08f5cc89106f0aa420616697e61402d78b5c670c980e8664", |
| 1048 |
"//go:build !go1.27": "9f0e74de55a6bd20c1567a81e707814dc221f07df176af2a0270392c6faf32fd", |
| 1049 |
}), |
| 1050 |
}, |
| 1051 |
{ |
| 1052 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.3.0"}, |
| 1053 |
vcs: "git", |
| 1054 |
url: "https://github.com/rsc/quote", |
| 1055 |
rev: "v1.3.0", |
| 1056 |
wantContentHash: "h1:aPUoHx/0Cd7BTZs4SAaknT4TaKryH766GcFTvJjVbHU=", |
| 1057 |
wantZipHash: match(t, map[string]string{ |
| 1058 |
"//go:build go1.27": "90ff5f6b274f747260acb2bfd35115fdc34c6fb78728fdd79911531822c02f7b", |
| 1059 |
"//go:build !go1.27": "03872ee7d6747bc2ee0abadbd4eb09e60f6df17d0a6142264abe8a8a00af50e7", |
| 1060 |
}), |
| 1061 |
}, |
| 1062 |
{ |
| 1063 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.4.0"}, |
| 1064 |
vcs: "git", |
| 1065 |
url: "https://github.com/rsc/quote", |
| 1066 |
rev: "v1.4.0", |
| 1067 |
wantContentHash: "h1:tYuJspOzwTRMUOX6qmSDRTEKFVV80GM0/l89OLZuVNg=", |
| 1068 |
wantZipHash: match(t, map[string]string{ |
| 1069 |
"//go:build go1.27": "e955cc309ba4796910969684654c8bee45f8ea467f1ed181b693a820a1bb7371", |
| 1070 |
"//go:build !go1.27": "f60be8193c607bf197da01da4bedb3d683fe84c30de61040eb5d7afaf7869f2e", |
| 1071 |
}), |
| 1072 |
}, |
| 1073 |
{ |
| 1074 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.5.0"}, |
| 1075 |
vcs: "git", |
| 1076 |
url: "https://github.com/rsc/quote", |
| 1077 |
rev: "v1.5.0", |
| 1078 |
wantContentHash: "h1:mVjf/WMWxfIw299sOl/O3EXn5qEaaJPMDHMsv7DBDlw=", |
| 1079 |
wantZipHash: match(t, map[string]string{ |
| 1080 |
"//go:build go1.27": "5a23c3461bcff3a2182ba19c9ddb7d641c0da3378a6d65b29e16ec7429dd8511", |
| 1081 |
"//go:build !go1.27": "a2d281834ce159703540da94425fa02c7aec73b88b560081ed0d3681bfe9cd1f", |
| 1082 |
}), |
| 1083 |
}, |
| 1084 |
{ |
| 1085 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.5.1"}, |
| 1086 |
vcs: "git", |
| 1087 |
url: "https://github.com/rsc/quote", |
| 1088 |
rev: "v1.5.1", |
| 1089 |
wantContentHash: "h1:ptSemFtffEBvMed43o25vSUpcTVcqxfXU8Jv0sfFVJs=", |
| 1090 |
wantZipHash: match(t, map[string]string{ |
| 1091 |
"//go:build go1.27": "fdc567b6bb602f66267d461688295536691a472a57a95d30a6dbc27ec3077047", |
| 1092 |
"//go:build !go1.27": "4ecd78a6d9f571e84ed2baac1688fd150400db2c5b017b496c971af30aaece02", |
| 1093 |
}), |
| 1094 |
}, |
| 1095 |
{ |
| 1096 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.5.2"}, |
| 1097 |
vcs: "git", |
| 1098 |
url: "https://github.com/rsc/quote", |
| 1099 |
rev: "v1.5.2", |
| 1100 |
wantContentHash: "h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=", |
| 1101 |
wantZipHash: match(t, map[string]string{ |
| 1102 |
"//go:build go1.27": "62f1ad9011d0abbf40fb14cd4d0fab155574da291b7dfeb60b3ac2a9e6f61958", |
| 1103 |
"//go:build !go1.27": "643fcf8ef4e4cbb8f910622c42df3f9a81f3efe8b158a05825a81622c121ca0a", |
| 1104 |
}), |
| 1105 |
}, |
| 1106 |
{ |
| 1107 |
m: module.Version{Path: "rsc.io/quote", Version: "v1.5.3-pre1"}, |
| 1108 |
vcs: "git", |
| 1109 |
url: "https://github.com/rsc/quote", |
| 1110 |
rev: "v1.5.3-pre1", |
| 1111 |
wantContentHash: "h1:c3EJ21kn75/hyrOL/Dvj45+ifxGFSY8Wf4WBcoWTxF0=", |
| 1112 |
wantZipHash: match(t, map[string]string{ |
| 1113 |
"//go:build go1.27": "cef234502eb0dce0ba5ab8abb2c601f5564c4fe938ec4d39cc2be05ac011d787", |
| 1114 |
"//go:build !go1.27": "24106f0f15384949df51fae5d34191bf120c3b80c1c904721ca2872cf83126b2", |
| 1115 |
}), |
| 1116 |
}, |
| 1117 |
{ |
| 1118 |
m: module.Version{Path: "rsc.io/quote/v2", Version: "v2.0.1"}, |
| 1119 |
vcs: "git", |
| 1120 |
url: "https://github.com/rsc/quote", |
| 1121 |
rev: "v2.0.1", |
| 1122 |
wantContentHash: "h1:DF8hmGbDhgiIa2tpqLjHLIKkJx6WjCtLEqZBAU+hACI=", |
| 1123 |
wantZipHash: match(t, map[string]string{ |
| 1124 |
"//go:build go1.27": "ae715a163c69369ed3dbfd00deff028e7531d0bfce4bf27215dea31342146709", |
| 1125 |
"//go:build !go1.27": "009ed42474a59526fe56a14a9dd02bd7f977d1bd3844398bd209d0da0484aade", |
| 1126 |
}), |
| 1127 |
}, |
| 1128 |
{ |
| 1129 |
m: module.Version{Path: "rsc.io/quote/v3", Version: "v3.0.0"}, |
| 1130 |
vcs: "git", |
| 1131 |
url: "https://github.com/rsc/quote", |
| 1132 |
rev: "v3.0.0", |
| 1133 |
subdir: "v3", |
| 1134 |
wantContentHash: "h1:OEIXClZHFMyx5FdatYfxxpNEvxTqHlu5PNdla+vSYGg=", |
| 1135 |
wantZipHash: match(t, map[string]string{ |
| 1136 |
"//go:build go1.27": "afa74a797f3e159db044e553d333bcd753d09b02cf52f600d3dc65dcee7b2326", |
| 1137 |
"//go:build !go1.27": "cf3ff89056b785d7b3ef3a10e984efd83b47d9e65eabe8098b927b3370d5c3eb", |
| 1138 |
}), |
| 1139 |
}, |
| 1140 |
|
| 1141 |
// Test cases from vcs-test.golang.org |
| 1142 |
{ |
| 1143 |
m: module.Version{Path: "vcs-test.golang.org/git/v3pkg.git/v3", Version: "v3.0.0"}, |
| 1144 |
vcs: "git", |
| 1145 |
url: "https://vcs-test.golang.org/git/v3pkg", |
| 1146 |
rev: "v3.0.0", |
| 1147 |
wantContentHash: "h1:mZhljS1BaiW8lODR6wqY5pDxbhXja04rWPFXPwRAtvA=", |
| 1148 |
wantZipHash: "9c65f0d235e531008dc04e977f6fa5d678febc68679bb63d4148dadb91d3fe57", |
| 1149 |
}, |
| 1150 |
{ |
| 1151 |
m: module.Version{Path: "vcs-test.golang.org/go/custom-hg-hello", Version: "v0.0.0-20171010233936-a8c8e7a40da9"}, |
| 1152 |
vcs: "hg", |
| 1153 |
url: "https://vcs-test.golang.org/hg/custom-hg-hello", |
| 1154 |
rev: "a8c8e7a40da9", |
| 1155 |
wantContentHash: "h1:LU6jFCbwn5VVgTcj+y4LspOpJHLZvl5TGPE+LwwpMw4=", |
| 1156 |
wantZipHash: "a1b12047da979d618c639ee98f370767a13d0507bd77785dc2f8dad66b40e2e6", |
| 1157 |
}, |
| 1158 |
|
| 1159 |
// Latest versions of selected golang.org/x repos |
| 1160 |
{ |
| 1161 |
m: module.Version{Path: "golang.org/x/arch", Version: "v0.0.0-20190927153633-4e8777c89be4"}, |
| 1162 |
vcs: "git", |
| 1163 |
url: "https://go.googlesource.com/arch", |
| 1164 |
rev: "4e8777c89be4d9e61691fbe5d4e6c8838a7806f3", |
| 1165 |
wantContentHash: "h1:QlVATYS7JBoZMVaf+cNjb90WD/beKVHnIxFKT4QaHVI=", |
| 1166 |
wantZipHash: match(t, map[string]string{ |
| 1167 |
"//go:build go1.27": "c6a4647d1a604ce4c17b677fb437f1ed1c5123a70499b866889450dba3655160", |
| 1168 |
"//go:build !go1.27": "d17551a0c4957180ec1507065d13dcdd0f5cd8bfd7dd735fb81f64f3e2b31b68", |
| 1169 |
}), |
| 1170 |
}, |
| 1171 |
{ |
| 1172 |
m: module.Version{Path: "golang.org/x/blog", Version: "v0.0.0-20191017104857-0cd0cdff05c2"}, |
| 1173 |
vcs: "git", |
| 1174 |
url: "https://go.googlesource.com/blog", |
| 1175 |
rev: "0cd0cdff05c251ad0c796cc94d7059e013311fc6", |
| 1176 |
wantContentHash: "h1:IKGICrORhR1aH2xG/WqrnpggSNolSj5urQxggCfmj28=", |
| 1177 |
wantZipHash: match(t, map[string]string{ |
| 1178 |
"//go:build go1.27": "a2fad272a9264b42fd309fb5d5ee8100460ee8a4e459b50d19191b4c4e1a5b27", |
| 1179 |
"//go:build !go1.27": "0fed6b400de54da34b52b464ef2cdff45167236aaaf9a99ba8eba8855036faff", |
| 1180 |
}), |
| 1181 |
}, |
| 1182 |
{ |
| 1183 |
m: module.Version{Path: "golang.org/x/crypto", Version: "v0.0.0-20191011191535-87dc89f01550"}, |
| 1184 |
vcs: "git", |
| 1185 |
url: "https://go.googlesource.com/crypto", |
| 1186 |
rev: "87dc89f01550277dc22b74ffcf4cd89fa2f40f4c", |
| 1187 |
wantContentHash: "h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=", |
| 1188 |
wantZipHash: match(t, map[string]string{ |
| 1189 |
"//go:build go1.27": "2c7e512183a38e8d7aba86e20e1a5629b545bff3bae886f9b49f61fdaa1a0ada", |
| 1190 |
"//go:build !go1.27": "88e47aa05eb25c6abdad7387ccccfc39e74541896d87b7b1269e9dd2fa00100d", |
| 1191 |
}), |
| 1192 |
}, |
| 1193 |
{ |
| 1194 |
m: module.Version{Path: "golang.org/x/net", Version: "v0.0.0-20191014212845-da9a3fd4c582"}, |
| 1195 |
vcs: "git", |
| 1196 |
url: "https://go.googlesource.com/net", |
| 1197 |
rev: "da9a3fd4c5820e74b24a6cb7fb438dc9b0dd377c", |
| 1198 |
wantContentHash: "h1:p9xBe/w/OzkeYVKm234g55gMdD1nSIooTir5kV11kfA=", |
| 1199 |
wantZipHash: match(t, map[string]string{ |
| 1200 |
"//go:build go1.27": "dcdb96b6bf56cf11047530c7a93bee1252dafdffc900d1ddd68fd9a7066efcd5", |
| 1201 |
"//go:build !go1.27": "34901a85e6c15475a40457c2393ce66fb0999accaf2d6aa5b64b4863751ddbde", |
| 1202 |
}), |
| 1203 |
}, |
| 1204 |
{ |
| 1205 |
m: module.Version{Path: "golang.org/x/sync", Version: "v0.0.0-20190911185100-cd5d95a43a6e"}, |
| 1206 |
vcs: "git", |
| 1207 |
url: "https://go.googlesource.com/sync", |
| 1208 |
rev: "cd5d95a43a6e21273425c7ae415d3df9ea832eeb", |
| 1209 |
wantContentHash: "h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=", |
| 1210 |
wantZipHash: match(t, map[string]string{ |
| 1211 |
"//go:build go1.27": "5821c2f9ec68b279b0f2ff47cbaa3b6940d4a62b21b2e2473f3f3085a8c6156a", |
| 1212 |
"//go:build !go1.27": "9c63fe51b0c533b258d3acc30d9319fe78679ce1a051109c9dea3105b93e2eef", |
| 1213 |
}), |
| 1214 |
}, |
| 1215 |
{ |
| 1216 |
m: module.Version{Path: "golang.org/x/sys", Version: "v0.0.0-20191010194322-b09406accb47"}, |
| 1217 |
vcs: "git", |
| 1218 |
url: "https://go.googlesource.com/sys", |
| 1219 |
rev: "b09406accb4736d857a32bf9444cd7edae2ffa79", |
| 1220 |
wantContentHash: "h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=", |
| 1221 |
wantZipHash: match(t, map[string]string{ |
| 1222 |
"//go:build go1.27": "be942fcf114e729e4d54ab18a39624218f8ac0ce16f63b6c198d37187f9c1fa0", |
| 1223 |
"//go:build !go1.27": "f26f2993757670b4d1fee3156d331513259757f17133a36966c158642c3f61df", |
| 1224 |
}), |
| 1225 |
}, |
| 1226 |
{ |
| 1227 |
m: module.Version{Path: "golang.org/x/talks", Version: "v0.0.0-20191010201600-067e0d331fee"}, |
| 1228 |
vcs: "git", |
| 1229 |
url: "https://go.googlesource.com/talks", |
| 1230 |
rev: "067e0d331feee4f8d0fa17d47444db533bd904e7", |
| 1231 |
wantContentHash: "h1:8fnBMBUwliuiHuzfFw6kSSx79AzQpqkjZi3FSNIoqYs=", |
| 1232 |
wantZipHash: match(t, map[string]string{ |
| 1233 |
"//go:build go1.27": "fd76864eec51ab145bcfdb7d5e3cbca30eab69f637397f6c7ccf1c78f1d8a30d", |
| 1234 |
"//go:build !go1.27": "fab2129f3005f970dbf2247378edb3220f6bd36726acdc7300ae3bb0f129e2f2", |
| 1235 |
}), |
| 1236 |
}, |
| 1237 |
{ |
| 1238 |
m: module.Version{Path: "golang.org/x/tools", Version: "v0.0.0-20191017205301-920acffc3e65"}, |
| 1239 |
vcs: "git", |
| 1240 |
url: "https://go.googlesource.com/tools", |
| 1241 |
rev: "920acffc3e65862cb002dae6b227b8d9695e3d29", |
| 1242 |
wantContentHash: "h1:GwXwgmbrvlcHLDsENMqrQTTIC2C0kIPszsq929NruKI=", |
| 1243 |
wantZipHash: match(t, map[string]string{ |
| 1244 |
"//go:build go1.27": "0e9d300762a62ed33dca9ee60df5142a646854a705ee7831d7390e3cb93375c4", |
| 1245 |
"//go:build !go1.27": "7f0ab7466448190f8ad1b8cfb05787c3fb08f4a8f9953cd4b40a51c76ddebb28", |
| 1246 |
}), |
| 1247 |
}, |
| 1248 |
{ |
| 1249 |
m: module.Version{Path: "golang.org/x/tour", Version: "v0.0.0-20191002171047-6bb846ce41cd"}, |
| 1250 |
vcs: "git", |
| 1251 |
url: "https://go.googlesource.com/tour", |
| 1252 |
rev: "6bb846ce41cdca087b14c8e3560a679691c424b6", |
| 1253 |
wantContentHash: "h1:EUlK3Rq8iTkQERnCnveD654NvRJ/ZCM9XCDne+S5cJ8=", |
| 1254 |
wantZipHash: match(t, map[string]string{ |
| 1255 |
"//go:build go1.27": "5a7a6c942f386829b5028677c335edc06c533a0ab4c466a01e522505f143e013", |
| 1256 |
"//go:build !go1.27": "d6a7e03e02e5f7714bd12653d319a3b0f6e1099c01b1f9a17bc3613fb31c9170", |
| 1257 |
}), |
| 1258 |
}, |
| 1259 |
} { |
| 1260 |
testName := strings.ReplaceAll(test.m.String(), "/", "_") |
| 1261 |
t.Run(testName, func(t *testing.T) { |
| 1262 |
if have, ok := haveVCS[test.vcs]; !ok { |
| 1263 |
t.Fatalf("unknown vcs: %s", test.vcs) |
| 1264 |
} else if !have { |
| 1265 |
t.Skipf("no %s executable in path", test.vcs) |
| 1266 |
} |
| 1267 |
t.Parallel() |
| 1268 |
|
| 1269 |
repo, dl, err := downloadVCSZip(t, test.vcs, test.url, test.rev, test.subdir) |
| 1270 |
if err != nil { |
| 1271 |
// This may fail if there's a problem with the network or upstream |
| 1272 |
// repository. The package being tested doesn't directly interact with |
| 1273 |
// VCS tools; the test just does this to simulate what the go command |
| 1274 |
// does. So an error should cause a skip instead of a failure. But we |
| 1275 |
// should fail after too many errors so we don't lose test coverage |
| 1276 |
// when something changes permanently. |
| 1277 |
n := atomic.AddInt32(&downloadErrorCount, 1) |
| 1278 |
if n < downloadErrorLimit { |
| 1279 |
t.Skipf("failed to download zip from repository: %v", err) |
| 1280 |
} else { |
| 1281 |
t.Fatalf("failed to download zip from repository (repeated failure): %v", err) |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
// Create a module zip from that archive. |
| 1286 |
// (adapted from cmd/go/internal/modfetch.codeRepo.Zip) |
| 1287 |
info, err := dl.Stat() |
| 1288 |
if err != nil { |
| 1289 |
t.Fatal(err) |
| 1290 |
} |
| 1291 |
zr, err := zip.NewReader(dl, info.Size()) |
| 1292 |
if err != nil { |
| 1293 |
t.Fatal(err) |
| 1294 |
} |
| 1295 |
|
| 1296 |
var files []modzip.File |
| 1297 |
topPrefix := "" |
| 1298 |
subdir := test.subdir |
| 1299 |
if subdir != "" && !strings.HasSuffix(subdir, "/") { |
| 1300 |
subdir += "/" |
| 1301 |
} |
| 1302 |
haveLICENSE := false |
| 1303 |
for _, f := range zr.File { |
| 1304 |
if !f.FileInfo().Mode().IsRegular() { |
| 1305 |
continue |
| 1306 |
} |
| 1307 |
if topPrefix == "" { |
| 1308 |
i := strings.Index(f.Name, "/") |
| 1309 |
if i < 0 { |
| 1310 |
t.Fatal("missing top-level directory prefix") |
| 1311 |
} |
| 1312 |
topPrefix = f.Name[:i+1] |
| 1313 |
} |
| 1314 |
if strings.HasSuffix(f.Name, "/") { // drop directory dummy entries |
| 1315 |
continue |
| 1316 |
} |
| 1317 |
if !strings.HasPrefix(f.Name, topPrefix) { |
| 1318 |
t.Fatal("zip file contains more than one top-level directory") |
| 1319 |
} |
| 1320 |
name := strings.TrimPrefix(f.Name, topPrefix) |
| 1321 |
if !strings.HasPrefix(name, subdir) { |
| 1322 |
continue |
| 1323 |
} |
| 1324 |
name = strings.TrimPrefix(name, subdir) |
| 1325 |
if name == ".hg_archival.txt" { |
| 1326 |
// Inserted by hg archive. |
| 1327 |
// Not correct to drop from other version control systems, but too bad. |
| 1328 |
continue |
| 1329 |
} |
| 1330 |
if name == "LICENSE" { |
| 1331 |
haveLICENSE = true |
| 1332 |
} |
| 1333 |
files = append(files, zipFile{name: name, f: f}) |
| 1334 |
} |
| 1335 |
if !haveLICENSE && subdir != "" { |
| 1336 |
license, err := downloadVCSFile(t, test.vcs, repo, test.rev, "LICENSE") |
| 1337 |
if err != nil { |
| 1338 |
t.Fatal(err) |
| 1339 |
} |
| 1340 |
files = append(files, fakeFile{ |
| 1341 |
name: "LICENSE", |
| 1342 |
size: uint64(len(license)), |
| 1343 |
data: license, |
| 1344 |
}) |
| 1345 |
} |
| 1346 |
|
| 1347 |
tmpModZipFile, err := os.CreateTemp(t.TempDir(), "TestVCS-*.zip") |
| 1348 |
if err != nil { |
| 1349 |
t.Fatal(err) |
| 1350 |
} |
| 1351 |
tmpModZipPath := tmpModZipFile.Name() |
| 1352 |
defer tmpModZipFile.Close() |
| 1353 |
h := sha256.New() |
| 1354 |
w := io.MultiWriter(tmpModZipFile, h) |
| 1355 |
if err := modzip.Create(w, test.m, files); err != nil { |
| 1356 |
t.Fatal(err) |
| 1357 |
} |
| 1358 |
if err := tmpModZipFile.Close(); err != nil { |
| 1359 |
t.Fatal(err) |
| 1360 |
} |
| 1361 |
|
| 1362 |
gotZipHash := hex.EncodeToString(h.Sum(nil)) |
| 1363 |
if gotZipHash != test.wantZipHash { |
| 1364 |
// If the test fails because the hash of the zip file itself differs, |
| 1365 |
// that may be okay as long as the hash of the data within the zip file |
| 1366 |
// does not change. For example, we might change the compression, |
| 1367 |
// order, or alignment of files without affecting the extracted output. |
| 1368 |
// We shouldn't make such a change unintentionally though, so this |
| 1369 |
// test will fail either way. |
| 1370 |
if gotSum, err := dirhash.HashZip(tmpModZipPath, dirhash.Hash1); err != nil { |
| 1371 |
t.Fatal("dirhash.HashZip:", err) |
| 1372 |
} else if gotSum != test.wantContentHash { |
| 1373 |
t.Fatalf("got file with sum %q, want %q", gotSum, test.wantContentHash) |
| 1374 |
} else { |
| 1375 |
t.Fatalf("got file with hash %q, want %q (but content has correct sum)", gotZipHash, test.wantZipHash) |
| 1376 |
} |
| 1377 |
} |
| 1378 |
}) |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
// match takes a map whose keys are a Go version query expressed using //go:build constraint syntax, |
| 1383 |
// and returns the single value that matches the current Go version. |
| 1384 |
func match[T any](t *testing.T, m map[string]T) T { |
| 1385 |
t.Helper() |
| 1386 |
var matches = make([]T, 0, 1) |
| 1387 |
for k, v := range m { |
| 1388 |
c, err := constraint.Parse(k) |
| 1389 |
if err != nil { |
| 1390 |
t.Fatal(err) |
| 1391 |
} |
| 1392 |
if c.Eval(func(tag string) bool { return slices.Contains(build.Default.ReleaseTags, tag) }) { |
| 1393 |
matches = append(matches, v) |
| 1394 |
} |
| 1395 |
} |
| 1396 |
if len(matches) != 1 { |
| 1397 |
t.Fatalf("ambiguous query %q for version %q: got %d matches, want 1", slices.Sorted(maps.Keys(m)), runtime.Version(), len(matches)) |
| 1398 |
} |
| 1399 |
return matches[0] |
| 1400 |
} |
| 1401 |
|
| 1402 |
func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string, dl *os.File, err error) { |
| 1403 |
repoDir = t.TempDir() |
| 1404 |
|
| 1405 |
switch vcs { |
| 1406 |
case "git": |
| 1407 |
// Create a repository and download the revision we want. |
| 1408 |
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "init", "--bare"); err != nil { |
| 1409 |
return "", nil, err |
| 1410 |
} |
| 1411 |
if err := os.MkdirAll(filepath.Join(repoDir, "info"), 0777); err != nil { |
| 1412 |
return "", nil, err |
| 1413 |
} |
| 1414 |
attrFile, err := os.OpenFile(filepath.Join(repoDir, "info", "attributes"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) |
| 1415 |
if err != nil { |
| 1416 |
return "", nil, err |
| 1417 |
} |
| 1418 |
if _, err := attrFile.Write([]byte("\n* -export-subst -export-ignore\n")); err != nil { |
| 1419 |
attrFile.Close() |
| 1420 |
return "", nil, err |
| 1421 |
} |
| 1422 |
if err := attrFile.Close(); err != nil { |
| 1423 |
return "", nil, err |
| 1424 |
} |
| 1425 |
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "remote", "add", "origin", "--", url); err != nil { |
| 1426 |
return "", nil, err |
| 1427 |
} |
| 1428 |
var refSpec string |
| 1429 |
if strings.HasPrefix(rev, "v") { |
| 1430 |
refSpec = fmt.Sprintf("refs/tags/%[1]s:refs/tags/%[1]s", rev) |
| 1431 |
} else { |
| 1432 |
refSpec = fmt.Sprintf("%s:refs/dummy", rev) |
| 1433 |
} |
| 1434 |
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "fetch", "-f", "--depth=1", "origin", refSpec); err != nil { |
| 1435 |
return "", nil, err |
| 1436 |
} |
| 1437 |
|
| 1438 |
// Create an archive. |
| 1439 |
tmpZipFile, err := os.CreateTemp(t.TempDir(), "downloadVCSZip-*.zip") |
| 1440 |
if err != nil { |
| 1441 |
return "", nil, err |
| 1442 |
} |
| 1443 |
t.Cleanup(func() { tmpZipFile.Close() }) |
| 1444 |
subdirArg := subdir |
| 1445 |
if subdir == "" { |
| 1446 |
subdirArg = "." |
| 1447 |
} |
| 1448 |
|
| 1449 |
cmd := exec.Command("git", "-c", "core.autocrlf=input", "-c", "core.eol=lf", "archive", "--format=zip", "--prefix=prefix/", rev, "--", subdirArg) |
| 1450 |
cmd.Dir = repoDir |
| 1451 |
cmd.Env = append(cmd.Environ(), "GIT_DIR="+repoDir) |
| 1452 |
cmd.Stdout = tmpZipFile |
| 1453 |
stderr := new(strings.Builder) |
| 1454 |
cmd.Stderr = stderr |
| 1455 |
|
| 1456 |
err = cmd.Run() |
| 1457 |
if stderr.Len() > 0 && (err != nil || testing.Verbose()) { |
| 1458 |
t.Logf("%v: %v\n%s", err, cmd, stderr) |
| 1459 |
} else if err != nil { |
| 1460 |
t.Logf("%v: %v", err, cmd) |
| 1461 |
} else { |
| 1462 |
t.Logf("%v", cmd) |
| 1463 |
} |
| 1464 |
if err != nil { |
| 1465 |
return "", nil, err |
| 1466 |
} |
| 1467 |
|
| 1468 |
if _, err := tmpZipFile.Seek(0, 0); err != nil { |
| 1469 |
return "", nil, err |
| 1470 |
} |
| 1471 |
return repoDir, tmpZipFile, nil |
| 1472 |
|
| 1473 |
case "hg": |
| 1474 |
// Clone the whole repository. |
| 1475 |
if _, err := run(t, repoDir, "hg", "clone", "-U", "--", url, "."); err != nil { |
| 1476 |
return "", nil, err |
| 1477 |
} |
| 1478 |
|
| 1479 |
// Create an archive. |
| 1480 |
tmpZipFile, err := os.CreateTemp(t.TempDir(), "downloadVCSZip-*.zip") |
| 1481 |
if err != nil { |
| 1482 |
return "", nil, err |
| 1483 |
} |
| 1484 |
tmpZipPath := tmpZipFile.Name() |
| 1485 |
tmpZipFile.Close() |
| 1486 |
args := []string{"archive", "-t", "zip", "--no-decode", "-r", rev, "--prefix=prefix/"} |
| 1487 |
if subdir != "" { |
| 1488 |
args = append(args, "-I", subdir+"/**") |
| 1489 |
} |
| 1490 |
args = append(args, "--", tmpZipPath) |
| 1491 |
if _, err := run(t, repoDir, "hg", args...); err != nil { |
| 1492 |
return "", nil, err |
| 1493 |
} |
| 1494 |
if tmpZipFile, err = os.Open(tmpZipPath); err != nil { |
| 1495 |
return "", nil, err |
| 1496 |
} |
| 1497 |
t.Cleanup(func() { tmpZipFile.Close() }) |
| 1498 |
return repoDir, tmpZipFile, err |
| 1499 |
|
| 1500 |
default: |
| 1501 |
return "", nil, fmt.Errorf("vcs %q not supported", vcs) |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
func downloadVCSFile(t testing.TB, vcs, repo, rev, file string) ([]byte, error) { |
| 1506 |
t.Helper() |
| 1507 |
switch vcs { |
| 1508 |
case "git": |
| 1509 |
return runWithGitDir(t, repo, repo, "git", "cat-file", "blob", rev+":"+file) |
| 1510 |
default: |
| 1511 |
return nil, fmt.Errorf("vcs %q not supported", vcs) |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
func runWithGitDir(t testing.TB, gitDir, dir string, name string, args ...string) ([]byte, error) { |
| 1516 |
t.Helper() |
| 1517 |
|
| 1518 |
cmd := exec.Command(name, args...) |
| 1519 |
cmd.Dir = dir |
| 1520 |
if gitDir != "" { |
| 1521 |
cmd.Env = append(cmd.Environ(), "GIT_DIR="+gitDir) |
| 1522 |
} |
| 1523 |
stderr := new(strings.Builder) |
| 1524 |
cmd.Stderr = stderr |
| 1525 |
|
| 1526 |
out, err := cmd.Output() |
| 1527 |
if stderr.Len() > 0 && (err != nil || testing.Verbose()) { |
| 1528 |
t.Logf("%v: %v\n%s", err, cmd, stderr) |
| 1529 |
} else if err != nil { |
| 1530 |
t.Logf("%v: %v", err, cmd) |
| 1531 |
} else { |
| 1532 |
t.Logf("%v", cmd) |
| 1533 |
} |
| 1534 |
return out, err |
| 1535 |
} |
| 1536 |
|
| 1537 |
func run(t testing.TB, dir string, name string, args ...string) ([]byte, error) { |
| 1538 |
t.Helper() |
| 1539 |
|
| 1540 |
return runWithGitDir(t, "", dir, name, args...) |
| 1541 |
} |
| 1542 |
|
| 1543 |
type zipFile struct { |
| 1544 |
name string |
| 1545 |
f *zip.File |
| 1546 |
} |
| 1547 |
|
| 1548 |
func (f zipFile) Path() string { return f.name } |
| 1549 |
func (f zipFile) Lstat() (os.FileInfo, error) { return f.f.FileInfo(), nil } |
| 1550 |
func (f zipFile) Open() (io.ReadCloser, error) { return f.f.Open() } |
| 1551 |
|
| 1552 |
func TestCreateFromVCS_basic(t *testing.T) { |
| 1553 |
mustHaveGit(t) |
| 1554 |
|
| 1555 |
// Write files to a temporary directory. |
| 1556 |
tmpDir, err := extractTxtarToTempDir(t, txtar.Parse([]byte(`-- go.mod -- |
| 1557 |
module example.com/foo/bar |
| 1558 |
|
| 1559 |
go 1.12 |
| 1560 |
-- LICENSE -- |
| 1561 |
root license |
| 1562 |
-- a.go -- |
| 1563 |
package a |
| 1564 |
|
| 1565 |
var A = 5 |
| 1566 |
-- b.go -- |
| 1567 |
package a |
| 1568 |
|
| 1569 |
var B = 5 |
| 1570 |
-- c/c.go -- |
| 1571 |
package c |
| 1572 |
|
| 1573 |
var C = 5 |
| 1574 |
-- d/d.go -- |
| 1575 |
package c |
| 1576 |
|
| 1577 |
var D = 5 |
| 1578 |
-- e/LICENSE -- |
| 1579 |
e license |
| 1580 |
-- e/e.go -- |
| 1581 |
package e |
| 1582 |
|
| 1583 |
var E = 5 |
| 1584 |
-- f/go.mod -- |
| 1585 |
module example.com/foo/bar/f |
| 1586 |
|
| 1587 |
go 1.12 |
| 1588 |
-- f/f.go -- |
| 1589 |
package f |
| 1590 |
|
| 1591 |
var F = 5 |
| 1592 |
-- .gitignore -- |
| 1593 |
b.go |
| 1594 |
c/`))) |
| 1595 |
if err != nil { |
| 1596 |
t.Fatal(err) |
| 1597 |
} |
| 1598 |
|
| 1599 |
gitInit(t, tmpDir) |
| 1600 |
gitCommit(t, tmpDir) |
| 1601 |
|
| 1602 |
for _, tc := range []struct { |
| 1603 |
desc string |
| 1604 |
version module.Version |
| 1605 |
subdir string |
| 1606 |
wantFiles []string |
| 1607 |
wantData map[string]string |
| 1608 |
}{ |
| 1609 |
{ |
| 1610 |
desc: "from root", |
| 1611 |
version: module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}, |
| 1612 |
subdir: "", |
| 1613 |
wantFiles: []string{"go.mod", "LICENSE", "a.go", "d/d.go", "e/LICENSE", "e/e.go", ".gitignore"}, |
| 1614 |
wantData: map[string]string{"LICENSE": "root license\n"}, |
| 1615 |
}, |
| 1616 |
{ |
| 1617 |
desc: "from subdir", |
| 1618 |
version: module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}, |
| 1619 |
subdir: "d/", |
| 1620 |
// Note: File paths are zipped as if the subdir were the root, i.e., d.go instead of d/d.go. |
| 1621 |
// subdirs without a license hoist the license from the root |
| 1622 |
wantFiles: []string{"d.go", "LICENSE"}, |
| 1623 |
wantData: map[string]string{"LICENSE": "root license\n"}, |
| 1624 |
}, |
| 1625 |
{ |
| 1626 |
desc: "from subdir with license", |
| 1627 |
version: module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}, |
| 1628 |
subdir: "e/", |
| 1629 |
// Note: File paths are zipped as if the subdir were the root, i.e., e.go instead of e/e.go. |
| 1630 |
// subdirs with a license use their own |
| 1631 |
wantFiles: []string{"LICENSE", "e.go"}, |
| 1632 |
wantData: map[string]string{"LICENSE": "e license\n"}, |
| 1633 |
}, |
| 1634 |
{ |
| 1635 |
desc: "from submodule subdir", |
| 1636 |
version: module.Version{Path: "example.com/foo/bar/f", Version: "v0.0.1"}, |
| 1637 |
subdir: "f/", |
| 1638 |
// Note: File paths are zipped as if the subdir were the root, i.e., f.go instead of f/f.go. |
| 1639 |
// subdirs without a license hoist the license from the root |
| 1640 |
wantFiles: []string{"go.mod", "f.go", "LICENSE"}, |
| 1641 |
wantData: map[string]string{"LICENSE": "root license\n"}, |
| 1642 |
}, |
| 1643 |
} { |
| 1644 |
t.Run(tc.desc, func(t *testing.T) { |
| 1645 |
// Create zip from the directory. |
| 1646 |
tmpZip := &bytes.Buffer{} |
| 1647 |
|
| 1648 |
if err := modzip.CreateFromVCS(tmpZip, tc.version, tmpDir, "HEAD", tc.subdir); err != nil { |
| 1649 |
t.Fatal(err) |
| 1650 |
} |
| 1651 |
|
| 1652 |
wantData := map[string]string{} |
| 1653 |
for f, data := range tc.wantData { |
| 1654 |
p := path.Join(tc.version.String(), f) |
| 1655 |
wantData[p] = data |
| 1656 |
} |
| 1657 |
|
| 1658 |
readerAt := bytes.NewReader(tmpZip.Bytes()) |
| 1659 |
r, err := zip.NewReader(readerAt, int64(tmpZip.Len())) |
| 1660 |
if err != nil { |
| 1661 |
t.Fatal(err) |
| 1662 |
} |
| 1663 |
var gotFiles []string |
| 1664 |
gotMap := map[string]bool{} |
| 1665 |
for _, f := range r.File { |
| 1666 |
gotMap[f.Name] = true |
| 1667 |
gotFiles = append(gotFiles, f.Name) |
| 1668 |
|
| 1669 |
if want, ok := wantData[f.Name]; ok { |
| 1670 |
r, err := f.Open() |
| 1671 |
if err != nil { |
| 1672 |
t.Errorf("CreatedFromVCS: error opening %s: %v", f.Name, err) |
| 1673 |
continue |
| 1674 |
} |
| 1675 |
defer r.Close() |
| 1676 |
got, err := io.ReadAll(r) |
| 1677 |
if err != nil { |
| 1678 |
t.Errorf("CreatedFromVCS: error reading %s: %v", f.Name, err) |
| 1679 |
continue |
| 1680 |
} |
| 1681 |
if want != string(got) { |
| 1682 |
t.Errorf("CreatedFromVCS: zipped file %s contains %s, expected %s", f.Name, string(got), want) |
| 1683 |
continue |
| 1684 |
} |
| 1685 |
} |
| 1686 |
} |
| 1687 |
wantMap := map[string]bool{} |
| 1688 |
for _, f := range tc.wantFiles { |
| 1689 |
p := path.Join(tc.version.String(), f) |
| 1690 |
wantMap[p] = true |
| 1691 |
} |
| 1692 |
|
| 1693 |
// The things that should be there. |
| 1694 |
for f := range gotMap { |
| 1695 |
if !wantMap[f] { |
| 1696 |
t.Errorf("CreatedFromVCS: zipped file contains %s, but expected it not to", f) |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
// The things that are missing. |
| 1701 |
for f := range wantMap { |
| 1702 |
if !gotMap[f] { |
| 1703 |
t.Errorf("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v", f, gotFiles) |
| 1704 |
} |
| 1705 |
} |
| 1706 |
for f := range wantData { |
| 1707 |
if !gotMap[f] { |
| 1708 |
t.Errorf("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v", f, gotFiles) |
| 1709 |
} |
| 1710 |
} |
| 1711 |
}) |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
// Test what the experience of creating a zip from a v2 module is like. |
| 1716 |
func TestCreateFromVCS_v2(t *testing.T) { |
| 1717 |
mustHaveGit(t) |
| 1718 |
|
| 1719 |
// Write files to a temporary directory. |
| 1720 |
tmpDir, err := extractTxtarToTempDir(t, txtar.Parse([]byte(`-- go.mod -- |
| 1721 |
module example.com/foo/bar |
| 1722 |
|
| 1723 |
go 1.12 |
| 1724 |
-- a.go -- |
| 1725 |
package a |
| 1726 |
|
| 1727 |
var A = 5 |
| 1728 |
-- b.go -- |
| 1729 |
package a |
| 1730 |
|
| 1731 |
var B = 5 |
| 1732 |
-- go.mod -- |
| 1733 |
module example.com/foo/bar |
| 1734 |
|
| 1735 |
go 1.12 |
| 1736 |
-- gaz/v2/a_2.go -- |
| 1737 |
package a |
| 1738 |
|
| 1739 |
var C = 9 |
| 1740 |
-- gaz/v2/b_2.go -- |
| 1741 |
package a |
| 1742 |
|
| 1743 |
var B = 11 |
| 1744 |
-- gaz/v2/go.mod -- |
| 1745 |
module example.com/foo/bar/v2 |
| 1746 |
|
| 1747 |
go 1.12 |
| 1748 |
-- .gitignore -- |
| 1749 |
`))) |
| 1750 |
if err != nil { |
| 1751 |
t.Fatal(err) |
| 1752 |
} |
| 1753 |
|
| 1754 |
gitInit(t, tmpDir) |
| 1755 |
gitCommit(t, tmpDir) |
| 1756 |
|
| 1757 |
// Create zip from the directory. |
| 1758 |
tmpZip := &bytes.Buffer{} |
| 1759 |
|
| 1760 |
m := module.Version{Path: "example.com/foo/bar/v2", Version: "v2.0.0"} |
| 1761 |
|
| 1762 |
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", "gaz/v2"); err != nil { |
| 1763 |
t.Fatal(err) |
| 1764 |
} |
| 1765 |
|
| 1766 |
readerAt := bytes.NewReader(tmpZip.Bytes()) |
| 1767 |
r, err := zip.NewReader(readerAt, int64(tmpZip.Len())) |
| 1768 |
if err != nil { |
| 1769 |
t.Fatal(err) |
| 1770 |
} |
| 1771 |
var gotFiles []string |
| 1772 |
gotMap := map[string]bool{} |
| 1773 |
for _, f := range r.File { |
| 1774 |
gotMap[f.Name] = true |
| 1775 |
gotFiles = append(gotFiles, f.Name) |
| 1776 |
} |
| 1777 |
wantMap := map[string]bool{ |
| 1778 |
"example.com/foo/bar/[email protected]/a_2.go": true, |
| 1779 |
"example.com/foo/bar/[email protected]/b_2.go": true, |
| 1780 |
"example.com/foo/bar/[email protected]/go.mod": true, |
| 1781 |
} |
| 1782 |
|
| 1783 |
// The things that should be there. |
| 1784 |
for f := range gotMap { |
| 1785 |
if !wantMap[f] { |
| 1786 |
t.Errorf("CreatedFromVCS: zipped file contains %s, but expected it not to", f) |
| 1787 |
} |
| 1788 |
} |
| 1789 |
|
| 1790 |
// The things that are missing. |
| 1791 |
for f := range wantMap { |
| 1792 |
if !gotMap[f] { |
| 1793 |
t.Errorf("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v", f, gotFiles) |
| 1794 |
} |
| 1795 |
} |
| 1796 |
} |
| 1797 |
|
| 1798 |
func TestCreateFromVCS_nonGitDir(t *testing.T) { |
| 1799 |
mustHaveGit(t) |
| 1800 |
|
| 1801 |
// Write files to a temporary directory. |
| 1802 |
tmpDir, err := extractTxtarToTempDir(t, txtar.Parse([]byte(`-- go.mod -- |
| 1803 |
module example.com/foo/bar |
| 1804 |
|
| 1805 |
go 1.12 |
| 1806 |
-- a.go -- |
| 1807 |
package a |
| 1808 |
|
| 1809 |
var A = 5 |
| 1810 |
`))) |
| 1811 |
if err != nil { |
| 1812 |
t.Fatal(err) |
| 1813 |
} |
| 1814 |
|
| 1815 |
// Create zip from the directory. |
| 1816 |
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip") |
| 1817 |
if err != nil { |
| 1818 |
t.Fatal(err) |
| 1819 |
} |
| 1820 |
defer tmpZip.Close() |
| 1821 |
|
| 1822 |
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"} |
| 1823 |
|
| 1824 |
err = modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", "") |
| 1825 |
if err == nil { |
| 1826 |
t.Fatal("CreateFromVCS: expected error, got nil") |
| 1827 |
} |
| 1828 |
var gotErr *modzip.UnrecognizedVCSError |
| 1829 |
if !errors.As(err, &gotErr) { |
| 1830 |
t.Errorf("CreateFromVCS: returned error does not unwrap to modzip.UnrecognizedVCSError, but expected it to. returned error: %v", err) |
| 1831 |
} else if gotErr.RepoRoot != tmpDir { |
| 1832 |
t.Errorf("CreateFromVCS: returned error has RepoRoot %q, but want %q. returned error: %v", gotErr.RepoRoot, tmpDir, err) |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
func TestCreateFromVCS_zeroCommitsGitDir(t *testing.T) { |
| 1837 |
mustHaveGit(t) |
| 1838 |
|
| 1839 |
// Write files to a temporary directory. |
| 1840 |
tmpDir, err := extractTxtarToTempDir(t, txtar.Parse([]byte(`-- go.mod -- |
| 1841 |
module example.com/foo/bar |
| 1842 |
|
| 1843 |
go 1.12 |
| 1844 |
-- a.go -- |
| 1845 |
package a |
| 1846 |
|
| 1847 |
var A = 5 |
| 1848 |
`))) |
| 1849 |
if err != nil { |
| 1850 |
t.Fatal(err) |
| 1851 |
} |
| 1852 |
|
| 1853 |
gitInit(t, tmpDir) |
| 1854 |
|
| 1855 |
// Create zip from the directory. |
| 1856 |
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip") |
| 1857 |
if err != nil { |
| 1858 |
t.Fatal(err) |
| 1859 |
} |
| 1860 |
defer tmpZip.Close() |
| 1861 |
|
| 1862 |
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"} |
| 1863 |
|
| 1864 |
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", ""); err == nil { |
| 1865 |
t.Error("CreateFromVCS: expected error, got nil") |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
// gitInit runs "git init" at the specified dir. |
| 1870 |
// |
| 1871 |
// Note: some environments - and trybots - don't have git installed. This |
| 1872 |
// function will cause the calling test to be skipped if that's the case. |
| 1873 |
func gitInit(t testing.TB, dir string) { |
| 1874 |
t.Helper() |
| 1875 |
mustHaveGit(t) |
| 1876 |
|
| 1877 |
if _, err := run(t, dir, "git", "init"); err != nil { |
| 1878 |
t.Fatal(err) |
| 1879 |
} |
| 1880 |
if _, err := run(t, dir, "git", "config", "user.name", "Go Gopher"); err != nil { |
| 1881 |
t.Fatal(err) |
| 1882 |
} |
| 1883 |
if _, err := run(t, dir, "git", "config", "user.email", "[email protected]"); err != nil { |
| 1884 |
t.Fatal(err) |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
func gitCommit(t testing.TB, dir string) { |
| 1889 |
t.Helper() |
| 1890 |
mustHaveGit(t) |
| 1891 |
|
| 1892 |
if _, err := run(t, dir, "git", "add", "-A"); err != nil { |
| 1893 |
t.Fatal(err) |
| 1894 |
} |
| 1895 |
if _, err := run(t, dir, "git", "commit", "-m", "some commit"); err != nil { |
| 1896 |
t.Fatal(err) |
| 1897 |
} |
| 1898 |
} |