github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/internal/robustio/robustio_test.go (about) 1 // Copyright 2022 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 robustio_test 6 7 import ( 8 "os" 9 "path/filepath" 10 "runtime" 11 "testing" 12 "time" 13 14 "golang.org/x/tools/internal/robustio" 15 ) 16 17 func TestFileInfo(t *testing.T) { 18 // A nonexistent file has no ID. 19 nonexistent := filepath.Join(t.TempDir(), "nonexistent") 20 if _, _, err := robustio.GetFileID(nonexistent); err == nil { 21 t.Fatalf("GetFileID(nonexistent) succeeded unexpectedly") 22 } 23 24 // A regular file has an ID. 25 real := filepath.Join(t.TempDir(), "real") 26 if err := os.WriteFile(real, nil, 0644); err != nil { 27 t.Fatalf("can't create regular file: %v", err) 28 } 29 realID, realMtime, err := robustio.GetFileID(real) 30 if err != nil { 31 t.Fatalf("can't get ID of regular file: %v", err) 32 } 33 34 // Sleep so that we get a new mtime for subsequent writes. 35 time.Sleep(2 * time.Second) 36 37 // A second regular file has a different ID. 38 real2 := filepath.Join(t.TempDir(), "real2") 39 if err := os.WriteFile(real2, nil, 0644); err != nil { 40 t.Fatalf("can't create second regular file: %v", err) 41 } 42 real2ID, real2Mtime, err := robustio.GetFileID(real2) 43 if err != nil { 44 t.Fatalf("can't get ID of second regular file: %v", err) 45 } 46 if realID == real2ID { 47 t.Errorf("realID %+v == real2ID %+v", realID, real2ID) 48 } 49 if realMtime.Equal(real2Mtime) { 50 t.Errorf("realMtime %v == real2Mtime %v", realMtime, real2Mtime) 51 } 52 53 // A symbolic link has the same ID as its target. 54 if runtime.GOOS != "plan9" { 55 symlink := filepath.Join(t.TempDir(), "symlink") 56 if err := os.Symlink(real, symlink); err != nil { 57 t.Fatalf("can't create symbolic link: %v", err) 58 } 59 symlinkID, symlinkMtime, err := robustio.GetFileID(symlink) 60 if err != nil { 61 t.Fatalf("can't get ID of symbolic link: %v", err) 62 } 63 if realID != symlinkID { 64 t.Errorf("realID %+v != symlinkID %+v", realID, symlinkID) 65 } 66 if !realMtime.Equal(symlinkMtime) { 67 t.Errorf("realMtime %v != symlinkMtime %v", realMtime, symlinkMtime) 68 } 69 } 70 71 // Two hard-linked files have the same ID. 72 if runtime.GOOS != "plan9" && runtime.GOOS != "android" { 73 hardlink := filepath.Join(t.TempDir(), "hardlink") 74 if err := os.Link(real, hardlink); err != nil { 75 t.Fatal(err) 76 } 77 hardlinkID, hardlinkMtime, err := robustio.GetFileID(hardlink) 78 if err != nil { 79 t.Fatalf("can't get ID of hard link: %v", err) 80 } 81 if realID != hardlinkID { 82 t.Errorf("realID %+v != hardlinkID %+v", realID, hardlinkID) 83 } 84 if !realMtime.Equal(hardlinkMtime) { 85 t.Errorf("realMtime %v != hardlinkMtime %v", realMtime, hardlinkMtime) 86 } 87 } 88 }